4
votes

I'm creating two different vertex buffers, that use two different shaders to render them. As soon as I bind the second vertex buffer, the data I parked in the first vertex buffer seems to be corrupted or lost.

If I generate and draw one vertex buffer only, like so:

glGenBuffers( 1, &vb1 ) ;
glBindBuffer( GL_ARRAY_BUFFER, vb1 ) ;

// fill it..
glBufferData( .. )

Then, in the draw() loop,

glUseProgram( shader1 ) ;
glBindBuffer( vb1 ) ; // make sure it is bound
glDrawArrays( ... )   // draw it

Then it works fine, no problems, no errors (I am glGettingLastError() after every gl* call, so it seems this code is perfectly fine)

Now if I so much as generate and bind a second vertex buffer, any time after the first one was generated and bound,

// ran in init() -- then previously working code in draw() is broken
glGenBuffers( 1, &vb2 ) ;              // ok.. no problems with this line
glBindBuffer( GL_ARRAY_BUFFER, vb2 ) ; // spoils data in vb1?

As soon as I call glBindBuffer with this newly generated vb2 buffer, it seems the data in vb1 is completely dumped or lost. On attempts to draw vb1 (not vb2!), i get this crash:

enter image description here

I even filled the array with GL_STATIC_DRAW.

I don't understand, I thought these vertex buffers were supposed to retain data, even if another vertex buffer is created and initialized? .. what am I doing wrong?

1
what am I doing wrong? It's hard to say, considering that you've provided so little code, and the code you have put up is non-functional (binding a buffer has no effect on DrawArrays, only setting pointers controls what data is drawn). Can you show what you're actually doing?Tim
Admittedly, this isn't a very good question. I'll delete it.bobobobo
We're locked out of Meta at the moment, but I undeleted this question for you, so you're good to edit it now.Brad Larson

1 Answers

4
votes

I found the answer to this question to be very subtle and difficult to find.

I thought I didn't need the vertex array declarations that the standard example uses, and you can see I left them out in the question. But it turns out using vertex array objects is vital to correctly binding indices.

So for example,

// Make vertex array OBJECT, _this does not store any data!_
// A "vertex array object" is a bit like a display list in
// that it will just remember and repeat the function calls you make,
// with the values you make them with.
glGenVertexArraysOES( 1, &va ) ; // make vertex array OBJECT. NOT used
// to store data..

// Bind the vertex array
glBindVertexArrayOES( va ) ; // "start recording the calls I make.."

// Make a buffer for the vertex array (place to store the data)
glGenBuffers( 1, &vb ) ;     // make the vertex BUFFER (used to
// store the actual data)

// Make the vertex array buffer active
glBindBuffer( GL_ARRAY_BUFFER, vb ) ;

glEnableVertexAttribArray( ... ) ;  // REMEMBERED IN VAO (vertex array object)
glVertexAttribPointer( ... ) ;      // REMEMBERED IN VAO

glBufferData( .. ) ; // send data down to gpu, not remembered by vao

// "Stop remembering" calls to glBindBuffer( .. ) and glEnableVertexAttribArray( .. )
glBindVertexArrayOES( 0 ) ; // "stop recording"

At draw time, simply:

glBindVertexArrayOES( va ) ; // runs ALL your glEnableVertexAttribArray() calls,
// as well as your glBindBuffer( .. ) call,

// now all that's left to do is draw
glDrawArrays( glDrawingStyle, 0, vertexCount );