optimize rendering

This commit is contained in:
2025-05-16 12:14:44 +02:00
parent be6d8c35ba
commit b17c81769b
5 changed files with 35 additions and 16 deletions

View File

@@ -70,10 +70,10 @@ public class Renderer implements Closeable {
this.pieceShader.SetCamMatrix(cam); this.pieceShader.SetCamMatrix(cam);
} }
public void Render(DDDModel model, Vector3f color, Vector3f position, float rotation) { public void Render(DDDModel model, Vector3f color, Matrix4f transform) {
this.pieceShader.Start(); this.pieceShader.Start();
this.pieceShader.setModelColor(color); this.pieceShader.setModelColor(color);
this.pieceShader.setModelTransform(new Matrix4f().translate(position).rotate(rotation, new Vector3f(0, 1, 0))); this.pieceShader.setModelTransform(transform);
Render(model); Render(model);
} }

View File

@@ -93,15 +93,6 @@ public class Window implements Closeable {
ImGui.destroyContext(); ImGui.destroyContext();
} }
private void setCallbacks() {
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
if (this.lastCell.isValid())
this.OnCellClick.emit(this.lastCell);
}
});
}
private void initImGui() { private void initImGui() {
ImGui.setCurrentContext(ImGui.createContext()); ImGui.setCurrentContext(ImGui.createContext());
@@ -163,8 +154,6 @@ public class Window implements Closeable {
GL.createCapabilities(); GL.createCapabilities();
setCallbacks();
initImGui(); initImGui();
renderer.Init(); renderer.Init();
@@ -218,6 +207,11 @@ public class Window implements Closeable {
this.OnCellEnter.emit(selectedCell); this.OnCellEnter.emit(selectedCell);
this.lastCell = selectedCell; this.lastCell = selectedCell;
} }
if (ImGui.getIO().getMouseClicked(0)) {
if (this.lastCell != null && this.lastCell.isValid())
this.OnCellClick.emit(this.lastCell);
}
} }
private void newFrame() { private void newFrame() {
@@ -229,10 +223,10 @@ public class Window implements Closeable {
} }
private void renderWindow() { private void renderWindow() {
// ImGui.showDemoWindow(); ImGui.showDemoWindow();
ImGui.begin("Hello"); ImGui.begin("Hello");
ImGui.button("test"); ImGui.text("FPS : " + ImGui.getIO().getFramerate());
ImVec2 mousePos = ImGui.getIO().getMousePos(); ImVec2 mousePos = ImGui.getIO().getMousePos();
ImVec2 framePos = ImGui.getCursorScreenPos(); ImVec2 framePos = ImGui.getCursorScreenPos();
checkCursor(mousePos.x - framePos.x, 800 - (mousePos.y - framePos.y), 800, 800); checkCursor(mousePos.x - framePos.x, 800 - (mousePos.y - framePos.y), 800, 800);

View File

@@ -2,6 +2,7 @@ package chess.view.DDDrender.world;
import java.io.IOException; import java.io.IOException;
import org.joml.Matrix4f;
import org.joml.Vector3f; import org.joml.Vector3f;
import chess.model.Coordinate; import chess.model.Coordinate;
@@ -45,4 +46,9 @@ public class BoardEntity extends Entity {
vao.close(); vao.close();
} }
@Override
public Matrix4f getTransform() {
return null;
}
} }

View File

@@ -2,10 +2,14 @@ package chess.view.DDDrender.world;
import java.io.Closeable; import java.io.Closeable;
import org.joml.Matrix4f;
import chess.view.DDDrender.Renderer; import chess.view.DDDrender.Renderer;
public abstract class Entity implements Closeable{ public abstract class Entity implements Closeable{
public abstract void render(Renderer renderer); public abstract void render(Renderer renderer);
public abstract Matrix4f getTransform();
} }

View File

@@ -2,6 +2,7 @@ package chess.view.DDDrender.world;
import java.io.IOException; import java.io.IOException;
import org.joml.Matrix4f;
import org.joml.Vector3f; import org.joml.Vector3f;
import chess.view.DDDrender.DDDModel; import chess.view.DDDrender.DDDModel;
@@ -14,20 +15,24 @@ public class ModelEntity extends Entity {
protected float rotation; protected float rotation;
protected DDDModel model; protected DDDModel model;
private Matrix4f transform;
public ModelEntity(DDDModel model, Vector3f position, Vector3f color, float rotation) { public ModelEntity(DDDModel model, Vector3f position, Vector3f color, float rotation) {
this.position = position; this.position = position;
this.color = color; this.color = color;
this.rotation = rotation; this.rotation = rotation;
this.model = model; this.model = model;
updateTransform();
} }
@Override @Override
public void render(Renderer renderer) { public void render(Renderer renderer) {
renderer.Render(model, color, position, rotation); renderer.Render(model, color, transform);
} }
public void setPosition(Vector3f position) { public void setPosition(Vector3f position) {
this.position = position; this.position = position;
updateTransform();
} }
public void setColor(Vector3f color) { public void setColor(Vector3f color) {
@@ -36,6 +41,7 @@ public class ModelEntity extends Entity {
public void setRotation(float rotation) { public void setRotation(float rotation) {
this.rotation = rotation; this.rotation = rotation;
updateTransform();
} }
@Override @Override
@@ -43,4 +49,13 @@ public class ModelEntity extends Entity {
this.model.close(); this.model.close();
} }
private void updateTransform() {
this.transform = new Matrix4f().translate(this.position).rotate(this.rotation, new Vector3f(0, 1, 0));
}
@Override
public Matrix4f getTransform() {
return transform;
}
} }