3

I am rendering to a frame buffer, and then I want to use that as a texture.

I can save the frame buffer to a file using glReadPixels so I am sure that I am rendering OK.

I can bind to fixed texture and render that OK.

But I cannot bind to the frame buffer to render that in place of the fixed texture.

 //setup:
 private void setupRenderToTexture() {
    fb = new int[numberOfBuffers];
    depthRb = new int[numberOfBuffers];
    renderTex = new int[numberOfBuffers];
    texBuffer = new IntBuffer[numberOfBuffers];

    // generate
    GLES20.glGenFramebuffers(fb.length, fb, 0);
    GLES20.glGenRenderbuffers(depthRb.length, depthRb, 0);
    GLES20.glGenTextures(renderTex.length, renderTex, 0);

    GLErrorHandler.checkGlError("glGenFramebuffers");

    for (int i = 0; i < fb.length; i++) {
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[i]);

        // generate color texture
        GLState.Instance.bindTexture(0, renderTex[i]);

        // parameters
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
        // GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
                GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
        // GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
                GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);

        // create it
        // create an empty intbuffer first?
        int[] fileData = new int[width * height];
        texBuffer[i] = IntBuffer.wrap(fileData);
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width,
                height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,
                texBuffer[i]);

        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
                renderTex[i], 0);

        // create render buffer and bind 16-bit depth buffer
        GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depthRb[i]);
        GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,
                GLES20.GL_DEPTH_COMPONENT16, width, height);
        GLErrorHandler.checkGlError("glRenderbufferStorage or above");
    }


    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}

Use the frame buffer:

GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]);

Save the frame buffer (for testing):

GLES20.glReadPixels(sourceX, sourceY, width, height, GL10.GL_RGBA,  
GL10.GL_UNSIGNED_BYTE, ib);

Then to render to screen:

GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture); //some simple texture works but:
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, renderTex[0]); //frame buffer texture does not work

All I get is a black screen if I bind to renderTex[0]

weston
  • 54,145
  • 21
  • 145
  • 203

1 Answers1

2

You are mis-using the FBO. You have to attach an empty texture so that it can render into it. See this page

Fabien R
  • 685
  • 1
  • 8
  • 24