Zebrafish
Zebrafish

Reputation: 14424

What is the meaning of the index argument in glDrawElements?

There are two OpenGL documentation pages which have slightly different descriptions of the "index" parameter of the glDrawElements function. On www.opengl.org/sdk/docs/man4/ it says:

indices
    Specifies a pointer to the location where the indices are stored. 

And on www.khronos.org/opengles/sdk/docs/man3 it says:

indices
    Specifies a byte offset (cast to a pointer type) into the buffer bound 
    to GL_ELEMENT_ARRAY_BUFFER to start reading indices from. If no buffer
    is bound, specifies a pointer to the location where the indices are stored. 

I am on Windows by the way, using OpenGL 4+.

So I've copied my index array into the element buffer object I created, the indices pointer argument I need to supply is the offset in bytes of the first index? So if I want to start drawing at index 3, the argument would be 2 * sizeof(GLuint), cast as a pointer?

I actually went to the effort of creating an EBO for this, but from the looks of it it says that if no EBO is bound the pointer points straight to the location where the indices are, not the EBO. Am I right that this means it will point to your array on the system RAM? (EDIT: I just realised this doesn't make sense, if the pointer is at 0x00000008 it can't go to that address in system memory.) And if so, does it then copy the index array to the graphics card each time in order to be able to use it? Thanks.

Upvotes: 3

Views: 1969

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72539

As per OpenGL 4.5, core profile, reading from client memory is unsupported (§10.3.10 OpenGL 4.5 core spec):

DrawElements, DrawRangeElements, and DrawElementsInstanced source their indices from the buffer object whose name is bound to ELEMENT_ARRAY_BUFFER, using their indices parameters as offsets into the buffer object in the same fashion as described in section 10.3.9. [...] If zero is bound to ELEMENT_ARRAY_BUFFER, the result of these drawing commands is undefined.

So your approach of creating an EBO is correct. Except if your 0th index is located at offset zero then the 3rd index is located at offset 3*sizeof(type).

As for your second quotation: in the older OpenGL versions you could pass a pointer to the client memory (in your process virtual address space, not the physical address) and leave ELEMENT_ARRAY_BUFFER unbound.

Upvotes: 5

Related Questions