Game Development Reference
In-Depth Information
The Wavefront OBJ Format
We will implement a loader for a subset of this format. Our loader will support models that are
composed of triangles only and that optionally may contain texture coordinates and normals.
The OBJ format also supports storage of arbitrary convex polygons, but we won't go into
that. Whether you simply find an OBJ model or create your own, just make sure that it is
triangulated—meaning that it's composed of triangles only.
The OBJ format is line based. Here are the parts of the syntax we are going to process:
ï?® v x y z : The v indicates that the line encodes a vertex position, while x , y ,
and z are the coordinates encoded as floating-point numbers.
ï?® vn i j k : The vn indicates that the line encodes a vertex normal, with i , j ,
and k being the x, y, and z components of the vertex normal.
ï?® vt u v : The vt indicates that the line encodes a texture coordinate pair, with
u and v being the texture coordinates.
ï?® f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 : The f indicates that the line encodes
a triangle. Each of the v/vt/vn blocks contains the indices of the position,
texture coordinates, and vertex normal of a single vertex of the triangle. The
indices are relative to the vertex positions, texture coordinates, and vertex
normal defined previously by the other three line formats. The vt and vn
indices can be left out, to indicate that there are no texture coordinates or
normals for a specific vertex of a triangle.
We will ignore any line that does not start with v , vn , vt , or f ; we will also output an error if any
of the permissible lines don't follow the formatting just described. Items within a single line are
delimited by whitespaces, which can include spaces, tabs, and so on.
Note The OBJ format can store a lot more information than we are going to parse here. We can
get away with only parsing the syntax shown here, and ignoring anything else, as long as the
models are triangulated and have normal and texture coordinates.
Here's a very simple example of a texture triangle with normals in OBJ format:
v -0.5 -0.5 0
v 0.5 -0.5 0
v 0 0.5 0
vn 0 0 1
vn 0 0 1
vn 0 0 1
vt 0 1
vt 1 1
vt 0.5 0
f 1/1/1 2/2/2 3/3/3
 
 
Search WWH ::




Custom Search