/** * Binds a single color texture of this FBO for rendering. * * <p>You can write to this texture by setting either `gl_FragColor` or `gl_FragData[0]` from your * shaders. While bound for rendering, the specified texture cannot be used for texturing * operations. */ public void bindOne(GL2 gl, int colorTextureIndex) throws OpenGLException { /* Sanity check. */ if (colorTextureIndex < 0 || colorTextureIndex >= getColorTextureCount()) { throw new AssertionError( "Color texture index out of range: required that 0 <= " + colorTextureIndex + " < " + getColorTextureCount() + "."); } /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Set draw buffer to the requested color attachment. */ gl.glDrawBuffer(GL2.GL_COLOR_ATTACHMENT0 + colorTextureIndex); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
/** * Binds several specified color textures of this FBO for rendering. * * <p>You can write to them by using the `gl_FragData[]` array from your shaders. While bound for * rendering, the specified textures cannot be used for texturing operations. */ public void bindSome(GL2 gl, int indices[]) throws OpenGLException { /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Set draw buffers to all color attachments. */ int bindings[] = new int[indices.length]; for (int i = 0; i < indices.length; ++i) { if (indices[i] >= getColorTextureCount()) { throw new OpenGLException( "FBO bind index " + indices[i] + " out of range; only " + getColorTextureCount() + " color textures."); } bindings[i] = GL2.GL_COLOR_ATTACHMENT0 + indices[i]; } gl.glDrawBuffers(indices.length, bindings, 0); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
/** * Binds a given texture to the first color target This is useful when we want to render to a * dynamic cube map texture. * * <p>You can write to them by using the `gl_FragData[]` array from your shaders. While bound for * rendering, the specified textures cannot be used for texturing operations. */ public void bindGiven(GL2 gl, int textureTarget, int textureHandle, int colorTextureIndex) throws OpenGLException { /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Attach the given texture */ gl.glFramebufferTexture2D( GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0 + colorTextureIndex, textureTarget, textureHandle, 0); /* Set draw buffer to the requested color attachment. */ gl.glDrawBuffer(GL2.GL_COLOR_ATTACHMENT0 + colorTextureIndex); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }
/** Unbinds all color textures for rendering. You can now use them for texturing operations. */ public void unbind(GL2 gl) throws OpenGLException { if (mIsBound) { gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, mPreviousBinding[0]); gl.glPopAttrib(); mIsBound = false; OpenGLException.checkOpenGLError(gl); } }
/** * Binds all color textures of this FBO for rendering. * * <p>You can write to them by using the `gl_FragData[]` array from your shaders. While bound for * rendering, the specified textures cannot be used for texturing operations. */ public void bindAll(GL2 gl) throws OpenGLException { /* Save state and adjust viewport if this is the first bind. */ if (!mIsBound) { gl.glPushAttrib(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_VIEWPORT_BIT); gl.glGetIntegerv(GL2.GL_FRAMEBUFFER_BINDING, mPreviousBinding, 0); gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, getHandle()); gl.glViewport(0, 0, mWidth, mHeight); mIsBound = true; } /* Set draw buffers to all color attachments. */ int bindings[] = new int[getColorTextureCount()]; for (int i = 0; i < getColorTextureCount(); ++i) { bindings[i] = GL2.GL_COLOR_ATTACHMENT0 + i; } gl.glDrawBuffers(getColorTextureCount(), bindings, 0); /* Make sure it worked. */ OpenGLException.checkOpenGLError(gl); }