2011-05-02 54 views
0

我在SDL库的帮助下创建了我的opengl窗口的屏幕截图,但它全是黑色的,我不明白为什么。如何解决它?SDL OpenGL截图是黑色的

代码:

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); 
+0

我发现,需要删除glReadBuffer(GL_FRONT)...但现在截图是倒过来的 – EthanHunt 2011-05-02 20:32:45

+2

OpenGL将图像的原点放在左下角(参见http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels .xml),而大多数图像文件格式都在左上角。您必须自己翻转或使用支持原点的图像文件格式; PNG文件格式支持这一点,但我不知道如何告诉SDL在PNG中指定原点。 – datenwolf 2011-05-02 21:43:16

回答

2

我已经看到了你发现glReadBuffer通话的去除,以及垂直翻转,你可以从这里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; 
} 
+0

我希望这对你有好处,但我已经将你的代码包含在一些示例代码中http://dsource.org/forums/viewtopic.php?p=28459#28459。 – ste3e 2012-05-02 00:12:37

+0

一切都好,爱心分享:) – tito 2012-05-02 10:48:37