From 2113ae54df2da867f553df3a9ee457c0a3856a33 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Fri, 14 Jun 2024 20:29:43 -0500 Subject: initial commit --- .../java/dev/figboot/cuberender/math/MathUtil.java | 9 ++ .../java/dev/figboot/cuberender/math/Matrix3f.java | 121 +++++++++++++++++ .../java/dev/figboot/cuberender/math/Matrix4f.java | 149 +++++++++++++++++++++ .../java/dev/figboot/cuberender/math/Vector2f.java | 17 +++ .../java/dev/figboot/cuberender/math/Vector3f.java | 42 ++++++ .../java/dev/figboot/cuberender/math/Vector4f.java | 47 +++++++ 6 files changed, 385 insertions(+) create mode 100644 src/main/java/dev/figboot/cuberender/math/MathUtil.java create mode 100644 src/main/java/dev/figboot/cuberender/math/Matrix3f.java create mode 100644 src/main/java/dev/figboot/cuberender/math/Matrix4f.java create mode 100644 src/main/java/dev/figboot/cuberender/math/Vector2f.java create mode 100644 src/main/java/dev/figboot/cuberender/math/Vector3f.java create mode 100644 src/main/java/dev/figboot/cuberender/math/Vector4f.java (limited to 'src/main/java/dev/figboot/cuberender/math') diff --git a/src/main/java/dev/figboot/cuberender/math/MathUtil.java b/src/main/java/dev/figboot/cuberender/math/MathUtil.java new file mode 100644 index 0000000..cf42a62 --- /dev/null +++ b/src/main/java/dev/figboot/cuberender/math/MathUtil.java @@ -0,0 +1,9 @@ +package dev.figboot.cuberender.math; + +public final class MathUtil { + private MathUtil() { } + + public static float clamp(float f1, float min, float max) { + return Math.min(Math.max(f1, min), max); + } +} diff --git a/src/main/java/dev/figboot/cuberender/math/Matrix3f.java b/src/main/java/dev/figboot/cuberender/math/Matrix3f.java new file mode 100644 index 0000000..521e68e --- /dev/null +++ b/src/main/java/dev/figboot/cuberender/math/Matrix3f.java @@ -0,0 +1,121 @@ +package dev.figboot.cuberender.math; + +public class Matrix3f { + public float m00, m01, m02, + m10, m11, m12, + m20, m21, m22; + + public Matrix3f() { + this(1, 0, 0, 0, 1, 0, 0, 0, 1); + } + + public Matrix3f(Matrix3f m) { + this(m.m00, m.m01, m.m02, m.m10, m.m11, m.m12, m.m20, m.m21, m.m22); + } + + public Matrix3f(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) { + this.m00 = m00; + this.m01 = m01; + this.m02 = m02; + this.m10 = m10; + this.m11 = m11; + this.m12 = m12; + this.m20 = m20; + this.m21 = m21; + this.m22 = m22; + } + + public Matrix3f identity() { + return identity(this); + } + + public Matrix3f identity(Matrix3f target) { + target.m00 = 1f; + target.m11 = 1f; + target.m22 = 1f; + + target.m01 = target.m02 = target.m10 = target.m12 = target.m20 = target.m21 = 0; + return target; + } + + public Matrix3f times(Matrix3f other) { + return times(other, this); + } + + public Matrix3f times(Matrix3f other, Matrix3f target) { + float m00 = this.m00 * other.m00 + this.m01 * other.m10 + this.m02 * other.m20; + float m01 = this.m00 * other.m01 + this.m01 * other.m11 + this.m02 * other.m21; + float m02 = this.m00 * other.m02 + this.m01 * other.m21 + this.m02 * other.m22; + + float m10 = this.m10 * other.m00 + this.m11 * other.m10 + this.m12 * other.m20; + float m11 = this.m10 * other.m01 + this.m11 * other.m11 + this.m12 * other.m21; + float m12 = this.m10 * other.m02 + this.m11 * other.m12 + this.m12 * other.m22; + + float m20 = this.m20 * other.m00 + this.m21 * other.m10 + this.m22 * other.m20; + float m21 = this.m20 * other.m01 + this.m21 * other.m11 + this.m22 * other.m21; + float m22 = this.m20 * other.m02 + this.m21 * other.m12 + this.m22 * other.m22; + + target.m00 = m00; + target.m01 = m01; + target.m02 = m02; + + target.m10 = m10; + target.m11 = m11; + target.m12 = m12; + + target.m20 = m20; + target.m21 = m21; + target.m22 = m22; + + return target; + } + + public Vector3f transform(Vector3f other) { + return new Vector3f( + this.m00 * other.x + this.m01 * other.y + this.m02 * other.z, + this.m10 * other.x + this.m11 * other.y + this.m12 * other.z, + this.m20 * other.x + this.m21 * other.y + this.m22 * other.z); + } + + public static Matrix3f rotateX(float radians) { + float f1 = (float)Math.cos(radians); + float f2 = (float)Math.sin(radians); + + return new Matrix3f( + 1, 0, 0, + 0, f1, f2, + 0, -f2, f1); + } + + public static Matrix3f rotateY(float radians) { + float f1 = (float)Math.cos(radians); + float f2 = (float)Math.sin(radians); + + return new Matrix3f( + f1, 0, -f2, + 0, 1, 0, + f2, 0, f1); + } + + public static Matrix3f rotateZ(float radians) { + float f1 = (float)Math.cos(radians); + float f2 = (float)Math.sin(radians); + + return new Matrix3f( + f1, -f2, 0, + f2, f1, 0, + 0, 0, 1); + } + + public static Matrix3f scaleX(float by) { + return new Matrix3f(by, 0, 0, 0, 1, 0, 0, 0, 1); + } + + public static Matrix3f scaleY(float by) { + return new Matrix3f(1, 0, 0, 0, by, 0, 0, 0, 1); + } + + public static Matrix3f scaleZ(float by) { + return new Matrix3f(1, 0, 0, 0, 1, 0, 0, 0, by); + } +} diff --git a/src/main/java/dev/figboot/cuberender/math/Matrix4f.java b/src/main/java/dev/figboot/cuberender/math/Matrix4f.java new file mode 100644 index 0000000..e86db41 --- /dev/null +++ b/src/main/java/dev/figboot/cuberender/math/Matrix4f.java @@ -0,0 +1,149 @@ +package dev.figboot.cuberender.math; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class Matrix4f { + public float m00, m01, m02, m03, + m10, m11, m12, m13, + m20, m21, m22, m23, + m30, m31, m32, m33; + + public Matrix4f() { + identity(); + } + + public Matrix4f(Matrix4f mat) { + this(mat.m00, mat.m01, mat.m02, mat.m03, mat.m10, mat.m11, mat.m12, mat.m13, mat.m20, mat.m21, mat.m22, mat.m23, mat.m30, mat.m31, mat.m32, mat.m33); + } + + public Matrix4f identity() { + return identity(this); + } + + public Matrix4f identity(Matrix4f target) { + target.m00 = target.m11 = target.m22 = target.m33 = 1f; + target.m01 = target.m02 = target.m03 + = target.m10 = target.m12 = target.m13 + = target.m20 = target.m21 = target.m23 + = target.m30 = target.m31 = target.m32 = 0; + + return target; + } + + public Matrix4f times(Matrix4f right) { + return times(right, this); + } + + public Matrix4f times(Matrix4f right, Matrix4f target) { + float m00 = this.m00 * right.m00 + this.m01 * right.m10 + this.m02 * right.m20 + this.m03 * this.m30; + float m01 = this.m00 * right.m01 + this.m01 * right.m11 + this.m02 * right.m21 + this.m03 * this.m31; + float m02 = this.m00 * right.m02 + this.m01 * right.m12 + this.m02 * right.m22 + this.m03 * this.m32; + float m03 = this.m00 * right.m03 + this.m01 * right.m13 + this.m02 * right.m23 + this.m03 * this.m33; + + float m10 = this.m10 * right.m00 + this.m11 * right.m10 + this.m12 * right.m20 + this.m13 * this.m30; + float m11 = this.m10 * right.m01 + this.m11 * right.m11 + this.m12 * right.m21 + this.m13 * this.m31; + float m12 = this.m10 * right.m02 + this.m11 * right.m12 + this.m12 * right.m22 + this.m13 * this.m32; + float m13 = this.m10 * right.m03 + this.m11 * right.m13 + this.m12 * right.m23 + this.m13 * this.m33; + + float m20 = this.m20 * right.m00 + this.m21 * right.m10 + this.m22 * right.m20 + this.m23 * this.m30; + float m21 = this.m20 * right.m01 + this.m21 * right.m11 + this.m22 * right.m21 + this.m23 * this.m31; + float m22 = this.m20 * right.m02 + this.m21 * right.m12 + this.m22 * right.m22 + this.m23 * this.m32; + float m23 = this.m20 * right.m03 + this.m21 * right.m13 + this.m22 * right.m23 + this.m23 * this.m33; + + float m30 = this.m30 * right.m00 + this.m31 * right.m10 + this.m32 * right.m20 + this.m33 * this.m30; + float m31 = this.m30 * right.m01 + this.m31 * right.m11 + this.m32 * right.m21 + this.m33 * this.m31; + float m32 = this.m30 * right.m02 + this.m31 * right.m12 + this.m32 * right.m22 + this.m33 * this.m32; + float m33 = this.m30 * right.m03 + this.m31 * right.m13 + this.m32 * right.m23 + this.m33 * this.m33; + + target.m00 = m00; + target.m01 = m01; + target.m02 = m02; + target.m03 = m03; + + target.m10 = m10; + target.m11 = m11; + target.m12 = m12; + target.m13 = m13; + + target.m20 = m20; + target.m21 = m21; + target.m22 = m22; + target.m23 = m23; + + target.m30 = m30; + target.m31 = m31; + target.m32 = m32; + target.m33 = m33; + + return target; + } + + public Vector4f transform(Vector4f in) { + return transform(in, in); + } + + public Vector4f transform(Vector4f in, Vector4f target) { + float x = in.x * m00 + in.y * m01 + in.z * m02 + in.w * m03; + float y = in.x * m10 + in.y * m11 + in.z * m12 + in.w * m13; + float z = in.x * m20 + in.y * m21 + in.z * m22 + in.w * m23; + float w = in.x * m30 + in.y * m31 + in.z * m32 + in.w * m33; + + target.x = x; + target.y = y; + target.z = z; + target.w = w; + + return target; + } + + public static Matrix4f scale(float factor) { + return scale(factor, factor, factor); + } + + public static Matrix4f scale(float x, float y, float z) { + Matrix4f mat = new Matrix4f(); + mat.m00 = x; + mat.m11 = y; + mat.m22 = z; + return mat; + } + + public static Matrix4f translate(float x, float y, float z) { + Matrix4f mat = new Matrix4f(); + mat.m03 = x; + mat.m13 = y; + mat.m23 = z; + return mat; + } + + public static Matrix4f rotateX(float rad) { + float cos = (float)Math.cos(rad); + float sin = (float)Math.sin(rad); + return new Matrix4f( + 1, 0, 0, 0, + 0, cos, sin, 0, + 0, -sin, cos, 0, + 0, 0, 0, 1); + } + + public static Matrix4f rotateY(float rad) { + float cos = (float)Math.cos(rad); + float sin = (float)Math.sin(rad); + return new Matrix4f( + cos, 0, -sin, 0, + 0, 1, 0, 0, + sin, 0, cos, 0, + 0, 0, 0, 1); + } + + public static Matrix4f rotateZ(float rad) { + float cos = (float)Math.cos(rad); + float sin = (float)Math.sin(rad); + return new Matrix4f( + cos, -sin, 0, 0, + sin, cos, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); + } +} diff --git a/src/main/java/dev/figboot/cuberender/math/Vector2f.java b/src/main/java/dev/figboot/cuberender/math/Vector2f.java new file mode 100644 index 0000000..66711d6 --- /dev/null +++ b/src/main/java/dev/figboot/cuberender/math/Vector2f.java @@ -0,0 +1,17 @@ +package dev.figboot.cuberender.math; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class Vector2f { + public float x, y; + + public Vector2f() { + this(0, 0); + } + + public Vector2f(Vector2f vector) { + this.x = vector.x; + this.y = vector.y; + } +} diff --git a/src/main/java/dev/figboot/cuberender/math/Vector3f.java b/src/main/java/dev/figboot/cuberender/math/Vector3f.java new file mode 100644 index 0000000..40720e8 --- /dev/null +++ b/src/main/java/dev/figboot/cuberender/math/Vector3f.java @@ -0,0 +1,42 @@ +package dev.figboot.cuberender.math; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class Vector3f { + public float x, y, z; + + public Vector3f() { + this(0, 0, 0); + } + + public Vector3f(Vector3f vector) { + this.x = vector.x; + this.y = vector.y; + this.z = vector.z; + } + + public float dot(Vector3f vector) { + return this.x * vector.x + this.y * vector.y + this.z * vector.z; + } + + public float lengthSquared() { + return this.x * this.x + this.y * this.y + this.z * this.z; + } + + public float length() { + return (float)Math.sqrt(lengthSquared()); + } + + public Vector3f normalize() { + return normalize(this); + } + + public Vector3f normalize(Vector3f target) { + float len = length(); + target.x /= len; + target.y /= len; + target.z /= len; + return target; + } +} diff --git a/src/main/java/dev/figboot/cuberender/math/Vector4f.java b/src/main/java/dev/figboot/cuberender/math/Vector4f.java new file mode 100644 index 0000000..7a29010 --- /dev/null +++ b/src/main/java/dev/figboot/cuberender/math/Vector4f.java @@ -0,0 +1,47 @@ +package dev.figboot.cuberender.math; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class Vector4f { + public float x, y, z, w; + + public Vector4f() { + this(0, 0, 0, 0); + } + + public Vector4f(Vector4f vec) { + this(vec.x, vec.y, vec.z, vec.w); + } + + public Vector4f fromARGB(int argb) { + return fromARGB(argb, this); + } + + public Vector4f fromARGB(int argb, Vector4f target) { + target.x = ((argb & 0x00FF0000) >>> 16) / 255f; + target.y = ((argb & 0x0000FF00) >>> 8) / 255f; + target.z = ((argb & 0x000000FF)) / 255f; + target.w = ((argb & 0xFF000000) >>> 24) / 255f; + return target; + } + + public int toARGB() { + return ((int)(MathUtil.clamp(w, 0, 1) * 255) << 24) + | ((int)(MathUtil.clamp(x, 0, 1) * 255) << 16) + | ((int)(MathUtil.clamp(y, 0, 1) * 255) << 8) + | ((int)(MathUtil.clamp(z, 0, 1) * 255)); + } + + public Vector4f times(float fact) { + return times(fact, this); + } + + public Vector4f times(float fact, Vector4f target) { + target.x *= fact; + target.y *= fact; + target.z *= fact; + target.w *= fact; + return target; + } +} -- cgit v1.2.3-70-g09d2