Copy Viewport

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…

all non visible pixels for an opengl window context are undefined.

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

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.

ok, I need an offscreen render…thanks

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 ) {
	return;
}
	
char *_buffer = new char[ _size ];
if( _buffer == NULL ) {
	return;
}

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;
	return;
}
	
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
#.
%i %i
255
", 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;
	return;
}
	
strcpy(_thefile,_header);
memcpy(_thefile + _header_size, ppm, _size);
delete [] ppm;

//write _thefile chars to file...

glReadPixels can read pixels that are offscreen with the FBO?

and how to set the dimension of the buffer? thanks!

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.

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.

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.

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!