imgui #10
@@ -10,7 +10,7 @@ public class Camera {
|
||||
public static final float zNear = 0.01f;
|
||||
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 final float distance = 1.5f;
|
||||
|
||||
@@ -232,12 +232,10 @@ public class DDDView extends GameAdaptator implements GameListener {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
/*
|
||||
this.window.addRegularTask((delta) -> {
|
||||
final float angle = 1f;
|
||||
this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta);
|
||||
});
|
||||
*/
|
||||
// this.window.addRegularTask((delta) -> {
|
||||
// final float angle = 1f;
|
||||
// this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta);
|
||||
// });
|
||||
this.window.run();
|
||||
|
||||
// free OpenGL resources
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
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.glBindTexture;
|
||||
import static org.lwjgl.opengl.GL11.glTexImage2D;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
@@ -26,6 +33,32 @@ public class Renderer implements Closeable{
|
||||
public void Init() {
|
||||
boardShader.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) {
|
||||
@@ -35,10 +68,10 @@ public class Renderer implements Closeable{
|
||||
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.setModelColor(color);
|
||||
this.pieceShader.setModelTransform(new Matrix4f().translate(position).rotate(rotation, new Vector3f(0, 1, 0)));
|
||||
this.pieceShader.setModelTransform(transform);
|
||||
Render(model);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import chess.view.DDDrender.world.World;
|
||||
import common.Signal1;
|
||||
import imgui.ImFontConfig;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.gl3.ImGuiImplGl3;
|
||||
import imgui.glfw.ImGuiImplGlfw;
|
||||
|
||||
@@ -92,15 +93,6 @@ public class Window implements Closeable {
|
||||
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() {
|
||||
ImGui.setCurrentContext(ImGui.createContext());
|
||||
|
||||
@@ -162,8 +154,6 @@ public class Window implements Closeable {
|
||||
|
||||
GL.createCapabilities();
|
||||
|
||||
setCallbacks();
|
||||
|
||||
initImGui();
|
||||
|
||||
renderer.Init();
|
||||
@@ -176,9 +166,13 @@ public class Window implements Closeable {
|
||||
}
|
||||
|
||||
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);
|
||||
renderWorld();
|
||||
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
private void renderWorld() {
|
||||
@@ -198,11 +192,8 @@ public class Window implements Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkCursor(int windowWidth, int windowHeight) {
|
||||
double x[] = new double[1];
|
||||
double y[] = new double[1];
|
||||
glfwGetCursorPos(this.window, x, y);
|
||||
Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f((float) x[0], (float) y[0]), windowWidth,
|
||||
private void checkCursor(float cursorPosX, float cursorPosY, int windowWidth, int windowHeight) {
|
||||
Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f(cursorPosX, cursorPosY), windowWidth,
|
||||
windowHeight);
|
||||
Coordinate selectedCell = DDDPlacement.vectorToCoordinates(cursorPos);
|
||||
if (this.lastCell == null) {
|
||||
@@ -216,6 +207,11 @@ public class Window implements Closeable {
|
||||
this.OnCellEnter.emit(selectedCell);
|
||||
this.lastCell = selectedCell;
|
||||
}
|
||||
|
||||
if (ImGui.getIO().getMouseClicked(0)) {
|
||||
if (this.lastCell != null && this.lastCell.isValid())
|
||||
this.OnCellClick.emit(this.lastCell);
|
||||
}
|
||||
}
|
||||
|
||||
private void newFrame() {
|
||||
@@ -226,6 +222,18 @@ public class Window implements Closeable {
|
||||
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() {
|
||||
|
||||
// Set the clear color
|
||||
@@ -255,7 +263,7 @@ public class Window implements Closeable {
|
||||
float deltaTime = (float) (currentTime - lastTime);
|
||||
render(deltaTime, (float) width[0] / (float) height[0]);
|
||||
|
||||
// ImGui.showDemoWindow();
|
||||
renderWindow();
|
||||
ImGui.render();
|
||||
implGl3.renderDrawData(ImGui.getDrawData());
|
||||
|
||||
@@ -267,8 +275,6 @@ public class Window implements Closeable {
|
||||
// invoked during this call.
|
||||
glfwPollEvents();
|
||||
|
||||
checkCursor(width[0], height[0]);
|
||||
|
||||
executeTasks(deltaTime);
|
||||
|
||||
glfwGetWindowSize(window, width, height);
|
||||
|
||||
@@ -46,7 +46,7 @@ public class BoardShader extends ShaderProgram {
|
||||
in vec3 toCameraVector;
|
||||
in vec3 surfaceNormal;
|
||||
|
||||
out vec4 out_color;
|
||||
layout(location = 0) out vec4 out_color;
|
||||
|
||||
void main(void){
|
||||
const float shineDamper = 10.0;
|
||||
|
||||
@@ -48,7 +48,7 @@ public class PieceShader extends ShaderProgram {
|
||||
|
||||
uniform vec3 modelColor;
|
||||
|
||||
out vec4 out_color;
|
||||
layout(location = 0) out vec4 out_color;
|
||||
|
||||
void main(void){
|
||||
const float shineDamper = 10.0;
|
||||
|
||||
@@ -2,6 +2,7 @@ package chess.view.DDDrender.world;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import chess.model.Coordinate;
|
||||
@@ -45,4 +46,9 @@ public class BoardEntity extends Entity {
|
||||
vao.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matrix4f getTransform() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@ package chess.view.DDDrender.world;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import chess.view.DDDrender.Renderer;
|
||||
|
||||
public abstract class Entity implements Closeable{
|
||||
|
||||
public abstract void render(Renderer renderer);
|
||||
|
||||
public abstract Matrix4f getTransform();
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package chess.view.DDDrender.world;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import chess.view.DDDrender.DDDModel;
|
||||
@@ -14,20 +15,24 @@ public class ModelEntity extends Entity {
|
||||
protected float rotation;
|
||||
protected DDDModel model;
|
||||
|
||||
private Matrix4f transform;
|
||||
|
||||
public ModelEntity(DDDModel model, Vector3f position, Vector3f color, float rotation) {
|
||||
this.position = position;
|
||||
this.color = color;
|
||||
this.rotation = rotation;
|
||||
this.model = model;
|
||||
updateTransform();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Renderer renderer) {
|
||||
renderer.Render(model, color, position, rotation);
|
||||
renderer.Render(model, color, transform);
|
||||
}
|
||||
|
||||
public void setPosition(Vector3f position) {
|
||||
this.position = position;
|
||||
updateTransform();
|
||||
}
|
||||
|
||||
public void setColor(Vector3f color) {
|
||||
@@ -36,6 +41,7 @@ public class ModelEntity extends Entity {
|
||||
|
||||
public void setRotation(float rotation) {
|
||||
this.rotation = rotation;
|
||||
updateTransform();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,4 +49,13 @@ public class ModelEntity extends Entity {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user