编程语言
首页 > 编程语言> > Java-Android Opengl ES 2.0 FrameBuffer无法正常工作

Java-Android Opengl ES 2.0 FrameBuffer无法正常工作

作者:互联网

我正在研究2D Android项目,我希望使用FBO将东西绘制为空纹理,然后在屏幕上绘制该纹理.出于调试目的,FBO附加的纹理应立即变为蓝色,因此我可以编辑该纹理,因为当前它甚至不会影响该纹理.

帧缓冲区:

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

渲染代码:

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

纹理创建:

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

所以我有两个问题.

1.)我需要带有帧缓冲区的渲染缓冲区吗?我不想跟踪深度.我只想编辑一个现有的纯2D纹理.

2.)为什么不起作用?

解决方法:

如果不需要FBO渲染的深度缓冲区,则无需创建渲染缓冲区.

对于ES 2.0中的glRenderbufferStorage()的internalformat参数,GL_DEPTH_COMPONENT不是有效值.如果查看支持的值(http://www.khronos.org/opengles/sdk/docs/man/xhtml/glRenderbufferStorage.xml),则唯一的深度格式是GL_DEPTH_COMPONENT16.

与往常一样,glGetError()是您的朋友.它应该从glRenderbufferStorage()调用中指示GL_INVALID_ENUM错误.

标签:framebuffer,java,android,opengl-es-2-0
来源: https://codeday.me/bug/20191121/2053461.html