使用两个VBO导致仅绘制第二个

时间:2014-05-06 22:53:00

标签: ios opengl-es-2.0 glkit

我遇到了一个问题,我试图在glkviewcontroller中使用两个VBO。第一个保存三角形的顶点数据。第二个包含行的数据。但是,当我尝试绑定第二个vbo时,代码停止绘制(或覆盖)第一个VBO中包含的场景 - 即只绘制线条。

在我的绘画方法中:

首先我绘制三角形

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

[self.effect prepareToDraw];

glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, nLiveSquares*6, GL_UNSIGNED_SHORT, 0);

现在我想绘制线条

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

glBindBuffer(GL_ARRAY_BUFFER, _lineBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,2,GL_FLOAT,GL_FALSE,2*4,0);

// Set the line width
glLineWidth(5.0);

// Render the line
glDrawArrays(GL_LINE_STRIP, 0, 2);

glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);

如何同时绘制三角形和线条?我是否正确地认为每个人都应该拥有自己的VBO?

1 个答案:

答案 0 :(得分:0)

我通过在 glBindBuffer / glEnableVertexAttribArray之前将呼叫转移到glVertexAttribPointer 来解决了问题:

glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),     (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));