imgui #10

Merged
l merged 4 commits from imgui into 3dunstable 2025-05-16 13:35:38 +00:00
9 changed files with 95 additions and 33 deletions

View File

@@ -10,7 +10,7 @@ public class Camera {
public static final float zNear = 0.01f; public static final float zNear = 0.01f;
public static final float zFar = 1000.0f; public static final float zFar = 1000.0f;
private static final Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f); private static final Vector3f up = new Vector3f(0.0f, -1.0f, 0.0f);
private static final Vector3f center = new Vector3f(0.0f, 0.0f, 0.0f); private static final Vector3f center = new Vector3f(0.0f, 0.0f, 0.0f);
private final float distance = 1.5f; private final float distance = 1.5f;

View File

@@ -232,12 +232,10 @@ public class DDDView extends GameAdaptator implements GameListener {
} }
public void run() { public void run() {
/* // this.window.addRegularTask((delta) -> {
this.window.addRegularTask((delta) -> { // final float angle = 1f;
final float angle = 1f; // this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta);
this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta); // });
});
*/
this.window.run(); this.window.run();
// free OpenGL resources // free OpenGL resources

View File

@@ -1,9 +1,16 @@
package chess.view.DDDrender; package chess.view.DDDrender;
import static org.lwjgl.opengl.GL11.GL_DEPTH_COMPONENT;
import static org.lwjgl.opengl.GL11.GL_RGB;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.nio.IntBuffer;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Vector3f; import org.joml.Vector3f;
@@ -14,7 +21,7 @@ import chess.view.DDDrender.shader.BoardShader;
import chess.view.DDDrender.shader.PieceShader; import chess.view.DDDrender.shader.PieceShader;
import chess.view.DDDrender.shader.ShaderProgram; import chess.view.DDDrender.shader.ShaderProgram;
public class Renderer implements Closeable{ public class Renderer implements Closeable {
private BoardShader boardShader; private BoardShader boardShader;
private PieceShader pieceShader; private PieceShader pieceShader;
@@ -26,6 +33,32 @@ public class Renderer implements Closeable{
public void Init() { public void Init() {
boardShader.LoadShader(); boardShader.LoadShader();
pieceShader.LoadShader(); pieceShader.LoadShader();
// The frame buffer
int fbo = GL30.glGenFramebuffers();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
// The texture
int renderTexture = GL30.glGenTextures();
glBindTexture(GL_TEXTURE_2D, renderTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 800, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
GL30.glTexParameteri(GL_TEXTURE_2D, GL30.GL_TEXTURE_MAG_FILTER, GL30.GL_NEAREST);
GL30.glTexParameteri(GL_TEXTURE_2D, GL30.GL_TEXTURE_MIN_FILTER, GL30.GL_NEAREST);
// The depth buffer
int depthBuffer = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 800, 800);
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
depthBuffer);
// Set "renderedTexture" as our colour attachement #0
GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTexture, 0);
// Set the list of draw buffers.
int[] drawBuffers = { GL30.GL_COLOR_ATTACHMENT0 };
GL30.glDrawBuffers(drawBuffers);
} }
public void Update(Camera cam) { public void Update(Camera cam) {
@@ -35,10 +68,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

@@ -13,6 +13,7 @@ import chess.view.DDDrender.world.World;
import common.Signal1; import common.Signal1;
import imgui.ImFontConfig; import imgui.ImFontConfig;
import imgui.ImGui; import imgui.ImGui;
import imgui.ImVec2;
import imgui.gl3.ImGuiImplGl3; import imgui.gl3.ImGuiImplGl3;
import imgui.glfw.ImGuiImplGlfw; import imgui.glfw.ImGuiImplGlfw;
@@ -92,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());
@@ -162,8 +154,6 @@ public class Window implements Closeable {
GL.createCapabilities(); GL.createCapabilities();
setCallbacks();
initImGui(); initImGui();
renderer.Init(); renderer.Init();
@@ -176,9 +166,13 @@ public class Window implements Closeable {
} }
private void render(float delta, float aspectRatio) { private void render(float delta, float aspectRatio) {
cam.setAspectRatio(aspectRatio); cam.setAspectRatio(1.0f);
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 1);
glViewport(0, 0, 800, 800);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
renderer.Update(cam); renderer.Update(cam);
renderWorld(); renderWorld();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
} }
private void renderWorld() { private void renderWorld() {
@@ -198,11 +192,8 @@ public class Window implements Closeable {
} }
} }
private void checkCursor(int windowWidth, int windowHeight) { private void checkCursor(float cursorPosX, float cursorPosY, int windowWidth, int windowHeight) {
double x[] = new double[1]; Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f(cursorPosX, cursorPosY), windowWidth,
double y[] = new double[1];
glfwGetCursorPos(this.window, x, y);
Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f((float) x[0], (float) y[0]), windowWidth,
windowHeight); windowHeight);
Coordinate selectedCell = DDDPlacement.vectorToCoordinates(cursorPos); Coordinate selectedCell = DDDPlacement.vectorToCoordinates(cursorPos);
if (this.lastCell == null) { if (this.lastCell == null) {
@@ -216,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() {
@@ -226,6 +222,18 @@ public class Window implements Closeable {
ImGui.newFrame(); ImGui.newFrame();
} }
private void renderWindow() {
ImGui.showDemoWindow();
ImGui.begin("Hello");
ImGui.text("FPS : " + ImGui.getIO().getFramerate());
ImVec2 mousePos = ImGui.getIO().getMousePos();
ImVec2 framePos = ImGui.getCursorScreenPos();
checkCursor(mousePos.x - framePos.x, 800 - (mousePos.y - framePos.y), 800, 800);
ImGui.image(1, new ImVec2(800, 800));
ImGui.end();
}
private void loop() { private void loop() {
// Set the clear color // Set the clear color
@@ -255,7 +263,7 @@ public class Window implements Closeable {
float deltaTime = (float) (currentTime - lastTime); float deltaTime = (float) (currentTime - lastTime);
render(deltaTime, (float) width[0] / (float) height[0]); render(deltaTime, (float) width[0] / (float) height[0]);
// ImGui.showDemoWindow(); renderWindow();
ImGui.render(); ImGui.render();
implGl3.renderDrawData(ImGui.getDrawData()); implGl3.renderDrawData(ImGui.getDrawData());
@@ -267,8 +275,6 @@ public class Window implements Closeable {
// invoked during this call. // invoked during this call.
glfwPollEvents(); glfwPollEvents();
checkCursor(width[0], height[0]);
executeTasks(deltaTime); executeTasks(deltaTime);
glfwGetWindowSize(window, width, height); glfwGetWindowSize(window, width, height);

View File

@@ -46,7 +46,7 @@ public class BoardShader extends ShaderProgram {
in vec3 toCameraVector; in vec3 toCameraVector;
in vec3 surfaceNormal; in vec3 surfaceNormal;
out vec4 out_color; layout(location = 0) out vec4 out_color;
void main(void){ void main(void){
const float shineDamper = 10.0; const float shineDamper = 10.0;

View File

@@ -48,7 +48,7 @@ public class PieceShader extends ShaderProgram {
uniform vec3 modelColor; uniform vec3 modelColor;
out vec4 out_color; layout(location = 0) out vec4 out_color;
void main(void){ void main(void){
const float shineDamper = 10.0; const float shineDamper = 10.0;

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