refactor: fbo

This commit is contained in:
2025-05-17 12:38:29 +02:00
parent 0a1582cf07
commit c80805fc66
3 changed files with 153 additions and 58 deletions

View File

@@ -9,14 +9,18 @@ import org.lwjgl.system.*;
import chess.model.Coordinate;
import chess.view.AssetManager;
import chess.view.DDDrender.opengl.FrameBuffer;
import chess.view.DDDrender.world.Entity;
import chess.view.DDDrender.world.World;
import common.Signal1;
import imgui.ImFontConfig;
import imgui.ImGui;
import imgui.ImVec2;
import imgui.flag.ImGuiWindowFlags;
import imgui.gl3.ImGuiImplGl3;
import imgui.glfw.ImGuiImplGlfw;
import imgui.type.ImBoolean;
import imgui.type.ImInt;
import java.io.Closeable;
import java.io.IOException;
@@ -57,6 +61,9 @@ public class Window implements Closeable {
public final Signal0 OnImGuiTopRender = new Signal0();
public final Signal0 OnImGuiBottomRender = new Signal0();
private ImInt detailLevel = new ImInt(10);
private ImBoolean pixelatedFrame = new ImBoolean(true);
public Window(Renderer renderer, World world, Camera camera) {
this.renderer = new Renderer();
this.cam = camera;
@@ -153,17 +160,17 @@ public class Window implements Closeable {
(vidmode.height() - pHeight.get(0)) / 2);
glfwSetWindowSize(window, vidmode.width(), vidmode.height());
// Make the OpenGL context current
glfwMakeContextCurrent(window);
GL.createCapabilities();
initImGui();
renderer.Init(vidmode.width(), vidmode.height());
} // the stack frame is popped automatically
// Make the OpenGL context current
glfwMakeContextCurrent(window);
GL.createCapabilities();
initImGui();
renderer.Init();
// Enable v-sync
glfwSwapInterval(1);
@@ -172,13 +179,14 @@ public class Window implements Closeable {
}
private void render(float delta, float 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
final FrameBuffer frameBuffer = this.renderer.getFrameBuffer();
cam.setAspectRatio(frameBuffer.getAspectRatio());
frameBuffer.Bind();
frameBuffer.Clear();
renderer.Update(cam);
renderWorld();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
frameBuffer.Unbind();
}
private void renderWorld() {
@@ -231,13 +239,25 @@ public class Window implements Closeable {
private void renderWindow() {
ImGui.showDemoWindow();
ImGui.begin("Main Window");
final FrameBuffer frameBuffer = this.renderer.getFrameBuffer();
final int frameWidth = (int) ImGui.getIO().getDisplaySizeX();
final int frameHeight = (int) (ImGui.getIO().getDisplaySizeY() * 8.0f / 10.0f);
ImGui.setNextWindowSize(ImGui.getIO().getDisplaySize());
ImGui.setNextWindowPos(new ImVec2());
ImGui.begin("Main Window", ImGuiWindowFlags.NoDecoration);
this.OnImGuiTopRender.emit();
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));
checkCursor(mousePos.x - framePos.x, frameHeight - (mousePos.y - framePos.y), frameWidth, frameHeight);
ImGui.image(frameBuffer.getRenderTexture(), new ImVec2(frameWidth, frameHeight));
this.OnImGuiBottomRender.emit();
if(ImGui.sliderInt("Niveau de détail", detailLevel.getData(), 1, 10)){
frameBuffer.Resize((int) ((float) frameWidth * detailLevel.get() / 10.0f), (int) ((float) frameHeight * detailLevel.get() / 10.0f));
}
if (ImGui.checkbox("Minecraft", pixelatedFrame)) {
frameBuffer.SetPixelScaling(pixelatedFrame.get());
}
ImGui.end();
}
@@ -254,8 +274,6 @@ public class Window implements Closeable {
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
double lastTime = glfwGetTime();
int width[] = new int[1];
int height[] = new int[1];
glfwGetWindowSize(window, width, height);
@@ -266,23 +284,19 @@ public class Window implements Closeable {
newFrame();
double currentTime = glfwGetTime();
float deltaTime = (float) (currentTime - lastTime);
render(deltaTime, (float) width[0] / (float) height[0]);
render(ImGui.getIO().getDeltaTime(), (float) width[0] / (float) height[0]);
renderWindow();
ImGui.render();
implGl3.renderDrawData(ImGui.getDrawData());
lastTime = glfwGetTime();
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
executeTasks(deltaTime);
executeTasks(ImGui.getIO().getDeltaTime());
glfwGetWindowSize(window, width, height);
glViewport(0, 0, width[0], height[0]);