Saving images from OpenGL applications

Written by Paul Bourke


It is not uncommon that one wants to be able to create and save images from an OpenGL application. This might be for promotional purposes, a WWW page, documentation, creating frames for an animation etc. This short note gives a simple example on how this can be done, making it elegant and suited to your own needs is left as an exercise.

Window Dump

The first example creates a raw image file but of course it can be modified to write any image format. The source code is given here: WindowDump(). The key parts are setting the read buffer correctly, glReadBuffer(GL_BACK_LEFT); and reading the buffer image glReadPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,image);

Note

Higher resolution

One restriction of the above method is that one is limited to screen resolution, how does one create a higher resolution image? One way to handle this is to render the scene N times (N = 4 in the following example), each time the viewport is set so as to capture the appropriate subimage.

   if (dobigdump) {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      CreateTheProjection();                /* - Application Specific - */
      for (i=0;i<4;i++) {
         for (j=0;j<4;j++) {
            fprintf(stderr,"Frame (%d,%d)\n",i,j);
            ClearTheBuffers();              /* - Application Specific - */
            glViewport(-i*width,-j*height,4*width,4*height);
            glMatrixMode(GL_MODELVIEW);
            glDrawBuffer(GL_BACK);
            glLoadIdentity();
            gluLookAt(vp.x,vp.y,vp.z,focus.x,focus.y,focus.z,up.x,up.y,up.z);
            MakeLighting();
            MakeMaterial();
            DrawTheModel();                 /* - Application Specific - */
            WindowDump();
            glutSwapBuffers();
         }
      }
      dobigdump = FALSE;
   }
Note