From 2cefc2e2289d46ad81482a3771b28978b703f424 Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 14 May 2025 22:35:34 -0400 Subject: [PATCH] work on fixing framebuffer bugs --- docs/src/progress/renderertodo.md | 1 + .../categories/ControlCategoryMenuNav.java | 2 +- .../electrosphere/renderer/OpenGLState.java | 1 + .../renderer/RenderingEngine.java | 10 +++++--- .../renderer/framebuffer/Framebuffer.java | 25 +++++++++++-------- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index ba5d426e..30b4d2c0 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1787,6 +1787,7 @@ Grid alignment offsets work Inventory state in non-creatures actually saves/loads to/from disk Fix virtual scrollable mouse alignment for events Remove warnings for fast loading +Work on debugging framebuffers diff --git a/src/main/java/electrosphere/controls/categories/ControlCategoryMenuNav.java b/src/main/java/electrosphere/controls/categories/ControlCategoryMenuNav.java index 25a22a0d..5e7b205c 100644 --- a/src/main/java/electrosphere/controls/categories/ControlCategoryMenuNav.java +++ b/src/main/java/electrosphere/controls/categories/ControlCategoryMenuNav.java @@ -49,7 +49,7 @@ public class ControlCategoryMenuNav { handler.addControl(INPUT_CODE_MENU_MOUSE_PRIMARY, new Control(ControlType.MOUSE_BUTTON,GLFW.GLFW_MOUSE_BUTTON_LEFT,false,"Mouse primary","")); handler.addControl(MENU_SCROLL, new Control(ControlType.MOUSE_SCROLL,0,false,"","")); handler.addControl(MENU_DRAG_START, new Control(ControlType.MOUSE_MOVEMENT,0,false,"","")); - handler.addControl(MENU_CAPTURE_SCREEN, new Control(ControlType.KEY,GLFW.GLFW_KEY_F4,true,"Screenshot","Takes a screenshot of the engine")); + handler.addControl(MENU_CAPTURE_SCREEN, new Control(ControlType.KEY,GLFW.GLFW_KEY_F12,true,"Screenshot","Takes a screenshot of the engine")); handler.addControl(MENU_DRAG_MANIPULATE, new Control(ControlType.MOUSE_BUTTON,GLFW.GLFW_MOUSE_BUTTON_1,false,"","")); } diff --git a/src/main/java/electrosphere/renderer/OpenGLState.java b/src/main/java/electrosphere/renderer/OpenGLState.java index 0af4b436..3aaa091f 100644 --- a/src/main/java/electrosphere/renderer/OpenGLState.java +++ b/src/main/java/electrosphere/renderer/OpenGLState.java @@ -115,6 +115,7 @@ public class OpenGLState { viewport.x = x; viewport.y = y; GL40.glViewport(0, 0, viewport.x, viewport.y); + Globals.renderingEngine.checkError(); } } diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index 01bfcccc..e5269ea0 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -144,11 +144,15 @@ public class RenderingEngine { static float aspectRatio = 1.0f; static float verticalFOV = 90.0f; - //the current state of the rendering pipeline + /** + * the current state of the rendering pipeline + */ static RenderPipelineState renderPipelineState = new RenderPipelineState(); - //the opengl state - static OpenGLState openGLState = new OpenGLState(); + /** + * the opengl state + */ + OpenGLState openGLState = new OpenGLState(); /** * The opengl context the rendering engine is running within diff --git a/src/main/java/electrosphere/renderer/framebuffer/Framebuffer.java b/src/main/java/electrosphere/renderer/framebuffer/Framebuffer.java index 30bbda0d..dc54fd71 100644 --- a/src/main/java/electrosphere/renderer/framebuffer/Framebuffer.java +++ b/src/main/java/electrosphere/renderer/framebuffer/Framebuffer.java @@ -20,7 +20,6 @@ import org.lwjgl.opengl.GL45; import static org.lwjgl.opengl.GL11.GL_NONE; import static org.lwjgl.opengl.GL11.GL_TEXTURE; import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER; -import static org.lwjgl.opengl.GL45.glCheckNamedFramebufferStatus; /** * Framebuffer object @@ -88,7 +87,7 @@ public class Framebuffer { * @param openGLState The opengl state */ public void bind(OpenGLState openGLState){ - openGLState.glBindFramebuffer(GL40.GL_FRAMEBUFFER, framebufferPointer); + openGLState.glBindFramebuffer(GL40.GL_FRAMEBUFFER, this.framebufferPointer); } /** @@ -99,7 +98,7 @@ public class Framebuffer { if(this.framebufferPointer == DEFAULT_FRAMEBUFFER_POINTER){ throw new Error("Pointer is the default framebuffer!"); } - return glCheckNamedFramebufferStatus(this.framebufferPointer,GL40.GL_FRAMEBUFFER) == GL40.GL_FRAMEBUFFER_COMPLETE; + return GL45.glCheckNamedFramebufferStatus(this.framebufferPointer,GL40.GL_FRAMEBUFFER) == GL40.GL_FRAMEBUFFER_COMPLETE; } @@ -158,7 +157,7 @@ public class Framebuffer { * Blocks the thread until the framebuffer has compiled */ public void blockUntilCompiled(){ - while(glCheckNamedFramebufferStatus(this.framebufferPointer,GL40.GL_FRAMEBUFFER) != GL40.GL_FRAMEBUFFER_UNDEFINED){ + while(GL45.glCheckNamedFramebufferStatus(this.framebufferPointer,GL40.GL_FRAMEBUFFER) != GL40.GL_FRAMEBUFFER_UNDEFINED){ try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException ex) { @@ -172,7 +171,7 @@ public class Framebuffer { * @return The status */ public String getStatus(){ - switch(glCheckNamedFramebufferStatus(this.framebufferPointer,GL40.GL_FRAMEBUFFER)){ + switch(GL45.glCheckNamedFramebufferStatus(this.framebufferPointer,GL40.GL_FRAMEBUFFER)){ case GL40.GL_FRAMEBUFFER_UNDEFINED: { return "The specified framebuffer is the default read or draw framebuffer, but the default framebuffer does not exist."; } @@ -292,16 +291,23 @@ public class Framebuffer { this.bind(openGLState); - GL40.glReadBuffer(GL40.GL_COLOR_ATTACHMENT0); - if(this.framebufferPointer == 0){ + if(this.framebufferPointer == Framebuffer.DEFAULT_FRAMEBUFFER_POINTER){ //this is the default framebuffer, read from backbuffer because it is default GL40.glReadBuffer(GL40.GL_BACK); + + //set viewport + openGLState.glViewport(Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT); } else if(attachTextureMap.containsKey(0)){ + //this is NOT the default framebuffer, read from the first color attachment + GL40.glReadBuffer(GL40.GL_COLOR_ATTACHMENT0); + + //set viewport Texture texture = attachTextureMap.get(0); width = texture.getWidth(); height = texture.getHeight(); + openGLState.glViewport(width, height); } else { - LoggerInterface.loggerRenderer.ERROR(new IllegalStateException("Tried to get pixels from a framebuffer that does not have a texture attached to attachment point 0.")); + LoggerInterface.loggerRenderer.ERROR(new Error("Tried to get pixels from a framebuffer that does not have a texture attached to attachment point 0.")); } //error check @@ -314,13 +320,12 @@ public class Framebuffer { //get pixel data try { - int bytesPerPixel = pixelFormatToBytes(pixelFormat,type); + int bytesPerPixel = Framebuffer.pixelFormatToBytes(pixelFormat,type); int bufferSize = width * height * bytesPerPixel; ByteBuffer buffer = BufferUtils.createByteBuffer(bufferSize); if(buffer == null || buffer.limit() < bufferSize){ throw new Error("Failed to create buffer!"); } - openGLState.glViewport(width, height); GL40.glReadPixels(offsetX, offsetY, width, height, pixelFormat, type, buffer); Globals.renderingEngine.checkError(); //convert to a buffered images