3.1.4 Mesh::draw()

To draw different models in a scene, we need to

  • Bind the correct shader program before drawing each model.

  • Bind the corresponding model's VAO/VBO/etc.

  • Set the appropriate uniforms for each shader (like transformation matrices, lighting, etc.)

  • Draw the model.

The following is a demonstrative implementation of Mesh::draw()

void Mesh::draw(glm::mat4 mat)
{

    // 1. Bind the correct shader program
    glUseProgram(shaderId);

    // 2. Set the appropriate uniforms for each shader
    // set transforms
    glm::mat4 mat_modelview = mat;
    
    GLuint modelview_loc = glGetUniformLocation(shaderId, "modelview" );
    glUniformMatrix4fv(modelview_loc, 1, GL_FALSE, &mat_modelview[0][0]);

    // you must set the projection to get correct rendering with depth
    // we use the default orthographic projection.
    // As projection is shared by all models in a scene
    // This will be moved out in the future.
    glm::mat4 mat_projection = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
    GLuint projection_loc = glGetUniformLocation( shaderId, "projection" );
    glUniformMatrix4fv(projection_loc, 1, GL_FALSE, &mat_projection[0][0]);

    // 3. Bind the corresponding model's VAO
    glBindVertexArray(buffers[0]);

    // 4. Draw the model
    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
}

Last updated