Why is this OpenGL ES code slow on iPhone?

Posted by f3r3nc on Stack Overflow See other posts from Stack Overflow or by f3r3nc
Published on 2009-01-16T10:46:03Z Indexed on 2010/03/28 7:33 UTC
Read the original article Hit count: 310

Filed under:
|

I've slightly modified the iPhone SDK's GLSprite example while learning OpenGL ES and it turns out to be quite slow. Even in the simulator (on the hw worst) so I must be doing something wrong since it's only 400 textured triangles.

const GLfloat spriteVertices[] = {
  0.0f, 0.0f, 
  100.0f, 0.0f,  
  0.0f, 100.0f,
  100.0f, 100.0f
};

const GLshort spriteTexcoords[] = {
  0,0,
  1,0,
  0,1,
  1,1
};

- (void)setupView {
	glViewport(0, 0, backingWidth, backingHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrthof(0.0f, backingWidth, backingHeight,0.0f, -10.0f, 10.0f);
	glMatrixMode(GL_MODELVIEW);

	glClearColor(0.3f, 0.0f, 0.0f, 1.0f);

	glVertexPointer(2, GL_FLOAT, 0, spriteVertices);
	glEnableClientState(GL_VERTEX_ARRAY);
	glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	// sprite data is preloaded. 512x512 rgba8888	
	glGenTextures(1, &spriteTexture);
	glBindTexture(GL_TEXTURE_2D, spriteTexture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
	free(spriteData);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glEnable(GL_TEXTURE_2D);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
} 

- (void)drawView {
  ..
  	glClear(GL_COLOR_BUFFER_BIT);
  	glLoadIdentity();
  	glTranslatef(tx-100, ty-100,10);
  	for (int i=0; i<200; i++) {	
		glTranslatef(1, 1, 0);
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  	}
  ..
}

drawView is called every time the screen is touched or the finger on the screen is moved and tx,ty are set to the x,y coordinates where that touch happened.

I've also tried using GLBuffer, when translation was pre-generated and there was only one DrawArray but gave the same performance (~4 FPS).

===EDIT===

Meanwhile I've modified this so that much smaller quads are used (sized: 34x20) and much less overlapping is done. There are ~400 quads->800 triangles spread on the whole screen. Texture size is 512x512 atlas and RGBA_8888 while the texture coordinates are in float. The code is very ugly in terms of API efficiency: there are two MatrixMode change along with two loads and two translation then a drawarrays for a triangle strip (quad). Now this produces ~45 FPS.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about opengl