Efficiency problem with OBJ loader

I am making a simple OBJ loader that basically works like this. I parse the text file and store all the vertices/normals/faces etc in some data structures. Then in my draw function I extract the faces and make array lookups based on the positions given by the faces. I originally had implemented the Draw function to just draw quads but when I wanted to make my program work with faces of any number of vertices I had to switch to polygons and this killed my efficiency. My original code was something like this (The pseudo code is based on common lisp but its pretty basic so should be okay):

(glBegin GL_Quads)

loops to extract and draw

(glEnd)

But since now my new code has to accept faces of any number it looks more like this:
//start loops
(glBegin GL_polygon)

//Draw stuff

(glEnd)
//end of loops

I think because I am beginning and ending the polygon every loop cycle instead of building the draw list and drawing at the end I am killing my frame rate ( it dropped by a factor greater then 10). Is there a way I can just start the GL_polygon before the loop and then every time through just tell it how many vertices to expect then end the polygon after my loop? Or is there some other more efficient way to do this? Thanks.

You can try using display lists. Google for a tutorial, or here’s one: http://www.lighthouse3d.com/opengl/displaylists/

I import many, many OBJ files into my OpenGL simulations. Most of them are defined in Display Lists on the first iteration through the draw routine. As long as you don’t have to interactively change vertex coordinates in your geometry, I’d recommend this. Are you creating the OBJ files yourself with a modeler? If so, try triangulating all of the polys before writing out the OBJ. That way you can use GL_TRIANGLES instead of GL_POLYGON in your GL_Begin statement. If you are not creating the models yourself you may be able to triangulate them using some free software such as the evaluation version of Deep Exploration (1 month only), or some other package.

Good luck.

Since GL_POLYGON requires convex polygons, why not simply convert the poly to triangles?
For a clockwise 5-point poly, the triangle indices are:
0 1 2, 0 2 3, 0 3 4

For a clockwise 6-point poly, the triangle indices are:
0 1 2, 0 2 3, 0 3 4, 0 4 5

Easy :slight_smile:

as others have mentioned use quads or triangles instead of GL_POLYGON
I remember reading in some opengl performance pdf,
GL_POLYGONS == bad

then again if youre comparing immediate mode (what u have now) with display lists (what u had first) then it will be slower, yes 10x sounds possible
solution use VertexArrays,or VBOs to improve the speed

I found these pages very helpful when making the jump from immediate mode to array / buffer objects…

http://www.songho.ca/opengl/