REVIEWED: raymath: MatrixCompose()

This commit is contained in:
Ray
2025-11-02 19:53:45 +01:00
parent 81004135a4
commit 87d49262f8

View File

@@ -2553,38 +2553,37 @@ RMAPI int QuaternionEquals(Quaternion p, Quaternion q)
} }
// Compose a transformation matrix from rotational, translational and scaling components // Compose a transformation matrix from rotational, translational and scaling components
RMAPI Matrix MatrixCompose( Vector3 translation, Quaternion rotation, Vector3 scale ) // TODO: This function is not following raymath conventions defined in header: NOT self-contained
RMAPI Matrix MatrixCompose(Vector3 translation, Quaternion rotation, Vector3 scale)
{ {
// Initialize vectors
Vector3 right = { 1.0f, 0.0f, 0.0f };
Vector3 up = { 0.0f, 1.0f, 0.0f };
Vector3 forward = { 0.0f, 0.0f, 1.0f };
//Initialize Vectors // Scale vectors
Vector3 right = { 1, 0, 0 }; right = Vector3Scale(right, scale.x);
Vector3 up = { 0, 1, 0 }; up = Vector3Scale(up, scale.y);
Vector3 forward = { 0, 0, 1 }; forward = Vector3Scale(forward , scale.z);
//Scale Vectors // Rotate vectors
right = Vector3Scale( right , scale.x ); right = Vector3RotateByQuaternion(right, rotation);
up = Vector3Scale( up , scale.y ); up = Vector3RotateByQuaternion(up, rotation);
forward = Vector3Scale( forward , scale.z ); forward = Vector3RotateByQuaternion(forward, rotation);
//Rotate Vectors // Set result matrix output
right = Vector3RotateByQuaternion( right , rotation );
up = Vector3RotateByQuaternion( up , rotation );
forward = Vector3RotateByQuaternion( forward, rotation );
// Set matrix output
Matrix result = { Matrix result = {
right.x, up.x, forward.x, position.x, right.x, up.x, forward.x, position.x,
right.y, up.y, forward.y, position.y, right.y, up.y, forward.y, position.y,
right.z, up.z, forward.z, position.z, right.z, up.z, forward.z, position.z,
0, 0, 0, 1 0.0f, 0.0f, 0.0f, 1.0f
}; };
// Return matrix output
return result; return result;
} }
// Decompose a transformation matrix into its rotational, translational and scaling components and remove shear // Decompose a transformation matrix into its rotational, translational and scaling components and remove shear
// TODO: This function is not following raymath conventions defined in header: NOT self-contained
RMAPI void MatrixDecompose(Matrix mat, Vector3 *translation, Quaternion *rotation, Vector3 *scale) RMAPI void MatrixDecompose(Matrix mat, Vector3 *translation, Quaternion *rotation, Vector3 *scale)
{ {
float eps = (float)1e-9; float eps = (float)1e-9;
@@ -2619,10 +2618,7 @@ RMAPI void MatrixDecompose(Matrix mat, Vector3 *translation, Quaternion *rotatio
// X Scale // X Scale
scl.x = Vector3Length(matColumns[0]); scl.x = Vector3Length(matColumns[0]);
if (scl.x > eps) if (scl.x > eps) matColumns[0] = Vector3Scale(matColumns[0], 1.0f / scl.x);
{
matColumns[0] = Vector3Scale(matColumns[0], 1.0f / scl.x);
}
// Compute XY shear and make col2 orthogonal // Compute XY shear and make col2 orthogonal
shear[0] = Vector3DotProduct(matColumns[0], matColumns[1]); shear[0] = Vector3DotProduct(matColumns[0], matColumns[1]);