Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glReadPixels vs. glGetTexImage

Tags:

opengl

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) use glGetTexImage and pass in the buffer which receives the whole texture, and read the appropriate pixels from that

b) create a framebuffer, draw into it using the texture with only the portion needed, and extract the pixels produced with glReadPixels.

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.

Steve

like image 290
stevenkucera Avatar asked Dec 17 '25 22:12

stevenkucera


1 Answers

Given that the image data is in a texture, there are several possible solutions. Ordered from most desired to least:

  • Employ glGetTextureSubImage (requires OpenGL 4.5 or ARB_get_texture_sub_image) to just do the job directly.

  • 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 glGetTexImageon that.

  • 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.

Rendering the texture to a framebuffer with triangles would only be needed in the event of working under very old OpenGL implementations.

like image 63
Nicol Bolas Avatar answered Dec 21 '25 01:12

Nicol Bolas