glReadPixels与glGetTexImage [英] glReadPixels vs. glGetTexImage
问题描述
我需要从较大的纹理(2048x2048)中提取一个较小的矩形(200x200),并将RGBA像素放入内存中.似乎有两种方法可以做到这一点:
I need to extract a smallish rectangle (200x200) from a large texture (2048x2048), and put the RGBA pixels in memory. There seems to be two ways to do this:
a)使用glGetTexImage并传入接收整个纹理的缓冲区,并从中读取适当的像素
a) use glGetTexImage and pass in the buffer which receives the whole texture, and read the appropriate pixels from that
b)创建一个帧缓冲区,仅使用需要的部分使用纹理将其绘制到其中,并提取使用glReadPixels生成的像素.
b) create a framebuffer, draw into it using the texture with only the portion needed, and extract the pixels produced with glReadPixels.
我猜b)更快,但是我是相对新手,我想知道我是否朝着正确的方向前进. a)更容易编写代码,所以我想知道可能的速度影响是否可以忽略不计.
I'm guessing b) is faster, but I am relative novice, I'd like to know whether I'm heading in the right direction. a) is easier to code so I wonder whether the possible speed hit is negligible.
史蒂夫
推荐答案
鉴于图像数据在纹理中,有几种可能的解决方案.从所需的顺序到最少的顺序:
Given that the image data is in a texture, there are several possible solutions. Ordered from most desired to least:
-
使用
glGetTextureSubImage
(需要OpenGL 4.5或ARB_get_texture_sub_image)直接完成该工作.
Employ
glGetTextureSubImage
(requires OpenGL 4.5 or ARB_get_texture_sub_image) to just do the job directly.
使用glCopyImageSubData
(需要OpenGL 4.3或ARB_copy_image.或NV_copy_image.后者是复制所需的矩形制成适当大小的纹理,然后在其上使用glGetTexImage
.
Use glCopyImageSubData
(requires OpenGL 4.3 or ARB_copy_image. Or NV_copy_image. The latter is implemented on more hardware than just NVIDIAs) to copy the desired rectangle into a texture of the appropriate size, then use glGetTexImage
on that.
将大纹理附加到FBO,然后将小纹理附加到另一个FBO.使用glBlitFramebuffer
(需要OpenGL 3.0或ARB_framebuffer_objects)将大纹理的所需部分复制到小纹理一个.然后在小纹理上使用glGetTexImage
.
Attach the large texture to an FBO, then attach the small texture to another FBO. Use glBlitFramebuffer
(requires OpenGL 3.0 or ARB_framebuffer_objects) to copy the desired section of the large texture to the small one. Then use glGetTexImage
on the small texture.
仅当在非常老的OpenGL实现下工作时,才需要将纹理渲染到带有三角形的帧缓冲区中.
Rendering the texture to a framebuffer with triangles would only be needed in the event of working under very old OpenGL implementations.
这篇关于glReadPixels与glGetTexImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!