Lab#3A: Simple Static Scenes Viewed from a Moving
Camera with Perspective Projection
Goal: Download this demo
project as a basic framework and develop a moving-camera version of your
approximate circles in Lab#1.
- Model-view transformation: Explore the source code of the demo
project about a static 3D scene viewed from a moving camera.
Understand how gluLookAt (coming after glMatrixMode(GL_MODELVIEW) ) can introduce the
viewing-transformation effect of changing the eye location, the center to
look at, and the up vector of the camera and how to create the effect of a
moving camera around the origin on the y=0 plane. You may want to play
with projection.exe from Nate Robin’s tutor
repository to clarify your understanding of gluLookAt.
- Projection transformation: Instead of glOrtho in the sample code, please change it to gluPerspective or glFrustum to view the scene with perspective
projection instead of orthographic projection. You may want to play with
projection.exe from Nate
Robin’s tutor repository to see the effects of gluPerspective or glFrustum versus
those of glOrtho, and thus clarify your understanding of
the difference between orthographic projection and perspective projection.
Note that these commands come after glMatrixMode(GL_PROJECTION) to introduce the
projection-transformation effect.
- Add your own keyboard
call-back function such that the user can increase or decrease the rotational
speed of the camera by pressing
the keys j or k respectively.
- Enhance the keyboard
call-back function such that the user can increase or decrease the radius of
orbit of the rotating camera by
pressing the keys i or m respectively.
- Enhance the keyboard
call-back function such that the user can let the camera revolve around the
origin on either one of the three planes x=0, y=0, or z=0 by pressing either one of the three keys x, y, or z respectively.
In other words, rotating around the x axis, the y axis, and the z axis
respectively. Note that be careful about the setting of the up vector in gluLookAt. When rotating around the y axis, the up
vector should always be set as (0,1,0). However,
when rotating around either the x axis or the z axis, you need to handle
this carefully:
- When rotating around the x axis, the x coordinate of the camera is always 0
while the y coordinate and the z coordinate of the camera keep changing
according to a changing angle parameter theta. In other words, we have x=0, y=radius*cos(theta) and z=
radius*sin(theta). Let PI be 3.14 in radian. Set the up vector as <0, cos(theta+PI/2), sin(theta+PI/2)>,
which is PI/2 (90 degrees) ahead
of theta and thus
perpendicular to the viewing direction from the camera to the origin as
the center.
- When rotating around the z axis, the z coordinate of the camera
is always 0 while the x coordinate and the y coordinate of the camera
keep changing according to a changing angle parameter theta. In other words, we have x=radius*cos(theta),
y= radius*sin(theta), and z=0.
Let PI be 3.14 in radian. Set
the up vector as <cos(theta+PI/2),
sin(theta+PI/2), 0>, which is PI/2 (90 degrees) ahead of theta and thus perpendicular to
the viewing direction from the camera to the origin as the center.
- Comments:
For step 5 above, you are not required to make a smooth
transition from moving around the origin on one orbit to another orbit
when the user presses x, y, or z
on the keuboard. In other words, a sudden
discrete shift of the camera from is acceptable when the mouse events
trigger such changes of orbits. However, there are ways to write code to
make this transition very smooth. For example, you can have variables to
remember what the new current target plane (x=0, y=0, or z=0) and previous
target plane (x=0, y=0, or z=0) are respectively. When the current camera
location is still on the previous orbit plane but not on the new orbit
plane, keep moving the camera on the old orbit plane until the camera
reach the a point on the intersection of the two orbit planes. Then you
can really start moving the camera around the new orbit plane.
- Submit
your source code together the self-evaluation
report under Canvas.
/******************************************************************/
In addition to the graphics commands listed in the end of
lab#1.
Spend time exploring the following functions.
glut functions: for registrating
the call-back functions and posting a redisplay flag
- glutDisplayFunc
- glutMouseFunc
- glutReshapeFunc
- glutKeyboardFunc
- glutIdleFunc
- glutPostRedisplay // posting a redisplay flag
glut functions: for swapping display
buffers and
- glutIntitDisplayMode(GLUT_DOUBLE
| GLUT_RGB); // Set up double
buffers
- glutSwapBuffers