change project structure
This commit is contained in:
128
app/src/main/java/chess/view/render/Camera.java
Normal file
128
app/src/main/java/chess/view/render/Camera.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package chess.view.render;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void move(float x, float y) {
|
||||
this.pos.x += x;
|
||||
this.pos.y += y;
|
||||
}
|
||||
|
||||
public void rotate(float yaw, float pitch) {
|
||||
this.yaw += yaw;
|
||||
this.pitch += pitch;
|
||||
}
|
||||
|
||||
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 setY(float y) {
|
||||
this.pos.y = y;
|
||||
}
|
||||
|
||||
public void setZ(float 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) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
33
app/src/main/java/chess/view/render/ElementBuffer.java
Normal file
33
app/src/main/java/chess/view/render/ElementBuffer.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package chess.view.render;
|
||||
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
public class ElementBuffer {
|
||||
private int id;
|
||||
private int indiciesCount;
|
||||
|
||||
public ElementBuffer(int[] indicies) {
|
||||
this.indiciesCount = indicies.length;
|
||||
this.id = GL30.glGenBuffers();
|
||||
Bind();
|
||||
GL30.glBufferData(GL30.GL_ELEMENT_ARRAY_BUFFER, indicies.length * 4, GL30.GL_STATIC_DRAW);
|
||||
GL30.glBufferSubData(GL30.GL_ELEMENT_ARRAY_BUFFER, 0, indicies);
|
||||
Unbind();
|
||||
}
|
||||
|
||||
public void Destroy() {
|
||||
GL30.glDeleteBuffers(this.id);
|
||||
}
|
||||
|
||||
public void Bind() {
|
||||
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, this.id);
|
||||
}
|
||||
|
||||
public void Unbind() {
|
||||
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
public int GetIndiciesCount() {
|
||||
return this.indiciesCount;
|
||||
}
|
||||
}
|
||||
124
app/src/main/java/chess/view/render/Renderer.java
Normal file
124
app/src/main/java/chess/view/render/Renderer.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package chess.view.render;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import chess.view.render.shader.BoardShader;
|
||||
|
||||
import static org.lwjgl.opengl.GL30.*;
|
||||
|
||||
public class Renderer {
|
||||
private BoardShader shader;
|
||||
private VertexArray vao;
|
||||
|
||||
private static int BOARD_WIDTH = 8;
|
||||
private static int BOARD_HEIGHT = 8;
|
||||
private static int BOARD_SIZE = BOARD_WIDTH * BOARD_HEIGHT;
|
||||
private static int SQUARE_VERTEX_COUNT = 4;
|
||||
|
||||
public Renderer() {
|
||||
this.shader = new BoardShader();
|
||||
}
|
||||
|
||||
public void Init() {
|
||||
shader.LoadShader();
|
||||
InitBoard();
|
||||
}
|
||||
|
||||
private float[] GetBoardPositions() {
|
||||
float[] positions = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
||||
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||
float x = i / (float) BOARD_WIDTH;
|
||||
float dx = (i + 1) / (float) BOARD_WIDTH;
|
||||
float z = j / (float) BOARD_HEIGHT;
|
||||
float dz = (j + 1) / (float) BOARD_HEIGHT;
|
||||
|
||||
float trueX = 2 * x - 1;
|
||||
float trueZ = 2 * z - 1;
|
||||
float trueDX = 2 * dx - 1;
|
||||
float trueDZ = 2 * dz - 1;
|
||||
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3] = trueX;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 1] = 0.0f;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 2] = trueZ;
|
||||
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 3] = trueDX;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 4] = 0.0f;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 5] = trueZ;
|
||||
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 6] = trueX;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 7] = 0.0f;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 8] = trueDZ;
|
||||
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 9] = trueDX;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 10] = 0.0f;
|
||||
positions[(BOARD_WIDTH * i + j) * SQUARE_VERTEX_COUNT * 3 + 11] = trueDZ;
|
||||
}
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
private float[] GetBoardColors() {
|
||||
float[] colors = new float[BOARD_SIZE * SQUARE_VERTEX_COUNT * 3];
|
||||
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||
Vector3f color;
|
||||
if ((i + j) % 2 != 0) {
|
||||
color = new Vector3f(1.0f, 1.0f, 1.0f);
|
||||
} else {
|
||||
color = new Vector3f(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
int squareIndex = i * BOARD_WIDTH + j;
|
||||
for (int k = 0; k < SQUARE_VERTEX_COUNT; k++) {
|
||||
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3] = color.x;
|
||||
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3 + 1] = color.y;
|
||||
colors[squareIndex * SQUARE_VERTEX_COUNT * 3 + k * 3 + 2] = color.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
return colors;
|
||||
}
|
||||
|
||||
private int[] GetBoardIndicies() {
|
||||
int[] indices = new int[BOARD_SIZE * 6];
|
||||
for (int i = 0; i < BOARD_SIZE; i++) {
|
||||
indices[i * 6] = i * 4;
|
||||
indices[i * 6 + 1] = i * 4 + 1;
|
||||
indices[i * 6 + 2] = i * 4 + 2;
|
||||
indices[i * 6 + 3] = i * 4 + 1;
|
||||
indices[i * 6 + 4] = i * 4 + 2;
|
||||
indices[i * 6 + 5] = i * 4 + 3;
|
||||
}
|
||||
return indices;
|
||||
}
|
||||
|
||||
private void InitBoard() {
|
||||
ElementBuffer eBuffer = new ElementBuffer(GetBoardIndicies());
|
||||
this.vao = new VertexArray(eBuffer);
|
||||
|
||||
VertexBuffer positionBuffer = new VertexBuffer(GetBoardPositions(), 3);
|
||||
positionBuffer.AddVertexAttribPointer(0, 3, 0);
|
||||
|
||||
VertexBuffer colorBuffer = new VertexBuffer(GetBoardColors(), 3);
|
||||
colorBuffer.AddVertexAttribPointer(1, 3, 0);
|
||||
|
||||
this.vao.Bind();
|
||||
this.vao.BindVertexBuffer(positionBuffer);
|
||||
this.vao.BindVertexBuffer(colorBuffer);
|
||||
this.vao.Unbind();
|
||||
}
|
||||
|
||||
public void Render(Camera cam) {
|
||||
this.shader.Start();
|
||||
this.shader.SetCamMatrix(cam.getMatrix());
|
||||
RenderVao(vao);
|
||||
}
|
||||
|
||||
public void RenderVao(VertexArray vertexArray) {
|
||||
this.shader.Start();
|
||||
vertexArray.Bind();
|
||||
GL30.glDrawElements(GL30.GL_TRIANGLES, vertexArray.GetVertexCount(), GL_UNSIGNED_INT, 0);
|
||||
vertexArray.Unbind();
|
||||
}
|
||||
}
|
||||
47
app/src/main/java/chess/view/render/VertexArray.java
Normal file
47
app/src/main/java/chess/view/render/VertexArray.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package chess.view.render;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
public class VertexArray {
|
||||
private int id;
|
||||
private ElementBuffer elementBuffer;
|
||||
private List<VertexBuffer> vertexBuffers;
|
||||
|
||||
public VertexArray(ElementBuffer elementBuffer) {
|
||||
this.id = GL30.glGenVertexArrays();
|
||||
this.elementBuffer = elementBuffer;
|
||||
this.vertexBuffers = new ArrayList<VertexBuffer>();
|
||||
Bind();
|
||||
BindElementArrayBuffer();
|
||||
Unbind();
|
||||
}
|
||||
|
||||
public void Destroy() {
|
||||
GL30.glDeleteBuffers(this.id);
|
||||
}
|
||||
|
||||
public int GetVertexCount() {
|
||||
return this.elementBuffer.GetIndiciesCount();
|
||||
}
|
||||
|
||||
public void BindVertexBuffer(VertexBuffer buffer) {
|
||||
buffer.Bind();
|
||||
buffer.BindVertexAttribs();
|
||||
this.vertexBuffers.add(buffer);
|
||||
}
|
||||
|
||||
public void Bind() {
|
||||
GL30.glBindVertexArray(this.id);
|
||||
}
|
||||
|
||||
public void Unbind() {
|
||||
GL30.glBindVertexArray(0);
|
||||
}
|
||||
|
||||
private void BindElementArrayBuffer() {
|
||||
this.elementBuffer.Bind();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package chess.view.render;
|
||||
|
||||
public record VertexAttribPointer(int index, int size, int offset) {
|
||||
|
||||
}
|
||||
49
app/src/main/java/chess/view/render/VertexBuffer.java
Normal file
49
app/src/main/java/chess/view/render/VertexBuffer.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package chess.view.render;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
public class VertexBuffer {
|
||||
private int id;
|
||||
private int dataStride;
|
||||
List<VertexAttribPointer> vertexAttribs;
|
||||
|
||||
public VertexBuffer(float[] data, int stride) {
|
||||
this.id = GL30.glGenBuffers();
|
||||
this.dataStride = stride;
|
||||
this.vertexAttribs = new ArrayList<VertexAttribPointer>();
|
||||
Bind();
|
||||
GL30.glBufferData(GL30.GL_ARRAY_BUFFER, data.length * 4, GL30.GL_STATIC_DRAW);
|
||||
GL30.glBufferSubData(GL30.GL_ARRAY_BUFFER, 0, data);
|
||||
Unbind();
|
||||
}
|
||||
|
||||
public void Destroy() {
|
||||
GL30.glDeleteBuffers(id);
|
||||
}
|
||||
|
||||
public void Bind() {
|
||||
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, this.id);
|
||||
}
|
||||
|
||||
public void Unbind() {
|
||||
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
public void AddVertexAttribPointer(int index, int coordinateSize, int offset) {
|
||||
VertexAttribPointer pointer = new VertexAttribPointer(index, coordinateSize, offset);
|
||||
this.vertexAttribs.add(pointer);
|
||||
}
|
||||
|
||||
public void BindVertexAttribs() {
|
||||
for (VertexAttribPointer vertexAttribPointer : vertexAttribs) {
|
||||
GL30.glEnableVertexAttribArray(vertexAttribPointer.index());
|
||||
GL30.glVertexAttribPointer(vertexAttribPointer.index(), vertexAttribPointer.size(), GL_FLOAT, false,
|
||||
this.dataStride * 4, vertexAttribPointer.offset());
|
||||
}
|
||||
}
|
||||
}
|
||||
135
app/src/main/java/chess/view/render/Window.java
Normal file
135
app/src/main/java/chess/view/render/Window.java
Normal file
@@ -0,0 +1,135 @@
|
||||
package chess.view.render;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.system.MemoryStack.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
public class Window {
|
||||
|
||||
// The window handle
|
||||
private long window;
|
||||
|
||||
private Renderer renderer;
|
||||
private Camera cam;
|
||||
|
||||
public Window() {
|
||||
this.renderer = new Renderer();
|
||||
this.cam = new Camera();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
System.out.println("LWJGL " + Version.getVersion() + "!");
|
||||
|
||||
init();
|
||||
loop();
|
||||
|
||||
// Free the window callbacks and destroy the window
|
||||
glfwFreeCallbacks(window);
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
// Terminate GLFW and free the error callback
|
||||
glfwTerminate();
|
||||
glfwSetErrorCallback(null).free();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
// Setup an error callback. The default implementation
|
||||
// will print the error message in System.err.
|
||||
GLFWErrorCallback.createPrint(System.err).set();
|
||||
|
||||
// Initialize GLFW. Most GLFW functions will not work before doing this.
|
||||
if (!glfwInit())
|
||||
throw new IllegalStateException("Unable to initialize GLFW");
|
||||
|
||||
// Configure GLFW
|
||||
glfwDefaultWindowHints(); // optional, the current window hints are already the default
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
|
||||
|
||||
// Create the window
|
||||
window = glfwCreateWindow(1000, 1000, "3DChess", NULL, NULL);
|
||||
if (window == NULL)
|
||||
throw new RuntimeException("Failed to create the GLFW window");
|
||||
|
||||
// Get the thread stack and push a new frame
|
||||
try (MemoryStack stack = stackPush()) {
|
||||
IntBuffer pWidth = stack.mallocInt(1); // int*
|
||||
IntBuffer pHeight = stack.mallocInt(1); // int*
|
||||
|
||||
// Get the window size passed to glfwCreateWindow
|
||||
glfwGetWindowSize(window, pWidth, pHeight);
|
||||
|
||||
// Get the resolution of the primary monitor
|
||||
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
||||
|
||||
// Center the window
|
||||
glfwSetWindowPos(
|
||||
window,
|
||||
(vidmode.width() - pWidth.get(0)) / 2,
|
||||
(vidmode.height() - pHeight.get(0)) / 2);
|
||||
} // the stack frame is popped automatically
|
||||
|
||||
// Make the OpenGL context current
|
||||
glfwMakeContextCurrent(window);
|
||||
// Enable v-sync
|
||||
glfwSwapInterval(1);
|
||||
|
||||
// Make the window visible
|
||||
glfwShowWindow(window);
|
||||
}
|
||||
|
||||
private void render() {
|
||||
cam.rotate(0.01f, 0.01f);
|
||||
renderer.Render(cam);
|
||||
}
|
||||
|
||||
private void loop() {
|
||||
// This line is critical for LWJGL's interoperation with GLFW's
|
||||
// OpenGL context, or any context that is managed externally.
|
||||
// LWJGL detects the context that is current in the current thread,
|
||||
// creates the GLCapabilities instance and makes the OpenGL
|
||||
// bindings available for use.
|
||||
GL.createCapabilities();
|
||||
|
||||
renderer.Init();
|
||||
|
||||
// Set the clear color
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
// 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();
|
||||
|
||||
glfwSwapBuffers(window); // swap the color buffers
|
||||
|
||||
// Poll for window events. The key callback above will only be
|
||||
// invoked during this call.
|
||||
glfwPollEvents();
|
||||
|
||||
try (MemoryStack stack = stackPush()) {
|
||||
IntBuffer pWidth = stack.mallocInt(1); // int*
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
53
app/src/main/java/chess/view/render/shader/BoardShader.java
Normal file
53
app/src/main/java/chess/view/render/shader/BoardShader.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package chess.view.render.shader;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class BoardShader extends ShaderProgram {
|
||||
|
||||
private static String vertexShader = """
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in vec3 color;
|
||||
|
||||
uniform mat4 camMatrix;
|
||||
|
||||
flat out vec3 pass_color;
|
||||
|
||||
void main(void){
|
||||
gl_Position = camMatrix * vec4(position, 1.0);
|
||||
pass_color = color;
|
||||
}
|
||||
""";
|
||||
|
||||
private static String fragmentShader = """
|
||||
#version 330
|
||||
|
||||
flat in vec3 pass_color;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
void main(void){
|
||||
out_color = vec4(pass_color, 1.0);
|
||||
}
|
||||
""";
|
||||
|
||||
private int location_CamMatrix = 0;
|
||||
|
||||
public BoardShader() {
|
||||
|
||||
}
|
||||
|
||||
public void LoadShader() {
|
||||
super.LoadProgram(vertexShader, fragmentShader);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void GetAllUniformLocation() {
|
||||
location_CamMatrix = GetUniformLocation("camMatrix");
|
||||
}
|
||||
|
||||
public void SetCamMatrix(Matrix4f mat) {
|
||||
LoadMat4(location_CamMatrix, mat);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package chess.view.render.shader;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
public abstract class ShaderProgram {
|
||||
private int programId;
|
||||
private int vertexShaderId;
|
||||
private int fragmentShaderId;
|
||||
|
||||
public ShaderProgram() {
|
||||
this.programId = 0;
|
||||
this.vertexShaderId = 0;
|
||||
this.fragmentShaderId = 0;
|
||||
}
|
||||
|
||||
public void Start() {
|
||||
GL30.glUseProgram(this.programId);
|
||||
}
|
||||
|
||||
public void Stop() {
|
||||
GL30.glUseProgram(0);
|
||||
}
|
||||
|
||||
public void LoadProgram(String vertexSource, String fragmentSource) {
|
||||
this.vertexShaderId = LoadShader(vertexSource, GL30.GL_VERTEX_SHADER);
|
||||
this.fragmentShaderId = LoadShader(fragmentSource, GL30.GL_FRAGMENT_SHADER);
|
||||
|
||||
this.programId = GL30.glCreateProgram();
|
||||
GL30.glAttachShader(this.programId, vertexShaderId);
|
||||
GL30.glAttachShader(this.programId, this.fragmentShaderId);
|
||||
GL30.glLinkProgram(this.programId);
|
||||
GL30.glValidateProgram(this.programId);
|
||||
|
||||
if (GL30.glGetProgrami(programId, GL30.GL_VALIDATE_STATUS) == 0) {
|
||||
System.err.println("Warning validating Shader code: " + GL30.glGetProgramInfoLog(programId, 1024));
|
||||
}
|
||||
|
||||
GetAllUniformLocation();
|
||||
}
|
||||
|
||||
private int LoadShader(String source, int type) {
|
||||
int shaderId = GL30.glCreateShader(type);
|
||||
|
||||
GL30.glShaderSource(shaderId, source);
|
||||
GL30.glCompileShader(shaderId);
|
||||
|
||||
IntBuffer compileSuccesful = BufferUtils.createIntBuffer(1);
|
||||
GL30.glGetShaderiv(shaderId, GL30.GL_COMPILE_STATUS, compileSuccesful);
|
||||
|
||||
if (compileSuccesful.get() != 1) {
|
||||
System.out.println("Shader did not compile !");
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
return shaderId;
|
||||
}
|
||||
|
||||
protected abstract void GetAllUniformLocation();
|
||||
|
||||
protected int GetUniformLocation(String uniformName) {
|
||||
int location = GL30.glGetUniformLocation(programId, uniformName);
|
||||
if (location == -1) {
|
||||
System.out.println("Uniform value not found !");
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
public void LoadFloat(int location, float value) {
|
||||
GL30.glUniform1f(location, value);
|
||||
}
|
||||
|
||||
public void LoadInt(int location, int value) {
|
||||
GL30.glUniform1i(location, value);
|
||||
}
|
||||
|
||||
public void LoadVector(int location, Vector3f vector) {
|
||||
GL30.glUniform3f(location, vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
public void LoadMat4(int location, Matrix4f mat) {
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer buffer = mat.get(stack.mallocFloat(16));
|
||||
GL30.glUniformMatrix4fv(location, false, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
75
app/src/main/java/chess/view/simplerender/PieceIcon.java
Normal file
75
app/src/main/java/chess/view/simplerender/PieceIcon.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package chess.view.simplerender;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
import chess.model.Color;
|
||||
import chess.model.Piece;
|
||||
import chess.model.PieceVisitor;
|
||||
import chess.model.pieces.Bishop;
|
||||
import chess.model.pieces.King;
|
||||
import chess.model.pieces.Knight;
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
|
||||
public class PieceIcon implements PieceVisitor<String> {
|
||||
|
||||
private static final String basePath = "app/src/main/resources/pieces2D/";
|
||||
private static final Map<String, Icon> cache = new HashMap<>();
|
||||
|
||||
public Icon getIcon(Piece piece) {
|
||||
if (piece == null)
|
||||
return null;
|
||||
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".png";
|
||||
return getIcon(path);
|
||||
}
|
||||
|
||||
private Icon getIcon(String path) {
|
||||
Icon image = cache.get(path);
|
||||
if (image != null)
|
||||
return image;
|
||||
image = new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||
cache.put(path, image);
|
||||
return image;
|
||||
}
|
||||
|
||||
private String colorToString(Color color) {
|
||||
return color == Color.Black ? "black" : "white";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPiece(Bishop bishop) {
|
||||
return "bishop";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPiece(King king) {
|
||||
return "king";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPiece(Knight knight) {
|
||||
return "knight";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPiece(Pawn pawn) {
|
||||
return "pawn";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPiece(Queen queen) {
|
||||
return "queen";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visitPiece(Rook rook) {
|
||||
return "rook";
|
||||
}
|
||||
|
||||
}
|
||||
245
app/src/main/java/chess/view/simplerender/Window.java
Normal file
245
app/src/main/java/chess/view/simplerender/Window.java
Normal file
@@ -0,0 +1,245 @@
|
||||
package chess.view.simplerender;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import chess.controller.Command;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.OutputSystem;
|
||||
import chess.controller.Command.CommandResult;
|
||||
import chess.controller.commands.GetAllowedMovesCommand;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.controller.commands.MoveCommand;
|
||||
import chess.controller.commands.PromoteCommand;
|
||||
import chess.controller.commands.PromoteCommand.PromoteType;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Move;
|
||||
import chess.model.Piece;
|
||||
|
||||
public class Window extends JFrame implements OutputSystem {
|
||||
|
||||
private final JLabel cells[][];
|
||||
private final JLabel displayText;
|
||||
private final CommandExecutor commandExecutor;
|
||||
|
||||
private Coordinate lastClick = null;
|
||||
|
||||
public Window(CommandExecutor commandExecutor) {
|
||||
this.cells = new JLabel[8][8];
|
||||
this.displayText = new JLabel();
|
||||
this.commandExecutor = commandExecutor;
|
||||
setSize(800, 870);
|
||||
setVisible(true);
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
}
|
||||
|
||||
private CommandResult sendCommand(Command command) {
|
||||
return this.commandExecutor.executeCommand(command);
|
||||
}
|
||||
|
||||
private Color getCellColor(int x, int y) {
|
||||
return ((x + y) % 2 == 1) ? Color.BLACK : Color.WHITE;
|
||||
}
|
||||
|
||||
private void buildBoard() {
|
||||
JPanel content = new JPanel();
|
||||
JPanel grid = new JPanel(new GridLayout(8, 8));
|
||||
|
||||
content.add(this.displayText);
|
||||
content.add(grid);
|
||||
|
||||
setContentPane(content);
|
||||
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel label = new JLabel();
|
||||
label.setOpaque(true);
|
||||
label.setBackground(getCellColor(x, y));
|
||||
this.cells[x][y] = label;
|
||||
|
||||
final int xx = x;
|
||||
final int yy = y;
|
||||
|
||||
label.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
onCellClicked(xx, yy);
|
||||
}
|
||||
|
||||
});
|
||||
grid.add(label);
|
||||
}
|
||||
}
|
||||
updateBoard();
|
||||
}
|
||||
|
||||
private boolean isCellEmpty(int x, int y) {
|
||||
return pieceAt(x, y) == null;
|
||||
}
|
||||
|
||||
private Piece pieceAt(int x, int y) {
|
||||
GetPieceAtCommand command = new GetPieceAtCommand(new Coordinate(x, y));
|
||||
sendCommand(command);
|
||||
return command.getPiece();
|
||||
}
|
||||
|
||||
private void updateBoard() {
|
||||
PieceIcon pieceIcon = new PieceIcon();
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel cell = this.cells[x][y];
|
||||
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean previewMoves(int x, int y) {
|
||||
GetAllowedMovesCommand movesCommand = new GetAllowedMovesCommand(new Coordinate(x, y));
|
||||
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
|
||||
return false;
|
||||
|
||||
List<Coordinate> allowedMoves = movesCommand.getDestinations();
|
||||
if (allowedMoves.isEmpty())
|
||||
return false;
|
||||
|
||||
for (Coordinate destCoord : allowedMoves) {
|
||||
JLabel cell = this.cells[destCoord.getX()][destCoord.getY()];
|
||||
cell.setBackground(Color.CYAN);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void drawInvalid(Move move) {
|
||||
JLabel from = this.cells[move.getStart().getX()][move.getStart().getY()];
|
||||
JLabel to = this.cells[move.getFinish().getX()][move.getFinish().getY()];
|
||||
from.setBackground(Color.RED);
|
||||
to.setBackground(Color.RED);
|
||||
}
|
||||
|
||||
private void clearMoves() {
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel cell = this.cells[x][y];
|
||||
cell.setBackground(getCellColor(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onCellClicked(int x, int y) {
|
||||
clearMoves();
|
||||
if (this.lastClick == null) {
|
||||
if (isCellEmpty(x, y))
|
||||
return;
|
||||
if (!previewMoves(x, y))
|
||||
return;
|
||||
this.lastClick = new Coordinate(x, y);
|
||||
return;
|
||||
}
|
||||
if (!this.lastClick.equals(new Coordinate(x, y))) {
|
||||
Move move = new Move(lastClick, new Coordinate(x, y));
|
||||
if (sendCommand(new MoveCommand(move)) == CommandResult.NotAllowed) {
|
||||
drawInvalid(move);
|
||||
}
|
||||
}
|
||||
this.lastClick = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerTurn(chess.model.Color color) {
|
||||
this.displayText.setText("C'est au tour de " + color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void winnerIs(chess.model.Color color) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Victoire de " + color);
|
||||
this.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kingIsInCheck() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Échec !");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void kingIsInMat() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Échec et mat !");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void patSituation() {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Pat. Égalité !");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hasSurrendered(chess.model.Color color) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
JOptionPane.showMessageDialog(this, "Abandon de " + color);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gameStarted() {
|
||||
buildBoard();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void promotePawn(Coordinate pieceCoords) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
String result = null;
|
||||
|
||||
Object[] possibilities = new Object[PromoteType.values().length];
|
||||
int i = 0;
|
||||
for (PromoteType type : PromoteType.values()) {
|
||||
possibilities[i] = type.name();
|
||||
i++;
|
||||
}
|
||||
|
||||
while (result == null || result.isEmpty()) {
|
||||
result = (String) JOptionPane.showInputDialog(
|
||||
this,
|
||||
"Choose the type of piece to upgrade the pawn",
|
||||
"Promote Dialog",
|
||||
JOptionPane.PLAIN_MESSAGE,
|
||||
null,
|
||||
possibilities,
|
||||
possibilities[0]);
|
||||
}
|
||||
|
||||
PromoteType choosedType = null;
|
||||
|
||||
for (PromoteType type : PromoteType.values()) {
|
||||
if (type.name().equals(result)) {
|
||||
choosedType = type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (choosedType != null)
|
||||
sendCommand(new PromoteCommand(choosedType));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDisplay() {
|
||||
updateBoard();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user