yodaaaaaa
This commit is contained in:
@@ -8,7 +8,7 @@ public class Camera {
|
||||
// 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;
|
||||
public static final float zFar = 1000.0f;
|
||||
|
||||
private Vector3f pos;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class Camera {
|
||||
private float pitch = 0.0f;
|
||||
|
||||
public Camera() {
|
||||
this.pos = new Vector3f(0, 2.0f, 0);
|
||||
this.pos = new Vector3f(1, 1.0f, 0);
|
||||
setRotation(0.0f, -3.14150f / 2.0f);
|
||||
}
|
||||
|
||||
@@ -123,6 +123,6 @@ public class Camera {
|
||||
|
||||
return new Matrix4f()
|
||||
.perspective((float) (Math.toRadians(fov)), aspect, zNear, zFar)
|
||||
.lookAt(pos, forward, new Vector3f(0.0f, 1.0f, 0.0f));
|
||||
.lookAt(pos, new Vector3f(0.0f, 0, 0), new Vector3f(0.0f, 1.0f, 0.0f));
|
||||
}
|
||||
}
|
||||
|
||||
16
app/src/main/java/chess/view/DDDrender/DDDModel.java
Normal file
16
app/src/main/java/chess/view/DDDrender/DDDModel.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DDDModel {
|
||||
private final List<VertexArray> vaos;
|
||||
|
||||
public DDDModel(List<VertexArray> vaos) {
|
||||
this.vaos = vaos;
|
||||
}
|
||||
|
||||
public List<VertexArray> getVaos() {
|
||||
return vaos;
|
||||
}
|
||||
|
||||
}
|
||||
20
app/src/main/java/chess/view/DDDrender/DDDPlacement.java
Normal file
20
app/src/main/java/chess/view/DDDrender/DDDPlacement.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
|
||||
import chess.model.Coordinate;
|
||||
|
||||
class DDDPlacement {
|
||||
static public Vector2f coordinates_to_vector(Coordinate coo) {
|
||||
// float newX = switch (x) {
|
||||
// case 0 -> -1 + 0.125f;
|
||||
// case 1 -> -1 + 0.375f;
|
||||
// case 2 -> -1 + 0.625f;
|
||||
// case 3 -> -1 + 0.875f;
|
||||
|
||||
// default -> 0;
|
||||
// };
|
||||
return new Vector2f(-1.0f + 0.125f + coo.getX() * 0.250f, -1.0f + 0.125f + coo.getY() * 0.250f);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -87,11 +87,11 @@ public class ModelLoader {
|
||||
// textureCoords.add(buff.get(i).y());
|
||||
}
|
||||
|
||||
VertexBuffer positionVBO = new VertexBuffer(toFloatArray(positions), vertNumber);
|
||||
VertexBuffer positionVBO = new VertexBuffer(toFloatArray(positions), VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(VERTEX_POSITION_INDEX, VERTEX_SIZE, 0);
|
||||
VertexBuffer textureVBO = new VertexBuffer(toFloatArray(positions), vertNumber);
|
||||
VertexBuffer textureVBO = new VertexBuffer(toFloatArray(textureCoords), UV_SIZE);
|
||||
textureVBO.AddVertexAttribPointer(VERTEX_UV_INDEX, UV_SIZE, 0);
|
||||
VertexBuffer normalVBO = new VertexBuffer(toFloatArray(positions), vertNumber);
|
||||
VertexBuffer normalVBO = new VertexBuffer(toFloatArray(normals), VERTEX_SIZE);
|
||||
normalVBO.AddVertexAttribPointer(VERTEX_NORMAL_INDEX, VERTEX_SIZE, 0);
|
||||
|
||||
VertexArray vao = new VertexArray(new ElementBuffer(toIntArray(indicies)));
|
||||
@@ -115,7 +115,7 @@ public class ModelLoader {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<VertexArray> loadModel(String filename) throws IOException {
|
||||
public static DDDModel loadModel(String filename) throws IOException {
|
||||
InputStream input = AssetManager.getResource(filename);
|
||||
byte[] buffer = input.readAllBytes();
|
||||
ByteBuffer data = MemoryUtil.memCalloc(buffer.length);
|
||||
@@ -134,7 +134,7 @@ public class ModelLoader {
|
||||
|
||||
MemoryUtil.memFree(data);
|
||||
|
||||
return vertecies;
|
||||
return new DDDModel(vertecies);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,25 @@
|
||||
package chess.view.DDDrender;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_FLOAT;
|
||||
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import chess.model.Coordinate;
|
||||
import chess.view.DDDrender.shader.BoardShader;
|
||||
import chess.view.DDDrender.shader.PieceShader;
|
||||
import chess.view.DDDrender.shader.ShaderProgram;
|
||||
|
||||
public class Renderer {
|
||||
private BoardShader shader;
|
||||
private BoardShader boardShader;
|
||||
private PieceShader pieceShader;
|
||||
private VertexArray vao;
|
||||
private DDDModel yoda;
|
||||
|
||||
private static int BOARD_WIDTH = 8;
|
||||
private static int BOARD_HEIGHT = 8;
|
||||
@@ -17,12 +27,19 @@ public class Renderer {
|
||||
private static int SQUARE_VERTEX_COUNT = 4;
|
||||
|
||||
public Renderer() {
|
||||
this.shader = new BoardShader();
|
||||
this.boardShader = new BoardShader();
|
||||
this.pieceShader = new PieceShader();
|
||||
}
|
||||
|
||||
public void Init() {
|
||||
shader.LoadShader();
|
||||
boardShader.LoadShader();
|
||||
pieceShader.LoadShader();
|
||||
InitBoard();
|
||||
try {
|
||||
this.yoda = ModelLoader.loadModel("3d/king_yoda.glb");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private float[] GetBoardPositions() {
|
||||
@@ -110,13 +127,34 @@ public class Renderer {
|
||||
}
|
||||
|
||||
public void Render(Camera cam) {
|
||||
this.shader.Start();
|
||||
this.shader.SetCamMatrix(cam.getMatrix());
|
||||
RenderVao(vao);
|
||||
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 RenderVao(VertexArray vertexArray) {
|
||||
this.shader.Start();
|
||||
public void Render(DDDModel model, Vector2f position) {
|
||||
Vector3f realPos = new Vector3f(position.x(), 0, position.y());
|
||||
this.pieceShader.Start();
|
||||
this.pieceShader.setModelTransform(new Matrix4f().translate(realPos));
|
||||
Render(model);
|
||||
}
|
||||
|
||||
public void Render(DDDModel model) {
|
||||
for (int i = 0; i < model.getVaos().size(); i++) {
|
||||
VertexArray vao = model.getVaos().get(i);
|
||||
RenderVao(this.pieceShader, vao);
|
||||
}
|
||||
}
|
||||
|
||||
public void RenderVao(ShaderProgram shader, VertexArray vertexArray) {
|
||||
shader.Start();
|
||||
vertexArray.Bind();
|
||||
GL30.glDrawElements(GL30.GL_TRIANGLES, vertexArray.GetVertexCount(), GL_UNSIGNED_INT, 0);
|
||||
vertexArray.Unbind();
|
||||
|
||||
@@ -25,11 +25,6 @@ public class Window {
|
||||
public Window() {
|
||||
this.renderer = new Renderer();
|
||||
this.cam = new Camera();
|
||||
try {
|
||||
ModelLoader.loadModel("3d/bishop.glb");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -98,7 +93,7 @@ public class Window {
|
||||
}
|
||||
|
||||
private void render() {
|
||||
cam.rotate(0.01f, 0.01f);
|
||||
cam.move(0.001f, 0.001f);
|
||||
renderer.Render(cam);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package chess.view.DDDrender.shader;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class PieceShader extends ShaderProgram {
|
||||
|
||||
private static String vertexShader = """
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in vec2 uv;
|
||||
layout(location = 2) in vec3 normal;
|
||||
|
||||
uniform mat4 camMatrix;
|
||||
uniform mat4 modelTransform;
|
||||
|
||||
flat out vec3 pass_color;
|
||||
|
||||
void main(void){
|
||||
gl_Position = camMatrix * modelTransform * vec4(position, 1.0);
|
||||
pass_color = vec3(1, 0, 1);
|
||||
}
|
||||
""";
|
||||
|
||||
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;
|
||||
private int location_ModelTransform = 0;
|
||||
|
||||
public PieceShader() {
|
||||
|
||||
}
|
||||
|
||||
public void LoadShader() {
|
||||
super.LoadProgram(vertexShader, fragmentShader);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void GetAllUniformLocation() {
|
||||
location_CamMatrix = GetUniformLocation("camMatrix");
|
||||
location_ModelTransform = GetUniformLocation("modelTransform");
|
||||
}
|
||||
|
||||
public void SetCamMatrix(Matrix4f mat) {
|
||||
LoadMat4(location_CamMatrix, mat);
|
||||
}
|
||||
|
||||
public void setModelTransform(Matrix4f mat) {
|
||||
LoadMat4(location_ModelTransform, mat);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user