Skip to content

Commit

Permalink
Fix MatrixHelper producing slightly different results from vanilla (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt authored Nov 19, 2023
1 parent 5e40060 commit 0850b93
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/api/java/net/caffeinemc/mods/sodium/api/math/MatrixHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import org.joml.Matrix3f;
import org.joml.Matrix4f;

/**
* Implements optimized utilities for transforming vectors with a given matrix.
*
* Note: Brackets must be used carefully in the transform functions to ensure that floating-point errors are
* the same as those produced by JOML, otherwise Z-fighting will occur.
*/
public class MatrixHelper {
/**
* @param mat The transformation matrix to apply to the normal
Expand Down Expand Up @@ -45,7 +51,7 @@ public static int transformNormal(Matrix3f mat, int norm) {
* @return The transformed X-coordinate for the normal vector
*/
public static float transformNormalX(Matrix3f mat, float x, float y, float z) {
return (mat.m00() * x) + (mat.m10() * y) + (mat.m20() * z);
return (mat.m00() * x) + ((mat.m10() * y) + (mat.m20() * z));
}

/**
Expand All @@ -56,7 +62,7 @@ public static float transformNormalX(Matrix3f mat, float x, float y, float z) {
* @return The transformed Y-coordinate for the normal vector
*/
public static float transformNormalY(Matrix3f mat, float x, float y, float z) {
return (mat.m01() * x) + (mat.m11() * y) + (mat.m21() * z);
return (mat.m01() * x) + ((mat.m11() * y) + (mat.m21() * z));
}

/**
Expand All @@ -67,7 +73,7 @@ public static float transformNormalY(Matrix3f mat, float x, float y, float z) {
* @return The transformed Z-coordinate for the normal vector
*/
public static float transformNormalZ(Matrix3f mat, float x, float y, float z) {
return (mat.m02() * x) + (mat.m12() * y) + (mat.m22() * z);
return (mat.m02() * x) + ((mat.m12() * y) + (mat.m22() * z));
}

/**
Expand All @@ -78,7 +84,7 @@ public static float transformNormalZ(Matrix3f mat, float x, float y, float z) {
* @return The transformed X-coordinate for the vertex position
*/
public static float transformPositionX(Matrix4f mat, float x, float y, float z) {
return (mat.m00() * x) + (mat.m10() * y) + (mat.m20() * z) + mat.m30();
return (mat.m00() * x) + ((mat.m10() * y) + ((mat.m20() * z) + mat.m30()));
}

/**
Expand All @@ -89,7 +95,7 @@ public static float transformPositionX(Matrix4f mat, float x, float y, float z)
* @return The transformed Y-coordinate for the vertex position
*/
public static float transformPositionY(Matrix4f mat, float x, float y, float z) {
return (mat.m01() * x) + (mat.m11() * y) + (mat.m21() * z) + mat.m31();
return (mat.m01() * x) + ((mat.m11() * y) + ((mat.m21() * z) + mat.m31()));
}

/**
Expand All @@ -100,7 +106,7 @@ public static float transformPositionY(Matrix4f mat, float x, float y, float z)
* @return The transformed Z-coordinate for the vertex position
*/
public static float transformPositionZ(Matrix4f mat, float x, float y, float z) {
return (mat.m02() * x) + (mat.m12() * y) + (mat.m22() * z) + mat.m32();
return (mat.m02() * x) + ((mat.m12() * y) + ((mat.m22() * z) + mat.m32()));
}

/**
Expand Down

0 comments on commit 0850b93

Please sign in to comment.