OpenGL export to bmp

Hi,

i have a code that export my opengl window in a bmp file but it didnt work. I get this picture …


http://picfront.de/d/eEgms43jAC/Ausgabe.png

Where is the error ??

i use these code to get the opengl information …

int* viewport = new int[4];

glGetIntegerv( GL_VIEWPORT, viewport);
void* image = malloc(3 * viewport[2] * viewport[3]);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, image);

thx for help

Are you reading the correct buffer? glReadPixels() works in conjunction with glReadBuffer().

The initial value is GL_BACK if there is back buffer and GL_FRONT if there is no back buffer.

ref: OpenGL 2.1 spec, page 222-224 4.3.2 Reading Pixels.

You can query the current value with the GL_READ_BUFFER get value.

ref: OpenGL 2.1 spec, page 292 Table 6.27. Pixels (cont.)

code looks ok. thats how i recall to do it in OpenGL. are you sure your bmp exporter is working properly and did you saved anything else into bmp files besides glReadPixels buffer? GL_BACK buffer is set by default in OpenGL unless you changed it somewhere.

For non power-of-two sized BMP images, you must add padding pixels, so the ‘true’ width of a single row of pixel data is divisible by 4. This applies to 24-bit BMPs only AFAIK.

I.e. if you want to write an image 6 pixels wide, round it up to 8 and and add 2x padding pixels (=6 bytes for BGR).

To see if padding is indeed the problem, change the glReadPixels width to a power-of-two width, such as 256.

Hi i tryed glReadPixels with 256 … but it doesnt work … i get the same picture…and i have tryed GL_FONT and GL_BACK … its still the same … I think its a problem with

glReadPixels(…GL_RGB …) , i think it must be GL_BGR ???

The complete codes is the following …

int Out::uebergabe(int x, int y, int Width, int Height, const char *fname)
{
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;

unsigned char *image = (unsigned char*)malloc(sizeof(unsigned char)*Width*Height*3);
   FILE *file = fopen(fname, "wb");

    if( image!=NULL )
    {
		
		if( file!=NULL )
        {
			
			int*	 viewport  = new int[4];
			
			glGetIntegerv( GL_VIEWPORT, viewport);
			void* image = malloc(3 * viewport[2] * viewport[3]);
			glPixelStorei(GL_PACK_ALIGNMENT, 1);
			glReadPixels(0, 0, viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, image);
			
									
			char buff[400];
			sprintf(buff,"Results: %f %f

",viewport[2],viewport[3] );
msgout(“Result”, buff);

			memset( &bf, 0, sizeof( bf ) );
			memset( &bi, 0, sizeof( bi ) );

            bf.bfType = 'MB';
            bf.bfSize = sizeof(bf)+sizeof(bi)+Width*Height*3;
            bf.bfOffBits = sizeof(bf)+sizeof(bi);
            bi.biSize = sizeof(bi);
            bi.biWidth = Width;
            bi.biHeight = Height;
            bi.biPlanes = 1;
            bi.biBitCount = 24;
            bi.biSizeImage = Width*Height*3;

            fwrite( &bf, sizeof(bf), 1, file );
            fwrite( &bi, sizeof(bi), 1, file );
            fwrite( image, sizeof(unsigned char), Height*Width*3, file );

            fclose( file );
			
        }
        free( image );
    }
    return 0;
}

is Width == viewport[2] and Height == viewport[3]?
the GL_BGR and GL_RGB params won’t effect this, your channels are simply switched by driver. btw, what is displayed in your backbuffer? if its padding issue it should be obvious.

You cannot just dump the BITMAPFILEHEADER and BITMAPINFOHEADER headers into a file. The compiler may add byte padding at compile time (usually aligned to 4 bytes), so the size returned by sizeof() may not match the expected header size. You should fwrite() the individual header.members separately to be sure.

For more info:

http://en.wikipedia.org/wiki/Data_structure_alignment#Typical_alignment_of_C_structs_on_x86

And BGR/RGB order should not cause the ‘static noise’ in your image. You can call glReadPixels with GL_BGR yes. It looks more like bad header, and I see some diagonal pattern that is probably due to the other problem I described (pixel padding) in my previous post.

(Do not confuse ‘pixel padding’ and ‘byte alignment’, they are two separate things.)

Hi ,

i have changed the size to 256 *256 … now is the padding error solved … but the image is always colorful :frowning:

any ideas ??

you need greyscale?