View Full Version : VAO/VBO with multiple non-static objects

10-13-2012, 11:04 AM
Hello everyone! This marks my first post here, and ideally not my last.

I have been teaching myself OpenGL in my spare time as a hobby and I decided it was time to learn about using VAO and VBO.

In a current test program, I am currently only using a VBO to render a static quad with a texture on it. (This is not the complete source, only the relevant source)

#include <GL/glew.h>
#include <GL/glext.h>
#include <GL/glfw.h>
#include <GL/SOIL.h>

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

GLfloat myVertices[] =
0, 200,
200, 200,
200, 0,
0, 0

GLfloat myTexVertices[] =
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0

GLuint buf[2];

// ...

// Assign data from arrays to buffers
glGenBuffers(2, &buf);
glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(myVertices), myVertices, GL_STREAM_DRAW_ARB);

glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(myTexVertices), myTexVertices, GL_STATIC_DRAW);

// Bind vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glVertexPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

// Texture buffer
glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

// ...

// Enable vertex arrays

// Draw
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawArrays(GL_QUADS, 0, 4);

// Disable arrays

I ran across this tutorial on the OpenGL wiki: Tutorial: OpenGL 3.1 The First Triangle (C++/Win) (http://www.opengl.org/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_%28C%2B%2B/Win%29)

I understand that to get set up for 3.1, I need to add the usage of VAO and change some of my function calls. But I read something that got me thinking:

VAO saves all states for all vertex attributes. The maximum number supported by your video card can be obtained by calling glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &MaxVertexAttribs).

On my machine, that returned 29. I am able to use 29 VAOs on my machine. From what I understand, you need a different VAO for each object that you are rendering. This cannot be right, but I'm not sure what else would be valid.

I shall illustrate an image of what it is I'm trying to do:

The grey outlines are imaginary borders, representing the boundaries of the object's texture. Every other object on the screen, with the exception of the brown squares, represent a different object with different logic handling it's type of movement. The brown squares are static, they do not move. I plan on using the viewport and moving it around when necessary. I am using a 2d orthogonal view.

I'll summarize my questions/concerns in a list:

Do I need to create a new VAO/VBO for each of those shapes?
I assume a simple glBindTexture() call before drawing each item using a different texture would be sufficient. Am I correct?
How do I set up each vertex buffer such that if the position of an object changes, it is updated? DO I need to recreate and rebind the buffer in place of the old one or is there a way to update it?

I am all for learning. If you would do something differently, please let me know. If I am wrong in my assumptions, also please let me know. If you require any additional information, I will be gladly to provide it.

Thank you for your time.

Steven McGrath
11-11-2012, 02:48 PM
GL_MAX_VERTEX_ATTRIBS provides the number of distinct vertex attributes that can be read by your shader. It is not a limit on the number of of VAOs. In your case your only vertex attributes appear to be the locations and one set of texture coordinates, so the limit is of no concern. :)