Hello Guest

Orthographic projection matrix

  • 5 Replies
  • 217 Views
Orthographic projection matrix
« on: January 07, 2021, 13:29:24 »
I want to add the orthographic camera and I have this code:
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;
}
public static mat4 ortho(float zNear, float zFar) {
mat4 Result = new mat4();

Result.mat[0][0] = Main.WIDTH / 2;
Result.mat[1][1] = Main.HEIGHT / 2;
Result.mat[2][2] = (zFar - zNear) / (-2);
Result.mat[3][2] = -((zFar + zNear) / 2);

return Result;
}

As you can see the second method is for orthographic. The perspective projection matrix which is above works well, but when I switch to the ortho method, the scene is fully black, the skybox. The prespective code I took from this old wiki http://wiki.lwjgl.org/wiki/The_Quad_with_Projection,_View_and_Model_matrices.html#Projection_matrix and it works. The orthographic code I created using this site http://www.songho.ca/opengl/gl_projectionmatrix.html. In the shader I multiply like normal Projection * View * Model * vec4(VertexPos, 1) should I adapt the multiplication for the orthographic projection?
« Last Edit: January 07, 2021, 13:33:11 by Mihai_Ionut_Floares »

*

Offline KaiHH

  • ****
  • 318
Re: Orthographic projection matrix
« Reply #1 on: January 07, 2021, 14:03:08 »
Please look again at http://www.songho.ca/opengl/gl_projectionmatrix.html#ortho and at the matrix at the bottom and then look at the values you assign to the matrix elements. They are reciprocals of the actual values you need to use.

Re: Orthographic projection matrix
« Reply #2 on: January 07, 2021, 14:21:37 »
I tried with this code to
Code: [Select]
Result.mat[0][0] = 2 / Main.WIDTH;
Result.mat[1][1] = 2 / Main.HEIGHT;
Result.mat[2][2] = (-2) / (zFar - zNear) ;
Result.mat[3][2] = -((zFar + zNear) / (zFar - zNear));

This is exactly like on http://www.songho.ca/opengl/gl_projectionmatrix.html

Re: Orthographic projection matrix
« Reply #3 on: January 07, 2021, 14:26:48 »
I also found this image from wikipedia:


The calculations on column 3, row 0 and column 3 row 1 are useless.
« Last Edit: January 07, 2021, 14:28:39 by Mihai_Ionut_Floares »

Re: Orthographic projection matrix
« Reply #4 on: January 07, 2021, 14:42:51 »
With the wikipedia one it works, but I thought it wasn't because the skybox is black, Orthographic doesn't support object with the translation removed from view matrix like the cubemap?

Re: Orthographic projection matrix
« Reply #5 on: January 07, 2021, 15:00:09 »
Plus, when I face an object and approach it by pressing W and moving towards it, it stays at the same size(it doesn't look like I'm near it when I move side with A and D it moves relative to me and looks natural, but when I use the W and S it doesn't look bigger when I approach and it doesn't shrink when I walk backwards with S like normal. Is this a side effect of the orthographic projection or it something wrong). And I had to use the aspect ration for it to work(When on wikipedia photo it says right - left and top - bottom instead of Main.WIDTH for right - left I used 4f and instead of Main.HEIGHT for top - bottom I used 3f because my aspect ratio is 4:3)