OpenGL ES 2.0 - 渲染纹理全黑

时间:2016-04-03 16:24:22

标签: java android opengl-es-2.0 fragment-shader

我有一个渲染到屏幕没有问题的场景,但是当我尝试将其渲染为纹理时,它将全部为黑色。我想在准备纹理时我错过了一些阶段。

代码:

    int[] FBO_main = new int[1];
    int[] textureId = new int[1];
    int[] renderBufferId = new int[1]; 

    // create framebuffers
    GLES20.glGenFramebuffers(FBO_main.length, FBO_main, 0);

    // create texture object
    GLES20.glGenTextures(1, textureId, 0);

    // create render buffer
    GLES20.glGenRenderbuffers(1, renderBufferId, 0);

    // Bind Frame buffer
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]);

    // Bind texture
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);

    int[] buf = new int[800*400];
    IntBuffer texBuffer;
    texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer();

    // Texture parameters
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0,
            GLES20.GL_RGBA, 800, 400, 0,
            GLES20.GL_RGBA,
            GLES20.GL_UNSIGNED_BYTE, texBuffer);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameterf(
            GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameterf(
            GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(
            GLES20.GL_TEXTURE_2D,
            GLES20.GL_GENERATE_MIPMAP_HINT,
            GLES20.GL_TRUE); // automatic mipmap

    // Bind render buffer and define buffer dimension
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBufferId[0]);
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, 800, 400);

    // Attach texture FBO color attachment
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textureId[0], 0);

    // Attach render buffer to depth attachment
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderBufferId[0]);

    // Reset
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, 0);
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

    // Setup render to texture
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]);
    GLES20.glViewport(0, 0, 800, 400);

    // Draw gamespecific
    onDrawGame(timediff, time);

    // Draw to buffer
    GLES20.glFlush();

    // Use buffer as input texture
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
    GLES20.glUniform1i(GLES20.glGetUniformLocation(cube.program, "sampler_prev"), 0);

    // switch to screen output
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

    // Draw gamespecific
    onDrawGame(timediff, time);

    // Draw
    GLES20.glFlush();

知道我缺少什么吗?

1 个答案:

答案 0 :(得分:1)

我没有在很长一段时间内使用过opengl,但我认为你应该将GL_RGBA用于internalFormat和格式,并使用GL_UNSIGNED_BYTE类型并为数据传递null。

GLES20.glTexImage2D(
            GLES20.GL_TEXTURE_2D, 0,
            GLES20.GL_RGBA,
            800, 600, 0,
            GLES20.GL_RGBA,
            GLES20.GL_UNSIGNED_BYTE, null);