上面的 John Riselvato 评论说他真的希望他能看到更新:方法内容。我真的很希望自己能看到这一点,因为在使用相同的 GLKBaseEffect 对象时,我很难将头绕在屏幕上放置多个对象。这是我最终做到的方式:
// update method where I create 3 different modelViewMatrices to use with my GLKBaseEffect object later in the draw method
- (void)updateWithDeltaTime:(NSTimeInterval)deltaTime
{
_rotation1 += 120.0 * deltaTime;
_rotation2 += 150.0 * deltaTime;
_rotation3 += 180.0 * deltaTime;
GLKMatrix4 modelViewMatrix = GLKMatrix4Identity;
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, -1.0f, 0.0f, -4.0f);
modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(90.0f));
modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotation1));
_modelViewMatrix1 = modelViewMatrix;
modelViewMatrix = GLKMatrix4Identity;
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, 0.0f, 0.0f, -4.0f);
modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(90.0f));
modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotation2));
_modelViewMatrix2 = modelViewMatrix;
modelViewMatrix = GLKMatrix4Identity;
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, 1.0f, 0.0f, -4.0f);
modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(90.0f));
modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotation3));
_modelViewMatrix3 = modelViewMatrix;
}
// draw method where I re-use my GLKBaseEffect object, applying the 3 modelViewMatrices I set in the update: method above
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_effect.transform.modelviewMatrix = _modelViewMatrix1;
[_effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer1);
glDrawArrays(GL_TRIANGLES, 0, cylinderVertices);
_effect.transform.modelviewMatrix = _modelViewMatrix2;
[_effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer2);
glDrawArrays(GL_TRIANGLES, 0, cylinderVertices);
_effect.transform.modelviewMatrix = _modelViewMatrix3;
[_effect prepareToDraw];
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer3);
glDrawArrays(GL_TRIANGLES, 0, cylinderVertices);
}
// how i initially setup my GLKBaseEffect object and my opengl buffers
- (void)_setupGL
{
[EAGLContext setCurrentContext:_context];
//glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
_effect = [[GLKBaseEffect alloc] init];
[self _createEffect];
[_effect prepareToDraw];
glGenBuffers(1, &_vertexBuffer1);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer1);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &_vertexBuffer2);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer2);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &_vertexBuffer3);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer3);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
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, Texel));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)offsetof(Vertex, Normal));
}
希望这可以帮助某人。