OBJ File Format Question

Started by
9 comments, last by 6 18 years, 9 months ago
I have this extremely simple .Obj file, exported from 3D Studio Max 7.
Quote:# object Box # v -5 -5 -5 v 5 -5 -5 v -5 5 -5 v 5 5 -5 v -5 -5 5 v 5 -5 5 v -5 5 5 v 5 5 5 # 8 vertices vt 0 0 0 vt 1 0 0 vt 0 1 0 vt 1 1 0 vt 0 0 0 vt 1 0 0 vt 0 1 0 vt 1 1 0 vt 0 0 0 vt 1 0 0 vt 0 1 0 vt 1 1 0 # 12 texture vertices g Box f -8/-3 -6/-1 -5/-2 f -5/-2 -7/-4 -8/-3 f -4/-4 -3/-3 -1/-1 f -1/-1 -2/-2 -4/-4 f -8/-8 -7/-7 -3/-5 f -3/-5 -4/-6 -8/-8 f -7/-12 -5/-11 -1/-9 f -1/-9 -3/-10 -7/-12 f -5/-8 -6/-7 -2/-5 f -2/-5 -1/-6 -5/-8 f -6/-12 -8/-11 -4/-9 f -4/-9 -2/-10 -6/-12 # 12 faces g
My question is, in the face definitions, what does the negative numbering mean? Why f -8/-3 -6/-1 -5/-2 and not? f 8/3 6/1 5/2 Does it mean that the faces are being defined counter-clockwise or something? Thanks for any tips on this...
Advertisement
There's a pretty thorough specification of the .obj file format at wotsit.org.

Quote:
Referencing vertex data
For all elements, reference numbers are used to identify geometric
vertices, texture vertices, vertex normals, and parameter space
vertices.

Each of these types of vertices is numbered separately, starting with 1.
This means that the first geometric vertex in the file is 1, the second is
2, and so on. The first texture vertex in the file is 1, the second is 2, and
so on. The numbering continues sequentially throughout the entire
file. Frequently, files have multiple lists of vertex data. This
numbering sequence continues even when vertex data is separated by
other data.

In addition to counting vertices down from the top of the first list in
the file, you can also count vertices back up the list from an element’s
position in the file. When you count up the list from an element, the
reference numbers are negative. A reference number of -1 indicates
the vertex immediately above the element. A reference number of -2
indicates two references above and so on.

So, from the example you gave
Quote:
# object Box
#

# 1 or -8
v -5 -5 -5

# 2 or -7
v 5 -5 -5

# 3 or -6
v -5 5 -5

# 4 or -5
v 5 5 -5

# 5 or -4
v -5 -5 5

# 6 or -3
v 5 -5 5

# 7 or -2
v -5 5 5

# 8 or -1
v 5 5 5
# 8 vertices


[Edited by - microdot on July 19, 2005 9:13:38 AM]
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
Thx for the tip, I visit Wotsit's but didnt find that piece of information.

Rate+ [wink]
The negative numbers are reverse index, I believe. Counting backward in the v, vt or vn set from the current position.
Quote:Original post by Prozak
Thx for the tip, I visit Wotsit's but didnt find that piece of information.

Rate+ [wink]


Hey, no problem. The information I quoted is in the first .obj file specification (the one in .pdf format). That's the one that seems to be a pretty authoritative reference for the .obj file.
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
I still have a few doubts regarding some aspects of this format.

Here:
Quote:
Cube
This is a cube that measures two units on each side. Each vertex is
shared by three different faces.
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
f 1 2 3 4
f 8 7 6 5
f 4 3 7 8
f 5 1 4 8
f 5 6 2 1
f 2 6 7 3


The faces, instead of being defined by 3 components, are getting defined by 4. Does this mean that the face component is in fact defining a quad, and not a triangle?

How do I convert this face data to triangles?
Original:
f 1 2 3 4
Becomes:
f 1 2 4
f 2 3 4
?

For a text based format, it does have it's quircks...
Another quirk, though you probably already see this: the face indexing scheme starts at 1 instead of 0! Somehow, every time I've written an Obj reader/writer, I've had that same bug... hrrrg
Quote:Original post by Anonymous Poster
Another quirk, though you probably already see this: the face indexing scheme starts at 1 instead of 0! Somehow, every time I've written an Obj reader/writer, I've had that same bug... hrrrg


Thx, yea, I had already noticed that one, and was crashing a post-load filter my engine has, that detects if the loaded mesh has normals, and if not, calculates them... but the faces where pointing to the wrong vertices, so.. loads of problems...

Edit: Well, I just caught these two lines on my triceratops.obj file:
Quote:f 2730//2329 2734//2333 2744//2343 2737//2336
f 2710//2309 2714//2313 2718//2317 2722//2321 2731//2330 2741//2340


Well, I got the quad to triangle converter working, so I'm thinking that "f" instead of defining a triangle, or a quad, defines a plane, but it becomes increasingly complex to load this type of files, if you don't even know how many triangles the model will have.

At this moment I have to do 2 passes, the first just scans the files and gets the number of vertices, faces, etc.

The second actually does the grunt work. Now I hope that when a "f" defines a plane, it defines the points in a somewhat clockwise, or counter-clockwise fashion, so, when I re-tesselate that face back into triangles, I'll have to maintain that ordering rule...

[Edited by - Prozak on July 27, 2005 11:03:30 AM]
Quote:Original post by Prozak
How do I convert this face data to triangles?
Original:
f 1 2 3 4
Becomes:
f 1 2 4
f 2 3 4
?


I have never seen 4 indices per face in OBJ, but I'm not be that experienced with it. Anyway, your assumption on how to convert to triangles seems to be a correct interpretation (it's hard to imagine any other).

If you want an example of an obj loader (including source code) check out this site. I use it myself and it works well. However, I checked the code and it doesn't seem to support quads.

Tom

EDIT: you edited while I was writing :)
I believe f stands for face.
The loader I linked also contains two passes, so it sounds like you're on the same track :)
When 4 vertices are specified for a face, it means that it is a quad. I usually dont worry about quads and just triangulate all of my models before using them in my game (ctrl-T in Blender3d).

This topic is closed to new replies.

Advertisement