Results 1 to 3 of 3

Thread: How to put the “camera” inside a cube in OpenGL

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2015
    Posts
    7

    How to put the “camera” inside a cube in OpenGL

    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:

    Code :
     
    //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!

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,103
    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
    Code :
    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.
    Code :
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90, (float)width/height, 1e-3, 10);

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2015
    Posts
    7
    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.

Similar Threads

  1. inside cube and textures
    By alex2 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-25-2011, 09:26 AM
  2. Inside of a cube (quad)
    By Tomatosoup in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-04-2010, 09:02 PM
  3. Rotation with camera view inside cube
    By reps4ta@aol.com in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 07-06-2003, 02:58 PM
  4. display a cube inside another bigger cube
    By jyoung77 in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 05-08-2003, 06:12 PM
  5. games where you're inside a cube...
    By MarsuGL in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 07-17-2001, 12:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean