Start with the program, template3d.c, you developed in the last tutorial.
Add one camera.
GLdouble eye[]={
2.0, 2.0, 2.0
};
with
GLdouble eye[][3]={
{ 2.0, 2.0, 2.0 },
{ 0.0, 0.0, 5.0 }
};
GLdouble centre[]={
0.0, 0.0, 0.0
};
with
GLdouble centre[][3]={
{ 0.0, 0.0, 0.0 },
{ 0.0, 0.0, 3.0 }
};
GLdouble up[]={
0.0, 1.0, 0.0
};
with
GLdouble up[][3]={
{0.0, 1.0, 0.0},
{0.0, 1.0, 0.0}
};
enum {cameraOne, cameraTwo, END_CAMERAS};
GLint currentCamera = cameraOne;
setCamera replace
gluLookAt( eye[x], eye[y], eye[z],
centre[x], centre[y], centre[z],
up[x], up[y], up[z]);
with
gluLookAt(
eye[currentCamera][x], eye[currentCamera][y], eye[currentCamera][z],
centre[currentCamera][x], centre[currentCamera][y], centre[currentCamera][z],
up[currentCamera][x], up[currentCamera][y], up[currentCamera][z]);
keyboard/
case 'c':
currentCamera++;
if(currentCamera == END_CAMERAS)
currentCamera = cameraOne;
break;
Move the cameras using the special function keys. Below is a suggestion for responding to the up arrow key (move into the scene down along the negative z-axis).
special.
case GLUT_KEY_UP :
move(z, -0.1);
break;
/*
** TODO: Add comments
*/
void move(int axis, GLdouble amount ){
eye[currentCamera][axis] += amount;
if(currentCamera == cameraTwo)
centre[currentCamera][axis] += amount;
}
glutInitDisplayMode. You need to be able to do both double buffering and depth buffering.
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
You need to restore the position of the camera after it has moved.
One way to tackle this is to keep a copy of the default positions.
GLdouble eyeDefault[][3]={
{ 2.0, 2.0, 2.0 },
{ 0.0, 0.0, 5.0 }
};
GLdouble centreDefault[][3]={
{ 0.0, 0.0, 0.0 },
{ 0.0, 0.0, 3.0 }
};
GLdouble upDefault[][3]={
{0.0, 1.0, 0.0},
{0.0, 1.0, 0.0}
};