| 1 | package com.mojang.blaze3d.vertex;
|
| 2 |
|
| 3 | import com.mojang.math.Axis;
|
| 4 | import com.mojang.math.MatrixUtil;
|
| 5 | import com.mojang.math.Transformation;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.NoSuchElementException;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 | import org.joml.Matrix3f;
|
| 13 | import org.joml.Matrix4f;
|
| 14 | import org.joml.Matrix4fc;
|
| 15 | import org.joml.Quaternionfc;
|
| 16 | import org.joml.Vector3f;
|
| 17 | import org.joml.Vector3fc;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class PoseStack {
|
| 21 | private final List<PoseStack.Pose> poses = new ArrayList<>(16);
|
| 22 | private int lastIndex;
|
| 23 |
|
| 24 | public PoseStack() {
|
| 25 | this.poses.add(new PoseStack.Pose());
|
| 26 | }
|
| 27 |
|
| 28 | public void translate(final double xo, final double yo, final double zo) {
|
| 29 | this.translate((float)xo, (float)yo, (float)zo);
|
| 30 | }
|
| 31 |
|
| 32 | public void translate(final float xo, final float yo, final float zo) {
|
| 33 | this.last().translate(xo, yo, zo);
|
| 34 | }
|
| 35 |
|
| 36 | public void translate(final Vec3 offset) {
|
| 37 | this.translate(offset.x, offset.y, offset.z);
|
| 38 | }
|
| 39 |
|
| 40 | public void scale(final float xScale, final float yScale, final float zScale) {
|
| 41 | this.last().scale(xScale, yScale, zScale);
|
| 42 | }
|
| 43 |
|
| 44 | public void rotate(final Quaternionfc by) {
|
| 45 | this.last().rotate(by);
|
| 46 | }
|
| 47 |
|
| 48 | public void rotate(final Axis axis, final float angle) {
|
| 49 | this.last().rotate(axis, angle);
|
| 50 | }
|
| 51 |
|
| 52 | public void rotateDegrees(final Axis axis, final float angle) {
|
| 53 | this.last().rotateDegrees(axis, angle);
|
| 54 | }
|
| 55 |
|
| 56 | public void rotateAround(final Quaternionfc rotation, final float pivotX, final float pivotY, final float pivotZ) {
|
| 57 | this.last().rotateAround(rotation, pivotX, pivotY, pivotZ);
|
| 58 | }
|
| 59 |
|
| 60 | public void pushPose() {
|
| 61 | PoseStack.Pose lastPose = this.last();
|
| 62 | this.lastIndex++;
|
| 63 | if (this.lastIndex >= this.poses.size()) {
|
| 64 | this.poses.add(lastPose.copy());
|
| 65 | } else {
|
| 66 | this.poses.get(this.lastIndex).set(lastPose);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public void popPose() {
|
| 71 | if (this.lastIndex == 0) {
|
| 72 | throw new NoSuchElementException();
|
| 73 | }
|
| 74 |
|
| 75 | this.lastIndex--;
|
| 76 | }
|
| 77 |
|
| 78 | public PoseStack.Pose last() {
|
| 79 | return this.poses.get(this.lastIndex);
|
| 80 | }
|
| 81 |
|
| 82 | public boolean isEmpty() {
|
| 83 | return this.lastIndex == 0;
|
| 84 | }
|
| 85 |
|
| 86 | public void setIdentity() {
|
| 87 | this.last().setIdentity();
|
| 88 | }
|
| 89 |
|
| 90 | public void mulPose(final Matrix4fc matrix) {
|
| 91 | this.last().mulPose(matrix);
|
| 92 | }
|
| 93 |
|
| 94 | public void mulPose(final Transformation matrix) {
|
| 95 | this.last().mulPose(matrix);
|
| 96 | }
|
| 97 |
|
| 98 | @Environment(EnvType.CLIENT)
|
| 99 | public static final class Pose {
|
| 100 | private final Matrix4f pose = new Matrix4f();
|
| 101 | private final Matrix3f normal = new Matrix3f();
|
| 102 | private boolean trustedNormals = true;
|
| 103 |
|
| 104 | private void computeNormalMatrix() {
|
| 105 | this.normal.set(this.pose).invert().transpose();
|
| 106 | this.trustedNormals = false;
|
| 107 | }
|
| 108 |
|
| 109 | public void set(final PoseStack.Pose pose) {
|
| 110 | this.pose.set(pose.pose);
|
| 111 | this.normal.set(pose.normal);
|
| 112 | this.trustedNormals = pose.trustedNormals;
|
| 113 | }
|
| 114 |
|
| 115 | public Matrix4f pose() {
|
| 116 | return this.pose;
|
| 117 | }
|
| 118 |
|
| 119 | public Matrix3f normal() {
|
| 120 | return this.normal;
|
| 121 | }
|
| 122 |
|
| 123 | public Vector3f transformNormal(final Vector3fc normal, final Vector3f destination) {
|
| 124 | return this.transformNormal(normal.x(), normal.y(), normal.z(), destination);
|
| 125 | }
|
| 126 |
|
| 127 | public Vector3f transformNormal(final float x, final float y, final float z, final Vector3f destination) {
|
| 128 | Vector3f result = this.normal.transform(x, y, z, destination);
|
| 129 | return this.trustedNormals ? result : result.normalize();
|
| 130 | }
|
| 131 |
|
| 132 | public Matrix4f translate(final float xo, final float yo, final float zo) {
|
| 133 | return this.pose.translate(xo, yo, zo);
|
| 134 | }
|
| 135 |
|
| 136 | public void scale(final float xScale, final float yScale, final float zScale) {
|
| 137 | this.pose.scale(xScale, yScale, zScale);
|
| 138 | if (Math.abs(xScale) == Math.abs(yScale) && Math.abs(yScale) == Math.abs(zScale)) {
|
| 139 | if (xScale < 0.0F || yScale < 0.0F || zScale < 0.0F) {
|
| 140 | this.normal.scale(Math.signum(xScale), Math.signum(yScale), Math.signum(zScale));
|
| 141 | }
|
| 142 | } else {
|
| 143 | this.normal.scale(1.0F / xScale, 1.0F / yScale, 1.0F / zScale);
|
| 144 | this.trustedNormals = false;
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | public void rotate(final Quaternionfc by) {
|
| 149 | this.pose.rotate(by);
|
| 150 | this.normal.rotate(by);
|
| 151 | }
|
| 152 |
|
| 153 | public void rotate(final Axis axis, final float angle) {
|
| 154 | axis.rotate(this.pose, angle);
|
| 155 | axis.rotate(this.normal, angle);
|
| 156 | }
|
| 157 |
|
| 158 | public void rotateDegrees(final Axis axis, final float angle) {
|
| 159 | axis.rotateDegrees(this.pose, angle);
|
| 160 | axis.rotateDegrees(this.normal, angle);
|
| 161 | }
|
| 162 |
|
| 163 | public void rotateAround(final Quaternionfc rotation, final float pivotX, final float pivotY, final float pivotZ) {
|
| 164 | this.pose.rotateAround(rotation, pivotX, pivotY, pivotZ);
|
| 165 | this.normal.rotate(rotation);
|
| 166 | }
|
| 167 |
|
| 168 | public void setIdentity() {
|
| 169 | this.pose.identity();
|
| 170 | this.normal.identity();
|
| 171 | this.trustedNormals = true;
|
| 172 | }
|
| 173 |
|
| 174 | public void mulPose(final Matrix4fc matrix) {
|
| 175 | this.pose.mul(matrix);
|
| 176 | if (!MatrixUtil.isPureTranslation(matrix)) {
|
| 177 | if (MatrixUtil.checkPropertyRaw(matrix, 16)) {
|
| 178 | this.normal.mul(new Matrix3f(matrix));
|
| 179 | } else {
|
| 180 | this.computeNormalMatrix();
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | public void mulPose(final Transformation transformation) {
|
| 186 | this.mulPose(transformation.getMatrix());
|
| 187 | }
|
| 188 |
|
| 189 | public PoseStack.Pose copy() {
|
| 190 | PoseStack.Pose pose = new PoseStack.Pose();
|
| 191 | pose.set(this);
|
| 192 | return pose;
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|