【问题标题】:Binding a second vertex buffer seems to spoil my first vertex buffer, OpenGL OES ios 5.1绑定第二个顶点缓冲区似乎破坏了我的第一个顶点缓冲区,OpenGL OES ios 5.1
【发布时间】:2012-09-14 14:25:22
【问题描述】:

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

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

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 填充了数组。

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

【问题讨论】:

  • 我做错了什么? 很难说,考虑到你提供的代码这么少,而且你放的代码没有功能(绑定一个buffer 对 DrawArrays 没有影响,仅设置指针控制绘制的数据)。你能展示你实际在做什么吗?
  • 诚然,这不是一个很好的问题。我会删除它。
  • 我们目前被锁定在 Meta 之外,但我已为您取消删除此问题,所以您现在可以编辑它。

标签: ios opengl-es opengl-es-2.0 vertex-buffer


【解决方案1】:

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

我以为我不需要the vertex array declarations that the standard example uses,你可以看到我在问题中忽略了它们。但事实证明,使用vertex array objects 对正确绑定索引至关重要。

例如,

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

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多