在 android Native 代码 (ndk) 中使用 OpenGL ES 2.0 FrameBuffer (FBO) 和 Stencil

发布于 2024-11-07 05:49:20 字数 3750 浏览 4 评论 0原文

我正在尝试生成一个 frambuffer 对象,并使用 NDK (r5b) 在本机 Android 应用程序中使用模板。目标设备运行 froyo 2.2,支持 OpenGL ES 2.0。

因此,我一直在我的 c++ 本机库中编写大量 gl 代码,但除此之外还没有解决任何问题。我似乎无法让它发挥作用。

这是用于创建帧缓冲区的代码片段。完整性很好,但屏幕仍然是全黑的。就像我创建的 fbo 并没有真正绑定到应用程序的 Java 部分创建的 gl 表面。我的应用程序代码的其余部分都很好,如果我删除 fbo 创建和绑定,一切都工作得很好,除了我没有应用程序所需的模板。

    GLint backingWidth = 1024;
      GLint backingHeight = 1024;


    //Create the FrameBuffer and binds it
    glGenFramebuffers(1, &_defaultFramebuffer);
    checkGlError("glGenFramebuffers");
    glBindFramebuffer(GL_FRAMEBUFFER, _defaultFramebuffer);
    checkGlError("glBindFramebuffer");

    //Create the RenderBuffer for offscreen rendering // Color
    glGenRenderbuffers(1, &_colorRenderbuffer);
    checkGlError("glGenRenderbuffers color");
    glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderbuffer);
    checkGlError("glBindRenderbuffer color");
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, backingWidth, backingHeight);
    checkGlError("glRenderbufferStorage color");

    //Create the RenderBuffer for offscreen rendering // Depth
    glGenRenderbuffers(1, &_depthRenderbuffer);
    checkGlError("glGenRenderbuffers depth");
    glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderbuffer);
    checkGlError("glBindRenderbuffer depth");
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
    checkGlError("glRenderbufferStorage depth");

    //Create the RenderBuffer for offscreen rendering // Stencil
    glGenRenderbuffers(1, &_stencilRenderbuffer);
    checkGlError("glGenRenderbuffers stencil");
    glBindRenderbuffer(GL_RENDERBUFFER, _stencilRenderbuffer);
    checkGlError("glBindRenderbuffer stencil");
    glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, backingWidth, backingHeight);
    checkGlError("glRenderbufferStorage stencil");

    // bind renderbuffers to framebuffer object
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderbuffer);
    checkGlError("glFramebufferRenderbuffer depth");
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderbuffer);
    checkGlError("glFramebufferRenderbuffer color");
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _stencilRenderbuffer);
    checkGlError("glFramebufferRenderbuffer stencil");

