2021-05-11 20:02:43 +02:00
|
|
|
package com.jozufozu.flywheel.util;
|
2021-03-08 09:36:23 +01:00
|
|
|
|
2021-04-30 09:31:52 +02:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
2021-04-08 19:22:11 +02:00
|
|
|
import com.mojang.blaze3d.matrix.MatrixStack;
|
|
|
|
|
2021-04-30 09:31:52 +02:00
|
|
|
import net.minecraft.util.Direction;
|
2021-03-20 12:58:02 +01:00
|
|
|
import net.minecraft.util.math.vector.Matrix3f;
|
|
|
|
import net.minecraft.util.math.vector.Matrix4f;
|
2021-05-11 20:02:43 +02:00
|
|
|
import net.minecraft.util.math.vector.Vector3f;
|
2021-03-08 09:36:23 +01:00
|
|
|
|
|
|
|
public class RenderUtil {
|
2021-04-28 09:01:26 +02:00
|
|
|
public static int nextPowerOf2(int a) {
|
2021-03-23 04:20:52 +01:00
|
|
|
int h = Integer.highestOneBit(a);
|
|
|
|
return (h == a) ? h : (h << 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isPowerOf2(int n) {
|
|
|
|
int b = n & (n - 1);
|
|
|
|
return b == 0 && n != 0;
|
|
|
|
}
|
|
|
|
|
2021-04-28 09:01:26 +02:00
|
|
|
public static double lengthSqr(double x, double y, double z) {
|
|
|
|
return x * x + y * y + z * z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double length(double x, double y, double z) {
|
|
|
|
return Math.sqrt(lengthSqr(x, y, z));
|
|
|
|
}
|
|
|
|
|
2021-03-24 23:48:15 +01:00
|
|
|
public static float[] writeMatrixStack(MatrixStack stack) {
|
2021-06-30 21:43:54 +02:00
|
|
|
return writeMatrixStack(stack.peek()
|
|
|
|
.getModel(), stack.peek()
|
|
|
|
.getNormal());
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
2021-03-23 04:20:52 +01:00
|
|
|
|
2021-03-24 23:48:15 +01:00
|
|
|
// GPUs want matrices in column major order.
|
|
|
|
public static float[] writeMatrixStack(Matrix4f model, Matrix3f normal) {
|
2021-06-30 21:43:54 +02:00
|
|
|
return new float[]{model.a00, model.a10, model.a20, model.a30, model.a01, model.a11, model.a21, model.a31, model.a02, model.a12, model.a22, model.a32, model.a03, model.a13, model.a23, model.a33, normal.a00, normal.a10, normal.a20, normal.a01, normal.a11, normal.a21, normal.a02, normal.a12, normal.a22,};
|
2021-03-23 04:20:52 +01:00
|
|
|
}
|
2021-03-24 23:48:15 +01:00
|
|
|
|
|
|
|
public static float[] writeMatrix(Matrix4f model) {
|
2021-06-30 21:43:54 +02:00
|
|
|
return new float[]{model.a00, model.a10, model.a20, model.a30, model.a01, model.a11, model.a21, model.a31, model.a02, model.a12, model.a22, model.a32, model.a03, model.a13, model.a23, model.a33,};
|
2021-03-24 23:48:15 +01:00
|
|
|
}
|
2021-04-30 09:31:52 +02:00
|
|
|
|
|
|
|
public static Supplier<MatrixStack> rotateToFace(Direction facing) {
|
|
|
|
return () -> {
|
|
|
|
MatrixStack stack = new MatrixStack();
|
2021-06-30 21:43:54 +02:00
|
|
|
// MatrixStacker.of(stack)
|
|
|
|
// .centre()
|
|
|
|
// .rotateY(AngleHelper.horizontalAngle(facing))
|
|
|
|
// .rotateX(AngleHelper.verticalAngle(facing))
|
|
|
|
// .unCentre();
|
|
|
|
stack.peek()
|
|
|
|
.getModel()
|
|
|
|
.setTranslation(0.5f, 0.5f, 0.5f);
|
2021-05-11 20:02:43 +02:00
|
|
|
stack.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(AngleHelper.horizontalAngle(facing)));
|
|
|
|
stack.multiply(Vector3f.POSITIVE_X.getDegreesQuaternion(AngleHelper.verticalAngle(facing)));
|
|
|
|
stack.translate(-0.5f, -0.5f, -0.5f);
|
2021-04-30 09:31:52 +02:00
|
|
|
return stack;
|
|
|
|
};
|
|
|
|
}
|
2021-03-08 09:36:23 +01:00
|
|
|
}
|