dev #12

Merged
Persson-dev merged 44 commits from dev into main 2025-05-17 15:05:24 +00:00
5 changed files with 70 additions and 109 deletions
Showing only changes of commit b62dcffcb1 - Show all commits

View File

@@ -8,16 +8,21 @@ public class Camera {
public static final float zNear = 0.01f;
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(1.5f, 1.5f, 0);
setRotation(0.0f, -3.14150f / 2.0f);
this.pos = new Vector3f(0.0f, camHeight, 0.0f);
this.angle = 0.0f;
}
public void move(float x, float y) {
@@ -25,23 +30,25 @@ 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;
}
@@ -58,70 +65,19 @@ public class Camera {
this.pos.z = z;
}
public void setYaw(float yaw) {
this.yaw = yaw;
}
public void setPitch(float pitch) {
this.pitch = pitch;
}
public void reset() {
resetPosition();
resetRotation();
}
public void resetPosition() {
pos = new Vector3f(0.0f, 0.0f, 0.0f);
}
public void resetRotation() {
yaw = 0.0f;
pitch = 0.0f;
}
public void moveForward(float distance) {
pos.x += distance * (float) Math.cos(yaw);
pos.y += distance * (float) Math.sin(yaw);
}
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 void setAspectRatio(float aspectRatio) {
this.aspectRatio = aspectRatio;
}
public Matrix4f getMatrix() {
return new Matrix4f()
.perspective((float) (Math.toRadians(fov)), aspectRatio, zNear, zFar)
.lookAt(pos, new Vector3f(0.0f, 0, 0), new Vector3f(0.0f, 1.0f, 0.0f));
public Matrix4f getPerspectiveMatrix() {
return new Matrix4f().perspective((float) (Math.toRadians(fov)), aspectRatio, zNear, zFar);
}
public Matrix4f getViewMatrix() {
return new Matrix4f().lookAt(pos, center, up);
}
}

View File

@@ -49,9 +49,9 @@ public class Renderer {
public void Render(Camera cam) {
this.boardShader.Start();
this.boardShader.SetCamMatrix(cam.getMatrix());
this.boardShader.SetCamMatrix(cam);
this.pieceShader.Start();
this.pieceShader.SetCamMatrix(cam.getMatrix());
this.pieceShader.SetCamMatrix(cam);
RenderVao(this.boardShader, this.boardVao);
}

View File

@@ -1,6 +1,5 @@
package chess.view.DDDrender;
import org.joml.Vector3f;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
@@ -11,7 +10,6 @@ import chess.controller.commands.GetPieceAtCommand;
import chess.model.Coordinate;
import chess.model.Piece;
import java.io.IOException;
import java.nio.*;
import static org.lwjgl.glfw.Callbacks.*;
@@ -96,16 +94,11 @@ public class Window {
glfwShowWindow(window);
}
private void render() {
final float angle = 0.01f;
float x = cam.getPos().x();
float y = cam.getPos().z();
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)));
int width[] = new int[1];
int height[] = new int[1];
glfwGetWindowSize(window, width, height);
cam.setAspectRatio((float) width[0] / (float) height[0]);
private void render(float delta, float aspectRatio) {
final float angle = 1f;
cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
cam.setAspectRatio(aspectRatio);
renderer.Render(cam);
renderPieces();
}
@@ -149,12 +142,20 @@ public class Window {
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
double lastTime = glfwGetTime();
int width[] = new int[1];
int height[] = new int[1];
glfwGetWindowSize(window, width, height);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
render();
double currentTime = glfwGetTime();
render((float) (currentTime - lastTime), (float) width[0] / (float) height[0]);
lastTime = glfwGetTime();
glfwSwapBuffers(window); // swap the color buffers
@@ -162,15 +163,9 @@ public class Window {
// invoked during this call.
glfwPollEvents();
try (MemoryStack stack = stackPush()) {
IntBuffer pWidth = stack.mallocInt(1); // int*
IntBuffer pHeight = stack.mallocInt(1); // int*
glfwGetWindowSize(window, width, height);
glViewport(0, 0, width[0], height[0]);
// Get the window size passed to glfwCreateWindow
glfwGetWindowSize(window, pWidth, pHeight);
glViewport(0, 0, pWidth.get(), pHeight.get());
} // the stack frame is popped automatically
}
}

View File

@@ -1,6 +1,6 @@
package chess.view.DDDrender.shader;
import org.joml.Matrix4f;
import chess.view.DDDrender.Camera;
public class BoardShader extends ShaderProgram {
@@ -10,14 +10,15 @@ public class BoardShader extends ShaderProgram {
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
uniform mat4 camMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform vec3 lightPosition;
flat out vec3 pass_color;
out vec3 toLightVector;
void main(void){
gl_Position = camMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
toLightVector = lightPosition - position;
@@ -61,7 +62,8 @@ public class BoardShader extends ShaderProgram {
""";
private int location_CamMatrix = 0;
private int location_ProjectionMatrix = 0;
private int location_ViewMatrix = 0;
public BoardShader() {
@@ -73,10 +75,12 @@ public class BoardShader extends ShaderProgram {
@Override
protected void GetAllUniformLocation() {
location_CamMatrix = GetUniformLocation("camMatrix");
location_ProjectionMatrix = GetUniformLocation("projectionMatrix");
location_ViewMatrix = GetUniformLocation("viewMatrix");
}
public void SetCamMatrix(Matrix4f mat) {
LoadMat4(location_CamMatrix, mat);
public void SetCamMatrix(Camera camera) {
LoadMat4(location_ProjectionMatrix, camera.getPerspectiveMatrix());
LoadMat4(location_ViewMatrix, camera.getViewMatrix());
}
}

View File

@@ -3,6 +3,8 @@ package chess.view.DDDrender.shader;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import chess.view.DDDrender.Camera;
public class PieceShader extends ShaderProgram {
private static String vertexShader = """
@@ -12,7 +14,8 @@ public class PieceShader extends ShaderProgram {
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 normal;
uniform mat4 camMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelTransform;
uniform vec3 lightPosition = vec3(0, 1, 0);
@@ -25,7 +28,7 @@ public class PieceShader extends ShaderProgram {
toLightVector = lightPosition - worldPos.xyz;
surfaceNormal = (modelTransform * vec4(normal, 0.0)).xyz;
gl_Position = camMatrix * worldPos;
gl_Position = projectionMatrix * viewMatrix * worldPos;
}
""";
@@ -53,7 +56,8 @@ public class PieceShader extends ShaderProgram {
}
""";
private int location_CamMatrix = 0;
private int location_ProjectionMatrix = 0;
private int location_ViewMatrix = 0;
private int location_ModelTransform = 0;
private int location_ModelColor = 0;
@@ -67,13 +71,15 @@ public class PieceShader extends ShaderProgram {
@Override
protected void GetAllUniformLocation() {
location_CamMatrix = GetUniformLocation("camMatrix");
location_ProjectionMatrix = GetUniformLocation("projectionMatrix");
location_ViewMatrix = GetUniformLocation("viewMatrix");
location_ModelTransform = GetUniformLocation("modelTransform");
location_ModelColor = GetUniformLocation("modelColor");
}
public void SetCamMatrix(Matrix4f mat) {
LoadMat4(location_CamMatrix, mat);
public void SetCamMatrix(Camera camera) {
LoadMat4(location_ProjectionMatrix, camera.getPerspectiveMatrix());
LoadMat4(location_ViewMatrix, camera.getViewMatrix());
}
public void setModelTransform(Matrix4f mat) {