Open GL - ES 2.0:多个顶点缓冲区对象

时间:2012-02-09 15:37:55

标签: iphone opengl-es opengl-es-2.0

我对openGL ES 2.0有一个快速的问题我试图在屏幕上创建一些需要多个几何图形的图纸。这就是我创建几何的方法。

(void)setupBoxGeometry{

   GLuint vertexBuffer;
   glGenBuffers(1, &vertexBuffer);
   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizeof(BoxVertices), BoxVertices, GL_STATIC_DRAW);

   GLuint indexBuffer;
   glGenBuffers(1, &indexBuffer);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(BoxIndices), BoxIndices, GL_STATIC_DRAW);
}

- (void)setupLineGeometry{

   GLuint vertexBuffer;
   glGenBuffers(1, &vertexBuffer);
   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizeof(LineVertices), LineVertices, GL_STATIC_DRAW);

   GLuint indexBuffer;
   glGenBuffers(1, &indexBuffer);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(LineIndices), LineIndices, GL_STATIC_DRAW);
}

这是我的渲染功能

- (void)render:(CADisplayLink*)displayLink {
   glClearColor(0.0/255.0, 0.0/255.0, 0.0/255.0, 1.0);
   glClear(GL_COLOR_BUFFER_BIT);

   float h = 4.0f * self.frame.size.height / self.frame.size.width;
   glViewport(0, 0, self.frame.size.width, self.frame.size.height);

   glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 
                      sizeof(Vertex), 0);
   glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 
                      sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));


   // Do some thing to the projection    
   CC3GLMatrix *projection = [CC3GLMatrix matrix];    
   [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
   glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);


   // Create the base for the square to be drawn on the screen    
   _baseMatrix = [CC3GLMatrix matrix];


   [_baseMatrix populateFromTranslation:CC3VectorMake(-3.0, -5.2 + _yIncrease, -10)];    
   [_baseMatrix scaleByX:0.5];
   [_baseMatrix scaleByY:_yIncrease];
   [_baseMatrix rotateBy:CC3VectorMake(0, _rotationAngle, 0)];
   glUniformMatrix4fv(_modelViewUniform, 1, 0, _baseMatrix.glMatrix);
   glDrawElements(GL_TRIANGLES, sizeof(BoxIndices)/sizeof(BoxIndices[0]), 
               GL_UNSIGNED_BYTE, 0);
}

在调用线几何函数之前,我调用了框几何函数。问题是现在我的渲染功能只绘制线条。它不会绘制任何框。我究竟做错了什么。如果问题不明确,我可以给予更多澄清/

1 个答案:

答案 0 :(得分:3)

在绘图之前你在哪里绑定缓冲区? 致电

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

之前

glVertexAttribPointer(...);

并致电

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

glDrawElements(...);

您还需要启用正在使用的顶点指针:

glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);