Tom Kelliher, CS 320
Apr. 4, 2011
Read 6.1-3.
3-D projections, Movement in 3-D, Problems with 3-D movement.
Light.
Consider building a room.
Two approaches:
Again, two approaches:
Rotate, then translate:
glLoadIdentity(); glTranslatef(...); glRotatef(...); // Render polygon.
Translate, then rotate:
glLoadIdentity(); glTranslatef(...); glRotatef(...); // Render polygon.
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 8 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.
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():
// 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():
// // 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(); }
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;