JOML-CI / JOML

A Java math library for OpenGL rendering calculations
MIT License
729 stars 104 forks source link

Matrix4f.setFrustum with zNear values < 1.0f creates distortions #341

Closed farmboy0 closed 1 year ago

farmboy0 commented 1 year ago

I compared JOML's implementation with https://github.com/KhronosGroup/OpenXR-SDK-Source/blob/d6b6d7a10bdcf8d4fe806b4f415fde3dd5726878/src/common/xr_linear.h#L560

and there are only 2 differences m00 = 2.0f / (right - left) instead of (zNear + zNear) / (right - left) m11 = 2.0f / (top - bottom) instead of (zNear + zNear) / (top - bottom)

This works without distortions even for very small zNear values like 0.05f

httpdigest commented 1 year ago

I don't know what "distortions" you are referring to, but both functions are actually equivalent, as are the implementation of GLM and the derivations of http://www.songho.ca/opengl/gl_projectionmatrix.html and http://learnwebgl.brown37.net/08_projections/projections_perspective.html#building-the-prospective-projection-transform .

The OpenXR function just takes different parameters. It does not take the same parameters as the typical OpenGL/JOML frustum function, but instead solely the tangens of the angle from the viewpoint to the left, right, top and bottom plane edges of the near plane (so regardless of the distance to the near plane along the Z axis). The standard OpenGL/JOML frustum function expects the actual distances of the vector along the Z view direction to the left, right, top and bottom edges of the near plane.

In the end, all those function are exactly equivalent.

The OpenXR function XrMatrix4x4f_CreateProjection is more comparable to JOML's perspectiveOffCenterFov: https://javadoc.io/doc/org.joml/joml/latest/org/joml/Matrix4f.html#perspectiveOffCenterFov(float,float,float,float,float,float) in that this JOML function also takes just angular arguments to the left, right, top and bottom near plane edges (the actual angles in this case, not the tangens, but that's irrelevant here). To then compute those angles to actual distances, the near value (distance along z-axis to the near plane) is multiplied in.

farmboy0 commented 1 year ago

Thank you for pointing me to Matrix4f.setPerspectiveOffCenterPov, somehow I missed that function and only found setFrustum.

httpdigest commented 1 year ago

Yeah, what can I say: Naming things and trying to find things by name is hard. :)

Actually, if you have the effective tan of the angles, instead of the angles themselves, and you don't need to compute tan(angle) yourself, then you could simply call the setFrustum method like setPerspectiveOffCenterPov does: https://github.com/JOML-CI/JOML/blob/main/src/main/java/org/joml/Matrix4f.java#L10307

setFrustum(tanAngleLeft*zNear, tanAngleRight*zNear, tanAngleDown*zNear, tanAngleUp*zNear, zNear, zFar);

given that you already have tanAngleLeft, tanAngleRight, tanAngleDown and tanAngleUp (without requiring you to compute the tan(angle) function yourself from actual angles, of course). So, it's just about multiplying all the tan(angle) arguments with zNear and you have effectively the arguments that setFrustum needs.

farmboy0 commented 1 year ago

I was computing the tan(angle) values myself because I get the angles from the openxr runtime, unfortunately i only found the setFrustum function to use them.