Tuesday, March 8, 2016

opengl - Should unbind buffers?



I'm making some tests with OpenGL ES 2 and got some questions, my current program is like that:


Init
-------
-> create index buffer
-> fill index buffer glBufferData …
-> create vertex buffer
-> fill vertex buffer glBufferData …

Draw
-------

1. Apply vertex buffer

-> Bind VAO
-> bind vertex buffer
- enable attributs (glVertexPointer, …)
-> unbind vertex buffer
-> Unbind VAO
-> Bind VAO

3. Apply index buffer

4. Draw

The problem


The given code crash, after some researches, I've understood why: I need to unbind my index buffer in init part (after "fill index buffer glBufferData") or unbind it before the first "Bind VAO"


My questions are:



  • Can I put my index buffer in VAO (VAO stock index buffer?)?

  • Did I have to unbind buffers after each update (glBufferData)?


In my application I've got some buffers who are updated on each frame (Exemple: Particles) so I've got an OpenGL stack like that:



-> bind buffer 1
-> update buffer 1
-> close buffer 1
-> bind buffer 1
-> draw

First 3 lines update the Vertex buffer, the two last draw object, that should be something like that:


-> bind buffer 1
-> update buffer 1
-> draw


Thanks



Answer



You seem to be doing a lot on unnecessary binding/unbinding. If you are using a VAO, then you should only bind the VAO when you set it up and when drawing the geometry. You only bind the VBO/IBO again when you need to update them.


After drawing or updating a buffer, you don't necessarily have to unbind it, though it might be a good idea to do so to avoid accidental writes to buffers that were left bound.


Now taking your fist sequence of operations, this is the overall order I would expect to see:


On init:




  1. Create & bind a VAO. Any VBO and IBO that you bind in the sequence will be associated with the current VAO (this one).





  2. Create & bind index buffer.



    • Fill index buffer with glBufferData/glMapBuffer.




  3. Create & bind vertex buffer.




    • Fill vertex buffer glBufferData/glMapBuffer.




  4. Set up vertex attributes with glEnableVertexAttribArray/glVertexAttribPointer, etc.




  5. Optionally unbind everything to avoid accidental modification of the buffers and VAO. Remember to unbind the VAO first. E.g.: glBindVertexArray(0);





On draw:




  1. If only drawing the buffers:



    • Bind the VAO;

    • Perform the draw call(s).





  2. If updating and drawing:



    • Bind the VAO, VBO, IBO;

    • Update the buffers (updating vertex atributes is only necessary if the vertex format has changed);

    • Perform the draw call(s).




  3. Optionally unbind to avoid accidental modification of the buffers and VAO.





That's as simple as that. This order of operations should function without problems.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...