Reputation: 601
I am trying to save the openGL screen to a file using this explanation but the images I receive are not valid:
What can be a reason for this?
I am using glfw with double buffer, which has been reported to make problems in some cases. However, using glFlush() freezes my screen. :/
May it happen, that the glReadPixels method is called while buffer swap is performed? I actually use a Mutex in order to prevent this.
Do I need some trick in glfw for this?
Here is the code for reference:
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();
Thank you very much for any hint!
Upvotes: 1
Views: 2893
Reputation: 601
The problem was that I tried to grab the pixel buffer from another thread. All the openGL commands must be executed from the same thread.
Upvotes: 4