capture opengl scene

hello .
I use opengl and i would create the possibility of grab the current displayed image and export it to a .jpg or .png file.
is possible?
how?
thanks

jpg and png are complex file formats, you gonna need some library to export to these formats.

You can get a OpenGL frame using glReadPixels. Node that glReadPixels gives you raw data. If you need to export just for a quick look, take a look at PPM format.

http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml

try this, while it doesn’t save in jpg, it’s pretty easy to implement and you can always convert them later to jpg with an external program

http://www.flashbang.se/archives/155

I second that. A simple 12 byte header and you have saved your back buffer to a TGA file. The example posted is a neat example of how to do it.
Jpg and png are too complex for this kind of thing and best left as job for an art package.

Another downside of JPG and PNG is that they can be relatively slow to compress; if you ever want to export frames for creating video, they might not be good choices (TGA on the other hand may bottleneck you on disk IO so it’s a balancing act that you’ll have to tune).