Sign in to follow this  
storage

Optimizing rendering

Recommended Posts

I'm currently trying to optimize the way my engine renders it's 2D tiles. It is pretty fast already (around 800-900 frames per second when rendering 2000 32x32 particles), but any improvement is great :) Each sprite has a rotation and scaling value, which determines if the sprite should be rotated and scaled. Code:
	glBindTexture(GL_TEXTURE_2D, *texture[sprite->texture]);
	
	// Rotate
	if (sprite->Rotation() != 0 && sprite->Rotation() != 360)
	{
		glMatrixMode(GL_TEXTURE);
		glRotatef(sprite->Rotation(), 0, 0, 1);
		glMatrixMode(GL_MODELVIEW);
	}
	
	// Scale
	if (sprite->Scaling() != 1.0f && !ScalingScene)
		glScalef(sprite->Scaling(), sprite->Scaling(), 1);
	
	int sx = x / sprite->Scaling();
	int sy = y / sprite->Scaling();
	
	// Draw
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(sx, sy);
		glTexCoord2f(1.0f, 0.0f); glVertex2f(sx + sprite->w, sy);
		glTexCoord2f(1.0f, 1.0f); glVertex2f(sx + sprite->w, sy + sprite->h);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(sx, sy + sprite->h);
	glEnd();
	
	// Scale back
	if (sprite->Scaling() != 1.0f && !ScalingScene)
		glScalef(1.0f / sprite->Scaling(), 1.0f / sprite->Scaling(), 1);
	
	// Rotate back
	if (sprite->Rotation() != 0 && sprite->Rotation() != 360)
	{
		glMatrixMode(GL_TEXTURE);
		glRotatef(360 - sprite->Rotation(), 0, 0, 1);
		glMatrixMode(GL_MODELVIEW);
	}

If there is anything I could improve, feel free to post :) Thanks in advance!

Share this post


Link to post
Share on other sites
You could use glPushMatrix & glPopMatrix instead of returning to glMatrixMode(GL_MODELVIEW). Not sure if this will speed stuff up though.

Are the tiles static? Do you change their angle and size all the time? If no, you could load them once into a display list.

Share this post


Link to post
Share on other sites
Do not bind a separate texture for all tiles, instead have only a couple of big texture maps (with all the small tiles), with different uv-coordinates...

this way, bind the texture before drawing the tiles, then draw all tiles in order etc...

Share this post


Link to post
Share on other sites
Just out of curiosity why are you looking at optimizing it more ? It already is running at good speed. You won't gain anything with more optimization.

Well having a couple of big texture maps, prioritize the textures and this could improve the rendering. However you could also sort the textures and tiles which can improve the rendering. However I don't see the necessity with the framerate you are currently getting. Why not optimize when required ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this