OBJ files

Building out of polygons, spheres, cubes, and so forth is good enough for some things, but to do anything very complex requires a great deal of work. Fortunately, there are a number of very powerful 3D modeling applications out there that will help you build an object using a sophisticated GUI. Two such applications are:

Since Maya is available to us in Jewett Art Center, room 247, we will use that for now.

Once you have an object built in Maya, what can you do with it? We can export it in a file that can then be read into our OpenGL programs. The file format is ".obj"

Example

The program ~cs307/pub/tw/bin/show-object will read in an .obj file, define the twBoundingBox based on the bounding box of the object, and display it. There are some .obj files in ~cs307/pub/tw/objects and ~cs307/pub/tw/tutors/data, so try viewing some of those. You give the filename on the command line, thus:

% cd ~cs307/pub/tw/objects
% ls *.obj
al.obj        f-16.obj     porsche.obj    soccerball.obj
dolphins.obj  flowers.obj  rose+vase.obj
% ~cs307/pub/tw/bin/show-object soccerball.obj

Try all the objects!

Object Representation

How are these objects represented? The contents of the .obj file is essentially a polyhedron. It is represented in terms of:

There are a few other values in there as well. We won't learn about them all now, but we will dig into the file format and de-mystify it a little. We'll also look at the code that draws one of these objects.

Here is good documentation on the OBJ file format and some utilities for the OBJ file format.

Example File

Consider the following tiny OBJ file, which defines a simple cube.

# cube.obj
#
 
g cube
 
v  0.0  0.0  0.0
v  0.0  0.0  1.0
v  0.0  1.0  0.0
v  0.0  1.0  1.0
v  1.0  0.0  0.0
v  1.0  0.0  1.0
v  1.0  1.0  0.0
v  1.0  1.0  1.0

vn  0.0  0.0  1.0
vn  0.0  0.0 -1.0
vn  0.0  1.0  0.0
vn  0.0 -1.0  0.0
vn  1.0  0.0  0.0
vn -1.0  0.0  0.0
 
f  1//2  7//2  5//2
f  1//2  3//2  7//2 
f  1//6  4//6  3//6 
f  1//6  2//6  4//6 
f  3//3  8//3  7//3 
f  3//3  4//3  8//3 
f  5//5  7//5  8//5 
f  5//5  8//5  6//5 
f  1//4  5//4  6//4 
f  1//4  6//4  2//4 
f  2//1  6//1  8//1 
f  2//1  8//1  4//1 

Let's look at this in parts:

Reading an OBJ file

Nate Robbins, who wrote the tutors that we have used, wrote a library he called glm to work with OBJ files. We will dig into a little of his code to see how it works. Let's start with reading an OBJ file. Understanding Some of this code depends on how I/O works in C, which is not important in this course, so we will mostly skip that. Please feel free to ask.

Here's the outline of the code:

Drawing an OBJ

To draw an object, the "glm" code does the following:

The code is glmDraw in cs307/pub/tw/glm/glm.cc.