1

I create a screenshot of my opengl window with help of SDL library, but it was all black and i dont understand why. How to fix it?

Code:

SDL_Surface * image = SDL_CreateRGBSurface(SDL_SWSURFACE, current_w, current_h, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);

glReadBuffer(GL_FRONT);
glReadPixels(0, 0, current_w, current_h, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);

SDL_SaveBMP(image, "pic.bmp");
SDL_FreeSurface(image);
EthanHunt
  • 473
  • 2
  • 9
  • 19
  • I found, need to remove glReadBuffer(GL_FRONT)... But now screenshot is upside down – EthanHunt May 02 '11 at 20:32
  • 2
    OpenGL places the origin of images in the lower left corner (see http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml ), whereas most image file formats have it in the upper left. You'll have to flip it yourself or use a image file format that supports either origin; the PNG file format supports this, however I don't know how to tell SDL to specify the origin in a PNG. – datenwolf May 02 '11 at 21:43

1 Answers1

2

I've seen that you've found the removing of glReadBuffer call, and for vertical flip, you can take the function here from http://lists.libsdl.org/pipermail/sdl-libsdl.org/2005-January/047965.html :

SDL_Surface* flipVert(SDL_Surface* sfc)
{
     SDL_Surface* result = SDL_CreateRGBSurface(sfc.flags, sfc.w, sfc.h,
         sfc.format.BytesPerPixel * 8, sfc.format.Rmask, sfc.format.Gmask,
         sfc.format.Bmask, sfc.format.Amask);
     ubyte* pixels = cast(ubyte*) sfc.pixels;
     ubyte* rpixels = cast(ubyte*) result.pixels;
     uint pitch = sfc.pitch;
     uint pxlength = pitch*sfc.h;
     assert(result != null);

     for(uint line = 0; line < sfc.h; ++line) {
         uint pos = line * pitch;
         rpixels[pos..pos+pitch] = 
             pixels[(pxlength-pos)-pitch..pxlength-pos];
     }

     return result;
}
tito
  • 12,990
  • 1
  • 55
  • 75
  • I hope this is alright with you, but I have included your code in some example code http://dsource.org/forums/viewtopic.php?p=28459#28459. – ste3e May 02 '12 at 00:12
  • Everything is ok, caring is sharing :) – tito May 02 '12 at 10:48