Archived

This topic is now archived and is closed to further replies.

Catfish

How can I speed up this OpenGL routine?

Recommended Posts

Catfish    122
Heya....I was wondering if anyone could offer me some advice on speeding up some code which appears to be the major bottleneck in my program. Its the graphics drawing part, which I appreciate is going to be a bottleneck anyway, but if I''m doing something that might be a bit slow, I want to know.... Anyway - this function is called every frame:
  
int DrawGLScene(GLvoid)	// Here''s Where We Do All The Drawing

{
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer

	glBindTexture(GL_TEXTURE_2D, texture[0]);		// Select Our Texture


	glLoadIdentity();				// Reset The View Before We Draw the particles

	glTranslatef(0.0f,0.0f,zoom);			// Zoom Into The Screen (Using The Value In ''zoom'')

	glRotatef(rotate,0.0f,1.0f,0.0f);   //rotate view	


	for (int i=0; i<pList.size(); i++)				// Loop Through All The particles

	{	
		glPushMatrix();
		pList[i]->translate();
		glRotatef(-rotate,0.0f,1.0f,0.0f);   //rotate view back to facing screen - draw particles face on

		pList[i]->drawParticle(particleDisplay);  
		glPopMatrix();
	};	
	return TRUE;								// Everything Went OK

}
  
OK - some explanation might be called for. Its a particle engine. pList is a vector of pointers to each particle object. Each frame, I reset the view matrix, zoom into the screen & rotate according to user set variables, then enter a loop that goes through each particle, translating to the particles location:
  
void particle::translate()
{	
	glTranslatef(loc.x,loc.y,loc.z);
};
  
then rotating backwards to billboard the particle, & drawing the particle:
  
void particle::drawParticle(GLuint& displayList)
{	
	if(lifespan > 5)
		glColor4ub(r,g,b,255);			//Draw at full color

	else 
		glColor4ub(r,g,b,255*lifespan/5);  //Fade out while dying


	glCallList(displayList);
};
  
The top part sets the color, including a fade out as the particle nears the end of its life. Then I use a display list to draw the particle. The display list was defined in startup as
  
GLvoid BuildLists()					// Build Box Display List

{
	particleDisplay=glGenLists(1);				// Building One List

	glNewList(particleDisplay,GL_COMPILE);			// New Compiled box Display List

	glBegin(GL_TRIANGLE_STRIP);			// Begin Drawing The Textured Quad

		glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.25f, 0.25f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, 0.25f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.25f,-0.25f, 0.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f,-0.25f, 0.0f);
	glEnd();				// Done Drawing The Textured Quad	

	glEndList();
};	
  
Hmmm - that was a lot of code. So, assuming you''ve read this far - any hints? I was wondering if I could group those matrix translations & rotations into one operation, but can''t quite figure it out... Cheers Catfish

Share this post


Link to post
Share on other sites
TheGecko    122
Well....not a major optimisation i nthe short run,you should try to minimise the number of times you divide.Division costs more than multiplication.So in your code:

  
glColor4ub(r,g,b,255*lifespan/5);


you could write this:
  
glColor4ub(r,g,b,255*lifespan * 0.2);


Just a little thought

Share this post


Link to post
Share on other sites