PDA

View Full Version : When to call glEnableVertexAttribArray()



ManuelP
08-18-2011, 07:24 AM
Hello,

i a new to OpenGL (ES) and have difficulties understanding when to do which system call in order to get the things done.

More precisely, i want to draw Triangles using glDrawElements with Vertex Buffer Objects. I do something like this:

---------- Initialization -------------
#Load Shaders
#Attach Shaders to Program
#BindAttributeLocation of a Position Attribute in the Vertex Shader
#Link Program
#use Program

#Generate GL_ARRAY_BUFFER Buffers
#Load Vertex Information using glBufferData
#Call glVertexAttribPointer for the Position attribute

#Generate GL_ELEMENT_ARRAY_BUFFER Buffers
#Load Indexes Information

------- Draw Routine ---------------
#Call glClear
#Call glEnableVertexAttribArray on the Position Array
#Call glDrawElements
#Call eglSwapBuffers


Thats a order to do things that seem to work. But i dont get why for example i have to call glEnableVertexAttribArray every time before doing the drawing with glDrawElements, althoug i dont touch any buffer. Same thing with the glVertexAttribPointer() call in the Initialization phase. If i move the call before the generation of VOB, i get a segment fault.

It may be obvious when to call when with deeper insight in OpenGL, but for me this things almost look random. If for example i want to change a vertex buffer and not touch the rest, do i have to to load the other buffers as well, do i have to specify glVertexAttribPointer()?

Thank you very much!

Xmas
08-19-2011, 03:16 AM
But i dont get why for example i have to call glEnableVertexAttribArray every time before doing the drawing with glDrawElements, althoug i dont touch any buffer.
You don't have to repeatedly call glEnableVertexAttribArray, unless you are calling any code which might disable it.


Same thing with the glVertexAttribPointer() call in the Initialization phase. If i move the call before the generation of VOB, i get a segment fault.
That's because glVertexAttribPointer sets up the vertex attribute to use the buffer object bound to GL_ARRAY_BUFFER at the time you call glVertexAttribPointer. If no buffer object is bound at that time, the pointer you pass to glVertexAttribPointer is interpreted as a pointer to client memory.

So if you want a vertex attribute to be fed data from a buffer object, that buffer object must already exist and be bound to GL_ARRAY_BUFFER when you call glVertexAttribPointer. You can, however, initialise or update data contained in the buffer object at a later time without having to call glVertexAttribPointer again.

ManuelP
08-22-2011, 07:07 AM
Hello!

thank you for the insight!