PDA

View Full Version : How to put the “camera” inside a cube in OpenGL



drew_jewel
06-19-2015, 11:11 AM
I'm very new to OpenGL and can't seem to figure out how to put the "camera" inside the cube I've created and achieve an FPS-like style pov. 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:




//values
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 special keys

//pass control to GLUT for events
glutMainLoop();

return 0;
}


Feel free to ask more about what I'm trying to achieve if I wasn't clear enough. Thanks for your help!

GClements
06-19-2015, 12:59 PM
The fixed-function pipeline has 2 distinct matrices you need to deal with: the projection matrix and the model-view matrix. Matrix operations such as gluPerspective() and gluLookAt() operate upon the current matrix as selected using glMatrixMode().

The projection (perspective or orthographic) transformation should use the projection matrix, while the viewing transformation should use the model-view matrix.

For a FPS-style camera, you probably shouldn't be using gluLookAt() but a combination of glTranslate() and glRotate().

Bear in mind that OpenGL doesn't have a "camera" per se. You control the view by transforming the scene into the camera's space, effectively applying the inverse of the camera transformation to the model-view matrix. Typical code is


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-cam_x, -cam_y, -cam_z);
glRotatef(-cam_pitch, 1, 0, 0);
glRotatef(-cam_heading, 0, 1, 0);


For a cube of size 1 centred on the origin, the initial camera position should probably be (0,0,0). For the perspective projection, ensure that the near plane is significantly less than 0.5 and the far plane greater than 2 so that the cube isn't clipped away by those planes. E.g.


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, (float)width/height, 1e-3, 10);

drew_jewel
06-19-2015, 01:17 PM
Thank you GClements, now I have a more defined idea on how I should proceed.

In addition, I was wondering if you could link or name some useful resources (maybe tutorials) about OpenGL and in particular about how the "camera" works in OpenGL. I'm already exploring the Red Book but you know, the more the better. Thanks a lot once again, I'll let you know if I can make it work.