CS 5721 - Computer Graphics

Lab 7: Loading Objects into Your Scenes


In this lab, you will use Nate Robins' code for reading OBJ formatted files to load 3D models other than the default objects provided by GLUT. Nate Robins also provides some very useful OpenGL tutors to help people learning OpenGL.

Step 1: Load up an OBJ model

In this first step, you will need to make some modifications to your program to allow it to load up OBJ formatted model files. The OBJ format is a 3D model file format used by many 3D packages. The OBJ format encodes the vertices, triangle faces, normals, and other attributes needed to represent a 3D model.

First, bring the following two files into your project:


Compile your code to make sure that the newly added files don't cause problems.

Next, copy one or more of the following OBJ files into your project so you can eventually load up the models.

Stanford Bunny - bunny.obj
Dragon - dragon.obj
Happy Buddha - buddha.obj

In one of your functions used to initialize OpenGL state, but not in the display function, you will need to add the following code snippets to read in one of these models. First, make sure the glm.h file is included by your main program. Then, create a global variable that you can use to store a reference to your model

GLMmodel *objmodel_ptr;

Then, use this code to load the model and prepare it to be rendered

if (!objmodel_ptr)
   objmodel_ptr = glmReadOBJ("bunny.obj");
   if (!objmodel_ptr)

   glmVertexNormals(objmodel_ptr, 90.0);

After this code is executed, the model will have been loaded, normals will have been created and smoothed, and the model will be ready to render. To draw the loaded model, add the following code into your display function:

glmDraw(objmodel_ptr, GLM_SMOOTH | GLM_MATERIAL);

Compile your code and see if you have rendered the Stanford Bunny.

Step 2: Creating a Scene

Given what you have learned so far, create a lit scene with a ground plane and objects sitting on the ground plane. Use cubes, the OBJ files, or whatever objects you would like. If you want additional OBJ models, you can download them from CalTech model repository - http://www.cs.caltech.edu/~njlitke/meshes/toc.html. Be sure to download the OBJ files.

Note that some of the models have tons of triangles. Your application may start to slowdown with some of these models. In the next lab, we'll learn how to make your code more efficient and thus, run faster.

You might want to start thinking about better ways to organize your OpenGL code. If you've noticed, by now, you probably have amassed a nice mess of global variables. If you want to do anything interesting with OpenGL, like drawing lots of objects, you will likely need some decent C++ classes to encapsulate the state of your objects. You could then use the Standard Template Library vectors to hold the objects you are going to draw.

Show the TA your scene.

Step 3: Animate a Scene

In the final step for today's lab, animate one of your models in an interesting manner. Either tie it to your mouse or keyboard events and let it move around the ground plane, or make it autonomous by using the idle function to compute the object's new position or orientation. You can then use regular old glRotate and glTranslate calls combined with glPushMatrix and glPopMatrix to get the job done.

Show the TA your animated scene when you're done.

If there is time remaining, feel free to look over the glm.h file to see what other options you have for the glmDraw call. The glm code is well documented so you should be able to figure out what the options do.