Top ↑

Wavefront Files

The Wavefront .obj file structure is possibly one of the simplest model formats to parse (which is why I'm using it). Each line in the file defines a property of the object, whether that be a vertex, face or texture coordinate.

First off, let's have a look at the basic data required to make an object. It's a cube.

# Blender v2.59 (sub 3) OBJ File: ''
# www.blender.org
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
f 1//1 2//1 3//1
f 1//1 3//1 4//1
f 5//2 8//2 7//2
f 5//2 7//2 6//2
f 1//3 5//3 6//3
f 1//3 6//3 2//3
f 2//4 6//4 7//4
f 2//4 7//4 3//4
f 3//5 7//5 8//5
f 3//5 8//5 4//5
f 5//6 1//6 4//6
f 5//6 4//6 8//6

Format explanation

  • Lines starting with # are comments. They're ignored by the parser.
  • v at the beginning of a line denotes a vertex. This is a point in 3D space. The three numbers following the v are X, Y and Z coordinates for the vertex.
  • vn is a vertex normal. They have the same format as vertices but hold data about a face for correct lighting.
  • Finally, we have f lines. The format of these lines is a little strange, but is rather simple.
    • There are three groups of [number]//[number]. As this object is a triangle mesh, they are information pertaining to each vertex of the (triangular) face.
    • The first number in each group is the vertex index. The second number is the vertex normal.

Indicies

Wavefront files are brought together by indicies. When the list of verticies is loaded, it's stored in an array. Each element in the array has an index, which is where the face (f) lines come in.

The first number in a [number]//[number] string is the vertex index for that point of the triangle. You'll notice the f line doesn't store the position of the vertex in 3D space, but the list of verticies does, which is where the face gets it's information from.

The second number is the vertex normal. This is much the same as a vertex, but links to the vertex normals list.

In later parts of this tutorial, you'll see face lines like [number]/[number]/[number]. The second [number] is the vertex texture index.