简体   繁体   中英

OpenGL ES 2.0 Invalid operation on framebuffer creation

I'm trying to render to a texture, and then draw a texture into a square. While creating the framebuffer, I get Invalid operation after glFramebufferRenderbuffer and glFramebufferTexture2D calls. Here is the function that should create the framebuffer:

private void createFrameBuffer()
{
    final int[] texture = new int[1];
    frameTextureID = texture[0];
    GLES20.glGenTextures(1,texture,0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameTextureID);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);

    final int[] depthBuffer = new int[1];
    GLES20.glGenRenderbuffers(1, depthBuffer, 0);
    renderBufferID = depthBuffer[0];

    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depthBuffer[0]);
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, width, height);

    final int[] frameBuffer = new int[1];
    GLES20.glGenFramebuffers(1,frameBuffer,0);
    frameBufferID = frameBuffer[0];
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,frameBufferID);

    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, frameTextureID, 0);
    error("glFramebufferTexture2D");
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT  , GLES20.GL_RENDERBUFFER, renderBufferID);
    error("glFramebufferRenderbuffer");

    frameBufferComplete("createFrameBuffer");

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

I don't understand what's wrong with that.

ps: The framebuffer is complete when I check at the end of the function.

These first few statements are in the wrong order:

final int[] texture = new int[1];
frameTextureID = texture[0];
GLES20.glGenTextures(1,texture,0);

You get the value of texture[0] before it is set by glGenTextures() . So frameTextureID will always be 0.

Instead, this should be:

final int[] texture = new int[1];
GLES20.glGenTextures(1,texture,0);
frameTextureID = texture[0];

Also note that RGBA8 (RGBA with 8-bit components) is not guaranteed to be a renderable format in ES 2.0. This is what you're using for your texture with the combination of GL_RGBA and GL_UNSIGNED_BYTE . Many implementations support it with the OES_rgb8_rgba8 extension . But if you want to be completely sure tat your code works on all devices, you need to use one of the supported formats, like RGB565:

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
                    GLES20.GL_RGBA, GLES20.GL_UNSIGNED_SHORT_5_6_5, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM