Computer Graphics Workshop '96 Lecture Notes


Today's topics

The Computer Graphics Workshop will be taught Mondays, Wednesdays, and Fridays during IAP, except for the holiday of 1/15. That week the class will meet on Tuesday.

We will have laboratory sessions for those students who will be using Athena as their primary computer system. These sessions will meet every Tuesday and Thursday from 7-9 PM in the SGI cluster in 4-035. If we run short on space in the lab, priority will be given to students who registered first for the course. We strongly recommend that you attend these labs; if attendance is poor at the lab sessions, we will cancel them. Note, however, that 4-035 will be available as a general Athena cluster during IAP, so we do not have to be present for you to use the machines.

Purpose of the course

The early weeks of the course will be devoted primarily to learning how to write programs using Open Inventor. Early on, we will have frequent problem sets which hopefully will assist you in becoming familiar with the capabilities of the toolkit. Because we believe that you learn best by actually implementing something, we strongly recommend that you form groups for, and participate in, the creation of a final project.

The final project

This year the final project will be to implement a simple game using Open Inventor. The feasibility of this project has been demonstrated; however, it will almost definitely not be possible to write the next Doom-killer. We have a suggested list of games which should be implementable within the IAP timeframe: We suggest you begin thinking about which game you would like to help write and who you would like to work with. We hope to present all the material necessary for the construction of this game within the first two weeks, so that you can spend the rest of IAP working on your project.

The development environment

The development environment we will use on Athena is a version of Scheme which has an Open Inventor binding. This allows you to write Scheme programs which use the Open Inventor 3D graphics toolkit.

Note that currently Open Inventor is only available on Athena SGIs. Therefore, you will have to find an Indy in order to be able to work with Inventor.

To get started, type the following at an Athena prompt:

athena% add thingworld
athena% ivyscm
After several seconds, the Scheme system will print a banner message, and then will bring up a prompt:
> (display "Hello, world\n")
Hello, world
You can load in Scheme files which are in the current directory. For example:
athena% add thingworld
athena% cd /mit/thingworld/Ivy
athena% ivyscm
> (load "tetris") ;; loads tetris.scm
You can also load in Scheme files on the command line:
athena% add thingworld
athena% cd /mit/thingworld/Ivy
athena% ivyscm tetris
Example code, as well as documentation, resides in /mit/thingworld/Ivy (after you type "add thingworld").

To exit, type (exit) to the Scheme interpreter:

> (exit)
Paradigms of Open Inventor

Open Inventor is a high-level toolkit, unlike many existing 2D and 3D graphics libraries. The data structures and flow of control in programs will seem very abstract at first, but as you gain experience with the toolkit you will understand more about how and why things happen.

Open Inventor is an object-oriented 3D graphics toolkit. Each "object" on the screen is represented as an "object" in memory (in the 6.001 sense of the word). Each object is stored in the scene database, SoDB, which stores such information as the name of each part in the current scene.

The fundamental data structure in Open Inventor is called the scene graph. A scene graph is a directed acyclic graph; you can think of it, for the most part, as a tree. Most of the operations in Open Inventor involve applying actions to these scene graphs. Actions traverse this tree from left to right, and from the top down.

For example, when an application renders a scene, it does so by applying a render action to the scene graph. When a program wants to find a certain node in the scene graph, it does so by applying a search action to the scene graph.

Creation of a simple scene graph

As a first example, we will create a viewer (a window for viewing Open Inventor scenes) and add some objects to the scene. The first problem set will involve creating a simple scene graph like this one and modifying some of its parameters.

First, let's start up the Scheme interpreter, and create and show our window:

athena% ivyscm
Entering Inventor Xt mainLoop
(define my-viewer (new-SoXtExaminerViewer))
> (-> my-viewer 'show)
Now let's create the root node of our scene graph.
> (define root (new-SoSeparator))
> (-> root 'ref)
Note that we increment the reference count of the root node. We will discuss the reasons for this as we proceed through the course; for now, just remember to always ref your root node.

Now let's add a material node, which allows us to change the material properties of objects which follow it in the scene graph:

> (define my-mat (new-SoMaterial))
> (-> root 'addChild my-mat)
And a shape node (in our example, a cone):
> (define my-cone (new-SoCone))
> (-> root 'addChild my-cone)
Nothing has yet appeared in our window. In order to view our scene, we must tell the viewer which scene graph to render.
> (-> my-viewer 'setSceneGraph root)
Voila! A cone appears. Note that you can translate (middle mouse button), spin (left mouse button), and scale (left and middle mouse buttons) the cone. This is a standard ability that Open Inventor provides for viewing scenes.

Now let's try changing the color of our cone to be red.

> (-> (-> my-mat 'diffuseColor) 'setValue 0.9 0.0 0.0)
The values in the above expression indicate red, green, and blue values, from left to right, and they range from values of 0.0 to 1.0. They are additive, so that high values for red and green make yellow.

We will next add a transform node to the scene graph, in preparation for placing a sphere next to the cone.

> (define my-transform (new-SoTransform))
> (-> root 'addChild my-transform)
We will now add a sphere to our scene graph.
> (define my-sphere (new-SoSphere))
> (-> root 'addChild my-sphere)
Hmm. The sphere appears in the same place as our cone. Since we want to have the sphere next to the cone, we must change its translation:
> (-> (-> my-transform 'translation) 'setValue 2.0 0.0 0.0)
Note that the sphere is the same color as the cone. It inherits the material properties defined by the material node we added before it.

Finally, let's add a blue cube to our scene. This second material node will override the presence of the first one for all objects which follow it.

> (define my-mat-2 (new-SoMaterial))
> (-> root 'addChild my-mat-2)
> (-> (-> my-mat-2 'diffuseColor) 'setValue 0.0 0.0 0.9)
> (define my-transform-2 (new-SoTransform))
> (-> root 'addChild my-transform-2)
> (define my-cube (new-SoCube))
> (-> root 'addChild my-cube)
Note that the cube appears this time in the place where the sphere is, not where the cone is. Therefore we want to move the cube relative to the center of the sphere:
> (-> (-> my-transform-2 'translation) 'setValue 2.1 0.0 0.0)
The default orientation of the coordinate axes in Inventor (before any interaction with the viewer is done) is positive x to the right, positive y up, and positive z out of the screen.

Using just these basic tools, you should now be able to approach the first problem set , which is to design a simple scene composed of these objects.

Next lecture

Next time we will introduce more of the tools for manipulating scene graphs, explain the workings of nodes and fields, and show how to understand the manual pages from the perspective of Scheme programming.

Back to the CGW '96 home page

$Id: index.html,v 1.12 1996/01/08 22:22:32 kbrussel Exp $