在OpenGL中创建屏幕截图不起作用

时间:2015-11-17 13:01:38

标签: opengl glfw

我正在尝试使用this解释将openGL屏幕保存到文件中,但我收到的图片无效: enter image description here

这可能是什么原因?

  • 我正在使用带有双缓冲区的glfw,据报道在某些情况下会出现问题。但是,使用glFlush()会冻结我的屏幕。 :/

  • 可能会发生,在执行缓冲区交换时调用glReadPixels方法?我实际上使用Mutex来防止这种情况发生。

  • 我在glfw中需要一些技巧吗?

以下是供参考的代码:

glfwGetWindowSize(&mWidth, &mHeight);

glBufferLock.lock();
std::cout << "\nSaving screenshot (" << mWidth << ", " << mHeight << ")\n";

int n=3 * mWidth * mHeight;
GLubyte* pixels = new GLubyte[n];

glPixelStorei(GL_PACK_ALIGNMENT, 1);


glReadPixels(0,0,mWidth,mHeight, GL_RGB, GL_UNSIGNED_BYTE, pixels);
if (GL_NO_ERROR != glGetError()) throw "Error: Unable to read pixels.";

// Convert to FreeImage format & save to file
FIBITMAP* image = FreeImage_ConvertFromRawBits(pixels, mWidth, mHeight, 3 * mWidth, 24,  0xFF0000, 0x00FF00, 0x0000FF, false);
FreeImage_Save(FIF_BMP, image, "test.bmp", 0);

// Free resources
FreeImage_Unload(image);
delete [] pixels;
glBufferLock.unlock();

非常感谢你的提示!

1 个答案:

答案 0 :(得分:2)

问题是我试图从另一个线程中获取像素缓冲区。所有openGL命令必须从同一个线程执行。