我将 openGL 与 GLKit 一起使用来制作基于图块的游戏。在游戏中,屏幕中放置了大约 1024 个图块,问题是我每次调用 drawInRect 时都在渲染图块,并且性能损失非常大。
这就是我的渲染方式(tilesArray 是静态的,ballsArray 在屏幕上移动):
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
for (int i = 0; i < [self.tilesArray count]; i++){
RPSprite *tempTile = [self.tilesArray objectAtIndex:i];
[tempTile render];
}
for (RPSprite *ball in self.ballsArray){
[ball render];
}
}
正如我所说,tilesArray 在屏幕中是静态的,但 ballsArray 在屏幕上移动得非常快(每次更新)。如果我只放球,它会非常快,如果我添加瓷砖,它会非常慢(仅在模拟器中尝试过)
这就是我渲染的方式:
- (GLKMatrix4) modelMatrix {
GLKMatrix4 modelMatrix = GLKMatrix4Identity;
modelMatrix = GLKMatrix4Translate(modelMatrix, self.position.x,320 - self.position.y - self.textureInfo.height, 0);
return modelMatrix;
}
- (void)render {
self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;
self.effect.transform.modelviewMatrix = self.modelMatrix;
[self.effect prepareToDraw];
long offset = (long)&_quad;
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}