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:
// 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 which gives indices of the vertices in the Mesh attribute ‘vertices’ that form the polygon.
A Ver is a vector 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
{
glBegin(GL_TRIANGLES);
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]);
}
}
glEnd();
}
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 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
glTranslatef(0.0f,0.0f,z);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
character.view();
xrot+=xspeed;
yrot+=yspeed;
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.
Tom