I figure out how to put the "camera" inside the cube I've created so that I can move in an FPS-like style. I tried to use gluLookAt and gluPerspective but I'm clearly missing some steps. What should I do before gluLookAt?
Here's the code written so far:
int rotate_Y; //used to rotate the cube about the Y-axis
int rotate_X; //used to rotate the cube about the X-axis
//the display function draws the scene and redraws it
void display(){
//clear the screen and the z-buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //resets the transformations
glRotatef(rotate_X, 1.0, 0.0, 0.0);
glRotatef(rotate_Y, 0.0, 1.0, 0.0);
//Front Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
//Back Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
//Right Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
//Left Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.7, 0.7, 0.0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
//Upper Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.7, 0.7, 0.3);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glEnd();
//Bottom Face of the cube - vertex definition
glBegin(GL_POLYGON);
glColor3f(0.2, 0.2, 0.8);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
glFlush();
glutSwapBuffers(); //send image to the screen
}
//the special keys function allows interaction via keys (also special ones)
void specialKeys(int key, int x, int y){
switch (key){
case GLUT_KEY_F1:
exit(0);
break;
case GLUT_KEY_LEFT:
rotate_Y -= 5;
break;
case GLUT_KEY_UP:
rotate_X -= 5;
break;
case GLUT_KEY_RIGHT:
rotate_Y += 5;
break;
case GLUT_KEY_DOWN:
rotate_X += 5;
break;
}
glutPostRedisplay(); //request a screen refresh to see changes
}
int main(int argc, char*argv[]){
//initialize GLUT
glutInit(&argc, argv);
//request double buffering, RGB colors window and a z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
//create a window
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Space");
//enable depth
glEnable(GL_DEPTH_TEST);
//callback functions
glutDisplayFunc(display); //display - redraws the scene
glutSpecialFunc(specialKeys); //special - allows interaction with specialkeys
//pass control to GLUT for events
glutMainLoop();
return 0; //this line is never reached
}
You'll want to use glTranslatef(float x,float y,float z) to move the camera.
Note that because of how OpenGL works, the transformations actually apply to the rest of the world and not the camera. So the above function will actually move everything drawn afterwards (and not the camera) by the amount specified.
To get the camera to move to a certain position, you'll want to negate all the components of the position before passing them to the function. That will move the world in the opposite direction, putting the camera where you want it.