Search code examples
c++openglsfml

Trouble with the use of gluLookAt


I have looked at multiple questions about this, including this one. What I want is a person looking down a corridor, looking at the back wall with the left and right walls on the side view of the person. Here is my code:

Init

float lookAtX = 0, lookAtY = 0, lookAtZ = -2.5, posX = 0, posY = 0, posZ = 0;

Drawing

    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);

    gluLookAt(
        posX, posY, posZ, 
        lookAtX, lookAtY, lookAtZ, 
        0, 0, 1
    );
    // Back
    glBegin(GL_POLYGON);
    glColor3f(255, 0, 0); // Red
    glVertex3f(-2.5, -2.5, 2.5);
    glVertex3f(2.5, 2.5, 2.5);
    glVertex3f(2.5, -2.5, 2.5);
    glVertex3f(-2.5, -2.5, 2.5);
    glEnd();

    // Left
    glBegin(GL_POLYGON);
    glColor3f(0, 255, 0); // Green
    glVertex3f(-2.5, 2.5, 2.5);
    glVertex3f(-2.5, 2.5, -2.5);
    glVertex3f(-2.5, -2.5, 2.5);
    glVertex3f(-2.5, -2.5, -2.5);
    glEnd();

    // Right
    glBegin(GL_POLYGON); 
    glColor3f(0, 0, 255); // Yellow
    glVertex3f(2.5, 2.5, 2.5);
    glVertex3f(2.5, 2.5, -2.5);
    glVertex3f(2.5, -2.5, 2.5);
    glVertex3f(2.5, -2.5, -2.5);
    glEnd();

    glFlush();
    window.pushGLStates();

I am confident that the outside code is correct because I had another piece of code with the same skeleton. I was also confident that this would work... but I guess it didn't xD. Why is there a black screen and why does it not show the walls? Thank you for taking the time to read this


Solution

  • Firat of all note, that drawing by glBegin/glEnd sequences is deprecated since several years. Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.


    Anyway, you have to set up a perspective projection matrix. But note, operations like gluLookAt and gluPerspective do not only define a matrix.
    This operations define a matrix and multiply the current matrix on the matrix stack by the new matrix. This means you have to initialize the matrix on the matrix by the identity matrix first. This can be done by glLoadIdentity.

    Use gluPerspective to set the perspective projection on the projection matrix stack - glMatrixMode( GL_PROJECTION ):

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 90.0f, view_width / view_height, 0.1f, 10.0f );
    

    When you set the view matrix, then you have to change the point of view and look in the direction of the "corridor". Since you look alng the z axis, the up vector has to be (0, 1, 0):

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    float lookAtX = 0, lookAtY = 0, lookAtZ = 0, posX = 0, posY = 0, posZ = -5;
    gluLookAt( posX, posY, posZ, lookAtX, lookAtY, lookAtZ, 0, 1, 0 );
    

    Finally your vertex coordinates are mixed. Change it like this:

    // Back
    glBegin(GL_POLYGON);
    glColor3ub(255, 0, 0); // Red
    glVertex3f(-2.5, -2.5, 2.5);
    glVertex3f(-2.5,  2.5, 2.5);
    glVertex3f( 2.5,  2.5, 2.5);
    glVertex3f( 2.5, -2.5, 2.5);
    glEnd();
    
    // Left
    glBegin(GL_POLYGON);
    glColor3ub(0, 255, 0); // Green
    glVertex3f(-2.5,  2.5,  2.5);
    glVertex3f(-2.5,  2.5, -2.5);
    glVertex3f(-2.5, -2.5, -2.5);
    glVertex3f(-2.5, -2.5,  2.5);
    glEnd();
    
    // Right
    glBegin(GL_POLYGON); 
    glColor3ub(0, 0, 255); // Blue
    glVertex3f(2.5,  2.5,  2.5);
    glVertex3f(2.5,  2.5, -2.5);
    glVertex3f(2.5, -2.5, -2.5);
    glVertex3f(2.5, -2.5,  2.5);
    glEnd();
    

    Preview: