PDA

View Full Version : OBJ file translation



richie
07-11-2005, 04:12 AM
Hi,
I'm pretty new to OpenGL, so please bear with me!!!

I'm trying to figure out how to load an obj file into my program. I've been playing with this idea for a couple of hours now, and the only solution i have is to write custom routines to read the obj file and encapsulate the vertex data (v xval yval zval) and the normals (vn xnorm ynorm znorm) between a glBegin(); glEnd(); pair...

e.g.
glBegin(GL_TRIANGLES);
glNormal3f(-0.556971, 0.760480, 0.333847);
glVertex3f(-1.915925, 2.932206, 1.062864);
glNormal3f(-0.884029, 0.403707, 0.235612);
glVertex3f(-2.660180, 2.335078, 0.746116);
glNormal3f(-0.531439, 0.606457, 0.591423);
glVertex3f(-1.869035, 2.589165, 1.791945);
glNormal3f(-0.531439, 0.606457, 0.591423);
...
glEnd();

This doesnt even render correctly in GL_LINE_LOOP mode, so I have to perform another hack by reading 3 triplets of vertex coordinates(provided all mesh elements are triangles) and placing them between an glBegin(GL_LINE_LOOP); glEnd(); pair...

e.g.
glBegin(GL_LINE_LOOP);
glVertex3f(0.907787, -3.468165, 2.992616);
glVertex3f(0.930737, -3.371641, 2.946086);
glVertex3f(0.611883, -3.465947, 3.211004);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.611883, -3.465947, 3.211004);
glVertex3f(0.930737, -3.371641, 2.946086);
...
glEnd();

If this sounds right it isnt because i have to detatch ALL of the faces (triangles) of the whole model before exporting to obj... that way i can guarantee that the model will be drawn correctly using glBegin(GL_TRIANGLES);

this isn't what I want, it isn't covenient and its a big optimization issue as well...

How can I load an obj file without having to detatch the faces and perform hacks????

As far as I understand it won't work (using whole undetached mesh) with GL_TRIANGLE,GL_TRIANGLE_STRIP or other enumerated constants because normally a model is built (by an artist) in an order that doesnt have any relation to the order in which vertices are and should be connected.

e.g. an edge is added halfway between the two border edges (taking a quad plane for examplpe), so the two vertices of the new edge come last after the previous model state...

there must be a way of organizing vertices into a logical and sequential order. i just don't know enough about geometry to know how...

http://www.forumsigs.com/users/sys0p763//untitled.jpg

the left part is all messed up because the mesh had not been separated into individual triangles, unlike the two rightmost images which are rendered correctly... (please note the model is (c) mario ucci aka loganarts).

I hope someone can help me!!!! please!!! hehe!

cheers!!!

Mikkel Gjoel
07-11-2005, 05:46 AM
The easiest way to read obj to gl, is to read the .obj-file into an obj-specific datastructure, and later translate this to the format you need.

The fool-proof method is to just draw everything as GL_POLYGON, the way you do. I'm not really sure what you mean by detach, but when reading triangles from obj, they are ordered in an indexed faceset. This means that positions, normals and texture-coordinates are shared in a vertex - but only IF they are continuous across a vertex. So if, for example, you have hard edges (discontinuous normals), you need to take care of the split edges when drawing the object.

There is quite a nice .obj-specification at
http://astronomy.swin.edu.au/~pbourke/geomformats/obj/

kind regards,

richie
07-14-2005, 01:36 AM
hey!
thanks for the reply! i did try GL_POLYGON... maybe ill go home and recheck.

by detaching everything i meant i detached each individual triangle.... pretty silly, but anyway!

thanks for the link!

ill let u know if i get it working!

cheers :)