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
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Draw
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawArrays(GL_QUADS, 0, 4);
// Disable arrays
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
I ran across this tutorial on the OpenGL wiki: Tutorial: OpenGL 3.1 The First Triangle (C++/Win)
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:
[ol]
[li]Do I need to create a new VAO/VBO for each of those shapes?
[/li][li]I assume a simple glBindTexture() call before drawing each item using a different texture would be sufficient. Am I correct?
[/li][li]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?
[/li][/ol]
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.