merge branch 3dunstable
All checks were successful
Linux arm64 / Build (push) Successful in 38s

This commit is contained in:
2025-05-17 15:58:36 +02:00
39 changed files with 1814 additions and 244 deletions

View File

@@ -1,23 +1,29 @@
package chess.view.DDDrender;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.joml.Vector4f;
public class Camera {
public static final float fov = 70.0f;
// should be changed to match screen
public static final float aspect = 1.0f;
public static final float zNear = 0.01f;
public static final float zFar = 100.0f;
public static final float zFar = 1000.0f;
private static final Vector3f up = new Vector3f(0.0f, -1.0f, 0.0f);
private static final Vector3f center = new Vector3f(0.0f, 0.0f, 0.0f);
private final float distance = 1.5f;
private final float camHeight = 1.5f;
private float aspectRatio;
private float angle;
private Vector3f pos;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Camera() {
this.pos = new Vector3f(0, 2.0f, 0);
setRotation(0.0f, -3.14150f / 2.0f);
this.pos = new Vector3f(0.0f, camHeight, 0.0f);
setRotateAngle(0.0f);
}
public void move(float x, float y) {
@@ -25,104 +31,56 @@ public class Camera {
this.pos.y += y;
}
public void rotate(float yaw, float pitch) {
this.yaw += yaw;
this.pitch += pitch;
public void setRotateAngle(float angle) {
this.angle = angle;
updatePostion();
}
private void updatePostion() {
final float finalX = (float) Math.sin(angle);
final float finalZ = (float) -Math.cos(angle);
this.pos.set(distance * finalX, this.pos.get(1), distance * finalZ);
}
public float getRotateAngle() {
return angle;
}
public Vector3f getPos() {
return pos;
}
public float getYaw() {
return yaw;
}
public float getPitch() {
return pitch;
}
public float getFov() {
return fov;
}
public void setX(float x) {
this.pos.x = x;
public void setAspectRatio(float aspectRatio) {
this.aspectRatio = aspectRatio;
}
public void setY(float y) {
this.pos.y = y;
public Matrix4f getPerspectiveMatrix() {
return new Matrix4f().perspective((float) (Math.toRadians(fov)), aspectRatio, zNear, zFar);
}
public void setZ(float z) {
this.pos.z = z;
public Matrix4f getViewMatrix() {
return new Matrix4f().lookAt(pos, center, up);
}
public void setYaw(float yaw) {
this.yaw = yaw;
}
public Vector2f getCursorWorldFloorPos(Vector2f screenPos, int windowWidth, int windowHeight) {
float relativeX = (screenPos.x / (float) windowWidth * 2.0f) - 1.0f;
float relativeY = 1.0f - (screenPos.y / (float) windowHeight * 2.0f);
public void setPitch(float pitch) {
this.pitch = pitch;
}
Vector4f rayClip = new Vector4f(relativeX, relativeY, -1.0f, 1.0f);
public void reset() {
resetPosition();
resetRotation();
}
Vector4f rayEye = getPerspectiveMatrix().invert().transform(rayClip);
public void resetPosition() {
pos = new Vector3f(0.0f, 0.0f, 0.0f);
}
rayEye = new Vector4f(rayEye.x, rayEye.y, -1.0f, 0.0f);
public void resetRotation() {
yaw = 0.0f;
pitch = 0.0f;
}
Vector4f rayWorld = getViewMatrix().invert().transform(rayEye);
public void moveForward(float distance) {
pos.x += distance * (float) Math.cos(yaw);
pos.y += distance * (float) Math.sin(yaw);
}
float lambda = -this.pos.y / rayWorld.y;
public void moveRight(float distance) {
pos.x += distance * (float) Math.cos(yaw);
pos.y += distance * (float) Math.sin(yaw);
}
public void moveUp(float distance) {
pos.z += distance;
}
public void moveDown(float distance) {
pos.z -= distance;
}
public void addYaw(float angle) {
yaw += angle;
}
public void addPitch(float angle) {
pitch += angle;
}
public void setPosition(Vector3f pos) {
this.pos = pos;
}
public void setRotation(float yaw, float pitch) {
this.yaw = yaw;
this.pitch = pitch;
}
public Matrix4f getMatrix() {
Vector3f forward = new Vector3f(
(float) (Math.cos(yaw) * Math.cos(pitch)),
(float) (Math.sin(pitch)),
(float) (Math.sin(yaw) * Math.cos(pitch)));
return new Matrix4f()
.perspective((float) (Math.toRadians(fov)), aspect, zNear, zFar)
.lookAt(pos, forward, new Vector3f(0.0f, 1.0f, 0.0f));
return new Vector2f(lambda * rayWorld.x + this.pos.x, lambda *
rayWorld.z + this.pos.z);
}
}