【问题标题】:OpenGL ES 2.0 - Render to texture is all blackOpenGL ES 2.0 - 渲染到纹理全黑
【发布时间】:2016-07-23 03:01:52
【问题描述】:

我有一个场景可以渲染到屏幕上,但是当我尝试将其渲染到纹理时,它会全黑。我想我在准备纹理时错过了一些阶段。

代码:

    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();

知道我错过了什么吗?

【问题讨论】:

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


    【解决方案1】:

    我已经很久没有使用 opengl 了,但我认为你应该将 GL_RGBA 用于 internalFormat 和 format 并用作类型 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);
    

    【讨论】:

    • 谢谢,我已经更新了问题。虽然仍然是全黑的纹理:(
    • 我觉得你的 opengl 的痛苦 eheh ...尝试删除 texBuffer 并传递 null GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 800, 400, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE , null) 你不应该需要缓冲区。
    猜你喜欢
    • 2012-01-16
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多