Wavefront and Java3D .obj Format
The Wavefront .obj file format is a standard 3D object file format created for use with Wavefront's Advanced Visualizerâ„¢. Models are available for purchase from Viewpoint DataLabs, as well as other 3D model companies. Object Files are text based files supporting both polygonal and free-form geometry (curves and surfaces). The Java 3D .obj file loader supports a subset of the file format, but it is enough to load most of the commonly available Object Files. Free-form geometry is not supported in Java3D nor in JavaView.
The following text is close to the description of the .obj file format in the Sun Java3D documentation.
The Object File tokens currently supported by the JavaView loader are listed below. Unknown tokens are skipped without affecting the reading process.
# some text
- Line is a comment until the end of the line
v float float float
- A single vertex's geometric position in space. The first vertex listed in the file has index 1, and subsequent vertices are numbered sequentially.
vt float float
- A texture coordinate. The first texture coordinate in the file is index 1, and subsequent textures are numbered sequentially. The number of texture coordinate does not need to match the number of vertices.
vn float float float
- A vertex normal. The first normal in the file is index 1, and subsequent normals are numbered sequentially.
f int int int ...
- or
f int/int int/int int/int . . .
- or
f int/int/int int/int/int int/int/int ...
- A polygonal face. The numbers are indexes into the arrays of vertex positions, texture coordinates, and normals respectively. A number may be omitted if, for example, texture coordinates are not being defined in the model.
There is no maximum number of vertices that a single polygon may contain. The .obj file specification says that each face must be flat and convex. In JavaView polygonal face may be triangulated.- There is no space between numbers and the slashes. There may be more than one series of geometric vertex/texture vertex/vertex normal numbers on a line. The following is a portion of a sample file for a four-sided face element:
f 1/1/1 2/2/2 3/3/3 4/4/4
Using v, vt, and vn to represent geometric vertices, texture vertices, and vertex normals, the statement would read:- f v/vt/vn v/vt/vn v/vt/vn v/vt/vn
If there are only vertices and vertex normals for a face element (no texture vertices), you would enter two slashes (//). For example, to specify only the vertex and vertex normal reference numbers, you would enter:
f 1//1 2//2 3//3 4//4
When you are using a series of triplets, you must be consistent in the way you reference the vertex data. For example, it is illegal to give vertex normals for some vertices, but not all. The following is an example of an illegal statement.- f 1/1/1 2/2/2 3//3 4//4
The example file sample.obj is given below (its a cube):
v 1 1 1 v 1 1 -1 v 1 -1 1 v 1 -1 -1 v -1 1 1 v -1 1 -1 v -1 -1 1 v -1 -1 -1 f 1 3 4 2 f 5 7 8 6 f 1 5 6 2 f 3 7 8 4 f 1 5 7 3 f 2 6 8 4