Computer
Graphics
Summer
2007
G22.2270-001
Tuesdays,
6pm - 8:20pm
WWH 101
Assigment 1
DUE: 6/4/07 11:59pm -- DEADLINE CHANGE : 6/5/07 6pm
In this assignment you will write a program that draws a simple 3D
scene. Positioning the
objects and changing the position of the camera will require use of
modeling transformations. Reading Chapter 3
from the red book as well as the appendix on homogeneous coordinates would be useful.
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).
Make sure to pick an appropriate field of view, such that the
scene fills most of the picture.
Restrictions
Do not use the following commands in this assignment:
*any aux or glut commands for drawing objects
*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 these "disallowed" 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.
Possible extra credit ideas:
- implement visualization from the three axes and switch between these three views using key presses.
- add more objects based on transformations applied to box objects to the scene. Be creative!
Adapted from one of Denis Zorin's assignments.