A Rotating, Color-Interpolated Cube

Tom Kelliher, CS 320

Announcements: Pong assignment on Monday.

From last time:

  1. Transformations.

Outline:

  1. Color interpolation.

  2. 3-D rotations.

  3. Depth buffering and hidden surface removal.

  4. Non-commutativity of rotations.

Assignment: Read Chapter 6. (To date, Chapters 1, 5, and 6 have been assigned.)

Prelude

If you want to rotate an object about its center, in what order do you apply the three transformations?

A Rotating, Color-Interpolated Cube

Representation

GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, 
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, 
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

  1. Coordinate system: +x to right, +y up, +z towards us. Right-hand system.

  2. Vertex list and a numbering of the cube's vertices:

  3. Color interpolation: bilinear interpolation.

    Let p be the way from and . p's color is:

    (for each color)

    What about points on interior of polygon?

  4. Enumerating the vertices on each of the faces:
    void colorcube(void)
    {
    
    /* map vertices to faces */
    
       polygon(0,3,2,1);
       polygon(2,3,7,6);
       polygon(0,4,7,3);
       polygon(1,2,6,5);
       polygon(4,5,6,7);
       polygon(0,1,5,4);
    }
    
    
    void polygon(int a, int b, int c , int d)
    {
    
    /* draw a polygon via list of vertices */
    
       glBegin(GL_POLYGON);
          glColor3fv(colors[a]);
          glVertex3fv(vertices[a]);
          glColor3fv(colors[b]);
          glVertex3fv(vertices[b]);
          glColor3fv(colors[c]);
          glVertex3fv(vertices[c]);
          glColor3fv(colors[d]);
          glVertex3fv(vertices[d]);
       glEnd();
    }
    
    Does the order matter?

  5. Display function:
    void display(void)
    {
    /* display callback, clear frame buffer and z buffer,
       rotate cube and draw, swap buffers */
    
    #ifdef DEPTH
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    #else
       glClear(GL_COLOR_BUFFER_BIT);
    #endif
       glLoadIdentity();
       glRotatef(theta[0], 1.0, 0.0, 0.0);
       glRotatef(theta[1], 0.0, 1.0, 0.0);
       glRotatef(theta[2], 0.0, 0.0, 1.0);
    
       colorcube();
    
       glFlush();
       glutSwapBuffers();
    }
    

Rotating One and Two Faces

One face:

  1. The face is rotating about the origin.

  2. Perspective is not maintained.

Two faces:

  1. An unexpected result? Why?

  2. Fixing it: the depth buffer and hidden surface removal. Idea: associate a z-value with each pixel in the frame buffer and only conditionally write new pixels.

Rotating the Entire Cube

  1. Why doesn't the back face rotate the in the same direction as the front face?

       glLoadIdentity();
       glRotatef(theta[0], 1.0, 0.0, 0.0);
       glRotatef(theta[1], 0.0, 1.0, 0.0);
       glRotatef(theta[2], 0.0, 0.0, 1.0);
    



Thomas P. Kelliher
Thu Feb 26 20:18:59 EST 1998
Tom Kelliher