diff --git a/core/src/processing/opengl/FrameBuffer.java b/core/src/processing/opengl/FrameBuffer.java index 6cd05c893..31d50f559 100644 --- a/core/src/processing/opengl/FrameBuffer.java +++ b/core/src/processing/opengl/FrameBuffer.java @@ -198,16 +198,16 @@ public class FrameBuffer implements PConstants { } public void copy(FrameBuffer dest, int mask) { - pgl.bindFramebuffer(PGL.READ_FRAMEBUFFER, this.glFbo); - pgl.bindFramebuffer(PGL.DRAW_FRAMEBUFFER, dest.glFbo); + pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, this.glFbo); + pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, dest.glFbo); pgl.blitFramebuffer(0, 0, this.width, this.height, 0, 0, dest.width, dest.height, mask, PGL.NEAREST); - pgl.bindFramebuffer(PGL.READ_FRAMEBUFFER, pg.getCurrentFB().glFbo); - pgl.bindFramebuffer(PGL.DRAW_FRAMEBUFFER, pg.getCurrentFB().glFbo); + pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, pg.getCurrentFB().glFbo); + pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, pg.getCurrentFB().glFbo); } public void bind() { - pgl.bindFramebuffer(PGL.FRAMEBUFFER, glFbo); + pgl.bindFramebufferImpl(PGL.FRAMEBUFFER, glFbo); } public void disableDepthTest() { diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 42e338330..d00798c6a 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -511,8 +511,8 @@ public abstract class PGL { protected void syncBackTexture() { if (usingFrontTex) needSepFrontTex = true; if (1 < numSamples) { - bindFramebuffer(READ_FRAMEBUFFER, glMultiFbo.get(0)); - bindFramebuffer(DRAW_FRAMEBUFFER, glColorFbo.get(0)); + bindFramebufferImpl(READ_FRAMEBUFFER, glMultiFbo.get(0)); + bindFramebufferImpl(DRAW_FRAMEBUFFER, glColorFbo.get(0)); blitFramebuffer(0, 0, fboWidth, fboHeight, 0, 0, fboWidth, fboHeight, COLOR_BUFFER_BIT, NEAREST); @@ -529,12 +529,12 @@ public abstract class PGL { if (needFBOLayer(clear0)) { if (!fboLayerCreated) createFBOLayer(); - bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0)); + bindFramebufferImpl(FRAMEBUFFER, glColorFbo.get(0)); framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, TEXTURE_2D, glColorTex.get(backTex), 0); if (1 < numSamples) { - bindFramebuffer(FRAMEBUFFER, glMultiFbo.get(0)); + bindFramebufferImpl(FRAMEBUFFER, glMultiFbo.get(0)); } if (firstFrame) { @@ -580,7 +580,7 @@ public abstract class PGL { syncBackTexture(); // Draw the contents of the back texture to the screen framebuffer. - bindFramebuffer(FRAMEBUFFER, 0); + bindFramebufferImpl(FRAMEBUFFER, 0); clearDepth(1); clearColor(0, 0, 0, 0); @@ -670,14 +670,14 @@ public abstract class PGL { frontTex = 1; genFramebuffers(1, glColorFbo); - bindFramebuffer(FRAMEBUFFER, glColorFbo.get(0)); + bindFramebufferImpl(FRAMEBUFFER, glColorFbo.get(0)); framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0, TEXTURE_2D, glColorTex.get(backTex), 0); if (multisample) { // Creating multisampled FBO genFramebuffers(1, glMultiFbo); - bindFramebuffer(FRAMEBUFFER, glMultiFbo.get(0)); + bindFramebufferImpl(FRAMEBUFFER, glMultiFbo.get(0)); // color render buffer... genRenderbuffers(1, glColorBuf); @@ -766,7 +766,7 @@ public abstract class PGL { clearColor(r, g, b, a); clear(DEPTH_BUFFER_BIT | STENCIL_BUFFER_BIT | COLOR_BUFFER_BIT); - bindFramebuffer(FRAMEBUFFER, 0); + bindFramebufferImpl(FRAMEBUFFER, 0); fboLayerCreated = true; } @@ -2559,11 +2559,11 @@ public abstract class PGL { // to glReadPixels() should be done in readPixelsImpl(). public void readPixels(int x, int y, int width, int height, int format, int type, Buffer buffer){ - boolean needEndBegin = format != STENCIL_INDEX && - format != DEPTH_COMPONENT && format != DEPTH_STENCIL; - if (needEndBegin) pg.beginReadPixels(); + boolean pgCall = format != STENCIL_INDEX && + format != DEPTH_COMPONENT && format != DEPTH_STENCIL; + if (pgCall) pg.beginReadPixels(); readPixelsImpl(x, y, width, height, format, type, buffer); - if (needEndBegin) pg.endReadPixels(); + if (pgCall) pg.endReadPixels(); } protected abstract void readPixelsImpl(int x, int y, int width, int height, int format, int type, Buffer buffer); @@ -2745,7 +2745,13 @@ public abstract class PGL { // Framebuffers Objects - public abstract void bindFramebuffer(int target, int framebuffer); + public void bindFramebuffer(int target, int framebuffer) { + pg.beginBindFramebuffer(target, framebuffer); + bindFramebufferImpl(target, framebuffer); + pg.endBindFramebuffer(target, framebuffer); + } + protected abstract void bindFramebufferImpl(int target, int framebuffer); + public abstract void deleteFramebuffers(int n, IntBuffer framebuffers); public abstract void genFramebuffers(int n, IntBuffer framebuffers); public abstract void bindRenderbuffer(int target, int renderbuffer); diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 0c5102853..bb5e75f6d 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -1812,11 +1812,24 @@ public class PGraphicsOpenGL extends PGraphics { } } - public void beginReadPixels() { + protected void beginBindFramebuffer(int target, int framebuffer) { + // Actually, nothing to do here. + } + + protected void endBindFramebuffer(int target, int framebuffer) { + if (framebuffer == 0 && currentFramebuffer != null && + currentFramebuffer.glFbo != 0) { + // The user is setting the framebuffer to 0 (screen buffer), but the + // renderer is rendering to an offscreen buffer. + currentFramebuffer.bind(); + } + } + + protected void beginReadPixels() { beginPixelsOp(OP_READ); } - public void endReadPixels() { + protected void endReadPixels() { endPixelsOp(); } diff --git a/core/src/processing/opengl/PJOGL.java b/core/src/processing/opengl/PJOGL.java index e70af60e2..0bff30395 100644 --- a/core/src/processing/opengl/PJOGL.java +++ b/core/src/processing/opengl/PJOGL.java @@ -2414,7 +2414,7 @@ public class PJOGL extends PGL { // Framebuffers Objects @Override - public void bindFramebuffer(int target, int framebuffer) { + protected void bindFramebufferImpl(int target, int framebuffer) { gl.glBindFramebuffer(target, framebuffer); } diff --git a/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java b/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java index 7be2db1e0..3b3ef6aeb 100644 --- a/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java +++ b/java/libraries/lwjgl/src/processing/lwjgl/PLWJGL.java @@ -1925,7 +1925,7 @@ public class PLWJGL extends PGL { // Framebuffers Objects - public void bindFramebuffer(int target, int framebuffer) { + protected void bindFramebufferImpl(int target, int framebuffer) { GL30.glBindFramebuffer(target, framebuffer); }