PDA

View Full Version : How to get OpenGL geometry as a image?



betwixt
01-30-2010, 01:39 PM
Hi,

what i mean is: Let's say I have a simple cube rotating in OpenGL perspective that has black background and I wan't to make a picture of this cube every 10 seconds and save it as jpg in such way that I get raw picture data about the cube, about how it looks like on the screen without the black background (well actually it would be ok also if the background would be black).

I hope I was able to express myself good enough. Feel free to tell me what would be a shorter way to ask this :).

Is it possible to get screen data in such way that I described?
Is there like some buffer that holds it every frame and I can somehow get it from there?

ZbuffeR
01-30-2010, 03:51 PM
Here is a step-by-step overview, you can search the web or ask again on this thread if you need details for each step :

1) make sure you ask for an RGBA framebuffer, you will need the Alpha channel for transparency (for the "like on the screen without the black background")

2) glClear with default clearcolor, with is black and transparent, just like you need

3) render your scene

4) before swapbuffers, use glReadPixels (http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml) to copy the rendered image from the framebuffer to an array within your program

5) now you can swapbuffers if you also need to directly see the render, but it is not needed for the image file

6) from the raw pixel data, use any image load/save library (like Freeimage) to store it in a file. But watch out ! Jpg only store RGB values, so instead use other formats like .png (better) or .tif or .tga : each can store the full RGBA, with transparency.

7) if you use antialiasing and/or blending on the rendering, for best transparency results on the stored image, you will have to use it as "premultiplied alpha", but it is probably not important yet.

betwixt
01-31-2010, 04:37 AM
Thank You! I shall try it out. Btw cool name ZBuffeR.