Search code examples
openglopengl-4

Why does glDrawElements() require number of vertices and number of elements?


As per the OpenGL documentation,

void glDrawElements(GLenum mode,
GLsizei count,
GLenum type,
const GLvoid * indices);

The second argument count in the call to glDrawElements, "Specifies the number of elements to be rendered." I'm finding out that the valid value for this argument is the number of vertices and not the number of faces or elements that you want to render. Maybe I'm misunderstanding the term "elements". I'm thinking of elements as a connectivity element, for example a triangle or a quadrilateral that you specify in your element buffer object.

Why is this second argument even necessary? I mean when vertex buffer object is defined, you already specify the number of vertices; when your element buffer object is defined, the number of elements or faces is also specified. What is the rationale for this seemingly redundant and confusing way to specify the number of vertices in this glDrawElements() call.


Solution

  • I mean when vertex buffer object is defined, you already specify the number of vertices

    No, you don't; you specify the number of bytes in a buffer object's storage. How big a vertex is, and how many of them are in that object, depend entirely on the vertex's format. Furthermore, you can (and should) have vertices from many objects in the same buffer, allowing you to render multiple objects without changing vertex buffer bindings.

    when your element buffer object is defined, the number of elements or faces is also specified

    See above. All OpenGL knows is a byte count, not the number of indices.