fix aspectRatio

This commit is contained in:
2025-04-26 12:14:22 +02:00
parent 7af9807127
commit ab309ae48a
2 changed files with 11 additions and 8 deletions

View File

@@ -5,11 +5,11 @@ import org.joml.Vector3f;
public class Camera { public class Camera {
public static final float fov = 70.0f; 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 zNear = 0.01f;
public static final float zFar = 1000.0f; public static final float zFar = 1000.0f;
private float aspectRatio;
private Vector3f pos; private Vector3f pos;
private float yaw = 0.0f; private float yaw = 0.0f;
@@ -115,14 +115,13 @@ public class Camera {
this.pitch = pitch; this.pitch = pitch;
} }
public Matrix4f getMatrix() { public void setAspectRatio(float aspectRatio) {
Vector3f forward = new Vector3f( this.aspectRatio = aspectRatio;
(float) (Math.cos(yaw) * Math.cos(pitch)), }
(float) (Math.sin(pitch)),
(float) (Math.sin(yaw) * Math.cos(pitch)));
public Matrix4f getMatrix() {
return new Matrix4f() return new Matrix4f()
.perspective((float) (Math.toRadians(fov)), aspect, zNear, zFar) .perspective((float) (Math.toRadians(fov)), aspectRatio, zNear, zFar)
.lookAt(pos, new Vector3f(0.0f, 0, 0), new Vector3f(0.0f, 1.0f, 0.0f)); .lookAt(pos, new Vector3f(0.0f, 0, 0), new Vector3f(0.0f, 1.0f, 0.0f));
} }
} }

View File

@@ -102,6 +102,10 @@ public class Window {
float y = cam.getPos().z(); float y = cam.getPos().z();
cam.setPosition(new Vector3f(x * (float) Math.cos(angle) - y * (float) Math.sin(angle), 1.0f, cam.setPosition(new Vector3f(x * (float) Math.cos(angle) - y * (float) Math.sin(angle), 1.0f,
x * (float) Math.sin(angle) + y * (float) Math.cos(angle))); x * (float) Math.sin(angle) + y * (float) Math.cos(angle)));
int width[] = new int[1];
int height[] = new int[1];
glfwGetWindowSize(window, width, height);
cam.setAspectRatio((float) width[0] / (float) height[0]);
renderer.Render(cam); renderer.Render(cam);
renderPieces(); renderPieces();
} }