1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
void Context::draw(const mesh_t& mesh, const material_t& material, const vector3d_t& position)
{
const auto& buffer = mesh.vbo;
const auto& program = material.shader.glsl.id;
glm::mat4 model_matrix(1.0f);
glm::mat4 mvp_matrix;
debug::check_assertion(glIsProgram(program) == GL_TRUE, "Invalid shader program.");
handle_gl_error();
glUseProgram(program);
handle_gl_error();
const GLuint vert = glGetAttribLocation(program, "vertex");
const GLuint tex = glGetAttribLocation(program, "texture_coord");
const GLuint diffuse_texture = glGetUniformLocation(program, "diffuse_texture");
const GLuint mvp = glGetUniformLocation(program, "mvp_matrix");
model_matrix = glm::translate(model_matrix, glm::vec3(position.x, position.y, position.z));
mvp_matrix = projection_matrix * view_matrix * model_matrix;
glUniformMatrix4fv(mvp, 1, GL_FALSE, glm::value_ptr(mvp_matrix));
handle_gl_error();
// Expose diffuse texture
bind_texture(material.diffuse.handle);
handle_gl_error();
glActiveTexture(GL_TEXTURE0);
handle_gl_error();
glUniform1i(diffuse_texture, 0);
handle_gl_error();
// Bind VBO
glBindBuffer(GL_ARRAY_BUFFER, buffer);
handle_gl_error();
// Enable VAOs
glEnableVertexAttribArray(tex);
glEnableVertexAttribArray(vert);
// Texture coordinates
glBindVertexArray(mesh.texture_coords);
handle_gl_error();
glVertexAttribPointer(tex, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), ((void*)sizeof(vector3d_t)));
handle_gl_error();
// Vertices
glBindVertexArray(mesh.vertices);
handle_gl_error();
glVertexAttribPointer(vert, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_t), nullptr);
handle_gl_error();
// Draw the entire mesh
glDrawArrays(GL_TRIANGLES, 0, mesh.num_vertices);
glDisableVertexAttribArray(vert);
glDisableVertexAttribArray(tex);
glBindVertexArray(0);
}
|