TheCherno / Sparky

Cross-Platform High Performance 2D/3D game engine for people like me who like to write code.
Apache License 2.0
1.1k stars 222 forks source link

Perspective matrix seems incorrect #43

Open nyanzebra opened 9 years ago

nyanzebra commented 9 years ago

There seems to be a missing assignment for the last element in the matrix.

mat4 mat4::perspective(float fov, float aspectRatio, float near, float far)
    {
        mat4 result(1.0f);

        float q = 1.0f / tan(toRadians(0.5f * fov));
        float a = q / aspectRatio;

        float b = (near + far) / (near - far);
        float c = (2.0f * near * far) / (near - far);

        result.elements[0 + 0 * 4] = a;
        result.elements[1 + 1 * 4] = q;
        result.elements[2 + 2 * 4] = b;
        result.elements[3 + 2 * 4] = -1.0f;
        result.elements[2 + 3 * 4] = c;

        return result;
    }

I believe the 15th element should be 0. source = http://www.songho.ca/opengl/gl_projectionmatrix.html

So, fix would be adding

result.elements[3+3*4]=0;
Jacob-Mango commented 7 years ago

This should be fixed. I had a problem trying to setup a true first person camera but it kept on looking like a third person camera. By adding that line, the problem was fixed.

Crisspl commented 7 years ago

Make a pull request