PDA

View Full Version : Learning VBOs



allenmoatallen
01-02-2011, 11:37 PM
I am trying to learn VBOs and I have them sort of working but I wanted to get some clarification regarding glVertexPointers and glTexCoordPointers.



bool OGL_Renderer::drawImage(Image *image, int x, int y, float scale = 1.0f)
{
if(image)
{
GLfloat vertices[8] = {
x, y,
x + image->getWidth(), y,
x + image->getWidth(), y + image->getHeight(),
x, y + image->getHeight()
};

GLfloat texture[8] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};

glColor4ub(255, 255, 255, 255);

glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(texture), NULL, GL_STATIC_DRAW);

glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(texture), texture);

glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glBindTexture(mTextureTarget, getTextureId(image));

glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texture);

glDrawArrays(GL_QUADS, 0, 4);

glDisable(mTextureTarget);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, 0);

return true;
}

return false;
}


The last parameter of both should be the arrays that the coords are stored into right? I seem to have some confusion in this area.

Alfonse Reinheart
01-03-2011, 12:18 AM
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texture);

This makes no sense.

When a buffer object is bound to GL_ARRAY_BUFFER, the "pointer" functions no longer refer to client memory (ie: actual pointers). They now refer to byte offsets into the buffer object bound to GL_ARRAY_BUFFER.

The first byte of your buffer object stores your position data, so the byte offset should be 0. The same as how you uploaded it. The first byte where your texture coordinate data is is at "sizeof(vertices)". Again, just as how you uploaded it.

allenmoatallen
01-03-2011, 12:26 AM
Ahhhh that makes sense then. It should look like:



glVertexPointer(2, GL_FLOAT, 0, 0);
glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)(sizeof(vertices)));



That is what threw me off then, the fact it isn't talking about client-side at that point.