[ODE] X11 Capture frame on Mac OS X fix (via OpenGL)

john rieffel jrieffel at gmail.com
Thu Mar 10 22:37:47 MST 2005


All credit goes to Matt Hancher on this, but here is how to modify
drawstuff's captureFrame function (in x11.cpp) so that it'll work in
apple's x11 (and probably still work on linux, but I haven't tested
it).

The source of the problem is the XGetImage called by captureFrame.  In
hardware-rendered GL systems, like the mac, x11 never actually "sees"
the image being displayed - since it's all done in hardware - and so
as far as XGetImage is concerned, the window is empty.  This is why
all the captured frames are black, as both Adam Dershowitz and I
experienced.  The solution is to replace it with GLReadPixels.  Almost
everything else remains unchanged.

I've tested this in my own code (adapted from drawstuff) and it works
great.  Again, thanks to Matt Hancher.

static void captureFrame(int num)
{
  fprintf (stderr,"capturing frame %04d\n",num);

  char s[100];
  sprintf (s,"frame/%d.ppm",num);
  FILE *f = fopen (s,"wb");
    if (!f) dsError ("can't open \"%s\" for writing",s);
  fprintf (f,"P6\n%d %d\n255\n",width,height);

  void *buf = malloc( width * height * 3 );
  glReadPixels( 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, buf );
  
  for (int y=(height - 1); y>=0; y--) {
    for (int x=0; x<width; x++) {
      unsigned char *pixel = ((char *)buf)+((y*width+ x)*3);
      unsigned char b[3];
      b[0] = *pixel;
      b[1] = *(pixel+1);
      b[2] = *(pixel+2);
      fwrite (b,3,1,f);
    }
   }
  free(buf);
  fclose (f);
}


More information about the ODE mailing list