//Test for FrameBuffer completeness
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    checkGlError("glCheckFramebufferStatus");
    switch (status)
    {
    case GL_FRAMEBUFFER_COMPLETE: LOGI("\n\n\nFLIPBOOM : FBO complete  GL_FRAMEBUFFER_COMPLETE %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: LOGI("\n\n\nFLIPBOOM : FBO GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT  %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: LOGI("\n\n\nFLIPBOOM : FBO FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT  %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: LOGI("\n\n\nFLIPBOOM : FBO FRAMEBUFFER_INCOMPLETE_DIMENSIONS  %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_UNSUPPORTED: LOGI("\n\n\nFLIPBOOM : FBO GL_FRAMEBUFFER_UNSUPPORTED  %x\n\n\n", status);break;

    default : LOGI("\n\n\nFLIPBOOM : failed to make complete framebuffer object %x\n\n\n", status);
    }

我也尝试过渲染为 2D 纹理而不是渲染缓冲区...也不起作用。

那么,有什么办法可以解决这个问题吗?我这里有什么问题吗?如果有人有任何想法,请让我知道......花了太多时间查找这个问题......呵呵;)

提前致谢!

干杯!


编辑:

好的,我已经设法使模板缓冲区工作,但 FBO 只是不工作。我认为android不完全支持OpenGL ES 2.0(顺便说一句,这里使用r5b)。我认为方法存根已定义,但尚未完全实现。或者创建的 GlSurfaceView 无法与 FBO 正确链接。

至于模板缓冲区,我必须

glEnable(GL_DEPTH_TEST);

删除 glDepthMask 的使用才能使它们正常工作。

I am trying to generate a frambuffer object and use stencil inside a native android application using the NDK (r5b). Target device is running froyo 2.2, supporting OpenGL ES 2.0.

So, I've been coding lots of gl code in my c++ native libs and havent got through any problem except for this. I just can't seems to make it work.

Here's a code snipplet for the framebuffer creation. Completness is all good, but screen remains completly black. It's like the fbo I am creating is not really bound to the gl surface that is created by the Java part of the app. The rest of my app code is all good, if I remove the fbo creation and binding, everything works perfectly fine except that I don't have the stencils working which I need for my app.

    GLint backingWidth = 1024;
      GLint backingHeight = 1024;


    //Create the FrameBuffer and binds it
    glGenFramebuffers(1, &_defaultFramebuffer);
    checkGlError("glGenFramebuffers");
    glBindFramebuffer(GL_FRAMEBUFFER, _defaultFramebuffer);
    checkGlError("glBindFramebuffer");

    //Create the RenderBuffer for offscreen rendering // Color
    glGenRenderbuffers(1, &_colorRenderbuffer);
    checkGlError("glGenRenderbuffers color");
    glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderbuffer);
    checkGlError("glBindRenderbuffer color");
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, backingWidth, backingHeight);
    checkGlError("glRenderbufferStorage color");

    //Create the RenderBuffer for offscreen rendering // Depth
    glGenRenderbuffers(1, &_depthRenderbuffer);
    checkGlError("glGenRenderbuffers depth");
    glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderbuffer);
    checkGlError("glBindRenderbuffer depth");
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, backingWidth, backingHeight);
    checkGlError("glRenderbufferStorage depth");

    //Create the RenderBuffer for offscreen rendering // Stencil
    glGenRenderbuffers(1, &_stencilRenderbuffer);
    checkGlError("glGenRenderbuffers stencil");
    glBindRenderbuffer(GL_RENDERBUFFER, _stencilRenderbuffer);
    checkGlError("glBindRenderbuffer stencil");
    glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, backingWidth, backingHeight);
    checkGlError("glRenderbufferStorage stencil");

    // bind renderbuffers to framebuffer object
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderbuffer);
    checkGlError("glFramebufferRenderbuffer depth");
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderbuffer);
    checkGlError("glFramebufferRenderbuffer color");
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _stencilRenderbuffer);
    checkGlError("glFramebufferRenderbuffer stencil");

