# Which is the right way to build a OpenGL Projection Matrix

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 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 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.

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.