diff --git a/com/danitheskunk/skunkworks/backends/gl/Framebuffer.java b/com/danitheskunk/skunkworks/backends/gl/Framebuffer.java index 518885c..44f55fc 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Framebuffer.java +++ b/com/danitheskunk/skunkworks/backends/gl/Framebuffer.java @@ -9,11 +9,40 @@ import static org.lwjgl.opengl.GL32.glFramebufferTexture; public class Framebuffer { private final int framebuffer; private final int framebufferTex; + private Vec2i size; public Framebuffer(Vec2i size) { framebuffer = glGenFramebuffers(); - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); framebufferTex = glGenTextures(); + setSize(size); + } + + public void bind() { + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + } + + public void bindTexture() { + glBindTexture(GL_TEXTURE_2D, framebufferTex); + } + + public static void unbind() { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + } + + public Vec2i getSize() { + return size; + } + + public void setSize(Vec2i size) { + this.size = size; + if(framebuffer != 0) { + glDeleteFramebuffers(framebuffer); + } + if(framebufferTex != 0) { + glDeleteTextures(framebufferTex); + } + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); glBindTexture(GL_TEXTURE_2D, framebufferTex); glTexImage2D(GL_TEXTURE_2D, 0, @@ -53,17 +82,4 @@ public class Framebuffer { 0 ); } - - public void bind() { - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); - } - - public void bindTexture() { - glBindTexture(GL_TEXTURE_2D, framebufferTex); - } - - public static void unbind() { - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - } } diff --git a/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java b/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java index ba4c3f4..2dabdc6 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java +++ b/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java @@ -39,13 +39,11 @@ public class Pipeline2D extends BasePipeline impl """; private final TextureAtlas atlas; private boolean debug; - private Vec2i size; private final Framebuffer framebuffer; - public Pipeline2D(Vec2i size, Framebuffer framebuffer, TextureAtlas atlas) { + public Pipeline2D(Framebuffer framebuffer, TextureAtlas atlas) { super(new Program(vertexSource, fragmentSource)); - this.size = size; - renderContext = new RenderContext(program, size); + renderContext = new RenderContext(program, framebuffer.getSize()); this.atlas = atlas; this.framebuffer = framebuffer; debug = false; @@ -54,7 +52,7 @@ public class Pipeline2D extends BasePipeline impl @Override public void finishFrame() { if(debug) { - renderContext.drawTextureRectangle(new Recti(Vec2i.ZERO, size), + renderContext.drawTextureRectangle(new Recti(Vec2i.ZERO, framebuffer.getSize()), atlas.getAtlasTexture(), false ); @@ -71,6 +69,7 @@ public class Pipeline2D extends BasePipeline impl use(); atlas.bind(); framebuffer.bind(); + var size = framebuffer.getSize(); glViewport(0, 0, size.getX(), size.getY()); atlas.update(); glClear(GL_DEPTH_BUFFER_BIT); diff --git a/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java b/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java index e30e898..da70183 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java +++ b/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java @@ -34,15 +34,13 @@ public class Pipeline3D extends BasePipeline } """; - private Vec2i size; private final Framebuffer framebuffer; private final TextureAtlas atlas; - public Pipeline3D(Vec2i size, Framebuffer framebuffer, TextureAtlas atlas) { + public Pipeline3D(Framebuffer framebuffer, TextureAtlas atlas) { super(new Program(vertexSource, fragmentSource)); renderContext = new RenderContext3D(program); renderContext.setTextureAtlas(atlas); - this.size = size; this.framebuffer = framebuffer; this.atlas = atlas; } @@ -57,6 +55,7 @@ public class Pipeline3D extends BasePipeline use(); atlas.bind(); framebuffer.bind(); + var size = framebuffer.getSize(); glViewport(0, 0, size.getX(), size.getY()); atlas.update(); glClear(GL_DEPTH_BUFFER_BIT); diff --git a/com/danitheskunk/skunkworks/backends/gl/Window.java b/com/danitheskunk/skunkworks/backends/gl/Window.java index 25d4a92..de8d00f 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Window.java +++ b/com/danitheskunk/skunkworks/backends/gl/Window.java @@ -47,13 +47,13 @@ public class Window extends BaseWindow { } """; private final Framebuffer framebuffer; + private final Framebuffer framebuffer2; private final Program programScaler; private final Vec2i size; private final boolean[] stateMouseClicked; private final boolean[] stateMouseDown; private final TextureAtlas textureAtlas; private final long window; - private boolean debug; private boolean shouldClose; private Vec2i windowSize; private final Pipeline2D pipeline2D; @@ -66,7 +66,6 @@ public class Window extends BaseWindow { "Unable to initialize GLFW"); glfwDefaultWindowHints(); - debug = false; this.size = size; windowSize = size; @@ -94,11 +93,12 @@ public class Window extends BaseWindow { glfwSetMouseButtonCallback(window, this::mouseButtonCallback); framebuffer = new Framebuffer(size); + framebuffer2 = new Framebuffer(size); textureAtlas = new TextureAtlas(); programScaler = new Program(vertexSourceScaler, fragmentSourceScaler); - pipeline2D = new Pipeline2D(size, framebuffer, textureAtlas); - pipeline3D = new Pipeline3D(size, framebuffer, textureAtlas); + pipeline2D = new Pipeline2D(framebuffer, textureAtlas); + pipeline3D = new Pipeline3D(framebuffer, textureAtlas); glProgramUniform2f(programScaler.getProgram(), @@ -238,7 +238,8 @@ public class Window extends BaseWindow { //glEnd(); - Framebuffer.unbind(); + //Framebuffer.unbind(); + framebuffer2.bind(); glViewport(0, 0, windowSize.getX(), windowSize.getY()); programScaler.use(); @@ -313,11 +314,39 @@ public class Window extends BaseWindow { glVertex2i(tlx2, tly2); glEnd(); + + Framebuffer.unbind(); + framebuffer2.bindTexture(); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + tlx1 = 0; + tly1 = 0; + tlx2 = windowSize.getX(); + tly2 = windowSize.getY(); + glBegin(GL_TRIANGLES); + //counterclockwise triangles + glVertexAttrib2f(texCoordIndex, 0, 1); + glVertex2i(tlx1, tly2); + glVertexAttrib2f(texCoordIndex, 1, 0); + glVertex2i(tlx2, tly1); + glVertexAttrib2f(texCoordIndex, 0, 0); + glVertex2i(tlx1, tly1); + + glVertexAttrib2f(texCoordIndex, 1, 0); + glVertex2i(tlx2, tly1); + glVertexAttrib2f(texCoordIndex, 0, 1); + glVertex2i(tlx1, tly2); + glVertexAttrib2f(texCoordIndex, 1, 1); + glVertex2i(tlx2, tly2); + glEnd(); + + } @Override public void setDebug(boolean on) { - debug = on; + pipeline2D.setDebug(on); } @Override