A Room with a View

Tom Kelliher, CS 320

Apr. 2, 2003

Administrivia

Announcements

Assignment

From Last Time

3-D projections, Movement in 3-D, Problems with 3-D movement. A better way --- Finish!!!.

Outline

  1. roomView.

  2. Model construction.

  3. Another way of thinking about coordinate systems and transformations.

  4. Walk-through of roomView.c. Multiple viewports and projections.

  5. Lab exercise.

Coming Up

Lab, fractals.

Demonstration of roomView.c

Model Construction

Consider building a room.

Two approaches:

  1. Pre-fab:
    1. A few object types.

    2. Each is built at the origin.

    3. Transformed into place.

  2. Stick-built:
    1. Exactly what is needed is built.

    2. Built in final location.

Coordinate Systems

Again, two approaches:

  1. Global, fixed coordinate system:
    1. All transformations relative to global coordinate system.

    2. Transformation order, code order reversed.

      Rotate, then translate:

      glLoadIdentity();
      glTranslatef(...);
      glRotatef(...);
      // Render polygon.
      

  2. Local, movable coordinate system:
    1. Imagine a local coordinate system fixed to the object.

    2. Initially, local system is identical to global system.

    3. Transformations are applied to the local system.

    4. Transformation order, code order the same:

      Translate, then rotate:

      glLoadIdentity();
      glTranslatef(...);
      glRotatef(...);
      // Render polygon.
      

Constructing a Room

Consider the transformations necessary for constructing:

when what you have to start with is the one block.

Steps:

load identity matrix;
push matrix;
move 4 left and render;
move 4 right and render;
pop matrix;
push matrix;
move 11 left, 5 down;

// Repeat the following 4 times.
render;
move 10 up and render;
move 6 right and 6 up;
rotate -90 and render;
move 10 up and render;   // Don't forget that we also rotated the
                         // local coordinate system!

pop matrix;
See room() for the real code.

Pushes, pops must balance. Used so that we can get back to a previous position.

colorCube() and polygon()

A couple vertex lists:

GLfloat wall[][3] =
{ {-1.0, -5.0, 0.0}, { 1.0, -5.0, 0.0},
  { 1.0,  5.0, 0.0}, {-1.0,  5.0, 0.0},
  {-1.0, -5.0, 8.0}, { 1.0, -5.0, 8.0},
  { 1.0,  5.0, 8.0}, {-1.0,  5.0, 8.0}
};

GLfloat Floor[][3] =
{ {-12.0, -12.0, 0.0}, { 12.0, -12.0, 0.0},
  { 12.0,  12.0, 0.0}, {-12.0,  12.0, 0.0}
};
Is there a general way for rendering?

colorCube():

  1. ``Canonical'' cube vertex list.

  2. Rendering various cubes (vertex lists).

  3. Color vectors.

// colorCube renders a cube.
//    cube: the vertex list specifying the cube.
//    color: the color vector.
//
// Assumptions regarding the vertex list:
// Index   Vertex
//   0     Lower left vertex of back face.
//   1     Lower right vertex of back face.
//   2     Upper right vertex of back face.
//   3     Upper left vertex of back face.
// 4--7    Similar for front face.
// (Assumes we are looking at the origin from the +z axis with the +y axis
//  being "up."
//

void colorCube(GLfloat cube[][3], GLfloat *color)
{
   polygon(cube,0,3,2,1,color);
   polygon(cube,2,3,7,6,color);
   polygon(cube,0,4,7,3,color);
   polygon(cube,1,2,6,5,color);
   polygon(cube,4,5,6,7,color);
   polygon(cube,0,1,5,4,color);
}

polygon():

  1. Rendering various polygons.

//
// polygon renders a polygon:
//    polygon: name of the vertex list.
//    a, b, c, d: indices of the vertex list vertices to be rendered.
//    color: the color vector.
//

void polygon(GLfloat polygon[][3], int a, int b, int c , int d,
             GLfloat *color)
{
   glPushAttrib(GL_ALL_ATTRIB_BITS);
   glBegin(GL_POLYGON);
      glColor3fv(color);
      glVertex3fv(polygon[a]);
      glVertex3fv(polygon[b]);
      glVertex3fv(polygon[c]);
      glVertex3fv(polygon[d]);
   glEnd();
   glPopAttrib();
}

Multiple Viewports with Multiple Projections

Ordinarily, the projection mode is set-up in reshape().

What about this case?

Sketch of display():

clear color and depth buffers;

// Prepare for the overhead view.
set an orthographic projection;
set viewport;

render room;
render viewer;

flush all polygons;   // Ensure that none are "hanging" around.

// Prepare for the immersed view
set a perspective projection;
set viewport;

position the camera;   // Remember: viewer transformation, then
                       // model transformations.

render room;
render ceiling;

swap color buffers;

Lab Exercise

  1. From class Web site, grab roomViewLab.c.

  2. Build and run. Note:
    1. Use arrow keys to increment/decrement x/y to move.

    2. After about 16 moves, ceiling and walls are lost. Why?

  3. Insert the missing pop matrix.

  4. Add viewer rotation and fix left, right, forward, backward motion.

  5. Can you add collision detection?



Thomas P. Kelliher
Wed Apr 2 08:28:39 EST 2003
Tom Kelliher