/** * 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); }
/** * 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); }
public void drawTextureToOffScreenTextureUsingShader(int texId, int attachment, Program program) // public void drawTextureToOffScreenTextureUsingShader(Texture tex, int attachment, Program // program) { GL2 gl = getGL(); program.bind(); gl.glBindTexture(GL_TEXTURE_2D, texId); // gl.glBindTexture(GL_TEXTURE_2D, tex.getTextureObject()); gl.glDrawBuffer(attachment); gl.glEnable(GL_TEXTURE_2D); // gl.glActiveTexture(GL_TEXTURE0); gl.glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); gl.glViewport(0, 0, fboWidth, fboHeight); gl.glUniform1i(program.uniform("theTexture"), 0); // set projection to ortho gl.glMatrixMode(gl.GL_PROJECTION); gl.glPushMatrix(); { gl.glLoadIdentity(); Renderer.getInstance().glu.gluOrtho2D(0, fboWidth, fboHeight, 0); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glPushMatrix(); { gl.glLoadIdentity(); gl.glColor4f(1f, 1f, 1f, 1f); drawSquare(gl, 0, 0, fboWidth, fboHeight); } gl.glPopMatrix(); gl.glMatrixMode(gl.GL_PROJECTION); } gl.glPopMatrix(); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glDisable(GL_TEXTURE_2D); program.unbind(); }