Im using openGL with GLKit to make a tiles-based game. In the game, around 1024 tiles are placed in the screen, the problem is that I'm rendering the tiles every time drawInRect is called, and I'm having a lost of performance very big.
This is how I render (tilesArray is static, ballsArray is moving around the screen):
- (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];
}
}
As I said, tilesArray is static in the screen, but ballsArray is moving around the screen very fast (with every update). If I put only the balls, it goes very fast, if I add the tiles, it goes very slow (only tried in simulator)
This is how I render:
- (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);
}