From 80139cf67354029a9870a1eb0a5035ea5c13e157 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 26 Jan 2024 12:56:25 -0800 Subject: [PATCH] A fine change - Add Affine interface which combines Translate Rotate and Scale without directly using matrices. - Use joml's rotateAround where applicable. - Add Transform#transform(Pose) - Only the ctor for PoseTransformStack is internal. --- .../lib/instance/TransformedInstance.java | 7 +++++ .../flywheel/lib/transform/Affine.java | 27 ++++++++++++++++++ .../lib/transform/PoseTransformStack.java | 10 +++++-- .../flywheel/lib/transform/Rotate.java | 10 +++---- .../flywheel/lib/transform/Transform.java | 28 ++++--------------- .../flywheel/lib/transform/Translate.java | 6 ++-- 6 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/lib/transform/Affine.java diff --git a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java index 4443ca11c..66d8edd7b 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java +++ b/src/main/java/com/jozufozu/flywheel/lib/instance/TransformedInstance.java @@ -32,6 +32,13 @@ public class TransformedInstance extends ColoredLitInstance implements Transform return this; } + @Override + public TransformedInstance rotateAround(Quaternionf quaternion, float x, float y, float z) { + this.model.rotateAround(quaternion, x, y, z); + this.normal.rotate(quaternion); + return this; + } + @Override public TransformedInstance scale(float x, float y, float z) { model.scale(x, y, z); diff --git a/src/main/java/com/jozufozu/flywheel/lib/transform/Affine.java b/src/main/java/com/jozufozu/flywheel/lib/transform/Affine.java new file mode 100644 index 000000000..856790c1b --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/lib/transform/Affine.java @@ -0,0 +1,27 @@ +package com.jozufozu.flywheel.lib.transform; + +import org.joml.Quaternionf; + +import com.mojang.math.Axis; + +import net.minecraft.core.Direction; + +public interface Affine> extends Translate, Rotate, Scale { + default Self rotateAround(Quaternionf quaternion, float x, float y, float z) { + return translate(x, y, z).rotate(quaternion) + .translate(-x, -y, -z); + } + + default Self rotateCentered(Quaternionf q) { + return rotateAround(q, CENTER, CENTER, CENTER); + } + + default Self rotateCentered(float radians, Axis axis) { + return rotateCentered(axis.rotation(radians)); + } + + default Self rotateCentered(float radians, Direction axis) { + var step = axis.step(); + return rotateCentered(new Quaternionf().setAngleAxis(radians, step.x(), step.y(), step.z())); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/lib/transform/PoseTransformStack.java b/src/main/java/com/jozufozu/flywheel/lib/transform/PoseTransformStack.java index 55c06c862..2cb098981 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/transform/PoseTransformStack.java +++ b/src/main/java/com/jozufozu/flywheel/lib/transform/PoseTransformStack.java @@ -12,10 +12,10 @@ import com.mojang.blaze3d.vertex.PoseStack; *
* Only one instance of this class should exist per {@link PoseStack}. */ -@ApiStatus.Internal public class PoseTransformStack implements TransformStack { - private final PoseStack stack; + public final PoseStack stack; + @ApiStatus.Internal public PoseTransformStack(PoseStack stack) { this.stack = stack; } @@ -26,6 +26,12 @@ public class PoseTransformStack implements TransformStack { return this; } + @Override + public PoseTransformStack rotateAround(Quaternionf quaternion, float x, float y, float z) { + stack.rotateAround(quaternion, x, y, z); + return this; + } + @Override public PoseTransformStack scale(float factorX, float factorY, float factorZ) { stack.scale(factorX, factorY, factorZ); diff --git a/src/main/java/com/jozufozu/flywheel/lib/transform/Rotate.java b/src/main/java/com/jozufozu/flywheel/lib/transform/Rotate.java index cb5aea301..49beb428b 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/transform/Rotate.java +++ b/src/main/java/com/jozufozu/flywheel/lib/transform/Rotate.java @@ -98,12 +98,12 @@ public interface Rotate> { default S rotateToFace(Direction facing) { return switch (facing) { - case DOWN -> rotateXDegrees(-90); - case UP -> rotateXDegrees(90); + case DOWN -> rotateXDegrees(-90); + case UP -> rotateXDegrees(90); case NORTH -> self(); - case SOUTH -> rotateYDegrees(180); - case WEST -> rotateYDegrees(90); - case EAST -> rotateYDegrees(270); + case SOUTH -> rotateYDegrees(180); + case WEST -> rotateYDegrees(90); + case EAST -> rotateYDegrees(270); }; } diff --git a/src/main/java/com/jozufozu/flywheel/lib/transform/Transform.java b/src/main/java/com/jozufozu/flywheel/lib/transform/Transform.java index 7741b7f6c..9f8ba48f0 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/transform/Transform.java +++ b/src/main/java/com/jozufozu/flywheel/lib/transform/Transform.java @@ -2,14 +2,10 @@ package com.jozufozu.flywheel.lib.transform; import org.joml.Matrix3f; import org.joml.Matrix4f; -import org.joml.Quaternionf; import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.math.Axis; -import net.minecraft.core.Direction; - -public interface Transform> extends Scale, Rotate, Translate { +public interface Transform> extends Affine { Self mulPose(Matrix4f pose); Self mulNormal(Matrix3f normal); @@ -18,23 +14,11 @@ public interface Transform> extends Scale, Ro return mulPose(pose).mulNormal(normal); } + default Self transform(PoseStack.Pose pose) { + return transform(pose.pose(), pose.normal()); + } + default Self transform(PoseStack stack) { - PoseStack.Pose last = stack.last(); - return transform(last.pose(), last.normal()); - } - - default Self rotateCentered(Quaternionf q) { - return center().rotate(q) - .uncenter(); - } - - default Self rotateCentered(float radians, Axis axis) { - return center().rotate(radians, axis) - .uncenter(); - } - - default Self rotateCentered(float radians, Direction axis) { - return center().rotate(radians, axis) - .uncenter(); + return transform(stack.last()); } } diff --git a/src/main/java/com/jozufozu/flywheel/lib/transform/Translate.java b/src/main/java/com/jozufozu/flywheel/lib/transform/Translate.java index bfb829589..6535d2145 100644 --- a/src/main/java/com/jozufozu/flywheel/lib/transform/Translate.java +++ b/src/main/java/com/jozufozu/flywheel/lib/transform/Translate.java @@ -6,6 +6,8 @@ import net.minecraft.core.Vec3i; import net.minecraft.world.phys.Vec3; public interface Translate> { + float CENTER = 0.5f; + Self translate(double x, double y, double z); default Self translate(double v) { @@ -57,11 +59,11 @@ public interface Translate> { } default Self center() { - return translate(0.5); + return translate(CENTER); } default Self uncenter() { - return translate(-0.5); + return translate(-CENTER); } /**