如何从OpenGL ES中的渲染纹理读取像素 [英] How to read pixels from a rendered texture in OpenGL ES

查看:755
本文介绍了如何从OpenGL ES中的渲染纹理读取像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从动态生成的纹理(RTT,渲染到纹理)中读取像素.我通过实现此处.

I'm trying to read pixels from a texture which was generated on the fly (RTT, Render To Texture). I'm taking this snapshot by implementing Apple's suggested method listed here.

这对于显示在屏幕上的默认"颜色缓冲效果很好,但我无法将其用于写入纹理的像素.

This works fine for the "default" colorbuffer which gets presented to the screen but I can't get this to work for the pixels that were written to the texture.

我在以下几行中遇到问题:

I'm having trouble with these lines:

// Bind the color renderbuffer used to render the OpenGL ES view
// If your application only creates a single color renderbuffer which is already bound at this point,
// this call is redundant, but it is needed if you're dealing with multiple renderbuffers.
// Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);

// Get the size of the backing CAEAGLLayer
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

问题是我没有颜色缓冲,因为我正在渲染到纹理中.

The problem is that I don't have a colorbuffer because I'm rendering into a texture.

这是我创建纹理的方式:

Here's how I create my texture:

void Texture::generate()
{
    // Create texture to render into
    glActiveTexture(unit);
    glGenTextures(1, &handle);
    glBindTexture(GL_TEXTURE_2D, handle);

    // Configure texture
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

这是将其链接到用于渲染为的FBO的方式:

Here's how I link it to the FBO used for rendering into:

void Texture::attachToFrameBuffer(GLuint framebuffer)
{
    this->framebuffer = framebuffer;

    // Attach texture to the color attachment of the provided framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, handle, 0);
}

如您所见,我实际上并没有_colorRenderbuffer可以绑定到的地方.我首先认为绑定到帧缓冲区然后执行Apple的代码可以解决问题,但是backingWidth和backingHeight不能提供纹理的分辨率(2048x2048),而可以提供场景的分辨率(320x320).所以我想我在这里错过了一些东西.

As you can see, I don't really have a _colorRenderbuffer where I can bind to in the first place. I first thought that binding to the framebuffer and then executing Apple's code would do the trick but backingWidth and backingHeight don't give me the resolution of my texture (2048x2048) but the resolution of my scene (320x320). So I guess I'm missing something here.

推荐答案

在您可以渲染为纹理之前,它需要一些内部存储,因此您需要在渲染该纹理之前调用glTexImage2DNULL作为图像数据传递,从而仅分配纹理图像.

Befor you can render into a texture, it needs to have some internal storage, so you need to call glTexImage2D at some point before rendering into it (probably passing NULL as image data, thus just allocating the texture image).

但是backingWidth和backingHeight不能给我分辨率 纹理

but backingWidth and backingHeight don't give me the resolution of my texture

您为什么仍要检索这些值(当然,这些检索功能将无法正常工作,因为它们按照其名称所建议的那样在渲染缓冲区上工作).您已经知道了,因为创建纹理的是(请参见上文).

Why do you want to retrieve those values anyway (of course those retrieval functions won't work since they work on a renderbuffer as their names suggest). You already know it, since it was you who created the texture (see above).

一旦渲染到纹理中,您就可以像捕获普通屏幕一样调用glReadPixels,因为纹理现在是帧缓冲区,因此从帧缓冲区中读取像素会从纹理中读取像素(当然,调用glReadPixels时,FBO必须仍然绑定.

Once you rendered into the texture you can just call glReadPixels as you would do to capture the normal screen, since your texture is now the framebuffer, so reading pixels from the framebuffer reads the pixels from the texture (of course the FBO has to still be bound when calling that glReadPixels).

实际上,这是在ES中读取任何纹理数据的唯一方法,而ES没有glGetTexImage,但是由于无论如何您都已将其渲染,这绝对没有问题.

That's actually the only way to read back any texture data in ES, which doesn't have glGetTexImage, but since you just rendered into it anyway this is absolutely no problem.

这篇关于如何从OpenGL ES中的渲染纹理读取像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