Get JOML'd

- Add more rotate methods to Affine and Rotate
- Add optimized implementations in TransformedInstance
This commit is contained in:
Jozufozu 2025-01-19 11:25:21 -06:00
parent 10d3923bf5
commit 809a0eb616
3 changed files with 161 additions and 45 deletions

View file

@ -1,5 +1,6 @@
package dev.engine_room.flywheel.lib.instance;
import org.joml.AxisAngle4f;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.joml.Quaternionfc;
@ -9,6 +10,7 @@ import com.mojang.blaze3d.vertex.PoseStack;
import dev.engine_room.flywheel.api.instance.InstanceHandle;
import dev.engine_room.flywheel.api.instance.InstanceType;
import dev.engine_room.flywheel.lib.transform.Affine;
import net.minecraft.core.Direction;
public class TransformedInstance extends ColoredLitInstance implements Affine<TransformedInstance> {
public final Matrix4f pose = new Matrix4f();
@ -17,12 +19,6 @@ public class TransformedInstance extends ColoredLitInstance implements Affine<Tr
super(type, handle);
}
@Override
public TransformedInstance rotateAround(Quaternionfc quaternion, float x, float y, float z) {
pose.rotateAround(quaternion, x, y, z);
return this;
}
@Override
public TransformedInstance translate(float x, float y, float z) {
pose.translate(x, y, z);
@ -84,4 +80,80 @@ public class TransformedInstance extends ColoredLitInstance implements Affine<Tr
pose.zero();
return this;
}
@Override
public TransformedInstance rotateAround(Quaternionfc quaternion, float x, float y, float z) {
pose.rotateAround(quaternion, x, y, z);
return this;
}
@Override
public TransformedInstance rotateCentered(float radians, float axisX, float axisY, float axisZ) {
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
.rotate(radians, axisX, axisY, axisZ)
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
return this;
}
@Override
public TransformedInstance rotateXCentered(float radians) {
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
.rotateX(radians)
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
return this;
}
@Override
public TransformedInstance rotateYCentered(float radians) {
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
.rotateY(radians)
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
return this;
}
@Override
public TransformedInstance rotateZCentered(float radians) {
pose.translate(Affine.CENTER, Affine.CENTER, Affine.CENTER)
.rotateZ(radians)
.translate(-Affine.CENTER, -Affine.CENTER, -Affine.CENTER);
return this;
}
@Override
public TransformedInstance rotate(float radians, float axisX, float axisY, float axisZ) {
pose.rotate(radians, axisX, axisY, axisZ);
return this;
}
@Override
public TransformedInstance rotate(AxisAngle4f axisAngle) {
pose.rotate(axisAngle);
return this;
}
@Override
public TransformedInstance rotateX(float radians) {
pose.rotateX(radians);
return this;
}
@Override
public TransformedInstance rotateY(float radians) {
pose.rotateY(radians);
return this;
}
@Override
public TransformedInstance rotateZ(float radians) {
pose.rotateZ(radians);
return this;
}
@Override
public TransformedInstance rotateToFace(Direction facing) {
// Need to invert the step because the super default method rotates from North (-Z),
// but rotateTowards rotates from South (+Z)
pose.rotateTowards(-facing.getStepX(), -facing.getStepY(), -facing.getStepZ(), 0, 1, 0);
return this;
}
}

View file

