Android OpenGL ES Framebuffer 对象 - 将深度缓冲区渲染到纹理

发布于 2024-09-29 16:52:04 字数 679 浏览 1 评论 0原文

我正在使用运行支持 OpenGL ES 1.1 和 OpenGL ES 2.0 的 Froyo 的 Android 设备,

我想将深度缓冲区渲染到纹理。在看过其他平台(包括 iPhone)上的 OpenGL、OpenGL ES 的许多示例后,我尝试了许多 FBO 配置。

我似乎能够获得带有颜色纹理的 FBO 设置,但每次我附加深度纹理时都会失败。

我当前的代码基于此示例,但也创建了颜色纹理,而不是将绘制和读取缓冲区设置为无。

是否有一个在 Android 上配置 OpenGL ES FBO 以渲染纹理深度的简单示例?或者是否有文档描述什么是支持的,什么是不支持的?


感谢您的评论 - 我特别需要 ES 1.1 的解决方案,如果它可以在 Android 上找到并运行的话。我还想看看 ES 2 - 我不确定我是否理解将深度信息打包到颜色缓冲区中的想法 - 你有我可以查看的参考资料以更好地理解这个想法吗?

关于代码 - 我的来源与我上面发布的链接几乎没有什么不同。 Framebuffer的状态是不完整。


感谢片段着色器的建议 - 我现在明白了。如果我无法让其他解决方案发挥作用,我会看看。我的理想是同时获得深度和颜色 - 如果可以的话,我真的不想单独渲染颜色和深度。

I am using an Android device running Froyo supporting OpenGL ES 1.1 and OpenGL ES 2.0

I want to render the depth buffer to a texture. Having seen a number of examples for OpenGL, OpenGL ES on other platforms (including iPhone) I have tried a number of FBO configurations.

I seem to be able to get an FBO set-up with a colour texture but every time I attach a depth texture it fails.

My current code is based on this example but creating a colour texture as well instead of setting draw and read buffers to none.

Is there a simple example of configuring an OpenGL ES FBO on Android to render depth to a texture? Alternatively is there a document describing what is and is not supported?


Thanks for the comments - I specifically needed a solution for ES 1.1, if it could be found and work on Android. I also want to look at ES 2 - I am not sure I understand the idea of packing the depth information into colour buffer - do you have a reference I can look at to understand the idea better?

Regarding code - my source is barely different from the link I posted above. The Framebuffer status is that it is not complete.


Thanks for the fragment shader suggestion - I get the idea now. Will look at that if I can't get another solution working. My ideal is to get depth and colour at the same time - don't really want to render colour and depth separately if I can help it.

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

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

发布评论

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

评论(2

两人的回忆 2024-10-06 16:52:04

好吧,某种答案。使用 OpenGL ES 2 在 Android 2.2 上运行:

    // Create a frame buffer
    glGenFramebuffers( 1, &(frame_buffer ) );

    // Generate a texture to hold the colour buffer
    glGenTextures(1, &(colour_texture) );
    glBindTexture(GL_TEXTURE_2D, colour_texture);
    // Width and height do not have to be a power of two
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
            pixelWidth, pixelHeight,
            0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

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

    // Probably just paranoia
    glBindTexture(GL_TEXTURE_2D, 0);

    // Create a texture to hold the depth buffer
    glGenTextures(1, &(depth_texture) );
    glBindTexture(GL_TEXTURE_2D, depth_texture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
            pixelWidth, pixelHeight,
            0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);

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

    glBindTexture(GL_TEXTURE_2D, 0);        

    glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer);

    // Associate the textures with the FBO.

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                    GL_TEXTURE_2D, colour_texture, 0);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                    GL_TEXTURE_2D, depth_texture, 0);

    // Check FBO status.    
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

    if ( status == GL_FRAMEBUFFER_COMPLETE )
    {
        // Success
    }

非常简单,遵循 OpenGL ES2 手册并按正确的顺序执行所有操作。

使用 OpenGL ES 1.1 进行了尝试,将 OES 添加到所需的函数调用中,并将 _OES 添加到常量中,因为帧缓冲区对象是扩展。任何将深度纹理附加到帧缓冲区的尝试都会导致帧缓冲区对象不完整。

所以目前我的结论是,这不适用于 Android 上的 OpenGL ES 1.1,但它显然适用于 ES 2.2。

如果有人有 ES 1.1 的解决方案,那么看到它会很有趣。

OK, an answer of sorts. Got this to work on Android 2.2 with OpenGL ES 2:

    // Create a frame buffer
    glGenFramebuffers( 1, &(frame_buffer ) );

    // Generate a texture to hold the colour buffer
    glGenTextures(1, &(colour_texture) );
    glBindTexture(GL_TEXTURE_2D, colour_texture);
    // Width and height do not have to be a power of two
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
            pixelWidth, pixelHeight,
            0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

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

    // Probably just paranoia
    glBindTexture(GL_TEXTURE_2D, 0);

    // Create a texture to hold the depth buffer
    glGenTextures(1, &(depth_texture) );
    glBindTexture(GL_TEXTURE_2D, depth_texture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
            pixelWidth, pixelHeight,
            0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);

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

    glBindTexture(GL_TEXTURE_2D, 0);        

    glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer);

    // Associate the textures with the FBO.

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                    GL_TEXTURE_2D, colour_texture, 0);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                    GL_TEXTURE_2D, depth_texture, 0);

    // Check FBO status.    
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

    if ( status == GL_FRAMEBUFFER_COMPLETE )
    {
        // Success
    }

Pretty trivial really, follow the OpenGL ES2 manual and do everything in the right order.

Tried this with OpenGL ES 1.1, adding OES to the required function calls and _OES to the constants since frame buffer objects is an extension. Any attempt to attach a depth texture to the frame buffer results in an incomplete frame buffer object.

So at the moment my conclusion is that this doesn't work with OpenGL ES 1.1 on Android, but it clearly does work with ES 2.2.

If anyone has a solution for ES 1.1 it would be interesting to see it.

私野 2024-10-06 16:52:04

许多设备不支持渲染为深度纹理。您需要为深度缓冲区附加一个渲染缓冲区。

Rendering to a depth texture isn't supported on many devices. You'll need to attach a renderbuffer for your depth buffer.

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