How can I save OpenGL draw with OpenGL?

前端 未结 4 511
遥遥无期
遥遥无期 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 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);
    }
    

提交回复
热议问题