Android的OpenGL ES的帧缓冲区对象 - 渲染到纹理缓冲区、纹理、对象、Android

2023-09-05 06:16:56 作者:一声兄弟大过天

我使用的运行Froyo的Andr​​oid设备支持OpenGL ES 1.1和OpenGL ES 2.0

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

我要渲染的深度缓存到纹理。我们已经看到了大量的实例对OpenGL,Op​​enGL ES的其他平台(包括iPhone)我已经尝试了一些FBO配置。

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.

我似乎能够得到一个FBO建立一个颜色纹理,但每次我附上深度纹理失败的时间。

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.

我目前的code基于这个例子但创建一个颜色纹理以及代替设置绘制和读取缓冲区无法比拟的。

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

有没有在Android上配置的OpenGL ES FBO渲染深度贴图的一个简单的例子?或者是有描述是什么,是不支持的文件?

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?

感谢您的意见 - 我特别需要一个解决方案,ES 1.1,如果它可以被发现和工作在Android上。我也想看看ES 2 - 我不知道我的理解包装的深度信息转换成颜色缓冲区的想法 - 你有一个参考,我可以看明白的想法更好?

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?

对于code - 我的来源是从我上面贴的链接几乎没有不同。帧缓冲区状态是,它是不完整的。

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.

推荐答案

确定,各种各样的答案。得到这个工作在Android 2.2与OpenGL ES的2:

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的琐碎真的,遵循了OpenGL ES2手动和做的一切,在正确的顺序。

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

试过这种用OpenGL ES 1.1,增加OES所需的功能调用和_OES的常数,因为帧缓冲区对象是一个扩展。任何试图将附加在一个不完整的帧缓冲器的对象的深度纹理到帧缓冲器的效果。

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.

所以此刻我的结论是,这并不用OpenGL ES 1.1在Android上工作,但它显然不与ES 2.2的工作。

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.

如果任何人有ES 1.1的解决方案,将是有趣的,看看吧。

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