Aspect ratio problem

Started by Mihai_Ionut_Floares, December 01, 2020, 20:59:18

Previous topic - Next topic

Mihai_Ionut_Floares

Hi. This should be a square, instea it is a rectangle:
https://streamable.com/nympw7

Isn't that why we are using projection matrices?
public static mat4 perspective(float fov, float aspect, float zNear, float zFar) {
mat4 Result = new mat4();
		

		float tanHalfFovy = (float) Math.tan(fov / 2);

		
		Result.mat[0][0] = 1 / (aspect * tanHalfFovy);
		Result.mat[1][1] = 1 / (tanHalfFovy);
		Result.mat[2][2] = - (zFar + zNear) / (zFar - zNear);
		Result.mat[2][3] = - 1;
		Result.mat[3][2] = - (2 * zFar * zNear) / (zFar - zNear);
		return Result;
}



This is the perspective code. I copied it from c++ glm and adapt it to java. The code works, the camera movement feels good, the distances between object are normal, just it stretches objects sides.
My window is 1024:768 which is 4/3

methods.perspective((float)(Math.toRadians(45f)), (float)(4/3), 0.01f , 2000f);


I know that because the window is a rectangle it fits a square in it and that cause the stretch. I would prefer to render more of the scene instead of stretching the square.

KaiHH

Quote from: Mihai_Ionut_Floares on December 01, 2020, 20:59:18
methods.perspective((float)(Math.toRadians(45f)), (float)(4/3), 0.01f , 2000f);

The problem is that (float)(4/3) first performs an integer division, since both 4 and 3 are integers, whose result is 1, and then you cast that 1 to a float 1.0f. You should change this to (float) 4 / 3 in order to first cast the 4 to a float and then divide by 3 using a floating point division, since one of the operands is float.

Mihai_Ionut_Floares

I simply used 1.333f since 4/3=1,(3).

UPDATE: I casted the both 4 and 3 with f(4f/3f). It is working too.


Thank you.

Mihai_Ionut_Floares

Also, do you know why when I set the model matrix with a simple rotation, it rotates the object, but it scales it too?

KaiHH

Quote from: Mihai_Ionut_Floares on December 01, 2020, 22:36:20
Also, do you know why when I set the model matrix with a simple rotation, it rotates the object, but it scales it too?
Check if the three column vectors of the upper-left 3x3 submatrix of your 4x4 model matrix are unit vectors.

Mihai_Ionut_Floares

I don't know I build the matrices alreade transposed. The translation matrix should be:
1 0 0 X
0 1 0 Y
0 0 1 Z
0 0 0 1
(put in float[] each column)
Instead I built it like that
1 0 0 0
0 1 0 0
0 0 1 0
X Y Z 1

KaiHH

You asked about why your rotation matrix also contains a scaling. What you now showed is not a rotation matrix, only a translation matrix. So, what exactly is the problem?

Mihai_Ionut_Floares

cosθ+Rx2(1−cosθ)                         Rx*Ry(1−cosθ)−Rz*sinθ        Rx*Rz(1−cosθ)+Ry*sinθ   0   
Ry*Rx(1−cosθ)+Rz*sinθ         cosθ+Ry2(1−cosθ)                Ry*Rz(1−cosθ)−Rx*sinθ   0   
Rz*Rx(1−cosθ)−Ry*sinθ         Rz*Ry(1−cosθ)+Rx*sinθ        cosθ+Rz2(1−cosθ)           0
0                                                 0                                        0                                           1
(Where vec3(Rx, Ry, Rz) is the rotation axis and θ is the angle)

With this it works now. Thanks