Skip to content

Instantly share code, notes, and snippets.

@pkmital
Created August 16, 2012 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkmital/3368990 to your computer and use it in GitHub Desktop.
Save pkmital/3368990 to your computer and use it in GitHub Desktop.
asynchronous readback
void setup()
{
pboIds = new GLuint[2];
pboIds[0] = 0; pboIds[1] = 0;
const int DATA_SIZE = screenWidth * screenHeight * 4;
const int PBO_COUNT = 2;
glGenBuffersARB(PBO_COUNT, pboIds);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]);
glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_READ_ARB);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]);
glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_READ_ARB);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
}
void draw()
{
// draw a bunch of stuff
//
//
if (bSaveMovie) {
// Asynchronous readback
glReadBuffer(GL_BACK);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]);
glReadPixels(0, 0, screenWidth, screenHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]);
glReadPixels(0, 0, screenWidth, screenHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]);
GLubyte* src = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
int y_offset = 0;//screenHeight - videoHeight;
int x_offset = 0;//videoWidth;
// save movie // RGB <- BGRA
if(src)
{
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
int idx = (x+y*(screenWidth))*3;
int idx2 = 4*(x+x_offset)+4*(screenHeight-y-1+y_offset)*screenWidth;
readBackFrame[idx+0] = src[idx2+2];
readBackFrame[idx+1] = src[idx2+1];
readBackFrame[idx+2] = src[idx2+0];
}
}
if (bSaveImages)
{
ofImage img;
img.setFromPixels(readBackFrame, screenWidth, screenHeight, OF_IMAGE_COLOR, true);
char buf[256];
sprintf(buf, "frame_%08d.jpg", currentFrame);
img.saveImage(ofToDataPath(buf), OF_IMAGE_QUALITY_BEST);
}
videoSaver.addFrame(readBackFrame, 1.0f / fps);
}
glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment