aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
package chess;
|
||||
|
||||
import chess.view.DDDrender.Window;
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.NewGameCommand;
|
||||
import chess.model.Game;
|
||||
import chess.view.DDDrender.DDDView;
|
||||
|
||||
public class OpenGLMain {
|
||||
public static void main(String[] args) {
|
||||
new Window().run();
|
||||
Game game = new Game();
|
||||
CommandExecutor commandExecutor = new CommandExecutor(game);
|
||||
|
||||
DDDView ddd = new DDDView(commandExecutor);
|
||||
commandExecutor.addListener(ddd);
|
||||
|
||||
commandExecutor.executeCommand(new NewGameCommand());
|
||||
|
||||
ddd.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Camera {
|
||||
private float pitch = 0.0f;
|
||||
|
||||
public Camera() {
|
||||
this.pos = new Vector3f(1, 1.0f, 0);
|
||||
this.pos = new Vector3f(2, 2.0f, 0);
|
||||
setRotation(0.0f, -3.14150f / 2.0f);
|
||||
}
|
||||
|
||||
|
||||
22
app/src/main/java/chess/view/DDDrender/DDDView.java
Normal file
22
app/src/main/java/chess/view/DDDrender/DDDView.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.event.GameAdaptator;
|
||||
|
||||
public class DDDView extends GameAdaptator{
|
||||
|
||||
private final CommandExecutor commandExecutor;
|
||||
private final Window window;
|
||||
private final Renderer renderer;
|
||||
|
||||
public DDDView(CommandExecutor commandExecutor) {
|
||||
this.commandExecutor = commandExecutor;
|
||||
this.renderer = new Renderer();
|
||||
this.window = new Window(commandExecutor, this.renderer);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
this.window.run();
|
||||
}
|
||||
|
||||
}
|
||||
73
app/src/main/java/chess/view/DDDrender/PieceModel.java
Normal file
73
app/src/main/java/chess/view/DDDrender/PieceModel.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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 PieceModel implements PieceVisitor<String> {
|
||||
|
||||
private static final String basePath = "3d/";
|
||||
private static final Map<String, DDDModel> cache = new HashMap<>();
|
||||
|
||||
public DDDModel getModel(Piece piece) throws IOException {
|
||||
if (piece == null)
|
||||
return null;
|
||||
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".glb";
|
||||
return getModel(path);
|
||||
}
|
||||
|
||||
private DDDModel getModel(String path) throws IOException {
|
||||
DDDModel model = cache.get(path);
|
||||
if (model != null)
|
||||
return model;
|
||||
|
||||
model = ModelLoader.loadModel(path);
|
||||
cache.put(path, model);
|
||||
return model;
|
||||
}
|
||||
|
||||
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 "knight";
|
||||
}
|
||||
|
||||
@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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
|
||||
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
|
||||
import static org.lwjgl.opengl.GL11.glEnable;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -11,6 +12,7 @@ import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Piece;
|
||||
import chess.view.DDDrender.shader.BoardShader;
|
||||
import chess.view.DDDrender.shader.PieceShader;
|
||||
import chess.view.DDDrender.shader.ShaderProgram;
|
||||
@@ -19,7 +21,7 @@ public class Renderer {
|
||||
private BoardShader boardShader;
|
||||
private PieceShader pieceShader;
|
||||
private VertexArray vao;
|
||||
private DDDModel yoda;
|
||||
private final PieceModel models;
|
||||
|
||||
private static int BOARD_WIDTH = 8;
|
||||
private static int BOARD_HEIGHT = 8;
|
||||
@@ -29,17 +31,14 @@ public class Renderer {
|
||||
public Renderer() {
|
||||
this.boardShader = new BoardShader();
|
||||
this.pieceShader = new PieceShader();
|
||||
this.models = new PieceModel();
|
||||
}
|
||||
|
||||
public void Init() {
|
||||
boardShader.LoadShader();
|
||||
pieceShader.LoadShader();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
InitBoard();
|
||||
try {
|
||||
this.yoda = ModelLoader.loadModel("3d/king_yoda.glb");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private float[] GetBoardPositions() {
|
||||
@@ -126,17 +125,22 @@ public class Renderer {
|
||||
this.vao.Unbind();
|
||||
}
|
||||
|
||||
public void RenderPiece(Piece piece, Coordinate pos) {
|
||||
try {
|
||||
DDDModel pieceModel = this.models.getModel(piece);
|
||||
Render(pieceModel, DDDPlacement.coordinates_to_vector(pos));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
Render(yoda, DDDPlacement.coordinates_to_vector(new Coordinate(i, j)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Render(DDDModel model, Vector2f position) {
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import chess.controller.CommandExecutor;
|
||||
import chess.controller.commands.GetPieceAtCommand;
|
||||
import chess.model.Coordinate;
|
||||
import chess.model.Piece;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.*;
|
||||
|
||||
@@ -21,14 +27,12 @@ public class Window {
|
||||
|
||||
private Renderer renderer;
|
||||
private Camera cam;
|
||||
private final CommandExecutor commandExecutor;
|
||||
|
||||
public Window() {
|
||||
public Window(CommandExecutor commandExecutor, Renderer renderer) {
|
||||
this.renderer = new Renderer();
|
||||
this.cam = new Camera();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Window().run();
|
||||
this.commandExecutor = commandExecutor;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -93,8 +97,31 @@ public class Window {
|
||||
}
|
||||
|
||||
private void render() {
|
||||
cam.move(0.001f, 0.001f);
|
||||
final float angle = 0.01f;
|
||||
float x = cam.getPos().x();
|
||||
float y = cam.getPos().z();
|
||||
cam.setPosition(new Vector3f(x * (float) Math.cos(angle) - y * (float) Math.sin(angle), 1.0f,
|
||||
x * (float) Math.sin(angle) + y * (float) Math.cos(angle)));
|
||||
renderer.Render(cam);
|
||||
renderPieces();
|
||||
}
|
||||
|
||||
private Piece pieceAt(Coordinate pos) {
|
||||
GetPieceAtCommand cmd = new GetPieceAtCommand(pos);
|
||||
this.commandExecutor.executeCommand(cmd);
|
||||
return cmd.getPiece();
|
||||
}
|
||||
|
||||
private void renderPieces() {
|
||||
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||
Coordinate pos = new Coordinate(i, j);
|
||||
Piece piece = pieceAt(pos);
|
||||
if (piece == null)
|
||||
continue;
|
||||
this.renderer.RenderPiece(pieceAt(pos), pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loop() {
|
||||
|
||||
@@ -18,7 +18,7 @@ public class PieceShader extends ShaderProgram {
|
||||
|
||||
void main(void){
|
||||
gl_Position = camMatrix * modelTransform * vec4(position, 1.0);
|
||||
pass_color = vec3(1, 0, 1);
|
||||
pass_color = position;
|
||||
}
|
||||
""";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user