Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This time I'm trying to learn the core of matrix transformations. I dont like the idea to using a Math library and dont understand what is hapening on background, because this I'm having some dificulties with Projection Matrix. I've read many tutorials that explain how Projection Matrices show be and what they do. But at this moment I got two different Projection Matrices and I dont know which should I use.

Look at this, frist from this tutorial:

private float near = 0.1f;
private float far = 100.0f;
private float fov = (float)Math.toRadians(45.0);
private float aspect = (float) 400 / (float) 300;

private float f = (float)Math.tan(fov / 2.0f) * near;
private float Sx = (2.0f * near) / (f * aspect + f * aspect);
private float Sy = near / f;
private float Sz = -(far + near) / (far - near);
private float Pz = -(2.0f * far * near) / (far - near);

projection = new Matrix4f(new float[]{
        Sx,   0.0f, 0.0f, 0.0f,
        0.0f, Sy,   0.0f, 0.0f,
        0.0f, 0.0f, Sz,   -1.0f,
        0.0f, 0.f,  Pz,   0.0f,
    });

Now look this second, from this tutorial:

private float near = 0.1f;
private float far = 100.0f;
private float fov = (float)Math.toRadians(45.0);
private float aspect = (float) 400 / (float) 300;

private float f = 1.0f / (float)Math.tan(fov / 2.0f);
private float Sx = f / aspect;
private float Sy = f;
private float Sz = -(far+near) / (far-near);
private float Pz = -(2.0f * near * far) / (far – near);


projection = new Matrix4f(new float[]{
        Sx,   0.0f, 0.0f, 0.0f,
        0.0f, Sy,   0.0f, 0.0f,
        0.0f, 0.0f, Sz,   -1.0f,
        0.0f, 0.f,  Pz,   0.0f,
    });

So, which is correct? Or if both are correct, there is some recommendations? I think the second one is more easy to read.

P.S.: In my code i'm using Right Handed System and Column-Major Matrices.

Thanks for your attention!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

They are both correct and are mathematically equivalent - they should generate the same numerical values (up to some roundoff error). As you say, the second one is easier to read (and is a direct transcription of the matrix in the gluPerspective doc page). In the first one, the calculation of Sx and Sy isn't completely simplified: the factors of near should be canceled out since it appears in both numerator and denominator. It shouldn't affect the correctness but it is doing unnecessary work.

share|improve this answer
    
Thanks! This helped me so much! –  Afonso Lage Jun 21 '13 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.