View Full Version : Copy Viewport

05-28-2007, 01:56 AM
I need to copy the viewport into an image(or better a matrix). The problem is that the window is 600x400, while the vieport is double sized (1200x800). I try with glreadpixels, but it copyes only the window region, all the pixels over 600 for the X and 400 for the Y are black..

05-28-2007, 02:13 AM
all non visible pixels for an opengl window context are undefined.

You should use offscreen buffers like FBO to overcome that problem.

Olivier B.
05-28-2007, 02:15 AM
Your problem isn't clear. Viewport is usually used to define a sub part of screen or window, so its size is smaller than screen or window.

05-28-2007, 02:40 AM
ok, I need an offscreen render..thanks

05-28-2007, 08:10 PM
This works for me...


//take a screen shot and save to a ppm file!
conf_window &cfg_win = mConf.get_window();
glPixelStorei( GL_PACK_ROW_LENGTH, 0 ) ;
glPixelStorei( GL_PACK_ALIGNMENT, 1 ) ;
unsigned int _size = cfg_win.window_width * cfg_win.window_height * 3;

if( _size == 0 ) {

char *_buffer = new char[ _size ];
if( _buffer == NULL ) {

glReadPixels( 0, 0, cfg_win.window_width, cfg_win.window_height, GL_RGB, GL_UNSIGNED_BYTE, _buffer ) ;

//flip the image
//huff we now have t oflip the image upside down!
char *ppm = new char[_size] ;
if( ppm == NULL ) {
delete [] _buffer;

int image_rowbytes = cfg_win.window_width * 3;
int _rows = _size / image_rowbytes;
for(int x = 0; x < _rows; x++ ) {
memcpy(ppm + x*image_rowbytes,_buffer + image_rowbytes*_rows - (x + 1)*image_rowbytes,image_rowbytes);
delete [] _buffer;

//now lets create a ppm file
//easy to make and graphic converter can read them...
char _header[64];
sprintf(_header,"P6\n#.\n%i %i\n255\n", cfg_win.window_width, cfg_win.window_height);
int _header_size = strlen(_header);
char *_thefile = new char[_size + _header_size ];
if( _thefile == NULL ) {
delete [] ppm;

memcpy(_thefile + _header_size, ppm, _size);
delete [] ppm;

//write _thefile chars to file...

05-31-2007, 06:22 AM
glReadPixels can read pixels that are offscreen with the FBO?

05-31-2007, 06:27 AM
and how to set the dimension of the buffer? thanks!

Olivier B.
05-31-2007, 06:44 AM
glReadPixels can read pixels that are offscreen with the FBO?
Yes, you can use FBO attached textures exactly like other textures. You can also use glGetTexImage.

and how to set the dimension of the buffer? thanks!
Dimension of the buffer depends on attached textures size. All texture must have the sale size and format. To have a buffer with 1200x800 size use GL_TEXTURE_RECTANGLE_ARB/NV.

06-01-2007, 01:24 AM
but there's a way to do offscreen rendering with the GL_AUXi buffer? When I set this type with the glDrawBuffer, the screen is full of black..there's no render image.

06-01-2007, 04:07 AM
You need to check if you implementation actually supports aux buffers, and if it does they have the same size as the standard color buffers (window size).
For offscreen rendering at user defined sizes with the pixelownership test always succeeding you should use FBO. Really!
If FBOs are not supported you could fallback to the legacy p-buffers, but those are awkward in that they require an own context. Not recommended over FBOs.

To have a buffer with 1200x800 size use GL_TEXTURE_RECTANGLE_ARB/NV.Or if NPOT textures are supported just use GL_TEXTURE_2D as usual.

06-06-2007, 02:09 AM
FBO doesn't work.. uff!
So I need the pbuffer..but I can use pbuffer with the GL_DEPTH_COMPONENT? I need to see it. thanks!