1

This is a theoretical question, so I don't have code until now.

Assuming that I have a VBO with vertex position data and am using it within a VAO to render an indexed (glDrawElements()) figure out of triangles with a special index array.

Now I want to reuse this data buffer within a second VAO to render some other figures consisting out of lines, but with a different index array.

How do I need to bind the buffers, so that I can reuse the vertex data of the first VAO?

2
  • Idk why, but to me it makes more sense to me to create multiple element arrays and change them than multiple vao's. Commented May 8, 2014 at 13:17
  • It seems to me a bit more modular, but I will think again about it. Thanks. Commented May 8, 2014 at 14:59

1 Answer 1

4

no special code is needed to share the buffers between VAOs just bind them as you normally would.

setting up:

glBindVertexArray(VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, locBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangleBuffer);
glVertexAttrPointer(...);

glBindVertexArray(VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, locBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lineBuffer);
glVertexAttrPointer(...);

and when drawing:

glBindVertexArray(VAO[0]);
glDrawElements(...);

glBindVertexArray(VAO[1]);
glDrawElements(...);
Sign up to request clarification or add additional context in comments.

4 Comments

To streamline it further, you can bind the ELEMENT_ARRAY_BUFFER in the setup as well. That binding is part of the VAO state. Then you only need the glBindVertexArray() call before drawing.
@RetoKoradi wasn't sure about that, the docs are quite ambiguous about that.
I think it's running now. I will test it further and inform you. Thanks.
@ratchetfreak: It's not extensively documented. One conclusive data point is table 6.5 in the OpenGL 3.3 spec. The table is labeled "Vertex Array Object State", and contains ELEMENT_ARRAY_BUFFER_BINDING. This contrasts with table 6.6, labeled "Vertex Array Data (not in Vertex Array objects)", which contains ARRAY_BUFFER_BINDING.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.