View Full Version : Slow performance with loaded model (C++)

02-20-2010, 03:37 PM
Hi guys,

There's no doubt something obvious that is wrong with my code, but limited experience and the lack of success in google search has left me stumped.

I have created a parser that loads info from a .X file. This info is stored in a Mesh object, which has attributes:

int num_vertices;
std::vector<Ver> vertices;

int num_normals;
std::vector<std::vector<float>> normals;

int num_polys;
std::vector<Poly> polys;

A Poly is a vector<int> which gives indices of the vertices in the Mesh attribute 'vertices' that form the polygon.

A Ver is a vector<float> which stores the x, y, z locations of a vertex.

The mesh object has a view() function, that uses opengl commands to display the model.

void Mesh::view() const

Poly poly;
int num_polys = Mesh::get_num_polys();
int index;

for (int i = 0; i < num_polys; i++)
poly = Mesh::get_poly(i);
for (int j = 0; j < (int)poly.vertices.size(); j++)
index = poly.get_index(j);
glNormal3f(Mesh::get_normal(index)[0], Mesh::get_normal(index)[1], Mesh::get_normal(index)[2]);
glVertex3f(Mesh::get_vertex(index).get_coord()[0], Mesh::get_vertex(index).get_coord()[1], Mesh::get_vertex(index).get_coord()[2]);


get_vertex(i) and get_normal(i) both return the vertex/normal at the index i in the vectors storing all of the vertices/normals in the mesh object.

get_coord() returns a vector<float> of the vertex coordinates
For the openGL window, I am using NeHe tutorial lesson 7, with all glaux and texture code removed. http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=07

I do the intial parse of the .X file in InitGL, and call the Mesh.view() function in DrawGLScene.

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
character = load_x_file("Man4k.X");

glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
glEnable(GL_LIGHT1); // Enable Light One
return TRUE; // Initialization Went OK

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
glLoadIdentity(); // Reset The View



return TRUE; // Keep Going
The model loads fine. For those unfamiliar with NeHe lesson 7, pressing the arrow keys causes the model to spin. The problem I have is that the rotating model is shown at about 1FPS, and is using all 4GHz of my CPU. The model is around 3400 polygons. I could just reduce the polygon count until it works at a good FPS but that is hardly solving the problem.

Sorry if important info has been omitted, I thought listing the 500 line opengl code wouldn't be well received.

I have no idea where to even start looking on this problem, any advice would be much appreciated.


02-20-2010, 04:10 PM
things missing from your post :
1- what is your video card ? If you have no hardware acceleration, you can easily max out a CPU on a megapixel resolution.
2- is there any texturing in place in your scene, and what filtering do you use ?

Then : immediate mode is easy to use, but not good for any performance stuff. VBO is better read this :

When troubleshooting GL performance, it is interesting to check if the speed is related to rendering size.

02-20-2010, 04:49 PM
Thanks for replying.

1. It's an ATI Radeon HD 5850. Is hardware acceleration automatic in OpenGL?

2. There is no texturing in place. I am just trying to render a set of white triangles.

I am looking at the VBO info now.

The window size does not make a noticable difference in speed. What might this imply?

02-20-2010, 05:44 PM
1. yes, provided you have a correct graphic driver. For your ATI, you need a Catalyst driver (the full suite is not necessary), check here :
2. even more surprising...

No hardware acceleration is often easy to spot when you are fillrate limited.

02-20-2010, 06:22 PM
Installed the Catalyst driver which doesn't seem to improve the situation. Although thanks for pointing this out, as I now have a multitude of extra things that my card can do lol.

What do you mean 'fillrate limited'?

02-20-2010, 06:49 PM

02-21-2010, 03:13 PM
Just a simple question:

Do Mesh::get_poly(int), Mesh::get_normal(int) and Mesh::get_vertex(int) return references or copies of the data structures?

The reason for asking is that excessive copying of std::vectors is a real speed bump :)
And from your code I can see that it's already happening to the poly variable in the Mesh::view() function.

02-21-2010, 05:46 PM

Datsua, it's sometimes the simplest questions that give the best answers! Changed the functions to return references and now I can spin my models as fast as I can see!

And thankyou ZbuffeR for your help, too.