保存OpenGL窗口每一帧代码段(freeimage库)

本文介绍了一种使用FreeImage库保存OpenGL窗口每一帧的方法。通过调整读取像素和位图分配参数,可以将渲染结果保存为PNG图片。适用于游戏开发和图形渲染等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

保存OpenGL窗口每一帧代码段(freeimage库)

char screenshotOrder[10];
sprintf_s(screenshotOrder, "%05d", screenshotNum);
string screenshotName = screenshotOrder;
screenshotName = "renderOutputs/anim" + screenshotName + ".png";//文件名
//设置长宽,需要修改其位置数值
unsigned char *mpixels = new unsigned char[900 * 900 * 4];
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, 900, 900, GL_RGBA, GL_UNSIGNED_BYTE, mpixels);
glReadBuffer(GL_BACK);
for (int i = 0; i < (int)900 * 900 * 4; i += 4){
    mpixels[i] ^= mpixels[i + 2] ^= mpixels[i] ^= mpixels[i + 2];
}
FIBITMAP* bitmap = FreeImage_Allocate(900, 900, 32, 8, 8, 8);
for (int y = 0; y < FreeImage_GetHeight(bitmap); y++)
{
      BYTE *bits = FreeImage_GetScanLine(bitmap, y);
      for (int x = 0; x < FreeImage_GetWidth(bitmap); x++)
      {
           bits[0] = mpixels[(y * 900 + x) * 4 + 0];//900为长
           bits[1] = mpixels[(y * 900 + x) * 4 + 1];
           bits[2] = mpixels[(y * 900 + x) * 4 + 2];
           bits[3] = 255;
           bits += 4;

      }
}
bool bSuccess = FreeImage_Save(FIF_PNG, bitmap, screenshotName.c_str(), PNG_Z_NO_COMPRESSION);
if (bSuccess == false)
      cout << "图片存储失败" << endl;
FreeImage_Unload(bitmap);
delete[] mpixels;
screenshotNum++;//设置一个全局变量第几帧

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值