Custom Lookat

Because GLSL 1.3 onwards need a matrix sending to the shader, I need to make my own projection matrix for the “sky” layer. This entails setting up a perspective and then twisting the scene around to match the perspective of the player. I got the perspective down and it works wonderfully. But after three whole damned days of trying to get the lookat to work, I give up. It’s only using a view direction normal, it honestly shouldn’t be that hard.


    //Standard projection matrix.
    float fNear = 1.0f, fFar = 1024.0f;
    float fTop = 1.0f * tan( 45.0f * M_PI / 360.0f ), fAspect = ( float ) tsCurrentConfiguration.uiScreenWidth / ( float ) tsCurrentConfiguration.uiScreenHeight;
    float fBottom = -fTop, fLeft = fAspect * -fTop, fRight = fAspect * fTop;

    glfPerspective[ 0 ] = 2.0f * fNear / ( fRight - fLeft );
    glfPerspective[ 1 ] = 0.0f;
    glfPerspective[ 2 ] = ( fRight + fLeft ) / ( fRight - fLeft );
    glfPerspective[ 3 ] = 0.0f;

    glfPerspective[ 4 ] = 0.0f;
    glfPerspective[ 5 ] = 2.0f * fNear / ( fTop - fBottom );
    glfPerspective[ 6 ] = ( fTop + fBottom ) / ( fTop - fBottom );
    glfPerspective[ 7 ] = 0.0f;

    glfPerspective[ 8 ] = 0.0f;
    glfPerspective[ 9 ] = 0.0f;
    glfPerspective[ 10 ] = ( fFar + fNear ) / ( fFar - fNear );
    glfPerspective[ 11 ] = -2.0f * fFar * fNear / ( fFar - fNear );

    glfPerspective[ 12 ] = 0.0f;
    glfPerspective[ 13 ] = 0.0f;
    glfPerspective[ 14 ] = -1.0f;
    glfPerspective[ 15 ] = 0.0f;

    //A very simple lookat matrix, yaw only.
    //fCameraDirection X/Z are most definitely a unit vector.
    GLfloat glfLookAtThat[ 16 ];

    glfLookAtThat[ 0 ] = fCameraDirectionZ;
    glfLookAtThat[ 1 ] = 0.0f;
    glfLookAtThat[ 2 ] = fCameraDirectionX;
    glfLookAtThat[ 3 ] = 0.0f;

    glfLookAtThat[ 4 ] = 0.0f;
    glfLookAtThat[ 5 ] = 1.0f;
    glfLookAtThat[ 6 ] = 0.0f;
    glfLookAtThat[ 7 ] = 0.0f;

    glfLookAtThat[ 8 ] = fCameraDirectionX;
    glfLookAtThat[ 9 ] = 0;
    glfLookAtThat[ 10 ] = fCameraDirectionZ;
    glfLookAtThat[ 11 ] = 0.0f;

    glfLookAtThat[ 12 ] = 0.0f;
    glfLookAtThat[ 13 ] = 0.0f;
    glfLookAtThat[ 14 ] = 0.0f;
    glfLookAtThat[ 15 ] = 1.0f;

    //Let's put the two together.
    GLfloat glfComposite[ 16 ];

    glfComposite[ 0 ] = 1.0f;
    glfComposite[ 1 ] = 0.0f;
    glfComposite[ 2 ] = 0.0f;
    glfComposite[ 3 ] = 0.0f;

    glfComposite[ 4 ] = 0.0f;
    glfComposite[ 5 ] = 1.0f;
    glfComposite[ 6 ] = 0.0f;
    glfComposite[ 7 ] = 0.0f;

    glfComposite[ 8 ] = 0.0f;
    glfComposite[ 9 ] = 0.0f;
    glfComposite[ 10 ] = 1.0f;
    glfComposite[ 11 ] = 0.0f;

    glfComposite[ 12 ] = 0.0f;
    glfComposite[ 13 ] = 0.0f;
    glfComposite[ 14 ] = 0.0f;
    glfComposite[ 15 ] = 1.0f;

    //Multiply the perspective and the lookat.
    uint8_t ui8Y = 0;

    while( ui8Y < 4 )
    {

        uint8_t ui8X = 0;

        while( ui8X < 4 )
        {

            glfComposite[ ui8X + ( ui8Y * 4 ) ] = ( glfPerspective[ ui8X ] * glfLookAtThat[ ui8Y * 4 ] ) + ( glfPerspective[ ui8X + 4 ] * glfLookAtThat[ ( ui8Y * 4 ) + 1 ] ) + ( glfPerspective[ ui8X + 8 ] * glfLookAtThat[ ( ui8Y * 4 ) + 2 ] ) + ( glfPerspective[ ui8X + 12 ] * glfLookAtThat[ ( ui8Y * 4 ) + 3 ] );

            ui8X++;

        }

        ui8Y++;

    }

    //Testing this matrix
    glLoadMatrixf( glfComposite );

I’m fairly sure there’s nothing wrong with the matrix multiplication because if I use the fixed function pipeline to load the perspective and then multiply the lookat I get exactly the same result. Which is that it looks fine at first but as the camera yaws the scene folds up and moves to one side of the screen.

The perspective is also fine; if I load the perspective matrix then use gluLookAt, no problem. Works great. But I can’t get the result of that into my shader without using some slow function to retrieve the GL matrix.

There is MOST DEFINITELY something screwy going down because if I use GL’s load/multiply functions with a hand-made matrix (identity with the X and Z swapped around, should rotate the scene) it still does the same thing.

Check out the Mesa3D source code for the gluLookAt function.

See - http://www.opengl.org/sdk/docs/man/xhtml/gluLookAt.xml

I’ve tried a full reimplementation of gluLookAt from the docs, I got the exact same result. I’ve ended up using my own implementation of glRotate which works at least.