2011-05-02 55 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