Sunday, March 4, 2012

Controlling Camera and Line of Sight in OpenGL

Camera controlling is essential in 3D viewing. OpenGL provides a function gluLookAt() for this purpose. The syntax of the function gluLookAt() is
void gluLookAt ( GLdouble eyex,
GLdouble eyey,
GLdouble eyez,
GLdouble centerx,
GLdouble centery,
GLdouble centerz,
GLdouble upx,
GLdouble upy,
GLdouble upz );

It takes three sets of arguments, which specify the location of the viewpoint, define a reference point toward which the camera is aimed, and indicate which direction is up. The first three numbers represent where the camera is positioned.
The  next  three  values  represent  where  we  want  the  camera  to  look.  The last three values tell  OpenGL which direction represents up (that is, the direction from the bottom to the top of the viewing volume).

(exex, eyey, eyez) and (centerx, centery, centerz) constitutes a view vector.  There is no need for the view vector and the up vector to be deﬁned at right angles to each other. Often the up vector is set to a ﬁxed direction in the scene, e.g. pointing up the world Y axis. In the general case, OpenGL twists the camera around the view vector axis until the top of the camera matches the speciﬁed up direction as closely as possible.