0
\$\begingroup\$

I'm really new to OpenGL and can't seem to 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
}

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
}

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

\$\endgroup\$
3
  • \$\begingroup\$ What does your code give you? How does it differ from what you expected to see? \$\endgroup\$
    – Anko
    Jun 20, 2015 at 11:06
  • 1
    \$\begingroup\$ My code gives me a cube (seen in front) which I can rotate up, down, right and left. Now I want to place the "camera" inside the cube so I would like to see the colored walls of the cube surrounding me. I've tried to do gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0); because the cube is centered at (0,0,0) but it just shows a black screen. \$\endgroup\$
    – Andrew
    Jun 20, 2015 at 11:08
  • \$\begingroup\$ The technique you are after is called a skybox in case it helps you \$\endgroup\$
    – Alan Wolfe
    Jun 21, 2015 at 1:28

1 Answer 1

1
\$\begingroup\$

The reason for you getting a black screen when you put the camera inside the cube is most likely back face culling.

Try calling:

glDisable(GL_CULL_FACE);

In the beginning of your display function or in main (after enabling depth test) to disable back face culling.

If that is helpful you can either change the winding order of your polygons or change what is considered a "back face" (using glCullFace(GL_FRONT)) since you probably will want to have back face culling enabled for performance reasons.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .