I'm trying to learn how to make my own model, view, projection setup. I've managed to translate, rotate, and scale my models, but have an issue with my perspective projection matrix.
Even though I'm multiplying halfFOV with my aspect ratio, the image looks squished unless my window is a perfect square like this:
If it's not a perfect square: the wider my window the more stretched my object looks in the Z axis like an egg.
So it definitely has to do with with my projection matrix, specifically related to my aspect ratio. The way I'm multiplying my matrices is as follows, I'll show you my translation and projection keep it simple:
Translation
[1, 0, 0, 0,
0, 1 0, 0
0, 0, 1, 0,
x, y, z, 1]
Projection
let halfFOV = Math.tan(toRad(FOV/2.0));
let zRange = (NEAR - FAR);
let x = 1.0 / (halfFOV * aspect);
let y = 1.0 / (halfFOV);
let z = (NEAR + FAR) / zRange;
let w = 2 * FAR * NEAR / zRange;
[x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, -1,
0, 0, w, 0]
I normally see the -1 where the w is, but for some reason I need to set it up the way you see it in my matrix, if not it won't work. I'll also share how I'm multiplying matrices very quickly:
function (r, a, b)
r.mat[0] = (a.mat[0] * b.mat[0]) + (a.mat[1] * b.mat[4]) + (a.mat[2] * b.mat[8]) + (a.mat[3] * b.mat[12]);
r.mat[1] = (a.mat[0] * b.mat[1]) + (a.mat[1] * b.mat[5]) + (a.mat[2] * b.mat[9]) + (a.mat[3] * b.mat[13]);
r.mat[2] = (a.mat[0] * b.mat[2]) + (a.mat[1] * b.mat[6]) + (a.mat[2] * b.mat[10]) + (a.mat[3] * b.mat[14]);
r.mat[3] = (a.mat[0] * b.mat[3]) + (a.mat[1] * b.mat[7]) + (a.mat[2] * b.mat[11]) + (a.mat[3] * b.mat[15]);
// don't need to add the rest of it...
// How I use it
Mathf.mul(resultMat, position, projection);
So I take the left row and multiply it against the right column. I then get rows as a result as you can see. If you want to check out the full implementation it's here. I've also checked that I'm getting the correct window size. I divide width/height to get the aspect ratio too. Not sure what I'm doing wrong. I also tried multiplying my matrices on the gpu (glsl) and I gt the same results, so it's definitely my projection matrix
Hope this all makes sense.
Edit: I probably should have posted this thread in the Math categories, my apologies.