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);
2
  • I found, need to remove glReadBuffer(GL_FRONT)... But now screenshot is upside down
    – EthanHunt
    Commented May 2, 2011 at 20:32
  • 2
    OpenGL places the origin of images in the lower left corner (see 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
    Commented May 2, 2011 at 21:43

1 Answer 1

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;
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.