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.
This commit is contained in:
Jozufozu 2024-01-26 12:56:25 -08:00
parent 4698d7b394
commit 80139cf673
6 changed files with 57 additions and 31 deletions

View file

@ -32,6 +32,13 @@ public class TransformedInstance extends ColoredLitInstance implements Transform
return this; 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 @Override
public TransformedInstance scale(float x, float y, float z) { public TransformedInstance scale(float x, float y, float z) {
model.scale(x, y, z); model.scale(x, y, z);

View file

@ -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<Self extends Affine<Self>> extends Translate<Self>, Rotate<Self>, Scale<Self> {
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()));
}
}

View file

@ -12,10 +12,10 @@ import com.mojang.blaze3d.vertex.PoseStack;
* <br> * <br>
* Only one instance of this class should exist per {@link PoseStack}. * Only one instance of this class should exist per {@link PoseStack}.
*/ */
@ApiStatus.Internal
public class PoseTransformStack implements TransformStack<PoseTransformStack> { public class PoseTransformStack implements TransformStack<PoseTransformStack> {
private final PoseStack stack; public final PoseStack stack;
@ApiStatus.Internal
public PoseTransformStack(PoseStack stack) { public PoseTransformStack(PoseStack stack) {
this.stack = stack; this.stack = stack;
} }
@ -26,6 +26,12 @@ public class PoseTransformStack implements TransformStack<PoseTransformStack> {
return this; return this;
} }
@Override
public PoseTransformStack rotateAround(Quaternionf quaternion, float x, float y, float z) {
stack.rotateAround(quaternion, x, y, z);
return this;
}
@Override @Override
public PoseTransformStack scale(float factorX, float factorY, float factorZ) { public PoseTransformStack scale(float factorX, float factorY, float factorZ) {
stack.scale(factorX, factorY, factorZ); stack.scale(factorX, factorY, factorZ);

View file

@ -98,12 +98,12 @@ public interface Rotate<S extends Rotate<S>> {
default S rotateToFace(Direction facing) { default S rotateToFace(Direction facing) {
return switch (facing) { return switch (facing) {
case DOWN -> rotateXDegrees(-90); case DOWN -> rotateXDegrees(-90);
case UP -> rotateXDegrees(90); case UP -> rotateXDegrees(90);
case NORTH -> self(); case NORTH -> self();
case SOUTH -> rotateYDegrees(180); case SOUTH -> rotateYDegrees(180);
case WEST -> rotateYDegrees(90); case WEST -> rotateYDegrees(90);
case EAST -> rotateYDegrees(270); case EAST -> rotateYDegrees(270);
}; };
} }

View file

@ -2,14 +2,10 @@ package com.jozufozu.flywheel.lib.transform;
import org.joml.Matrix3f; import org.joml.Matrix3f;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Quaternionf;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.core.Direction; public interface Transform<Self extends Transform<Self>> extends Affine<Self> {
public interface Transform<Self extends Transform<Self>> extends Scale<Self>, Rotate<Self>, Translate<Self> {
Self mulPose(Matrix4f pose); Self mulPose(Matrix4f pose);
Self mulNormal(Matrix3f normal); Self mulNormal(Matrix3f normal);
@ -18,23 +14,11 @@ public interface Transform<Self extends Transform<Self>> extends Scale<Self>, Ro
return mulPose(pose).mulNormal(normal); return mulPose(pose).mulNormal(normal);
} }
default Self transform(PoseStack.Pose pose) {
return transform(pose.pose(), pose.normal());
}
default Self transform(PoseStack stack) { default Self transform(PoseStack stack) {
PoseStack.Pose last = stack.last(); return transform(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();
} }
} }

View file

@ -6,6 +6,8 @@ import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
public interface Translate<Self extends Translate<Self>> { public interface Translate<Self extends Translate<Self>> {
float CENTER = 0.5f;
Self translate(double x, double y, double z); Self translate(double x, double y, double z);
default Self translate(double v) { default Self translate(double v) {
@ -57,11 +59,11 @@ public interface Translate<Self extends Translate<Self>> {
} }
default Self center() { default Self center() {
return translate(0.5); return translate(CENTER);
} }
default Self uncenter() { default Self uncenter() {
return translate(-0.5); return translate(-CENTER);
} }
/** /**