OpenGL渲染到纹理 - 黑色屏幕

时间:2012-10-19 19:21:36

标签: android opengl-es-2.0 framebuffer

我正在渲染到帧缓冲区,然后我想将其用作纹理。

我可以使用glReadPixels将帧缓冲区保存到文件中,所以我确信我渲染正常。

我可以绑定到固定纹理并渲染确定。

但我无法绑定到帧缓冲区来代替固定纹理。

 //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);
}

使用帧缓冲区:

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

保存帧缓冲区(用于测试):

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

然后渲染到屏幕:

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

如果我绑定到renderTex[0]

,我得到的只是黑屏

1 个答案:

答案 0 :(得分:2)

你误用了FBO。您必须附加一个空纹理,以便它可以渲染到它。 见page