Hello Guest

Aspect ratio problem

  • 7 Replies
  • 4442 Views
Aspect ratio problem
« on: December 01, 2020, 20:59:18 »
Hi. This should be a square, instea it is a rectangle:
https://streamable.com/nympw7

Isn't that why we are using projection matrices?
Code: [Select]
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

Code: [Select]
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.

*

Offline KaiHH

  • ****
  • 334
Re: Aspect ratio problem
« Reply #1 on: December 01, 2020, 22:07:05 »
Code: [Select]
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.

Re: Aspect ratio problem
« Reply #2 on: December 01, 2020, 22:31:32 »
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.

Re: Aspect ratio problem
« Reply #3 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?

*

Offline KaiHH

  • ****
  • 334
Re: Aspect ratio problem
« Reply #4 on: December 02, 2020, 10:22:17 »
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.

Re: Aspect ratio problem
« Reply #5 on: December 02, 2020, 12:22:46 »
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

*

Offline KaiHH

  • ****
  • 334
Re: Aspect ratio problem
« Reply #6 on: December 02, 2020, 13:21:36 »
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?

Re: Aspect ratio problem
« Reply #7 on: December 02, 2020, 14:28:11 »
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