Orthographic projection matrix

Started by Mihai_Ionut_Floares, January 07, 2021, 13:29:24

Previous topic - Next topic

Mihai_Ionut_Floares

I want to add the orthographic camera and I have this code:
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?

KaiHH

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.

Mihai_Ionut_Floares

I tried with this code to
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

Mihai_Ionut_Floares

I also found this image from wikipedia:


The calculations on column 3, row 0 and column 3 row 1 are useless.

Mihai_Ionut_Floares

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?

Mihai_Ionut_Floares

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)