Minimal textured obj file

Written by Paul Bourke
March 2012


The following outlines the minimal requirements to describe a textured mesh using the obj file format.

Three example files are supplied here:

Using the above examples the various sections will be explained and commented on below. The first line indicates the name of the material file, in this case "capsule.mtl".

mtllib capsule.mtl

The subsequent lines starting with "v" are the list of vertices.

v 0.000000 0.000000 -1.500000
v 0.000000 0.000000 -1.500000
v 0.000000 0.000000 -1.500000
v 0.000000 0.000000 -1.500000
 :
 :
 :

The lines starting with "vn" indicate normals. In this example there is the same number of normals as vertices.

vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
 :
 :
 :

The following lines starting with "vt" give the texture coordinates, again in this example there is a matching number of texture coordinates.

vt 0.000000 0.000000
vt 0.010000 0.000000
vt 0.020000 0.000000
vt 0.030000 0.000000
 :
 :
 :

Note that while the vertices, normals, and tetxure coordinates are listed in separate blocks in this example, they can be intermixed in any order. Their respective index that is used in the next section for describing the faces simply gets incremented as they are encountered.

The next section has lines starting with "f" giving the vertices making up each face. The vertex numbers start from 1 (not 0). Preceding the face information is the material to be applied, in this case it defines the texture image (see later). Each vertex is defined by three numbers, the vertex, normal, and texture coordinate index. Noting that, unlike the example here where they are all the same, duplicate normals or texture coordinates can be listed only once in the normal and texture lists described above.

usemtl material0
f 1/1/1 2/2/2 103/103/103
f 1/1/1 103/103/103 102/102/102
f 2/2/2 3/3/3 104/104/104
 :
 :
 :

It is also permissable to add vertices, normals, and texture coordinate intermixed with the faces section.

The material file "capsule.mtl" defines the visual properties of the mesh, reflection coefficients (ambient, diffuse, specular), and of interest here the name of the texture file. The file can define a number of material types, the mesh faces defined above inherit the values after any "usemtl" line.

newmtl material0
Ka 1.000000 1.000000 1.000000
Kd 1.000000 1.000000 1.000000
Ks 0.000000 0.000000 0.000000
Tr 1.000000
illum 1
Ns 0.000000
map_Kd capsule0.jpg