简体   繁体   中英

Android Opengl ES 2.0 FrameBuffer not working

I'm working on a 2D Android project and I wish to draw stuff to an empty texture using an FBO and then draw that texture on screen. For debugging purposes, the FBO attached texture should immediately turn blue so I can tell that I am able to edit the texture because currently it won't even affect the texture.

FrameBuffer:

public static class FrameBuffer{

    private final FaustEngine engine;
    private final int width;
    private final int height;
    private final int[] fboId = new int[1];
    private final int[] renId = new int[1];

    public FrameBuffer( FaustEngine Engine, int Width, int Height ){
        engine = Engine;
        width = Width;
        height = Height;

        GLES20.glGenRenderbuffers( 1, renId, 0 );
        GLES20.glBindRenderbuffer( GLES20.GL_RENDERBUFFER, renId[0] );
        GLES20.glRenderbufferStorage( GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT, width, height );

        GLES20.glGenFramebuffers( 1, fboId, 0 );
        GLES20.glBindFramebuffer( GLES20.GL_FRAMEBUFFER, fboId[0] );
        GLES20.glFramebufferRenderbuffer( GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renId[0] );

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

    public void bind( int texId, int texWidth, int texHeight ){
        GLES20.glBindFramebuffer( GLES20.GL_FRAMEBUFFER, fboId[0] );
        GLES20.glFramebufferTexture2D( GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texId, 0 );
        GLES20.glViewport( 0, 0, texWidth, texHeight );
        GLES20.glClearColor(0f, 0f, 1f, 1f );
        GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT ); //Turn blue

        //Fix projection matrix
        Matrix.orthoM( engine.getOrthoMatrix() , 0, 0, texWidth, texHeight, 0, -1, 1 );
    }

    public void unbind(){
        GLES20.glBindFramebuffer( GLES20.GL_FRAMEBUFFER, 0 );
        GLES20.glViewport( 0, 0, engine.getScreenWidth(), engine.getScreenHeight() );

        //Reset projection matrix
        Matrix.orthoM( engine.getOrthoMatrix() , 0, 0, engine.getScreenWidth(), engine.getScreenHeight(), 0, -1, 1 );
    }
}

Rendering code:

FrameBuffer fbo = new FrameBuffer( game.getEngine(), 1024, 1024 );
final int[] texIds = new int[1];
game.getEngine().createBlankTextures( texIds, 1024, 1024 );

fbo.bind( texIds[0], 1024, 1024 );
//texture should turn blue here since in the bind() function I placed a glClear with blue
fbo.unbind();

Texture creation:

public void createBlankTextures( int[] texIds, int width, int height ){
    GLES20.glGenTextures( texIds.length, texIds, 0 );
    for( int i=0; i<texIds.length; i++ ){
        GLES20.glBindTexture( GLES20.GL_TEXTURE_2D, texIds[i] );
        GLES20.glTexParameteri( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST );
        GLES20.glTexParameteri( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST );
        GLES20.glTexImage2D( GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null );
        GLES20.glGenerateMipmap( GLES20.GL_TEXTURE_2D );
    }
}

So I have 2 questions.

1.) Do I need a Renderbuffer with the Framebuffer? I don't want to keep track of the depth. I just want to edit an existing purely 2D texture.

2.) Why is this not working?

You don't need to create a renderbuffer if you don't need a depth buffer for your FBO rendering.

GL_DEPTH_COMPONENT is not a valid value for the internalformat argument of glRenderbufferStorage() in ES 2.0. If you look at the supported values ( http://www.khronos.org/opengles/sdk/docs/man/xhtml/glRenderbufferStorage.xml ), the only depth format is GL_DEPTH_COMPONENT16 .

As always, glGetError() is your friend. It should indicate a GL_INVALID_ENUM error from your glRenderbufferStorage() call.

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