From 5b6fce11bc45ab141717fca7b638d33c28e4c7fd Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 26 Apr 2025 12:20:00 +0200 Subject: [PATCH] refactor board model --- .../view/DDDrender/BoardModelLoader.java | 96 +++++++++++++++++++ .../java/chess/view/DDDrender/Renderer.java | 96 +------------------ .../java/chess/view/DDDrender/Window.java | 2 + 3 files changed, 101 insertions(+), 93 deletions(-) create mode 100644 app/src/main/java/chess/view/DDDrender/BoardModelLoader.java diff --git a/app/src/main/java/chess/view/DDDrender/BoardModelLoader.java b/app/src/main/java/chess/view/DDDrender/BoardModelLoader.java new file mode 100644 index 0000000..07b3f60 --- /dev/null +++ b/app/src/main/java/chess/view/DDDrender/BoardModelLoader.java @@ -0,0 +1,96 @@ +package chess.view.DDDrender; + +import org.joml.Vector3f; + +public class BoardModelLoader { + + 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; + + private static 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 static 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 static 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; + } + + public static VertexArray GetBoardModel() { + ElementBuffer eBuffer = new ElementBuffer(GetBoardIndicies()); + VertexArray 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); + + vao.Bind(); + vao.BindVertexBuffer(positionBuffer); + vao.BindVertexBuffer(colorBuffer); + vao.Unbind(); + return vao; + } +} diff --git a/app/src/main/java/chess/view/DDDrender/Renderer.java b/app/src/main/java/chess/view/DDDrender/Renderer.java index 2044733..084fac3 100644 --- a/app/src/main/java/chess/view/DDDrender/Renderer.java +++ b/app/src/main/java/chess/view/DDDrender/Renderer.java @@ -20,14 +20,9 @@ import chess.view.DDDrender.shader.ShaderProgram; public class Renderer { private BoardShader boardShader; private PieceShader pieceShader; - private VertexArray vao; + private VertexArray boardVao; private final PieceModel models; - 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.boardShader = new BoardShader(); this.pieceShader = new PieceShader(); @@ -37,93 +32,9 @@ public class Renderer { public void Init() { boardShader.LoadShader(); pieceShader.LoadShader(); - glEnable(GL_DEPTH_TEST); - InitBoard(); + this.boardVao = BoardModelLoader.GetBoardModel(); } - 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 RenderPiece(Piece piece, Coordinate pos) { try { @@ -135,12 +46,11 @@ public class Renderer { } public void Render(Camera cam) { - GL30.glClear(GL30.GL_DEPTH_BUFFER_BIT); this.boardShader.Start(); this.boardShader.SetCamMatrix(cam.getMatrix()); this.pieceShader.Start(); this.pieceShader.SetCamMatrix(cam.getMatrix()); - RenderVao(this.boardShader, vao); + RenderVao(this.boardShader, this.boardVao); } public void Render(DDDModel model, Vector2f position) { diff --git a/app/src/main/java/chess/view/DDDrender/Window.java b/app/src/main/java/chess/view/DDDrender/Window.java index 1e7a833..3e36e2e 100644 --- a/app/src/main/java/chess/view/DDDrender/Window.java +++ b/app/src/main/java/chess/view/DDDrender/Window.java @@ -141,6 +141,8 @@ public class Window { // Set the clear color glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glEnable(GL_DEPTH_TEST); + glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // Run the rendering loop until the user has attempted to close