Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
farmboy0 opened this issue Jul 26, 2023 · 4 comments
Closed

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

farmboy0 opened this issue Jul 26, 2023 · 4 comments
Labels

Comments

@farmboy0
Copy link

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
Copy link
Member

httpdigest commented Jul 26, 2023

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
Copy link
Author

farmboy0 commented Jul 26, 2023

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

@httpdigest
Copy link
Member

httpdigest commented Jul 26, 2023

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants