Perspective calculation

I am not using GLU for perspective calculation because I need to do my frustum culling with the exact matrix that we use to render. But I am doing something wrong as it is slightly squishing my perspective from top to bottom.


    public void perspective(float fovx, float aspect, float zNear, float zFar)
    {

        float sine, cotangent, deltaZ;
        float radians = fovx / 2 * (float)Math.PI / 180;

        deltaZ = zNear - zFar;
        sine = (float)Math.sin(radians);
        if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
            return;
        }
        cotangent = (float)Math.cos(radians) / sine;

        setZero();
        matrix.m00 = cotangent / aspect;
        matrix.m11 = cotangent;
        matrix.m22 = (zFar + zNear) / deltaZ;
        matrix.m23 = -1;
        matrix.m32 = 2 * zNear * zFar / deltaZ;

           }

Any transformation wizards out there that could point out my mistake?

Wild guess; your aspect is inversed? =)

A fleeting look at my own stuff shows that
if you replace fovX with fovY and treat
aspect ratio as width / height, then your equations
look alright.

Actually, you could just compute w and h and call
glFrustum(…).

HTH

GLU for the most part is simply a set of convinience methods and wrappers that operate on GL. Its perfectly ok to use something like gluPerspective, since it will create the same GL state as if you’d done it manually. glGet will return the same matricies regardless of how you set it up.

You could use gluPerspective with the same numbers and compare the results to see if you’re doing it correctly. I’d guess your aspect is wrong. Often an aspect of 1 looks more correct than a properly calculated one…