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

2024-03-21

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

如果我只生成并绘制一个顶点缓冲区,如下所示:

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

// fill it..
glBufferData( .. )

然后,在draw()循环中,

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

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

现在如果我如此生成并绑定第二个顶点缓冲区,在第一个顶点缓冲区生成并绑定后的任何时间,

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

我一打电话glBindBuffer有了这个新生成的vb2缓冲区,看来数据在vb1被完全倾倒或丢失。关于绘画的尝试vb1 (not vb2!),我遇到了这个崩溃:

我什至用以下内容填充了数组GL_STATIC_DRAW.

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


我发现这个问题的答案非常微妙且难以找到。

我以为我不需要标准示例使用的顶点数组声明 https://stackoverflow.com/questions/12428473/in-the-stock-opengl-es-app-on-ios-5-1-are-they-really-using-the-vertex-arrays-t,你可以看到我在问题中遗漏了它们。但事实证明使用顶点数组对象 https://stackoverflow.com/a/12429065/111307对于正确绑定索引至关重要。

例如,

// 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 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