//Test for FrameBuffer completeness
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    checkGlError("glCheckFramebufferStatus");
    switch (status)
    {
    case GL_FRAMEBUFFER_COMPLETE: LOGI("\n\n\nFLIPBOOM : FBO complete  GL_FRAMEBUFFER_COMPLETE %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: LOGI("\n\n\nFLIPBOOM : FBO GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT  %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: LOGI("\n\n\nFLIPBOOM : FBO FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT  %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: LOGI("\n\n\nFLIPBOOM : FBO FRAMEBUFFER_INCOMPLETE_DIMENSIONS  %x\n\n\n", status);break;

    case GL_FRAMEBUFFER_UNSUPPORTED: LOGI("\n\n\nFLIPBOOM : FBO GL_FRAMEBUFFER_UNSUPPORTED  %x\n\n\n", status);break;

    default : LOGI("\n\n\nFLIPBOOM : failed to make complete framebuffer object %x\n\n\n", status);
    }

I've also tried rendering to a 2D texture instead of the renderbuffer...didn't worked either.

So, Is there a way I can fix this ? Am I getting something wrong here ? If anyone has any ideas please lemme know....been spending way too much time looking up this problem...hehe ;)

Thanks in advance !

Cheers !


EDIT :

Ok, I've manage to make the stencil buffer work but the FBO are just not working. I think OpenGL ES 2.0 is not fully supported by android (using r5b here btw). I think method stubs are defined, but not fully implemented. Or the GlSurfaceView created doesn't link correctly with the FBOs.

As for the stencil buffer, I had to do

glEnable(GL_DEPTH_TEST);

and remove the usage of glDepthMask in order for them to work correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2024-11-14 05:49:20

@Zennichimaro,对于模板缓冲区的使用!

在初始化期间:

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glEnable(GL_DEPTH_TEST);

在渲染期间:

glViewport(0, 0, GetViewWidth(), GetViewHeight());
checkGlError("glViewport");

if (_firstRenderDone == false)
{
    glClearDepthf( 0.9f );
    glDepthMask( GL_TRUE );
    glClear( GL_DEPTH_BUFFER_BIT );
    glDepthMask( GL_FALSE );
    _firstRenderDone = true;
}

glClearColor(M_channelToFloat(_backgroundColor.r),
               M_channelToFloat(_backgroundColor.g),
               M_channelToFloat(_backgroundColor.b),
               M_channelToFloat(_backgroundColor.a));
checkGlError("glClearColor");
glClearStencil( 0 );
checkGlError("glClearStencil");
glClear( GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
checkGlError("glClear");

_stencilLayer = 1;

//use our custom shaders
if( _program )
{
    glUseProgram(_program);


    if( transformMatrix3x3 != NULL )
    {
        glUniformMatrix3fv( _uniforms[OGL_UNIFORM_TRANSFORM], 1, false, transformMatrix3x3 );
    }

     // reset the shading.
    glUniform1f( _uniforms[ OGL_UNIFORM_SHADE ], 0.0f );
}


//Do the actual drawing (Triangle Slip)
 if( object )
  {
     _isRender = true;
    object->OglDraw(this);
    _isRender = false;
  }

当我需要使用模板时,我根据需要使用以下方法:

void GlEs2Renderer::StencilStartMask()
{
if (!USE_STENCIL)   //For debugging purpose
    return;


glEnable(GL_STENCIL_TEST);

//Turn off writing to the Color Buffer and Depth Buffer
//We want to draw to the Stencil Buffer only
glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );

//Set 1 into the stencil buffer
glStencilFunc( GL_ALWAYS, NewStencilLayer(), 0xFFFFFFFF );
glStencilOp( GL_ZERO, GL_ZERO, GL_REPLACE );
}

void GlEs2Renderer::StencilUseMask()
{
if (!USE_STENCIL)   //For debugging purpose
        return;

//Turn back on Color Buffer and Depth Buffer
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );

//Only write to the Stencil Buffer where 1 is set
glStencilFunc( GL_EQUAL, StencilLayer(), 0xFFFFFFFF);

//Keep the content of the Stencil Buffer
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
}

void GlEs2Renderer::StencilOverlayMask()
{
if (!USE_STENCIL)   //For debugging purpose
        return;

//Turn back on Color Buffer and Depth Buffer
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glDepthMask(true);

//Only write to the Stencil Buffer where 1 is set
glStencilFunc( GL_EQUAL, StencilLayer(), 0xFFFFFFFF);

//Keep the content of the Stencil Buffer and increase when z passed
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
}

最后,我使用双通道技术在模板内绘制...这是一个示例:

glVertexAttribPointer(OGL_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, _triangles);
glEnableVertexAttribArray(OGL_ATTRIB_VERTEX);
glVertexAttribPointer(OGL_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, _colors);
glEnableVertexAttribArray(OGL_ATTRIB_COLOR);