@ -7,6 +7,7 @@ import org.joml.Vector3fc;
import com.mojang.math.Axis;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
public interface Affine<Self extends Affine<Self>> extends Translate<Self>, Rotate<Self>, Scale<Self> {
default Self rotateAround(Quaternionfc quaternion, float x, float y, float z) {
@ -15,19 +16,18 @@ public interface Affine<Self extends Affine<Self>> extends Translate<Self>, Rota
}
default Self rotateAround(Quaternionfc quaternion, Vector3fc vec) {
return translate(vec.x(), vec.y(), vec.z()).rotate(quaternion)
.translateBack(vec.x(), vec.y(), vec.z());
return rotateAround(quaternion, vec.x(), vec.y(), vec.z());
}
default Self rotateCentered(Quaternionfc q) {
return rotateAround(q, CENTER, CENTER, CENTER);
}
default Self rotateCentered(float radians, Vector3fc axis) {
default Self rotateCentered(float radians, float axisX, float axisY, float axisZ) {
if (radians == 0) {
return self();
}
return rotateCentered(new Quaternionf().setAngleAxis(radians, axis.x(), axis.y(), axis.z()));
return rotateCentered(new Quaternionf().setAngleAxis(radians, axisX, axisY, axisZ));
}
default Self rotateCentered(float radians, Axis axis) {
@ -37,10 +37,59 @@ public interface Affine<Self extends Affine<Self>> extends Translate<Self>, Rota
return rotateCentered(axis.rotation(radians));
}
default Self rotateCentered(float radians, Vector3fc axis) {
return rotateCentered(radians, axis.x(), axis.y(), axis.z());
}
default Self rotateCentered(float radians, Direction.Axis axis) {
return rotateCentered(radians, Direction.fromAxisAndDirection(axis, Direction.AxisDirection.POSITIVE));
}
default Self rotateCentered(float radians, Direction axis) {
if (radians == 0) {
return self();
}
return rotateCentered(radians, axis.step());
return rotateCentered(radians, axis.getStepX(), axis.getStepY(), axis.getStepZ());
}
default Self rotateCenteredDegrees(float degrees, float axisX, float axisY, float axisZ) {
return rotateCentered(Mth.DEG_TO_RAD * degrees, axisX, axisY, axisZ);
}
default Self rotateCenteredDegrees(float degrees, Axis axis) {
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateCenteredDegrees(float degrees, Vector3fc axis) {
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateCenteredDegrees(float degrees, Direction axis) {
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateCenteredDegrees(float degrees, Direction.Axis axis) {
return rotateCentered(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateXCentered(float radians) {
return rotateCentered(radians, Axis.XP);
}
default Self rotateYCentered(float radians) {
return rotateCentered(radians, Axis.YP);
}
default Self rotateZCentered(float radians) {
return rotateCentered(radians, Axis.ZP);
}
default Self rotateXCenteredDegrees(float degrees) {
return rotateXCentered(Mth.DEG_TO_RAD * degrees);
}
default Self rotateYCenteredDegrees(float degrees) {
return rotateYCentered(Mth.DEG_TO_RAD * degrees);
}
default Self rotateZCenteredDegrees(float degrees) {
return rotateZCentered(Mth.DEG_TO_RAD * degrees);
}
}

View file

@ -8,6 +8,7 @@ import org.joml.Vector3fc;
import com.mojang.math.Axis;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
public interface Rotate<Self extends Rotate<Self>> {
Self rotate(Quaternionfc quaternion);
@ -16,11 +17,12 @@ public interface Rotate<Self extends Rotate<Self>> {
return rotate(new Quaternionf(axisAngle));
}
default Self rotate(float radians, Vector3fc axis) {
default Self rotate(float radians, float axisX, float axisY, float axisZ) {
if (radians == 0) {
return self();
}
return rotate(new Quaternionf().setAngleAxis(radians, axis.x(), axis.y(), axis.z()));
return rotate(new Quaternionf().setAngleAxis(radians, axisX, axisY, axisZ));
}
default Self rotate(float radians, Axis axis) {
@ -30,47 +32,36 @@ public interface Rotate<Self extends Rotate<Self>> {
return rotate(axis.rotation(radians));
}
default Self rotate(float radians, Vector3fc axis) {
return rotate(radians, axis.x(), axis.y(), axis.z());
}
default Self rotate(float radians, Direction axis) {
if (radians == 0) {
return self();
}
return rotate(radians, axis.step());
return rotate(radians, axis.getStepX(), axis.getStepY(), axis.getStepZ());
}
default Self rotate(float radians, Direction.Axis axis) {
return switch (axis) {
case X -> rotateX(radians);
case Y -> rotateY(radians);
case Z -> rotateZ(radians);
};
return rotate(radians, Direction.fromAxisAndDirection(axis, Direction.AxisDirection.POSITIVE));
}
default Self rotateDegrees(float degrees, Vector3fc axis) {
if (degrees == 0) {
return self();
}
return rotate((float) Math.toRadians(degrees), axis);
default Self rotateDegrees(float degrees, float axisX, float axisY, float axisZ) {
return rotate(Mth.DEG_TO_RAD * degrees, axisX, axisY, axisZ);
}
default Self rotateDegrees(float degrees, Axis axis) {
if (degrees == 0) {
return self();
}
return rotate(axis.rotationDegrees(degrees));
return rotate(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateDegrees(float degrees, Vector3fc axis) {
return rotate(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateDegrees(float degrees, Direction axis) {
if (degrees == 0) {
return self();
}
return rotate((float) Math.toRadians(degrees), axis);
return rotate(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateDegrees(float degrees, Direction.Axis axis) {
if (degrees == 0) {
return self();
}
return rotate((float) Math.toRadians(degrees), axis);
return rotate(Mth.DEG_TO_RAD * degrees, axis);
}
default Self rotateX(float radians) {
@ -86,15 +77,15 @@ public interface Rotate<Self extends Rotate<Self>> {
}
default Self rotateXDegrees(float degrees) {
return rotateDegrees(degrees, Axis.XP);
return rotateX(Mth.DEG_TO_RAD * degrees);
}
default Self rotateYDegrees(float degrees) {
return rotateDegrees(degrees, Axis.YP);
return rotateY(Mth.DEG_TO_RAD * degrees);
}
default Self rotateZDegrees(float degrees) {
return rotateDegrees(degrees, Axis.ZP);
return rotateZ(Mth.DEG_TO_RAD * degrees);
}
default Self rotateToFace(Direction facing) {
@ -108,6 +99,10 @@ public interface Rotate<Self extends Rotate<Self>> {
};
}
default Self rotateTo(Vector3fc from, Vector3fc to) {
return rotate(new Quaternionf().rotateTo(from, to));
}
@SuppressWarnings("unchecked")
default Self self() {
return (Self) this;