我正在创建两个不同的顶点缓冲区,它们使用两个不同的着色器来渲染它们。一旦我绑定(bind)第二个顶点缓冲区,我存放在第一个顶点缓冲区中的数据似乎已损坏或丢失。
如果我只生成并绘制一个顶点缓冲区,就像这样:
glGenBuffers( 1, &vb1 ) ;
glBindBuffer( GL_ARRAY_BUFFER, vb1 ) ;
// fill it..
glBufferData( .. )
然后,在 draw() 循环中,
glUseProgram( shader1 ) ;
glBindBuffer( vb1 ) ; // make sure it is bound
glDrawArrays( ... ) // draw it
然后它工作正常,没有问题,没有错误(我在 every gl* 调用后是 glGettingLastError(),所以看起来这段代码完全没问题)
现在,如果我生成并绑定(bind)第二个顶点缓冲区,在第一个顶点缓冲区生成并绑定(bind)之后的任何时间,
// 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
填充了数组。
我不明白,我认为这些顶点缓冲区应该保留数据,即使创建并初始化了另一个顶点缓冲区也是如此? ..我做错了什么?
最佳答案
我发现这个问题的答案非常微妙且难以找到。
我以为我不需要the vertex array declarations that the standard example uses ,您可以看到我在问题中将它们排除在外。但事实证明使用 vertex array objects对正确绑定(bind)索引至关重要。
例如,
// 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 );
关于ios - 绑定(bind)第二个顶点缓冲区似乎破坏了我的第一个顶点缓冲区,OpenGL OES ios 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426507/