Compare commits
4 Commits
ab309ae48a
...
f8ae19fee8
| Author | SHA1 | Date | |
|---|---|---|---|
| f8ae19fee8 | |||
| 1b22de17d8 | |||
| 24104fedf5 | |||
| 5b6fce11bc |
96
app/src/main/java/chess/view/DDDrender/BoardModelLoader.java
Normal file
96
app/src/main/java/chess/view/DDDrender/BoardModelLoader.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package chess.view.DDDrender;
|
package chess.view.DDDrender;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
|
|
||||||
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
|
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
|
||||||
import static org.lwjgl.opengl.GL11.glEnable;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -11,6 +9,7 @@ import org.joml.Vector2f;
|
|||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.view.DDDrender.shader.BoardShader;
|
import chess.view.DDDrender.shader.BoardShader;
|
||||||
@@ -20,13 +19,11 @@ 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 final Vector3f BLACK = new Vector3f(0.1f, 0.1f, 0.1f);
|
||||||
private static int BOARD_HEIGHT = 8;
|
private static final Vector3f WHITE = new Vector3f(0.7f, 0.7f, 0.7f);
|
||||||
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();
|
||||||
@@ -37,116 +34,32 @@ 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 {
|
||||||
DDDModel pieceModel = this.models.getModel(piece);
|
DDDModel pieceModel = this.models.getModel(piece);
|
||||||
Render(pieceModel, DDDPlacement.coordinates_to_vector(pos));
|
Render(pieceModel, piece.getColor() == Color.White ? WHITE : BLACK, DDDPlacement.coordinates_to_vector(pos),
|
||||||
|
piece.getColor() == Color.White ? 0.0f : 3.14f);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, Vector3f color, Vector2f position, float rotation) {
|
||||||
Vector3f realPos = new Vector3f(position.x(), 0, position.y());
|
Vector3f realPos = new Vector3f(position.x(), 0, position.y());
|
||||||
this.pieceShader.Start();
|
this.pieceShader.Start();
|
||||||
this.pieceShader.setModelTransform(new Matrix4f().translate(realPos));
|
this.pieceShader.setModelColor(color);
|
||||||
|
this.pieceShader.setModelTransform(new Matrix4f().translate(realPos).rotate(rotation, new Vector3f(0, 1, 0)));
|
||||||
Render(model);
|
Render(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -139,7 +139,9 @@ public class Window {
|
|||||||
renderer.Init();
|
renderer.Init();
|
||||||
|
|
||||||
// Set the clear color
|
// Set the clear color
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.4f, 0.4f, 0.6f, 1.0f);
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
|
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package chess.view.DDDrender.shader;
|
package chess.view.DDDrender.shader;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
public class PieceShader extends ShaderProgram {
|
public class PieceShader extends ShaderProgram {
|
||||||
|
|
||||||
@@ -18,8 +19,6 @@ public class PieceShader extends ShaderProgram {
|
|||||||
out vec3 toLightVector;
|
out vec3 toLightVector;
|
||||||
out vec3 surfaceNormal;
|
out vec3 surfaceNormal;
|
||||||
|
|
||||||
flat out vec3 pass_color;
|
|
||||||
|
|
||||||
void main(void){
|
void main(void){
|
||||||
vec4 worldPos = modelTransform * vec4(position, 1.0);
|
vec4 worldPos = modelTransform * vec4(position, 1.0);
|
||||||
|
|
||||||
@@ -27,7 +26,6 @@ public class PieceShader extends ShaderProgram {
|
|||||||
surfaceNormal = (modelTransform * vec4(normal, 0.0)).xyz;
|
surfaceNormal = (modelTransform * vec4(normal, 0.0)).xyz;
|
||||||
|
|
||||||
gl_Position = camMatrix * worldPos;
|
gl_Position = camMatrix * worldPos;
|
||||||
pass_color = position;
|
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
@@ -37,7 +35,7 @@ public class PieceShader extends ShaderProgram {
|
|||||||
in vec3 toLightVector;
|
in vec3 toLightVector;
|
||||||
in vec3 surfaceNormal;
|
in vec3 surfaceNormal;
|
||||||
|
|
||||||
flat in vec3 pass_color;
|
uniform vec3 modelColor = vec3(1, 1, 1);
|
||||||
|
|
||||||
out vec4 out_color;
|
out vec4 out_color;
|
||||||
|
|
||||||
@@ -49,7 +47,7 @@ public class PieceShader extends ShaderProgram {
|
|||||||
|
|
||||||
float brightness = diffuse;
|
float brightness = diffuse;
|
||||||
|
|
||||||
out_color = vec4(pass_color, 1.0) * brightness;
|
out_color = vec4(modelColor, 1.0) * brightness;
|
||||||
out_color.w = 1.0;
|
out_color.w = 1.0;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -57,6 +55,7 @@ public class PieceShader extends ShaderProgram {
|
|||||||
|
|
||||||
private int location_CamMatrix = 0;
|
private int location_CamMatrix = 0;
|
||||||
private int location_ModelTransform = 0;
|
private int location_ModelTransform = 0;
|
||||||
|
private int location_ModelColor = 0;
|
||||||
|
|
||||||
public PieceShader() {
|
public PieceShader() {
|
||||||
|
|
||||||
@@ -70,6 +69,7 @@ public class PieceShader extends ShaderProgram {
|
|||||||
protected void GetAllUniformLocation() {
|
protected void GetAllUniformLocation() {
|
||||||
location_CamMatrix = GetUniformLocation("camMatrix");
|
location_CamMatrix = GetUniformLocation("camMatrix");
|
||||||
location_ModelTransform = GetUniformLocation("modelTransform");
|
location_ModelTransform = GetUniformLocation("modelTransform");
|
||||||
|
location_ModelColor = GetUniformLocation("modelColor");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetCamMatrix(Matrix4f mat) {
|
public void SetCamMatrix(Matrix4f mat) {
|
||||||
@@ -79,4 +79,8 @@ public class PieceShader extends ShaderProgram {
|
|||||||
public void setModelTransform(Matrix4f mat) {
|
public void setModelTransform(Matrix4f mat) {
|
||||||
LoadMat4(location_ModelTransform, mat);
|
LoadMat4(location_ModelTransform, mat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setModelColor(Vector3f color) {
|
||||||
|
LoadVector(location_ModelColor, color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user