glContext->StencilStartMask();
glDrawArrays(GL_TRIANGLE_STRIP, 0, _nPoints);

glContext->StencilUseMask();;
glDrawArrays(GL_TRIANGLE_STRIP, 0, _nPoints);

glContext->StencilEndMask();

我的代码相当复杂,所以很难只发布与模板相关的内容,但我希望它会有所帮助;)

@ Zennichimaro, For the stencil buffer usage !

During Initialisation :

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glEnable(GL_DEPTH_TEST);

During the rendering :

glViewport(0, 0, GetViewWidth(), GetViewHeight());
checkGlError("glViewport");

if (_firstRenderDone == false)
{
    glClearDepthf( 0.9f );
    glDepthMask( GL_TRUE );
    glClear( GL_DEPTH_BUFFER_BIT );
    glDepthMask( GL_FALSE );
    _firstRenderDone = true;
}

glClearColor(M_channelToFloat(_backgroundColor.r),
               M_channelToFloat(_backgroundColor.g),
               M_channelToFloat(_backgroundColor.b),
               M_channelToFloat(_backgroundColor.a));
checkGlError("glClearColor");
glClearStencil( 0 );
checkGlError("glClearStencil");
glClear( GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
checkGlError("glClear");

_stencilLayer = 1;

//use our custom shaders
if( _program )
{
    glUseProgram(_program);


    if( transformMatrix3x3 != NULL )
    {
        glUniformMatrix3fv( _uniforms[OGL_UNIFORM_TRANSFORM], 1, false, transformMatrix3x3 );
    }

     // reset the shading.
    glUniform1f( _uniforms[ OGL_UNIFORM_SHADE ], 0.0f );
}


//Do the actual drawing (Triangle Slip)
 if( object )
  {
     _isRender = true;
    object->OglDraw(this);
    _isRender = false;
  }

When I need to use stencil I use the following methods depending on what I need :

void GlEs2Renderer::StencilStartMask()
{
if (!USE_STENCIL)   //For debugging purpose
    return;


glEnable(GL_STENCIL_TEST);

//Turn off writing to the Color Buffer and Depth Buffer
//We want to draw to the Stencil Buffer only
glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );

//Set 1 into the stencil buffer
glStencilFunc( GL_ALWAYS, NewStencilLayer(), 0xFFFFFFFF );
glStencilOp( GL_ZERO, GL_ZERO, GL_REPLACE );
}

void GlEs2Renderer::StencilUseMask()
{
if (!USE_STENCIL)   //For debugging purpose
        return;

//Turn back on Color Buffer and Depth Buffer
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );

//Only write to the Stencil Buffer where 1 is set
glStencilFunc( GL_EQUAL, StencilLayer(), 0xFFFFFFFF);

//Keep the content of the Stencil Buffer
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
}

void GlEs2Renderer::StencilOverlayMask()
{
if (!USE_STENCIL)   //For debugging purpose
        return;

//Turn back on Color Buffer and Depth Buffer
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
glDepthMask(true);

//Only write to the Stencil Buffer where 1 is set
glStencilFunc( GL_EQUAL, StencilLayer(), 0xFFFFFFFF);

//Keep the content of the Stencil Buffer and increase when z passed
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
}

And Finally I do the double pass technic to draw inside the stencil ... Here's an example :

glVertexAttribPointer(OGL_ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, _triangles);
glEnableVertexAttribArray(OGL_ATTRIB_VERTEX);
glVertexAttribPointer(OGL_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, _colors);
glEnableVertexAttribArray(OGL_ATTRIB_COLOR);

glContext->StencilStartMask();
glDrawArrays(GL_TRIANGLE_STRIP, 0, _nPoints);

glContext->StencilUseMask();;
glDrawArrays(GL_TRIANGLE_STRIP, 0, _nPoints);

glContext->StencilEndMask();

My code is fairly complex so it's hard to only post what's related to the stencil, But I hope It'll help ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文