8/14
4.4. Displaying data
In the animation loop, drawing data mainly consists in setting the current shader, VAO, and then call the draw.
The code corresponding to draw the triangle is the following
// ************************************************* //
// 3 - Displaying Data //
// ************************************************* //
glUseProgram(shader); // Activate shader program
glBindVertexArray(vao); // Activate attributes for the drawing
glDrawArrays(GL_TRIANGLES, 0, 3); // Draw 3 vertices
glBindVertexArray(0);
glUseProgram(0);
-
The actual draw is called by the function glDrawArrays(GLenum mode, GLint first, GLsizei count)
-
mode: Indicates the type of primitive to draw, here a set of triangle by GL_TRIANGLES.
-
first: First index to be displayed from the data, here we start at index 0
-
count: Number of index to be rendered, here 3 vertices.
-
Note on efficiency
In this example case, we use only one shader, VAO, and VBO. The recurrent binding of the current shader program and VAO, followed by their disable, is not necessary. For optimization purpose, some program tempts to limit the number of changes of context (such as changing shaders, etc).
This, however, may be error prone and harder to debug when dealing with multiple shaders and VAO. In our example, we will prefer set locally the active elements and disable them after their use.