glLookAt

Today we looked at the gluLookAt function and at producing shadows in OpenGL.

The gluLookAt function provides an alternative to glTranslatef and glRotatef sequences for positioning the camera. It does not, however, change the "lens" (or projection matrix) used. Here's the documentation:

gluLookAt

NAME

gluLookAt -- define a viewing transformation

C SPECIFICATION

void gluLookAt(GLdouble eyex,
GLdouble eyey,
GLdouble eyez,
GLdouble centerx,
GLdouble centery,
GLdouble centerz,
GLdouble upx,
GLdouble upy,
GLdouble upz)

PARAMETERS

eyex, eyey, eyez
Specifies the position of the eye point.
centerx
, centery, centerz
Specifies the position of the reference point.
upx
, upy, upz
Specifies the direction of the up vector.

DESCRIPTION

gluLookAt creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an up vector. The matrix maps the reference point to the negative z axis and the eye point to the origin, so that, when a typical projection matrix is used, the center of the scene maps to the center of the viewport. Similarly, the direction described by the up vector projected onto the viewing plane is mapped to the positive y axis so that it points upward in the viewport. The up vector must not be parallel to the line of sight from the eye to the reference point.

The matrix generated by gluLookAt postmultiplies the current matrix.


You should be able to convert gluLookAt to equivalent glTranslatef and glRotatef transformations, and vice versa. For example, if we want to view the cube bounded by (0,0,0) and (1,1,1) along it's main diagonal from a distance of 5 units from the origin, we could say:

glTranslate(0,0,-5);
glRotatef(35.2,1,0,0); //35.2 is acos(sqrt(2)/sqrt(3)) in degrees
glRotatef(-45,0,1,0);

or equivalently

gluLookAt(x,x,x,0,0,0,0,1,0);

The value 35.2 is the angle with a cosine of sqrt(2)/sqrt(3), which is the ration of the projection of the main diagonal into the x-z plane to the main diagonal. To determine x, we must have x*x + x*x +x*x = 5*5, or x = 5/sqrt(3).

A good quiz or exam question!

Next we looked at shadows. A shadow is just another projection (parallel for distant light sources such as the sun, otherwise perspective). One strategy is to create an image of an object entirely in black from a certain vantage point. Then clear the depth buffer

glClear(GL_DEPTH_BUFFER_BIT);

change the vantage point, then draw the object in its proper colors. Clearing the depth buffer essentially makes every point in the image "infinitely" far away. When we draw new points, they will always win the depth contest. This will put the shadow behind the object.

I haven't mastered the projections necessary for realistic shadows, but here's an example I produced with the above strategy:

Please read through section 5.5 in our text.