Archived

This topic is now archived and is closed to further replies.

Snapshot

This topic is 5970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest Anonymous Poster
Hi There,

Here''s a routine I''ve written for my OpenGL - there''s lots of comments so you should be ok. How you actually save the RGB data is up to you (grab the open-JPEG lib)

//-----------------------------------------------------------------------------------
// GLubyte * OpenGLScreenGrab()
//
// User must release the returned buffer ! Will be NULL if failed.
//
// Note : glPixelTransfer() and glPixelMap() control the output format of the data,
// but by default, provide us with what we need. Also includes glPixelStore() but
// that is called elsewhere for texture creation.
//-----------------------------------------------------------------------------------
GLubyte * OpenGLScreenGrab()
{
bool grabbed = false;

// Clear out errors as this is going to be a critical routine for the screen saver
// 2D grabs ...
OpenGLErrorDump(true);

// Allocate space ...
int bytesPerRow = windowWidth * 3;

GLubyte * buffer1 = new GLubyte[bytesPerRow * windowHeight],
* buffer2 = new GLubyte[bytesPerRow * windowHeight];

// Check all mem grabs and function pointers are available for use ...
if (buffer1 && buffer2 && glReadPixels)
{
// Reads from the back buffer only. Note that this command *I think* is not available in
// OpenGL V1.0. The default in V1.0 is "to read pixel data from the framebuffer rectangle"
// which I assume to be enough for our purposes.
if (glReadBuffer)
glReadBuffer(GL_BACK);

if (!OpenGLErrorDump(true))
{
// Fetch the pixels ...
glReadPixels(0,0,windowWidth,windowHeight,GL_RGB,GL_UNSIGNED_BYTE,buffer1);

if (!OpenGLErrorDump(true))
{
grabbed = true;

// And do a flip for conveniance ...
for (int r = 0 ; r < windowHeight ; r++)
memcpy(buffer2 + bytesPerRow * r,buffer1 + ((windowHeight - r) - 1) * bytesPerRow,bytesPerRow);
}
}
}

// Did we grab it ?
if (!grabbed)
{
SafeDelete(buffer2);
buffer2 = NULL;
}

// Free buffer1 and return buffer2 which is left to the calling routine to delete ...
SafeDelete(buffer1);

return buffer2;
}



Share this post


Link to post
Share on other sites