From ee3d31abe15a36ece2dde018d89449ebed3f949d Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 2 Sep 2024 10:27:35 -0400 Subject: [PATCH] linearize screen framebuffer creation --- .../electrosphere/renderer/OpenGLState.java | 4 +- .../renderer/RenderingEngine.java | 12 +++-- .../framebuffer/FramebufferUtils.java | 52 +++++++++++++++++-- .../renderer/texture/Texture.java | 23 -------- 4 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/main/java/electrosphere/renderer/OpenGLState.java b/src/main/java/electrosphere/renderer/OpenGLState.java index b5954fda..eacd4e36 100644 --- a/src/main/java/electrosphere/renderer/OpenGLState.java +++ b/src/main/java/electrosphere/renderer/OpenGLState.java @@ -159,12 +159,12 @@ public class OpenGLState { * @param framebufferPointer the pointer to the framebuffer */ public void glBindFramebuffer(int framebufferType, int framebufferPointer){ - // if(DISABLE_CACHING || this.framebufferType != framebufferType || this.framebufferPointer != framebufferPointer){ + if(DISABLE_CACHING || this.framebufferType != framebufferType || this.framebufferPointer != framebufferPointer){ this.framebufferType = framebufferType; this.framebufferPointer = framebufferPointer; GL40.glBindFramebuffer(this.framebufferType,this.framebufferPointer); Globals.renderingEngine.checkError(); - // } + } } /** diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index a24d2bf0..a5489c0c 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -327,13 +327,15 @@ public class RenderingEngine { // screenTextureShaders = ShaderProgram.loadSpecificShader("/Shaders/screentexture/drawDepthBuffer/drawDepthBuffer.vs", "/Shaders/screentexture/drawDepthBuffer/drawDepthBuffer.fs"); //default framebuffer - defaultFramebuffer = new Framebuffer(0); + defaultFramebuffer = new Framebuffer(GL_DEFAULT_FRAMEBUFFER); + FramebufferUtils.framebufferUtilsTest(); + //generate framebuffers - screenTextureColor = FramebufferUtils.generateScreenTextureColorAlpha(openGLState, Globals.userSettings.getRenderResolutionX(), Globals.userSettings.getRenderResolutionY()); - screenTextureDepth = FramebufferUtils.generateScreenTextureDepth(openGLState, Globals.userSettings.getRenderResolutionX(), Globals.userSettings.getRenderResolutionY()); - screenFramebuffer = FramebufferUtils.generateScreenTextureFramebuffer(openGLState, Globals.userSettings.getRenderResolutionX(), Globals.userSettings.getRenderResolutionY(), screenTextureColor, screenTextureDepth); - openGLState.glBindFramebuffer(GL_FRAMEBUFFER, GL_DEFAULT_FRAMEBUFFER); + screenTextureColor = FramebufferUtils.generateScreenTextureColorAlpha(openGLState, Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT); + screenTextureDepth = FramebufferUtils.generateScreenTextureDepth(openGLState, Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT); + screenFramebuffer = FramebufferUtils.generateScreenTextureFramebuffer(openGLState, Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT, screenTextureColor, screenTextureDepth); + defaultFramebuffer.bind(openGLState); // glBindRenderbuffer(GL_RENDERBUFFER, GL_DEFAULT_RENDERBUFFER); Globals.renderingEngine.checkError(); diff --git a/src/main/java/electrosphere/renderer/framebuffer/FramebufferUtils.java b/src/main/java/electrosphere/renderer/framebuffer/FramebufferUtils.java index 447d6db0..362640eb 100644 --- a/src/main/java/electrosphere/renderer/framebuffer/FramebufferUtils.java +++ b/src/main/java/electrosphere/renderer/framebuffer/FramebufferUtils.java @@ -2,7 +2,6 @@ package electrosphere.renderer.framebuffer; import electrosphere.engine.Globals; import electrosphere.renderer.OpenGLState; -import electrosphere.renderer.RenderingEngine; import electrosphere.renderer.texture.Texture; import java.nio.IntBuffer; @@ -44,9 +43,56 @@ import static org.lwjgl.opengl.GL45.glNamedFramebufferReadBuffer; */ public class FramebufferUtils { + public static void framebufferUtilsTest(){ + OpenGLState openGLState = Globals.renderingEngine.getOpenGLState(); + int width = Globals.WINDOW_WIDTH; + int height = Globals.WINDOW_HEIGHT; + + // + //Texture 1 + // + Texture screenTextureColor = new Texture(); + screenTextureColor.glTexImage2D(openGLState, width, height, GL_RGBA, GL_UNSIGNED_BYTE); + screenTextureColor.setMinFilter(openGLState, GL_LINEAR); + screenTextureColor.setMagFilter(openGLState, GL_LINEAR); + //these make sure the texture actually clamps to the borders of the quad + screenTextureColor.setWrap(openGLState, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + screenTextureColor.setWrap(openGLState, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + //guarantees that the texture object has actually been created (calling gen buffers does not guarantee object creation) + screenTextureColor.bind(openGLState); + openGLState.glBindTexture(GL40.GL_TEXTURE_2D, Texture.DEFAULT_TEXTURE); + + screenTextureColor.checkStatus(openGLState); + + // + //Texture 2 + // + Texture screenTextureDepth = new Texture(); + screenTextureDepth.glTexImage2D(openGLState, width, height, GL_DEPTH_COMPONENT, GL_FLOAT); + + //guarantees that the texture object has actually been created (calling gen buffers does not guarantee object creation) + screenTextureDepth.bind(openGLState); + openGLState.glBindTexture(GL40.GL_TEXTURE_2D, Texture.DEFAULT_TEXTURE); + + screenTextureDepth.checkStatus(openGLState); + + + // + //Franebuffer + // + Framebuffer screenFramebuffer = new Framebuffer(); + //bind texture to fbo + screenFramebuffer.setMipMapLevel(0); + screenFramebuffer.attachTexture(openGLState,screenTextureColor); + screenFramebuffer.setDepthAttachment(openGLState,screenTextureDepth); + //check make sure compiled + screenFramebuffer.checkStatus(); + } + public static Texture generateScreenTextureColor(OpenGLState openGLState, int width, int height){ Texture texture = new Texture(); - texture.glTexImage2D(openGLState, width, height, GL_RGB, GL40.GL_RGB32F, GL_UNSIGNED_BYTE); + texture.glTexImage2D(openGLState, width, height, GL_RGB, GL_UNSIGNED_BYTE); texture.setMinFilter(openGLState, GL_LINEAR); texture.setMagFilter(openGLState, GL_LINEAR); //these make sure the texture actually clamps to the borders of the quad @@ -63,7 +109,7 @@ public class FramebufferUtils { public static Texture generateScreenTextureColorAlpha(OpenGLState openGLState, int width, int height){ Texture texture = new Texture(); - texture.glTexImage2D(openGLState, width, height, GL_RGBA, GL40.GL_RGB32F, GL_UNSIGNED_BYTE); + texture.glTexImage2D(openGLState, width, height, GL_RGBA, GL_UNSIGNED_BYTE); texture.setMinFilter(openGLState, GL_LINEAR); texture.setMagFilter(openGLState, GL_LINEAR); //these make sure the texture actually clamps to the borders of the quad diff --git a/src/main/java/electrosphere/renderer/texture/Texture.java b/src/main/java/electrosphere/renderer/texture/Texture.java index dd38d428..aa656ee5 100644 --- a/src/main/java/electrosphere/renderer/texture/Texture.java +++ b/src/main/java/electrosphere/renderer/texture/Texture.java @@ -398,29 +398,6 @@ public class Texture { Globals.renderingEngine.checkError(); } - /** - * Specifies a 2d image - * @param width The width of the image - * @param height The height of the image - * @param format The format of the pixels (ie GL_RGB, GL_RGBA, etc) - * @param internalformat The specific format type (ie GL_RGB32F, GL_RGBA32F, GL_R8, etc) - * @param datatype The data type of a single component of a pixel (ie GL_BYTE, GL_UNSIGNED_INT, etc) - */ - public void glTexImage2D(OpenGLState openGLState, int width, int height, int format, int internalformat, int datatype){ - //store provided values - this.width = width; - this.height = height; - this.pixelFormat = format; - this.datatype = datatype; - //static values going into call - int level = 0; - int border = 0; //this must be 0 according to docs - openGLState.glBindTexture(GL_TEXTURE_2D,texturePointer); - Globals.renderingEngine.checkError(); - GL40.glTexImage2D(GL_TEXTURE_2D, level, internalformat, width, height, border, format, datatype, MemoryUtil.NULL); - Globals.renderingEngine.checkError(); - } - /** * Specifies a 2d image * @param width The width of the image