Jump to content
  • Advertisement
Sign in to follow this  
assainator

OpenGL Trouble using multiple VBO's

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone,

I'm having a bit of a problem. For some reason I can't display two different vbo's.
I've reduced my code to the following example (code below):
- A triangle and a quad.
- The triangle is drawn first, the quad second BUT only the quad is displayed.
- If I comment out the code that draws the quad, the triangle is displayed.
- If I swap the code that renders the triangle with the code that renders the quad, the opposite happens: The triangle is rendered, but the quad isn't.

I'm using OpenGL 2.0, I know that it's better to use 3.x or 4.x but for now I'm stuck with an integrated graphics chip that doesn't support higher.
Also, nothing else is drawn and I'm not using any shaders yet.

I have absolutely no idea what's causing this. It'll probably something simple/stupid but I appreciate any help you can give me.




This is the code I use for loading the data.
[source]
float Tri[9] = { -2, 1, -5,
-3, 0, -5,
-1, 0, -5};

float Quad[18] = {2, 0, -5,
3, 0, -5,
2, 1, -5,

3, 0, -5,
2, 1, -5,
3, 1, -5};

// Create two buffers
glGenBuffers(2, vboID);

// Load the triangle data
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*9, Tri, GL_STATIC_DRAW);

// Load the quad data
glBindBuffer(GL_ARRAY_BUFFER, vboID[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*18, Quad, GL_STATIC_DRAW);
[/source]

This is the code I use for rendering the vbo's:
[source]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();


glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(float) * 3, NULL);

// Draw triangle
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
glDrawArrays(GL_TRIANGLES, 0, 3);

// Draw quad
glBindBuffer(GL_ARRAY_BUFFER, vboID[1]);
glDrawArrays(GL_TRIANGLES, 0, 6);

glDisableClientState(GL_VERTEX_ARRAY);
[/source]
vboID is declared as:
unsigned int vboID[2];

Share this post


Link to post
Share on other sites
Advertisement
You need to set the pointer after binding the correct buffer. The pointer you pass to glVertexPointer is not relative to the bound buffer, but the pointer stored for drawing is the absolute pointer based on the VBO binding and the offset into that buffer.

That is, if you bind buffer A and call glVertexPointer, then bind buffer B and call glDrawElements, the vertex pointer is still pointing to the content of buffer A so the object in buffer A is drawn. It has to work like that if you ever want to have different attributes in different buffer objects, so you can bind each buffer and set the corresponding attribute pointer.

Share this post


Link to post
Share on other sites

You need to set the pointer after binding the correct buffer. The pointer you pass to glVertexPointer is not relative to the bound buffer, but the pointer stored for drawing is the absolute pointer based on the VBO binding and the offset into that buffer.

That is, if you bind buffer A and call glVertexPointer, then bind buffer B and call glDrawElements, the vertex pointer is still pointing to the content of buffer A so the object in buffer A is drawn. It has to work like that if you ever want to have different attributes in different buffer objects, so you can bind each buffer and set the corresponding attribute pointer.

Thanks, it's working like a charm now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!