• 12
• 10
• 10
• 13
• 10

Problems with VBOs

This topic is 3691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Can someone help me find what I'm overlooking here? I'm expecting a white triangle, but getting just a blank screen. In my init method, I'm doing:
		vertices[0].x = 0.0f; vertices[0].y = 0.0f; vertices[0].z = 0.0f;
vertices[1].x = 1.0f; vertices[1].y = 0.0f; vertices[1].z = 0.0f;
vertices[2].x = 0.0f; vertices[2].y = 1.0f; vertices[2].z = 0.0f;

colors[0].x = 1.0f; colors[0].y = 1.0f; colors[0].z = 1.0f;
colors[1].x = 1.0f; colors[1].y = 1.0f; colors[1].z = 1.0f;
colors[2].x = 1.0f; colors[2].y = 1.0f; colors[2].z = 1.0f;

glGenBuffersARB(1, &vertexBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 3*sizeof(GLfloat), vertices, GL_STATIC_DRAW_ARB);

glGenBuffersARB(1, &colorBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, colorBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 3*sizeof(GLfloat), colors, GL_STATIC_DRAW_ARB);


And in my Draw method:
		glPushMatrix();
glTranslatef(0.0f, 0.0f, -5.0f);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer );
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, colorBuffer);
glColorPointer(3, GL_FLOAT, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glPopMatrix();


I thought this would be a simple example to get VBOs working and build up to more complex things, but I'm having some trouble with it.

Share on other sites
You don't copy enough data into the VBO; you only tell it to copy 3 floats worth of data (colour and position), which is one point of the triangle. You need to copy 9 floats in this instance.

Share on other sites
D'oh

Thanks. I'd been working with this all afternoon and just needed another set of eyes. Or a break. Thanks, good call.

Share on other sites
This is related to my question above, so I figured I'd ask here. I'm drawing a simple textured quad using the VBO method I used above, here is my init():
	myVertices[0].x = -1.0f; myVertices[0].y = 1.0f; myVertices[0].z = 0.0f;	myVertices[0].r = 1.0f; myVertices[0].g = 0.0f; myVertices[0].b = 0.0f;	myVertices[0].s = 1.0f; myVertices[0].t = 0.0f;	myVertices[1].x = -1.0f; myVertices[1].y = -1.0f; myVertices[1].z = 0.0f;	myVertices[1].r = 1.0f; myVertices[1].g = 0.0f; myVertices[1].b = 0.0f;	myVertices[1].s = 0.0f; myVertices[1].t = 0.0f;	myVertices[2].x = 1.0f; myVertices[2].y = -1.0f; myVertices[2].z = 0.0f;	myVertices[2].r = 1.0f; myVertices[2].g = 0.0f; myVertices[2].b = 0.0f;	myVertices[2].s = 0.0f; myVertices[2].t = 1.0f;	myVertices[3].x = 1.0f; myVertices[3].y = 1.0f; myVertices[3].z = 0.0f;	myVertices[3].r = 1.0f; myVertices[3].g = 0.0f; myVertices[3].b = 0.0f;	myVertices[3].s = 1.0f; myVertices[3].t = 1.0f;	glGenBuffersARB(1, &myVertexBuffer);	glBindBufferARB(GL_ARRAY_BUFFER_ARB, myVertexBuffer);	glBufferDataARB(GL_ARRAY_BUFFER_ARB, 8*4*sizeof(GLfloat), myVertices, GL_STATIC_DRAW_ARB);

And again here is my draw():
	glEnable(GL_TEXTURE_2D);	glEnable(GL_BLEND);	glBindTexture(GL_TEXTURE_2D, myTexture->GetGLTexture());	glPushMatrix();	glLoadIdentity();	glTranslatef(x, y, z);	glRotatef(theta, rx, ry, rz);	glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_COLOR_ARRAY);	glEnableClientState(GL_TEXTURE_COORD_ARRAY);	glBindBufferARB(GL_ARRAY_BUFFER_ARB, myVertexBuffer );	glVertexPointer(3, GL_FLOAT, 8*sizeof(GLfloat), 0);	glColorPointer(3, GL_FLOAT, 8*sizeof(GLfloat), BUFFER_OFFSET(3*sizeof(GLfloat)));	glTexCoordPointer(2, GL_FLOAT, 8*sizeof(GLfloat), BUFFER_OFFSET(6*sizeof(GLfloat)));	glDrawArrays(GL_QUADS, 0, 4);	glDisableClientState(GL_TEXTURE_COORD_ARRAY);	glDisableClientState(GL_COLOR_ARRAY);	glDisableClientState(GL_VERTEX_ARRAY);	glPopMatrix();	glDisable(GL_TEXTURE_2D);	glDisable(GL_BLEND);	glDisable(GL_TEXTURE_2D);

The problem I'm having is I've had to move the texture coordinates around to get the texture to look right. When I used (0,1) as the top left, and (1,0) as the bottom right, the texture showed up mirrored, then rotated 90 degrees. Currently you can see if I use (1,0) as the top left, and (0,1) as the bottom right, it looks ok. I should note that I'm only translating -5 units along the z axis, and theta = rx = ry = rz = 0. I'm using an array of structures containing all my vertex data, and I'm pretty sure I have the glXPointer() functions correct, does anyone know why the texture coordinates are flipped around from what I would use if rendering the same quad in immediate mode with glTexCoord() and glVertex() functions?

Share on other sites
Not using QT or something for image loading, I know that flips OpenGL images for some stupid reason.

0, 0 should be bottom left, so work on that basis and i think the image is correct, just the coordinate system of yours is off.

It is 3am in the morning here, so forgive me if its wrong :P

Neutrinohunter

Share on other sites
Depending on your texture loader, you can have rotated and flipped images. OpenGL uses the lower left for origin position vs. upper left for win32/DX. IMO lower left is better as it syncs with the Cartesian coordinate system. So you will need to modify the texture coordinates as needed or do the flipping, rotating at load time on the imagedata.