我有一个带有纹理的立方体,当我将我的数据数组更改为VBO(glGen等)时,我的立方体渲染为灰色。但是如果我使用类似glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, myBuf);
的东西,一切都可以。什么问题?请帮帮我。
- (void)render:(CADisplayLink*)displayLink {
glClearColor(0.0f, 1.0f,0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:2 andFar:4];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -2)];
_currentRotation += displayLink.duration * 90;
[modelView rotateBy:CC3VectorMake(_currentRotation, _currentRotation, -1)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glUniform1i(uniformTexture, 0);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(_positionSlot);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndexes);
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, 0, 0, texCoord);
glEnableVertexAttribArray(ATTRIB_TEXCOORD);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (void*)0);
//glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
答案 0 :(得分:0)
您需要使用客户端状态在顶点缓冲区上应用详细信息,请参阅glEnableClientState和glClientActiveTexture手动条目。
尝试以这种方式更改代码。
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glEnableClientState(GL_VERTEX_ARRAY); // NEW! we need to enable client state.
glActiveClientTexture(GL_TEXTURE0); // CHANGED! We need to use textures in the client state.
另见How to call glDrawElements with static TexCoords and Dynamic Vertices
您使用的是哪个版本的OpenGL?我希望OpenGL ES 2.0