How can I save OpenGL draw with OpenGL?

前端 未结 4 509
遥遥无期
遥遥无期 2021-01-15 03:42

I draw a screen with OpenGL commands. And I must save this screen to .bmp or .png format. But I can\'t do it. I am using glReadpixels but I can\'t do continue. How can I sav

相关标签:
4条回答
  • 2021-01-15 03:57

    Unless you're feeling particularly ambitious (or perhaps masochistic) you probably want to use a library like DevIL that already supports this. The current version can load and/or save in both PNG and BMP formats, along with a few dozen others.

    Compared to something like IJG, this is oriented much more heavily toward working with OpenGL or DirectX (e.g., it can load a file fairly directly into an texture or vice versa).

    0 讨论(0)
  • 2021-01-15 04:00

    I know you're asking for raster formats, but an indirect way would be to first output vector graphics through gl2ps (http://www.geuz.org/gl2ps/). Examples of usage are provided with the package and on the site (http://www.geuz.org/gl2ps/#tth_sEc3).

    Then, the vector output can be converted to the format of your choice using another tool (Inkscape, Image/GraphicsMagick, etc.) or library. An added benefit is you can convert to bitmaps of any resolution in the future.

    0 讨论(0)
  • 2021-01-15 04:05

    Here it comes! you must include WinGDI.h (which i think the GL will do it!)

    void SaveAsBMP(const char *fileName)
    {
        FILE *file;
        unsigned long imageSize;
        GLbyte *data=NULL;
        GLint viewPort[4];
        GLenum lastBuffer;
        BITMAPFILEHEADER bmfh;
        BITMAPINFOHEADER bmih;
        bmfh.bfType='MB';
        bmfh.bfReserved1=0;
        bmfh.bfReserved2=0;
        bmfh.bfOffBits=54;
        glGetIntegerv(GL_VIEWPORT,viewPort);
        imageSize=((viewPort[2]+((4-(viewPort[2]%4))%4))*viewPort[3]*3)+2;
        bmfh.bfSize=imageSize+sizeof(bmfh)+sizeof(bmih);
        data=(GLbyte*)malloc(imageSize);
        glPixelStorei(GL_PACK_ALIGNMENT,4);
        glPixelStorei(GL_PACK_ROW_LENGTH,0);
        glPixelStorei(GL_PACK_SKIP_ROWS,0);
        glPixelStorei(GL_PACK_SKIP_PIXELS,0);
        glPixelStorei(GL_PACK_SWAP_BYTES,1);
        glGetIntegerv(GL_READ_BUFFER,(GLint*)&lastBuffer);
        glReadBuffer(GL_FRONT);
        glReadPixels(0,0,viewPort[2],viewPort[3],GL_BGR,GL_UNSIGNED_BYTE,data);
        data[imageSize-1]=0;
        data[imageSize-2]=0;
        glReadBuffer(lastBuffer);
        file=fopen(fileName,"wb");
        bmih.biSize=40;
        bmih.biWidth=viewPort[2];
        bmih.biHeight=viewPort[3];
        bmih.biPlanes=1;
        bmih.biBitCount=24;
        bmih.biCompression=0;
        bmih.biSizeImage=imageSize;
        bmih.biXPelsPerMeter=45089;
        bmih.biYPelsPerMeter=45089;
        bmih.biClrUsed=0;
        bmih.biClrImportant=0;
        fwrite(&bmfh,sizeof(bmfh),1,file);
        fwrite(&bmih,sizeof(bmih),1,file);
        fwrite(data,imageSize,1,file);
        free(data);
        fclose(file);
    }
    
    0 讨论(0)
  • 2021-01-15 04:06

    One thing need to be fixed at:

    bmih.biXPelsPerMeter = bmih.biYPelsPerMeter = 0;
    

    Otherwise, some picture edit can not open correctly.

    0 讨论(0)
提交回复
热议问题