Posts Tagged ‘graphics’

The .obj file format, demystified

November 27, 2007

If you have ever thought of doing any 3D games programming, your first thought might be how the graphics are done. From the highly polished Final Fantasy games, to the abstract look of Geometry Wars, the answer varies. But more often than not, you would need to use visual assets that were made with other programs. For 3D modeling programs the formats widely vary, but out of all these, the .obj (or object) file format is the most generally accepted. Think of it like the PDF for 3D modelers. Most popular packages will support this format, so it’s easy to pass along without worring too much about what software others are using. The following text is an introduction for people that are not yet familiar with this format.

The .obj file format is one of the easiest to work with in programming 3D games, since .obj files are all text and the data is organized in a very straightforward manner. A typical .obj file would contain the following:

  • Vertex locations in x, y, and z coordinates
  • Texture mapping in u and v coordinates (optional)
  • Orientation of normals in x, y, and z coordinates (optional)
  • Face represented by a set of one or more indices

An .obj file can have either texture or normal data, but it must always carry vertex data. They are represented as an array of values, each line being a new entry in the array. This line starts with “v” followed by the coordinates. Optionally, you have lines for the vertex texture data (starts with “vt”) and vertex normals (starts with “vn”). Near the end of the file, the face data is represented, as a group of integers. Each line starts with “f”, and each number corresponds to the item on the data list in the order that it’s displayed. A line containing the numbers “1 4 3” correspond to the 1st, 4th and 3rd entries of that data group. There’s something important to note here: There is no index numbered “0”, so keep this in mind when parsing .obj files.

Each line contains indices for exactly one face, whether a triangle, quad, or other convex polygon. Furthermore, the indices can be grouped as single numbers, pairs, or triplets. The grouping depends on whether any texture or normal data is given. The following groups are possible:

"n n . . . n" - vertex location only
"n/n n/n . . . n/n" - location plus texture, or vertex plus normals
"n/n/n n/n/n . . . n/n/n" - location, texture, and normals

It should be clear that in a set of pairs or triplets, the location indices are listed first. Following that are the texture mapping coordinates and then the normal coordinates.

Finally, there might be some relevant information about grouping polygons into sets (lines starting with “g”) and usage of materials specified by the program that made the file (lines starting with “usemtl”). These may be used for programming model renderers, but they are not really crucial for displaying the geometry, so we won’t talk much about them here.

Well, that’s it for understanding the structure of .obj files. In a future post, I will show you how to apply this information to create your own .obj file parser.

Advertisements