mojovski
mojovski

Reputation: 601

Creating Screenshot in OpenGL not working

I am trying to save the openGL screen to a file using this explanation but the images I receive are not valid: enter image description here

What can be a reason 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

Answers (1)

mojovski
mojovski

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

Related Questions