Question

I am making an OBJ importer and I happen to be stuck on how to construct the mesh from a set of given vertices. Consider a cube with these vertices (OBJ format, faces are triangles:

v -2.767533 -0.000000 2.927381

v 3.017295 -0.000000 2.927381

v -2.767533 6.311718 2.927381

v 3.017295 6.311718 2.927381

v -2.767533 6.311718 -2.845727

v 3.017295 6.311718 -2.845727

v -2.767533 -0.000000 -2.845727

v 3.017295 -0.000000 -2.845727

I know how to construct meshes using GLUT (to make my calls to GlBegin(GL_TRIANGLES), glVertex3f(x, y, z), glEnd(), etc.) Its just that I don't know how to combine the vertices to recreate the object. I thought it was to go v1, v2, v3, then v2, v3, v4, etc. until I have made enough triangles (and something like v7, v8, v1 (because it goes back to the begining)) counts. So 8 vertices is 12 triangles for the cube, and for, say, a sphere with 108 triangles and 56 vertices is (56 vertices * 2) - 4. For a cube, I make the 12 triangles, its ok but for a sphere, I make the 108 triangles with 56 vertices, it does not work. So how do I combine the vertices in my glVertex calls to make it work for any mesh? Thank you!

Was it helpful?

Solution

There should be a bunch of "face" lines in the file (lines beginning with the letter "f") that tell you how to combine the vertices into an object. For example,

f 1 2 3

would mean a triangle composed of the first three vertices in the file. You might also see something like

f 1/1 2/2 3/3

which is a triangle that also includes texture coordinates,

f 1//1 2//2 3//3

which includes vertex normal vectors, or

f 1/1/1 2/2/2 3/3/3

which is one that includes both.

Wikipedia has an article that includes an overview of the format: https://en.wikipedia.org/wiki/Wavefront_.obj_file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top