Understanding the OBJ file format

The goal of this project is to view 3D models in the Wavefront OBJ format. Before we begin coding, let's take a look at the file format. A reference can be found at http://www.fileformat.info/format/wavefrontobj/egff.htm.

As we know, 3D models can be represented as a mesh of X, Y, and Z vertices. Sets of vertices are connected to define a face of the mesh surface. A full mesh surface is a collection of these faces.

Each vertex can also be assigned a normal vector and/or a texture coordinate. The normal vector defines the outward facing direction at that vertex, used in lighting calculations. The UV texture coordinate can be used to map texture images onto the mesh surface. There are other features of the format, including free-form curves and materials, which we will not support in this project.

As a plain text file, an OBJ is organized as separate lines of text. Each nonblank line begins with a keyword and data for that keyword separated by spaces. Comments begin with # and are ignored by the parser.

The OBJ data keywords include:

  • v: Geometric vertices (for example, v 0.0 1.0 0.0)
  • vt: Texture vertices (for example, vt 0.0 1.0 0.0) [not supported in our project]
  • vn: Vertex normals (for example, vn 0.0 1.0 0.0)
  • f: Polygonal face indexes (for example, f 1 2 3)

The face values are indices pointing into the vertices list into the vertices (starting at 1 for the first one).

As for the f command specifying face indices, they're integer values that index into the vertex list. When there are three indices, it describes a triangle; four describes a quad, and so on.

When texture vertices exist, they are referenced as the second number after a slash, for example, f 1/1 2/2 3/3. We're not supporting them now, but we might need to parse them in an f command. When vertex normals exist, they are referenced as the third number after a slash, for example, f 1//1 2//2 3//3 or f 1/1/1 2/2/2 3/3/3.

Indices can be negative, in which case they reference the last (most recently encountered) item as -1, the previous one as -2, and so on.

Other lines, including data that we are not supporting here, will be ignored.

For example, the following data represents a simple triangle:

# Simple Wavefront file
v 0.0 0.0 0.0
v 0.0 1.0 0.0
v 1.0 0.0 0.0
f 1 2 3

Our OBJ implementation is limited. It safely handles the example models included with this book, and perhaps others that you'll find on the Internet or make yourself. However, this is an example code and a demonstration project. Writing a robust data importer and supporting the many features of OBJ in our RenderBox engine is beyond the scope of this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset