refactor
This commit is contained in:
@@ -8,16 +8,21 @@ public class Camera {
|
|||||||
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 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 aspectRatio;
|
||||||
|
private float angle;
|
||||||
|
|
||||||
private Vector3f pos;
|
private Vector3f pos;
|
||||||
|
|
||||||
private float yaw = 0.0f;
|
|
||||||
private float pitch = 0.0f;
|
|
||||||
|
|
||||||
public Camera() {
|
public Camera() {
|
||||||
this.pos = new Vector3f(1.5f, 1.5f, 0);
|
this.pos = new Vector3f(0.0f, camHeight, 0.0f);
|
||||||
setRotation(0.0f, -3.14150f / 2.0f);
|
this.angle = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move(float x, float y) {
|
public void move(float x, float y) {
|
||||||
@@ -25,23 +30,25 @@ public class Camera {
|
|||||||
this.pos.y += y;
|
this.pos.y += y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rotate(float yaw, float pitch) {
|
public void setRotateAngle(float angle) {
|
||||||
this.yaw += yaw;
|
this.angle = angle;
|
||||||
this.pitch += pitch;
|
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() {
|
public Vector3f getPos() {
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getYaw() {
|
|
||||||
return yaw;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getPitch() {
|
|
||||||
return pitch;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float getFov() {
|
public float getFov() {
|
||||||
return fov;
|
return fov;
|
||||||
}
|
}
|
||||||
@@ -58,70 +65,19 @@ public class Camera {
|
|||||||
this.pos.z = z;
|
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) {
|
public void setPosition(Vector3f pos) {
|
||||||
this.pos = pos;
|
this.pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRotation(float yaw, float pitch) {
|
|
||||||
this.yaw = yaw;
|
|
||||||
this.pitch = pitch;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAspectRatio(float aspectRatio) {
|
public void setAspectRatio(float aspectRatio) {
|
||||||
this.aspectRatio = aspectRatio;
|
this.aspectRatio = aspectRatio;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Matrix4f getMatrix() {
|
public Matrix4f getPerspectiveMatrix() {
|
||||||
return new Matrix4f()
|
return new Matrix4f().perspective((float) (Math.toRadians(fov)), aspectRatio, 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));
|
|
||||||
|
public Matrix4f getViewMatrix() {
|
||||||
|
return new Matrix4f().lookAt(pos, center, up);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,9 +49,9 @@ public class Renderer {
|
|||||||
|
|
||||||
public void Render(Camera cam) {
|
public void Render(Camera cam) {
|
||||||
this.boardShader.Start();
|
this.boardShader.Start();
|
||||||
this.boardShader.SetCamMatrix(cam.getMatrix());
|
this.boardShader.SetCamMatrix(cam);
|
||||||
this.pieceShader.Start();
|
this.pieceShader.Start();
|
||||||
this.pieceShader.SetCamMatrix(cam.getMatrix());
|
this.pieceShader.SetCamMatrix(cam);
|
||||||
RenderVao(this.boardShader, this.boardVao);
|
RenderVao(this.boardShader, this.boardVao);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package chess.view.DDDrender;
|
package chess.view.DDDrender;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
|
||||||
import org.lwjgl.*;
|
import org.lwjgl.*;
|
||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
import org.lwjgl.opengl.*;
|
import org.lwjgl.opengl.*;
|
||||||
@@ -11,7 +10,6 @@ import chess.controller.commands.GetPieceAtCommand;
|
|||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.*;
|
import java.nio.*;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.Callbacks.*;
|
import static org.lwjgl.glfw.Callbacks.*;
|
||||||
@@ -96,16 +94,11 @@ public class Window {
|
|||||||
glfwShowWindow(window);
|
glfwShowWindow(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void render() {
|
private void render(float delta, float aspectRatio) {
|
||||||
final float angle = 0.01f;
|
final float angle = 1f;
|
||||||
float x = cam.getPos().x();
|
cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
|
||||||
float y = cam.getPos().z();
|
|
||||||
cam.setPosition(new Vector3f(x * (float) Math.cos(angle) - y * (float) Math.sin(angle), 1.0f,
|
cam.setAspectRatio(aspectRatio);
|
||||||
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();
|
||||||
}
|
}
|
||||||
@@ -149,12 +142,20 @@ public class Window {
|
|||||||
|
|
||||||
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
|
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
|
// Run the rendering loop until the user has attempted to close
|
||||||
// the window or has pressed the ESCAPE key.
|
// the window or has pressed the ESCAPE key.
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
|
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
|
glfwSwapBuffers(window); // swap the color buffers
|
||||||
|
|
||||||
@@ -162,15 +163,9 @@ public class Window {
|
|||||||
// invoked during this call.
|
// invoked during this call.
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
try (MemoryStack stack = stackPush()) {
|
glfwGetWindowSize(window, width, height);
|
||||||
IntBuffer pWidth = stack.mallocInt(1); // int*
|
glViewport(0, 0, width[0], height[0]);
|
||||||
IntBuffer pHeight = stack.mallocInt(1); // int*
|
|
||||||
|
|
||||||
// Get the window size passed to glfwCreateWindow
|
|
||||||
glfwGetWindowSize(window, pWidth, pHeight);
|
|
||||||
|
|
||||||
glViewport(0, 0, pWidth.get(), pHeight.get());
|
|
||||||
} // the stack frame is popped automatically
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package chess.view.DDDrender.shader;
|
package chess.view.DDDrender.shader;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import chess.view.DDDrender.Camera;
|
||||||
|
|
||||||
public class BoardShader extends ShaderProgram {
|
public class BoardShader extends ShaderProgram {
|
||||||
|
|
||||||
@@ -10,14 +10,15 @@ public class BoardShader extends ShaderProgram {
|
|||||||
layout(location = 0) in vec3 position;
|
layout(location = 0) in vec3 position;
|
||||||
layout(location = 1) in vec3 color;
|
layout(location = 1) in vec3 color;
|
||||||
|
|
||||||
uniform mat4 camMatrix;
|
uniform mat4 viewMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
uniform vec3 lightPosition;
|
uniform vec3 lightPosition;
|
||||||
|
|
||||||
flat out vec3 pass_color;
|
flat out vec3 pass_color;
|
||||||
out vec3 toLightVector;
|
out vec3 toLightVector;
|
||||||
|
|
||||||
void main(void){
|
void main(void){
|
||||||
gl_Position = camMatrix * vec4(position, 1.0);
|
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||||
|
|
||||||
toLightVector = lightPosition - position;
|
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() {
|
public BoardShader() {
|
||||||
|
|
||||||
@@ -73,10 +75,12 @@ public class BoardShader extends ShaderProgram {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void GetAllUniformLocation() {
|
protected void GetAllUniformLocation() {
|
||||||
location_CamMatrix = GetUniformLocation("camMatrix");
|
location_ProjectionMatrix = GetUniformLocation("projectionMatrix");
|
||||||
|
location_ViewMatrix = GetUniformLocation("viewMatrix");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCamMatrix(Matrix4f mat) {
|
public void SetCamMatrix(Camera camera) {
|
||||||
LoadMat4(location_CamMatrix, mat);
|
LoadMat4(location_ProjectionMatrix, camera.getPerspectiveMatrix());
|
||||||
|
LoadMat4(location_ViewMatrix, camera.getViewMatrix());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package chess.view.DDDrender.shader;
|
|||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
|
import chess.view.DDDrender.Camera;
|
||||||
|
|
||||||
public class PieceShader extends ShaderProgram {
|
public class PieceShader extends ShaderProgram {
|
||||||
|
|
||||||
private static String vertexShader = """
|
private static String vertexShader = """
|
||||||
@@ -12,7 +14,8 @@ public class PieceShader extends ShaderProgram {
|
|||||||
layout(location = 1) in vec2 uv;
|
layout(location = 1) in vec2 uv;
|
||||||
layout(location = 2) in vec3 normal;
|
layout(location = 2) in vec3 normal;
|
||||||
|
|
||||||
uniform mat4 camMatrix;
|
uniform mat4 projectionMatrix;
|
||||||
|
uniform mat4 viewMatrix;
|
||||||
uniform mat4 modelTransform;
|
uniform mat4 modelTransform;
|
||||||
uniform vec3 lightPosition = vec3(0, 1, 0);
|
uniform vec3 lightPosition = vec3(0, 1, 0);
|
||||||
|
|
||||||
@@ -25,7 +28,7 @@ public class PieceShader extends ShaderProgram {
|
|||||||
toLightVector = lightPosition - worldPos.xyz;
|
toLightVector = lightPosition - worldPos.xyz;
|
||||||
surfaceNormal = (modelTransform * vec4(normal, 0.0)).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_ModelTransform = 0;
|
||||||
private int location_ModelColor = 0;
|
private int location_ModelColor = 0;
|
||||||
|
|
||||||
@@ -67,13 +71,15 @@ public class PieceShader extends ShaderProgram {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void GetAllUniformLocation() {
|
protected void GetAllUniformLocation() {
|
||||||
location_CamMatrix = GetUniformLocation("camMatrix");
|
location_ProjectionMatrix = GetUniformLocation("projectionMatrix");
|
||||||
|
location_ViewMatrix = GetUniformLocation("viewMatrix");
|
||||||
location_ModelTransform = GetUniformLocation("modelTransform");
|
location_ModelTransform = GetUniformLocation("modelTransform");
|
||||||
location_ModelColor = GetUniformLocation("modelColor");
|
location_ModelColor = GetUniformLocation("modelColor");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCamMatrix(Matrix4f mat) {
|
public void SetCamMatrix(Camera camera) {
|
||||||
LoadMat4(location_CamMatrix, mat);
|
LoadMat4(location_ProjectionMatrix, camera.getPerspectiveMatrix());
|
||||||
|
LoadMat4(location_ViewMatrix, camera.getViewMatrix());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setModelTransform(Matrix4f mat) {
|
public void setModelTransform(Matrix4f mat) {
|
||||||
|
|||||||
Reference in New Issue
Block a user