In OpenGL the indices
parameter for glDrawElements
has two different meanings, depending on if you are using GL_ELEMENT_ARRAY_BUFFER
or not.
If you have a VBO bound, then it is the offset to start in that buffer, rather than the buffer itself.
When using PyOpenGL, how can you specify the offset to start at in a glDrawElements
call? How can you specify multiple start offsets in a glMultiDrawElements
call?
In the following examples a list of 6 indices is used, which may form a quad which consists of 2 triangle primitives.
Since the data which is passed to the OpenGL functions has to consist of fixed size units in a coherent buffer, the list of values has to be stored to an array of floats. Coherent array buffers can be created either by ctypes library or
numpy.array
library.The type of the array elements has to match value type enumerator constant, which is set, at the call of
glDrawElements
orglMultiDrawElements
:Using
ctypes
:Using
numpy
:For using an index buffer and
glDrawElements
there are different opportunities.Using Legacy OpenGL (compatibility profile xontext), the buffer can be directly passed to
glDrawElements
. The pointer to the array data is passed to the function.If named element array buffer object is stated in the vertex array object, then the last parameter of
glDrawElements
is treated as a byte offset into the buffer object's data store.If the indices should be drawn, starting at the 1st element of the buffer, then the last parameter can be
None
, which is equivalent toctypes.c_void_p(0)
:If the drawing should not start with the first index, then the byte offset of the start index has to be calculated. e.g.
3*4
sets the start to the 3 index, for a buffer of typeGL_UNSIGNED_INT
:The use of
glMultiDrawElements
is very similar.Using a compatibility profile xontext, the buffer pointers can be directly passed to the OpenGL function.
To arrays of indices have to be generated:
Using
ctypes
:Using
numpy
:The pointers to the buffers have to be arranged to an array of pointers:
Using
ctypes
the pointer to the index data arrays is get byctypes.addressof()
:Using
numpy
the pointer to the index data arrays is get bynumpy.ndarray.ctypes
:This array of pointer can be passed to the OpenGL function:
If a vertex array object with an named element array buffer is used,
then the index parameter is treated as a pointer to an array of byte offsets. In the following an array with 2 offset is passed to the function. 0 identifies the 1st index in the array and 3*4 the 3rd index.
Using
ctypes
:Using
numpy
: