Performance Issue
Hi, I'm new to OpenGL, and right now I'm having trouble getting a smooth framerate/picture in some simple code :(
Here is the code; I'm looking for ways to improve performance, and any help would be greatly appreciated. Thanks
note: I'm using targa images for textures, would that have any effects?
void Building::draw()
{
glPushMatrix();
glTranslated(xpos, 0, zpos);
glBegin(GL_QUADS); // draw top w/o texture
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3d(0, height, 0);
glVertex3d(width, height, 0);
glVertex3d(width, height, length);
glVertex3d(0, height, length);
glEnd();
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, teximage->GetWidth(), teximage->GetHeight(),
0, GL_RGB, GL_UNSIGNED_BYTE, teximage->GetImage());
glBegin(GL_QUAD_STRIP);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(2.0, 0.0); glVertex3d(0, 0, 0);
glTexCoord2f(0.0, 0.0); glVertex3d(0, height, 0);
glTexCoord2f(2.0, 2.0); glVertex3d(width, 0, 0);
glTexCoord2f(0.0, 2.0); glVertex3d(width, height, 0);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(2.0, 0.0); glVertex3d(width, 0, length);
glTexCoord2f(0.0, 0.0); glVertex3d(width, height, length);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(2.0, 2.0); glVertex3d(0, 0, length);
glTexCoord2f(0.0, 2.0); glVertex3d(0, height, length);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(2.0, 0.0); glVertex3d(0, 0, 0);
glTexCoord2f(0.0, 0.0); glVertex3d(0, height, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
void CGfxOpenGL::Render()
{
// Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Load Identity Matrix
glLoadIdentity();
// Make sure that closer objects get drawn last
glEnable(GL_DEPTH_TEST);
//gluLookAt(25.0, 15.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// move back x units and rotate about all 3 axes
glTranslated(-50.0+trx, -50.0+trys, -150.0+trz);
//X-axis rotation
glRotatef(x_angle, 1.0f, 0.0f, 0.0f);
// Y-axis rotation
glRotatef(y_angle, 0.0f, 1.0f, 0.0f);
// Z-axis rotation
glRotatef(z_angle, 0.0f, 0.0f, 1.0f);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
Light1 l2(5.0f, 15.0f, 5.0f, 1);
l2.draw();
glEnable(GL_COLOR_MATERIAL);
drawAll(); // draws an array of buildings using building.draw()
}
Immediate mode can be pretty fast. It's certainly not the biggest problem in the code above.
The problem with the code above is that it re-uploads the texture data every frame. That will make it slow. You should use glGenTextures(), glBindTexture(), and only use glTexImage2D() once for the texture in question. Then re-bind the texture when you want to draw the building.
The problem with the code above is that it re-uploads the texture data every frame. That will make it slow. You should use glGenTextures(), glBindTexture(), and only use glTexImage2D() once for the texture in question. Then re-bind the texture when you want to draw the building.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement