From 970cb80f7c32fa83c6588ec4ad992a191d7c96ef Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Tue, 24 Dec 2024 09:35:35 +0100 Subject: [PATCH] Tmp --- src/main/java/math/Matrix4f.java | 134 ++++++++++++++++++++++++++----- 1 file changed, 112 insertions(+), 22 deletions(-) diff --git a/src/main/java/math/Matrix4f.java b/src/main/java/math/Matrix4f.java index 2494300d..10c1fb22 100644 --- a/src/main/java/math/Matrix4f.java +++ b/src/main/java/math/Matrix4f.java @@ -241,30 +241,66 @@ public static Matrix4f lookAt(Vector3f eye, Vector3f target, Vector3f up) { * @return This matrix for chaining calls. * @throws IllegalArgumentException if the input parameters are invalid. */ - public Matrix4f setPerspective(float fov, float aspect, float nearPlane, float farPlane) { - if (nearPlane > farPlane) { - throw new IllegalArgumentException( - String.format( - "Near plane (%.2f) cannot be greater than far plane (%.2f).", nearPlane, farPlane)); - } - if (aspect <= 0) { - throw new IllegalArgumentException("Aspect ratio must be a positive number."); - } - if (fov <= 0.0 || fov >= Math.PI) { - throw new IllegalArgumentException("Field of view must be between 0 and π radians."); +// public Matrix4f setPerspective(float fov, float aspect, float nearPlane, float farPlane) { +// if (nearPlane > farPlane) { +// throw new IllegalArgumentException( +// String.format( +// "Near plane (%.2f) cannot be greater than far plane (%.2f).", nearPlane, farPlane)); +// } +// if (aspect <= 0) { +// throw new IllegalArgumentException("Aspect ratio must be a positive number."); +// } +// if (fov <= 0.0 || fov >= Math.PI) { +// throw new IllegalArgumentException("Field of view must be between 0 and π radians."); +// } +// +// float f = (float) (1.0 / Math.tan(fov / 2.0)); +// Arrays.fill(values, 0); +// values[0] = f / aspect; +// values[5] = f; +// values[10] = (farPlane + nearPlane) / (nearPlane - farPlane); +// values[11] = -1; +// values[14] = (2 * farPlane * nearPlane) / (nearPlane - farPlane); +// +// return this; +// } + + public Matrix4f setPerspective(float fov, float aspect, float nearPlane, float farPlane) { + // Check if the near and far planes are valid + if (nearPlane <= 0 || farPlane <= 0) { + throw new IllegalArgumentException("Near and far planes must be positive values."); + } + if (nearPlane > farPlane) { + throw new IllegalArgumentException( + String.format( + "Near plane (%.2f) cannot be greater than far plane (%.2f).", nearPlane, + farPlane)); + } + if (aspect <= 0) { + throw new IllegalArgumentException("Aspect ratio must be a positive number."); + } + if (fov <= 0.0 || fov >= Math.PI) { + throw new IllegalArgumentException("Field of view must be between 0 and π radians."); + } + + // Calculate the tangent of the vertical field of view angle + float f = (float) (1.0 / Math.tan(fov / 2.0)); + + // Reset the matrix values to zero + Arrays.fill(values, 0); + + // Set the perspective matrix values + values[0] = f / aspect; // Horizontal scaling factor + values[5] = f; // Vertical scaling factor + values[10] = -(farPlane + nearPlane) / (farPlane - nearPlane); // Depth scaling factor + values[11] = -1; // Far/near plane relationship + values[14] = + -(2 * farPlane * nearPlane) / (farPlane - nearPlane); // Near and far planes interaction + values[15] = 0; // Perspective divide (used for homogeneous coordinates) + + return this; } - float f = (float) (1.0 / Math.tan(fov / 2.0)); - Arrays.fill(values, 0); - values[0] = f / aspect; - values[5] = f; - values[10] = (farPlane + nearPlane) / (nearPlane - farPlane); - values[11] = -1; - values[14] = (2 * farPlane * nearPlane) / (nearPlane - farPlane); - - return this; - } - // /** // * Constructs a right-handed view matrix for an FPS (First-Person Shooter) // * style camera with -Y as up. @@ -365,6 +401,60 @@ public static Matrix4f fpsViewRH(Vector3f eye, float pitch, float yaw) { return viewMatrix; } + + public Matrix4f rotateX(float angle) { + float cos = (float) Math.cos(angle); + float sin = (float) Math.sin(angle); + Matrix4f rotation = new Matrix4f( + 1, 0, 0, 0, + 0, cos, -sin, 0, + 0, sin, cos, 0, + 0, 0, 0, 1 + ); + return this.multiply(rotation); + } + + public Matrix4f rotateY(float angle) { + float cos = (float) Math.cos(angle); + float sin = (float) Math.sin(angle); + Matrix4f rotation = new Matrix4f( + cos, 0, sin, 0, + 0, 1, 0, 0, + -sin, 0, cos, 0, + 0, 0, 0, 1 + ); + return this.multiply(rotation); + } + + public Matrix4f rotateZ(float angle) { + float cos = (float) Math.cos(angle); + float sin = (float) Math.sin(angle); + Matrix4f rotation = new Matrix4f( + cos, -sin, 0, 0, + sin, cos, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + ); + return this.multiply(rotation); + } + + public Matrix4f translate(float x, float y, float z) { + Matrix4f translation = new Matrix4f( + 1, 0, 0, x, + 0, 1, 0, y, + 0, 0, 1, z, + 0, 0, 0, 1 + ); + return this.multiply(translation); + } + + public Matrix4f setViewMatrix(Vector3f rotation, Vector3f position) { + return this.identity() + .rotateX(-rotation.x) + .rotateY(-rotation.y) + .rotateZ(-rotation.z) + .translate(-position.x, -position.y, -position.z); + } @Override public String toString() {