vertex buffer array being overwritten

Started by
6 comments, last by karwosts 13 years, 3 months ago

//init
glGenBuffers(1, VAB_name);
glBindBuffer(GL_ARRAY_BUFFER, *VAB_name);
glBufferData(GL_ARRAY_BUFFER, length_bytes, VAB_data, GL_STATIC_DRAW);

glNormalPointer( GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0+0)));
glTexCoordPointer(2, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0+3)));
glVertexPointer( 3, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0+5)));

glGenBuffers(1, VAB_name2);
glBindBuffer(GL_ARRAY_BUFFER, *VAB_name2);
glBufferData(GL_ARRAY_BUFFER, length_bytes2, VAB_data2, GL_STATIC_DRAW);

glNormalPointer( GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0+0)));
glTexCoordPointer(2, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0+3)));
glVertexPointer( 3, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0+5)));

//draw
glBindBuffer(GL_ARRAY_BUFFER, *VAB_name);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *VAB_indices_name);
glDrawElements(GL_TRIANGLE_STRIP, VAB_count, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

I don't understand why that second glBufferData is overwriting the first one. Shouldn't each array buffer have their own location in memory on the GPU?

*EDIT: put gl*Pointer functions back in

ps source lang tag isn't working how i remember and the FAQ seems to be absent
Advertisement
I'm sure it's not overwriting the first buffer, you probably just misunderstand how to use the glPointer* operations (they are global, and not per buffer). Changing the bound buffer before drawing is not how you're supposed to set the source data for glDrawElements.

Post the rest of your ... and I can correct you.

Also it's called 'code' tag now, not 'source' tag anymore.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
This is so confusing. I added the gl*Pointer functions in the draw code just before glDrawElements and now it works as expected. The first and second set of gl*Pointers can contain meaningless random offsets but the objects are not drawn if I remove them.

Why do I need to specify gl*pointers every frame if they are not changing? It seems that binding an array buffer wipes them out.
How did you define VAB_name, VAB_name2, VAB_indices_name? Can you post the code for that?
Latest project: Sideways Racing on the iPad

This is so confusing. I added the gl*Pointer functions in the draw code just before glDrawElements and now it works as expected. The first and second set of gl*Pointers can contain meaningless random offsets but the objects are not drawn if I remove them.

Why do I need to specify gl*pointers every frame if they are not changing? It seems that binding an array buffer wipes them out.



You don't have 'sets' of pointers. There is only one glVertexPointer, and it is global. You don't get a separate glVertexPointer per vbo. So if you want to draw something out of VBO #1, you:

1) bind vbo #1
2) set the pointers
3) glDrawElements

If you want to draw both buffers:

1) bind vbo #1
2) set pointers
3) glDrawElements
4) bind vbo #2
5) set pointers
6) glDrawElements.

You have to set it each time you want to change to a new buffer if you're not using VAO.

I'm not sure what you mean about "random offsets", but if you are confused post your latest code snippet and I can look.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

//init
glGenBuffers(1, VAB_name);
glBindBuffer(GL_ARRAY_BUFFER, *VAB_name);
glBufferData(GL_ARRAY_BUFFER, length_bytes, VAB_data, GL_STATIC_DRAW);

//draw doesn't work without these gl*Pointers but these offsets don't point to anything
glNormalPointer( GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(77777)));
glTexCoordPointer(2, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(88888)));
glVertexPointer( 3, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(99999)));

glGenBuffers(1, VAB_name2);
glBindBuffer(GL_ARRAY_BUFFER, *VAB_name2);
glBufferData(GL_ARRAY_BUFFER, length_bytes2, VAB_data2, GL_STATIC_DRAW);

//draw doesn't work without these gl*Pointers but these offsets don't point to anything
glNormalPointer( GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(77777)));
glTexCoordPointer(2, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(88888)));
glVertexPointer( 3, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(99999)));

//draw
//gl*pointers must be defined every frame even though the values are always the same
glNormalPointer( GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(0)));
glTexCoordPointer(2, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(3)));
glVertexPointer( 3, GL_FLOAT, stride, BUFFER_OFFSET(sizeof(float)*(5)));
glBindBuffer(GL_ARRAY_BUFFER, *VAB_name);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *VAB_indices_name);
glDrawElements(GL_TRIANGLE_STRIP, VAB_count, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

How did you define VAB_name, VAB_name2, VAB_indices_name? Can you post the code for that?


GLuint **name or GLuint name[]
What you've posted doesn't make any sense, I think you are incorrect. You're obviously not showing us what you're really doing, because your glGenBuffers is in the same flow as your draw command. Also you do not have to define gl*pointers every frame unless you are resetting them elsewhere.

Explain what you're actually trying to do (which buffer are you trying to render from?), and post real code, and I can help you understand, but right now you're making claims that don't make sense.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement