Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: OpenGL export to bmp

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2009
    Posts
    3

    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

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2007
    Posts
    269

    Re: OpenGL export to bmp

    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.)

  3. #3
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: OpenGL export to bmp

    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.

  4. #4
    Member Regular Contributor remdul's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands
    Posts
    334

    Re: OpenGL export to bmp

    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.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2009
    Posts
    3

    Re: OpenGL export to bmp

    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\n",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;
    }

  6. #6
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: OpenGL export to bmp

    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.

  7. #7
    Member Regular Contributor remdul's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands
    Posts
    334

    Re: OpenGL export to bmp

    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_st...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.)

  8. #8
    Junior Member Newbie
    Join Date
    Feb 2009
    Posts
    3

    Re: OpenGL export to bmp

    Hi ,

    i have changed the size to 256 *256 .. now is the padding error solved .. but the image is always colorful :-(

    any ideas ??

  9. #9
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: OpenGL export to bmp

    you need greyscale?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •