1
\$\begingroup\$

I've tried wording and rewording this question in various forms of search and can't seem to find an answer to it. If I'm wording it wrong and/or just not finding the right information, feel free to throw me a link.

Here's what I'd like to accomplish: I want to do several different processing effects with GLSL, on several different images - each image a different effect - and then retrieve the end result and combine all the images to form a finished result.

Then, I want to save the finished product to an image I can later reuse.

So basically:

  • Make non-transparent blurry, greyscale, whatever else...

  • Do different effects on 3-10 images...

  • Layer all images on top of one another...

  • Flatten the images to result in a single image that looks identical...

  • Save to PNG.

I know 'save to PNG' isn't one of those things OpenGL can do and I have something I can use for that. However, I'm running into problems thinking up a way to retrieve the pixels from the end result (From the shader) and flatten them into a single result (in language of choice/OpenGL), and then get the final pixel data.

This doesn't have to be done in near realtime (Of course). It'll only happen when specific events are triggered, and only when absolutely necessary during those events. The events trigger 'permanent until further notice' situations, which is why this would be useful - on top of the idea of having multiple images being drawn over one another every frame being reduced to one, of course.

Thanks!

\$\endgroup\$
3
\$\begingroup\$

If the final shader outputs the image to the framebuffer, have a look at the glReadPIxels function (This function is often used to create screenshots in games example 1 | example 2)

If you wish to get the data from a texture (in case the texture is larger than the screen resolution, this might be a better solution), have a look at the glGetTexImage function.

// Create a buffer large enough to store a R, G, B value for every pixel
GLfloat* pixels = new GLfloat[width * height * 3];
// Request the pixels from the framebuffer. Make sure the shader outputted it's 
// results to the framebuffer first. 
// If this is not the case, make sure the required texture is bound and use
// glGetTexImage instead.
glReadPixels(0, 0, width, height,GL_RGB, GL_FLOAT, pixels);
// Pass on the pixels array to the png processing library
// clean up the pixel array
delete[] pixels;
\$\endgroup\$
  • \$\begingroup\$ it would have been nice, if you threw a link to frame-buffer too. \$\endgroup\$ – Ali1S232 Aug 3 '14 at 21:34
  • \$\begingroup\$ And if you really wanted to overengineer you could save on a CPU copy by using a pixel buffer (PBO) and DMA-ing the framebuffer directly to the PNG writer ;) \$\endgroup\$ – jmegaffin Aug 4 '14 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.