I am working on a 2D game in C++, with a simple rendering layer over OpenGL.

I have a wrapper for GLSL shaders and shader programs, these wrappers encapsulate shader objects into C++ classes and automatically handle loading/compiling/linking and using shader programs.

VertexArrayObjects, it seems to me, are basically a collection of configuration settings to use with a particular ShaderProgram.

My question is: Should I encapsulate VAO's within my ShaderProgram objects, or within the Sprite objects? Does a VAO change on a per-shader basis or do I need a new VAO for every type of drawable object? If the latter, do I need one VAO to draw all Sprites (each containing 4 vertices, a position/orientation etc), or one VAO per sprite object in the game?


No, you shouldn't attach the VAO to a Shader Program. The VAO has to be linked with a Vertex Buffer, which makes it somewhat not shareable. You can only share it to the same extent you would share a Vertex Buffer. Whereas a Shader Program is highly shareable (you may be able to render all objets with the same program).

The purpose of the VAO is to reduce some API overhead when setting a Vertex Buffer and Vertex Format, so you can think of it as a container for a Vertex Buffer and its state.

It is up to you how you want to lay it out on code, but my suggestion would be having 3 distinct classes: VertexBuffer, VertexFormat and VAO.

Give a unique VAO to each mesh/model and then attach shared VertexBuffers and VertexFormats to them.

  • \$\begingroup\$ What functionality would we wrap in the VertexFormat? \$\endgroup\$ – Sam Coulter Apr 21 '14 at 21:06
  • 1
    \$\begingroup\$ The "VertexFormat" in OpenGL is what you pass to the glVertexAttribPointer calls. \$\endgroup\$ – glampert Apr 21 '14 at 21:13
  • 1
    \$\begingroup\$ Well, actually. There literally is a vertex format in GL 4.3 (as part of ARB_vertex_attrib_binding), but I suspect that is not what the OP is asking about. glVertexAttribPointer (...) does setup the format by its very nature, but it also does some extra things. \$\endgroup\$ – Andon M. Coleman Apr 22 '14 at 2:25
  • \$\begingroup\$ @AndonM.Coleman out of curiosity what do you mean by extra things? \$\endgroup\$ – Sam Coulter Apr 22 '14 at 23:49
  • 1
    \$\begingroup\$ @SamCoulter: Well, it sets up the binding for the vertex buffer in addition to the format. ARB_vertex_attrib_binding broke the process that glVertexAttriPointer (...) performs into two separate commands for enhanced flexibility. \$\endgroup\$ – Andon M. Coleman Apr 22 '14 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.