绑定第二个顶点缓冲区似乎破坏了我的第一个顶点缓冲区,OpenGL OES ios 5.1

我创build了两个不同的顶点缓冲区, 使用两个不同的着色器来渲染它们 。 只要我绑定第二个顶点缓冲区,我停在第一个顶点缓冲区的数据似乎已经损坏或丢失。

如果我只生成和绘制一个顶点缓冲区,像这样:

glGenBuffers( 1, &vb1 ) ; glBindBuffer( GL_ARRAY_BUFFER, vb1 ) ; // fill it.. glBufferData( .. ) 

然后,在draw()循环中,

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

然后它工作正常,没有问题,没有错误(我 gl *调用glGettingLastError(),所以看来这个代码是完全没问题)

现在,如果我生成并绑定了第二个顶点缓冲区,那么在第一个顶点缓冲区生成并绑定之后的任何时候,

 // 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? 

只要我用这个新生成的vb2缓冲区调用glBindBuffer ,似乎在vb1的数据完全转储或丢失。 试图绘制vb1 (而不是vb2 !),我得到这个崩溃:

在这里输入图像说明

我甚至用GL_STATIC_DRAW填充了数组。

我不明白,我认为这些顶点缓冲区应该保留数据,即使另一个顶点缓冲区被创build和初始化? .. 我究竟做错了什么?

我发现这个问题的答案很微妙,很难find。

我以为我不需要标准示例使用的顶点数组声明,你可以看到我把它们留在问题中。 但事实certificate,使用顶点数组对象对正确绑定索引至关重要。

所以例如,

 // 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" 

在抽签的时候,简单地说:

 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 );