2021-05-02 01:32:09 +02:00
|
|
|
package com.jozufozu.flywheel.backend;
|
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 com.simibubi.create.foundation.utility.AngleHelper;
|
|
|
|
import com.simibubi.create.foundation.utility.MatrixStacker;
|
2021-04-08 19:22:11 +02:00
|
|
|
|
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-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) {
|
|
|
|
return writeMatrixStack(stack.peek().getModel(), stack.peek().getNormal());
|
|
|
|
}
|
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-04-28 09:01:26 +02:00
|
|
|
return new float[]{
|
2021-03-23 04:20:52 +01:00
|
|
|
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-24 23:48:15 +01:00
|
|
|
|
|
|
|
public static float[] writeMatrix(Matrix4f model) {
|
|
|
|
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-04-30 09:31:52 +02:00
|
|
|
|
|
|
|
public static Supplier<MatrixStack> rotateToFace(Direction facing) {
|
|
|
|
return () -> {
|
|
|
|
MatrixStack stack = new MatrixStack();
|
|
|
|
MatrixStacker.of(stack)
|
|
|
|
.centre()
|
|
|
|
.rotateY(AngleHelper.horizontalAngle(facing))
|
|
|
|
.rotateX(AngleHelper.verticalAngle(facing))
|
|
|
|
.unCentre();
|
|
|
|
return stack;
|
|
|
|
};
|
|
|
|
}
|
2021-03-08 09:36:23 +01:00
|
|
|
}
|