// Copies source texture tex into this. protected void copyTexture( int texTarget, int texName, int texWidth, int texHeight, int x, int y, int w, int h, boolean scale) { if (tempFbo == null) { tempFbo = new FrameBuffer(parent, glWidth, glHeight); } // This texture is the color (destination) buffer of the FBO. tempFbo.setColorBuffer(this); tempFbo.disableDepthTest(); // FBO copy: pg.pushFramebuffer(); pg.setFramebuffer(tempFbo); if (scale) { // Rendering tex into "this", and scaling the source rectangle // to cover the entire destination region. pgl.drawTexture(texTarget, texName, texWidth, texHeight, x, y, w, h, 0, 0, width, height); } else { // Rendering tex into "this" but without scaling so the contents // of the source texture fall in the corresponding texels of the // destination. pgl.drawTexture(texTarget, texName, texWidth, texHeight, x, y, w, h, x, y, w, h); } pg.popFramebuffer(); updateTexels(x, y, w, h); }
/** Copy texture to pixels. Involves video memory to main memory transfer (slow). */ public void get(int[] pixels) { if (pixels == null) { throw new RuntimeException("Trying to copy texture to null pixels array"); } if (pixels.length != width * height) { throw new RuntimeException("Trying to copy texture to pixels array of " + "wrong size"); } if (tempFbo == null) { tempFbo = new FrameBuffer(parent, glWidth, glHeight); } // Attaching the texture to the color buffer of a FBO, binding the FBO and // reading the pixels from the current draw buffer (which is the color // buffer of the FBO). tempFbo.setColorBuffer(this); pg.pushFramebuffer(); pg.setFramebuffer(tempFbo); tempFbo.readPixels(); pg.popFramebuffer(); tempFbo.getPixels(pixels); convertToARGB(pixels); if (invertedX) flipArrayOnX(pixels, 1); if (invertedY) flipArrayOnY(pixels, 1); }
protected void createDepthBuffer() { if (screenFb) return; if (width == 0 || height == 0) { throw new RuntimeException("PFramebuffer: size undefined."); } pg.pushFramebuffer(); pg.setFramebuffer(this); glDepthBufferID = pg.createRenderBufferObject(); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glDepthBufferID); int glConst = PGL.GL_DEPTH_COMPONENT16; if (depthBits == 16) { glConst = PGL.GL_DEPTH_COMPONENT16; } else if (depthBits == 24) { glConst = PGL.GL_DEPTH_COMPONENT24; } else if (depthBits == 32) { glConst = PGL.GL_DEPTH_COMPONENT32; } if (multisample) { pgl.glRenderbufferStorageMultisample(PGL.GL_RENDERBUFFER, nsamples, glConst, width, height); } else { pgl.glRenderbufferStorage(PGL.GL_RENDERBUFFER, glConst, width, height); } pgl.glFramebufferRenderbuffer( PGL.GL_FRAMEBUFFER, PGL.GL_DEPTH_ATTACHMENT, PGL.GL_RENDERBUFFER, glDepthBufferID); pg.popFramebuffer(); }
protected void createStencilBuffer() { if (screenFb) return; if (width == 0 || height == 0) { throw new RuntimeException("PFramebuffer: size undefined."); } pg.pushFramebuffer(); pg.setFramebuffer(this); glStencilBufferID = pg.createRenderBufferObject(); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glStencilBufferID); int glConst = PGL.GL_STENCIL_INDEX1; if (stencilBits == 1) { glConst = PGL.GL_STENCIL_INDEX1; } else if (stencilBits == 4) { glConst = PGL.GL_STENCIL_INDEX4; } else if (stencilBits == 8) { glConst = PGL.GL_STENCIL_INDEX8; } if (multisample) { pgl.glRenderbufferStorageMultisample(PGL.GL_RENDERBUFFER, nsamples, glConst, width, height); } else { pgl.glRenderbufferStorage(PGL.GL_RENDERBUFFER, glConst, width, height); } pgl.glFramebufferRenderbuffer( PGL.GL_FRAMEBUFFER, PGL.GL_STENCIL_ATTACHMENT, PGL.GL_RENDERBUFFER, glStencilBufferID); pg.popFramebuffer(); }
protected void createPackedDepthStencilBuffer() { if (screenFb) return; if (width == 0 || height == 0) { throw new RuntimeException("PFramebuffer: size undefined."); } pg.pushFramebuffer(); pg.setFramebuffer(this); glDepthStencilBufferID = pg.createRenderBufferObject(); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glDepthStencilBufferID); if (multisample) { pgl.glRenderbufferStorageMultisample( PGL.GL_RENDERBUFFER, nsamples, PGL.GL_DEPTH24_STENCIL8, width, height); } else { pgl.glRenderbufferStorage(PGL.GL_RENDERBUFFER, PGL.GL_DEPTH24_STENCIL8, width, height); } pgl.glFramebufferRenderbuffer( PGL.GL_FRAMEBUFFER, PGL.GL_DEPTH_ATTACHMENT, PGL.GL_RENDERBUFFER, glDepthStencilBufferID); pgl.glFramebufferRenderbuffer( PGL.GL_FRAMEBUFFER, PGL.GL_STENCIL_ATTACHMENT, PGL.GL_RENDERBUFFER, glDepthStencilBufferID); pg.popFramebuffer(); }
public void setColorBuffers(PTexture[] textures, int n) { if (screenFb) return; if (numColorBuffers != PApplet.min(n, textures.length)) { throw new RuntimeException("Wrong number of textures to set the color buffers."); } for (int i = 0; i < numColorBuffers; i++) { colorBufferTex[i] = textures[i]; } pg.pushFramebuffer(); pg.setFramebuffer(this); // Making sure nothing is attached. for (int i = 0; i < numColorBuffers; i++) { pgl.glFramebufferTexture2D( PGL.GL_FRAMEBUFFER, PGL.GL_COLOR_ATTACHMENT0 + i, PGL.GL_TEXTURE_2D, 0, 0); } for (int i = 0; i < numColorBuffers; i++) { pgl.glFramebufferTexture2D( PGL.GL_FRAMEBUFFER, PGL.GL_COLOR_ATTACHMENT0 + i, colorBufferTex[i].glTarget, colorBufferTex[i].glID, 0); } pgl.validateFramebuffer(); pg.popFramebuffer(); }
public void clear() { pg.pushFramebuffer(); pg.setFramebuffer(this); pgl.glClearColor(0, 0, 0, 0); pgl.glClear(PGL.GL_COLOR_BUFFER_BIT | PGL.GL_DEPTH_BUFFER_BIT | PGL.GL_STENCIL_BUFFER_BIT); pg.popFramebuffer(); }
protected void createColorBufferMultisample() { if (screenFb) return; pg.pushFramebuffer(); pg.setFramebuffer(this); glColorBufferMultisampleID = pg.createRenderBufferObject(); pgl.glBindRenderbuffer(PGL.GL_RENDERBUFFER, glColorBufferMultisampleID); pgl.glRenderbufferStorageMultisample( PGL.GL_RENDERBUFFER, nsamples, PGL.GL_RGBA8, width, height); pgl.glFramebufferRenderbuffer( PGL.GL_FRAMEBUFFER, PGL.GL_COLOR_ATTACHMENT0, PGL.GL_RENDERBUFFER, glColorBufferMultisampleID); pg.popFramebuffer(); }
// Copies source texture tex into this. protected void copyTexture(Texture tex, int x, int y, int w, int h, boolean scale) { if (tex == null) { throw new RuntimeException("Source texture is null"); } if (tempFbo == null) { tempFbo = new FrameBuffer(glWidth, glHeight); } // This texture is the color (destination) buffer of the FBO. tempFbo.setColorBuffer(this); tempFbo.disableDepthTest(); // FBO copy: PGraphicsOpenGL.pushFramebuffer(); PGraphicsOpenGL.setFramebuffer(tempFbo); // Clear the color buffer to make sure that the alpha channel is set to // full transparency pgl.clearColor(0, 0, 0, 0); pgl.clear(PGL.COLOR_BUFFER_BIT); if (scale) { // Rendering tex into "this", and scaling the source rectangle // to cover the entire destination region. pgl.drawTexture( tex.glTarget, tex.glName, tex.glWidth, tex.glHeight, tempFbo.width, tempFbo.height, x, y, w, h, 0, 0, width, height); } else { // Rendering tex into "this" but without scaling so the contents // of the source texture fall in the corresponding texels of the // destination. pgl.drawTexture( tex.glTarget, tex.glName, tex.glWidth, tex.glHeight, tempFbo.width, tempFbo.height, x, y, w, h, x, y, w, h); } PGraphicsOpenGL.popFramebuffer(); updateTexels(x, y, w, h); }