refactor board model

This commit is contained in:
2025-04-26 12:20:00 +02:00
parent ab309ae48a
commit 5b6fce11bc
3 changed files with 101 additions and 93 deletions

View File

@@ -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;
}
}

View File

@@ -20,14 +20,9 @@ import chess.view.DDDrender.shader.ShaderProgram;
public class Renderer { public class Renderer {
private BoardShader boardShader; private BoardShader boardShader;
private PieceShader pieceShader; private PieceShader pieceShader;
private VertexArray vao; private VertexArray boardVao;
private final PieceModel models; 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() { public Renderer() {
this.boardShader = new BoardShader(); this.boardShader = new BoardShader();
this.pieceShader = new PieceShader(); this.pieceShader = new PieceShader();
@@ -37,93 +32,9 @@ public class Renderer {
public void Init() { public void Init() {
boardShader.LoadShader(); boardShader.LoadShader();
pieceShader.LoadShader(); pieceShader.LoadShader();
glEnable(GL_DEPTH_TEST); this.boardVao = BoardModelLoader.GetBoardModel();
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 RenderPiece(Piece piece, Coordinate pos) { public void RenderPiece(Piece piece, Coordinate pos) {
try { try {
@@ -135,12 +46,11 @@ public class Renderer {
} }
public void Render(Camera cam) { public void Render(Camera cam) {
GL30.glClear(GL30.GL_DEPTH_BUFFER_BIT);
this.boardShader.Start(); this.boardShader.Start();
this.boardShader.SetCamMatrix(cam.getMatrix()); this.boardShader.SetCamMatrix(cam.getMatrix());
this.pieceShader.Start(); this.pieceShader.Start();
this.pieceShader.SetCamMatrix(cam.getMatrix()); this.pieceShader.SetCamMatrix(cam.getMatrix());
RenderVao(this.boardShader, vao); RenderVao(this.boardShader, this.boardVao);
} }
public void Render(DDDModel model, Vector2f position) { public void Render(DDDModel model, Vector2f position) {

View File

@@ -141,6 +141,8 @@ public class Window {
// Set the clear color // Set the clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f); glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
// Run the rendering loop until the user has attempted to close // Run the rendering loop until the user has attempted to close