Computer
Graphics
Summer
2006
G22.2270-001
Tuesdays,
6pm - 8:20pm
WWH 101
Assigment 1
DUE: 6/12/06 11:59pm
In this assignment you will write a program that draws a simple 3D
scene and allows you to move around in the scene. Positioning the
objects and changing the position of the camera will require use of
modeling, viewing and projection transformations. Reading Chapter 3
from the red book would be useful.
This assignment has two parts. First part involves drawing a
static scene on the screen, which you should be able to implement after
lecture 3. Second part involves user interaction and camera motion
which we will cover in lecture 4. I suggest that you try to finish part
1 before lecture 4.
Part 1
Your program should
display a scene consisting of a table and 4 chairs standing on a
rectangle, representing the floor(You can add more objects, if you'd
like). Positions and dimensions of objects are shown in this document.
Initially set the camera into the position 4 units above the ground, 4
units to the left from the center of the table, and 4 units
down(towards the side of the picture with the rotated chair). The
viewing direction should - at all times - be towards the center of the
table. Make sure to pick an appropriate field of view, such that the
scene fills most of the picture.
Part 2
Your program should have the following modes:
Mode 1: Fly to the center of the table.
Mode 2: Fly away from the center of the table.
Mode 3: Fly around the center of the table, looking at the center.
Mode 4: Camera does not move, one of the chairs move away from the table (perpendicular to the side it is at).
Mode 5: Camera does not move, one of the chairs move towards the table ( again in perpendicular direction ).
Once the mode is chosen, when the left mouse button is clicked, the
camera starts moving and the second click stops it (and third starts it
again). Modes can be changed at anytime where the last position of the
camera before the change should be the first position of the camera
after the change.
You can implement the modes as key presses (e.g. pressing key '1'
changes mode to 1, key '2' to mode 2, etc). You may choose to implement
the modes using a pop-up menu for extra credit.
Restrictions
Do not use the following commands in this assignment:
*any aux or glut commands for drawing objects
*gluLookAt
*glTranslate, glRotate, glScale - implement their functionality using the formulas we discussed in class.
For matrix operations only use glLoadMatrix, glMultMatrix, glPushMatrix, glPopMatrix.
Note: Feel free to use all these commands for debugging your code. Once
you are sure everything else works replace these calls with your
versions.
Lighting
To see the scene, we need a light source. If we do not put a light
somewhere, the polygons of the scene are still visible, but they are
flat- shaded i.e. all pixels in the image of any polygon have the same
color. Pictures
like this do not look very interesting, especially if all polygons have
the same color (default is white). To make things more interesting,
either make boxes of different colors, using glColor3f() function calls
or use a single light.
We will discuss lighting in greater detail later. For this
assignment, add the following OpenGL calls after you create your
GLUT window, but before you call the glutMainLoop function.
This will add two lights, red and blue, to your scene.
GLfloat diffuse[] = {0.8, 0.8, 0.8, 1.0};
GLfloat lgt1_diffuse[] = { 0.05f, 0.05f, 0.6f, 1.0f };
GLfloat lgt2_diffuse[] = { 0.6f, 0.05f, 0.05f, 1.0f };
GLfloat light_pos1[] = { 5.0f, 5.0f, 0.0f, 1.0f };
GLfloat light_pos2[] = { -5.0f, 5.0f, 0.0f, 1.0f };
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT1, GL_POSITION,light_pos1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lgt1_diffuse);
glLightfv(GL_LIGHT2, GL_POSITION,light_pos2);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lgt2_diffuse);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
To make the lighting work, you also need to specify normals for each polygon.
Here is an example of the code that defines a unit square in the XY plane with
normal pointing along Z:
glBegin(GL_POLYGON);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
Implementation Suggestions
Trying to calculate positions of all vertices of all objects in the
scene by hand is not a very good idea since it would be difficult to
move objects if you were to do this.
Typically, one builds scenes like this hierarchically as explained at
the lectures. First, notice that all objects can be built out of boxes.
Write a function that draws a box. Then write functions that draw the
table and the chair, setting up proper transforms and calling the
function that draws boxes to draw various parts of the object. Finally,
write a function that draws the whole scene.
Make as few preliminary calculations on paper as possible. Try to
structure your program in such a way that all objects and their
positions are computed directly from the data in the picture, without
any manual calculations.
Avoid having too many specific dimensions in your program: try to
parametrize each object by several numbers; for example, all you
need to specify for the table is the length of legs, the thickness of
the lid, the size of the cross-section of a leg, and the distance from
the leg to the edge.
While debugging the scene, use simple camera positions and orientations
(e.g. looking along X, Y and Z axes). By default, the camera can be
thought of as located at zero looking along negative Z axis. To make
the scene visible, simply add
glTranslatef(0.0f,0.0f,-5.0f);
in the beginning of your drawing routine. To get two other views, add
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(-90,1,0,0);
and
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(-90,1,0,0);
glRotatef(-90,0,1,0);
It is useful to be able to see the coordinate system for debugging
purposes. You can draw thin long boxes of different colors along the
positive directions of the axes.
Adapted from one of Denis Zorin's assignments.