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

Fix scaling of czm_normal #11553

Merged
merged 5 commits into from
Oct 5, 2023
Merged

Fix scaling of czm_normal #11553

merged 5 commits into from
Oct 5, 2023

Conversation

jjhembd
Copy link
Contributor

@jjhembd jjhembd commented Oct 3, 2023

Normals should be transformed by the inverse transpose of the model-view matrix. See lighthouse3d.com and Jason McKesson's Learning Modern 3D Graphics Programming.

The automatic uniform czm_normal, as computed in UniformState, is currently derived from the transpose of the rotation component of the inverse model-view matrix. This approach is only valid when the model-view matrix has uniform scale.

I think UniformState was actually correct before #8182. In that PR, we renamed Matrix4.getRotation asMatrix4.getMatrix3 to better describe what it was actually doing. But then in UniformState, we mistakenly replaced Matrix4.getRotation with a Matrix4.getMatrix3 followed by Matrix3.getRotation.

This PR removes the spurious Matrix3.getRotation call.

One caution: ever since #8182, czm_normal has been unitary. This PR will leave a scale component in the matrix, so normals will need to be re-normalized after the transform.

@jjhembd jjhembd requested a review from lilleyse October 3, 2023 23:41
@cesium-concierge
Copy link

Thanks for the pull request @jjhembd!

  • ✔️ Signed CLA found.
  • CHANGES.md was not updated.
    • If this change updates the public API in any way, please add a bullet point to CHANGES.md.
  • ❔ Unit tests were not updated.
    • Make sure you've updated tests to reflect your changes, added tests for any new code, and ran the code coverage tool.

Reviewers, don't forget to make sure that:

  • Cesium Viewer works.
  • Works in 2D/CV.

@jjhembd
Copy link
Contributor Author

jjhembd commented Oct 3, 2023

I pushed a similar fix for czm_normal3D.

I'm not sure yet what is the correct approach for czm_inverseNormal and czm_inverseNormal3D.

@lilleyse
Copy link
Contributor

lilleyse commented Oct 4, 2023

Excellent catch @jjhembd.

I'm not sure yet what is the correct approach for czm_inverseNormal and czm_inverseNormal3D.

Shouldn't the getRotation calls be removed there too? Or is it more involved than that?

@jjhembd
Copy link
Contributor Author

jjhembd commented Oct 4, 2023

@lilleyse I think czm_inverseNormal, to fit the definition, should be the transpose of the model-view matrix. This would make it the true inverse of czm_normal, which is the transpose of the inverse model-view matrix.

We were computing it as the rotation component of the inverse model-view matrix, without any transpose. This was actually valid after #8182 for model-view matrices with uniform scale, since the rotation component is orthogonal, and for orthogonal matrices the transpose and the inverse are equal.

To honor non-uniform scale throughout, I changed czm_inverseNormal to be the transpose of czm_modelView, and czm_inverseNormal3D to be the transpose of czm_modelView3D.

@lilleyse
Copy link
Contributor

lilleyse commented Oct 5, 2023

I ran the equations on godbolt just to confirm: https://godbolt.org/z/ar7z8dYa5. It all checks out. Thanks!

@lilleyse lilleyse merged commit 4ce2d16 into main Oct 5, 2023
2 checks passed
@lilleyse lilleyse deleted the fix-normal-scaling branch October 5, 2023 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants