copying the z-buffer


I’m pulling my hair out trying to figure out how to do this with
OpenGL. I have a program with a scene that takes forever to render.
I also have one small object that I want to move around in that scene.
So what I want to do is render the big scene just once, then
repeatedly copy the rendered scene and draw the small moving object
into the scene whenever I need to move it.

I’ve been trying to do this by rendering into the back buffer and then
using the glCopyPixels() function to copy the GL_COLOR and GL_DEPTH
stuff to the front buffer, then drawing the small object in the front
buffer. The glCopyPixels(…, GL_COLOR) seems to work, but the call
to glCopyPixels(…, GL_DEPTH) just makes a mess of the front buffer.
What am I doing wrong? Is there a better way? eg. I looked for
examples using LPD_SWAP_COPY but didn’t find anything substantial.

Here’s a code sample that doesn’t work.

int staticbuffer = GL_BACK;
int screenbuffer = GL_FRONT;

// Render the huge scene once into the staticbuffer
reshape(Width, Height); // Set up viewport, projection, aspect…
glEnable( GL_DEPTH_TEST ); // Enable z-buffer for scene.
render(); // Render the huge scene.

while (1)
// get fresh copy of the static rendered scene.
glReadBuffer(staticbuffer); // set pixel source
glDrawBuffer(screenbuffer); // set pixel destination
glDisable( GL_DEPTH_TEST ); // Speed up copying?
glDisable(GL_LIGHTING); // Speed up copying?
glMatrixMode( GL_PROJECTION );
gluOrtho2D(0, Width, 0, Height);
glMatrixMode( GL_MODELVIEW );
glRasterPos2i(0, 0);
glCopyPixels(0, 0, Width, Height, GL_COLOR); // This seems to work.
glRasterPos2i(0, 0);
glCopyPixels(0, 0, Width, Height, GL_DEPTH); // This makes a MESS!!
reshape(Width, Height); // Set up viewport, projection, aspect…
glEnable( GL_DEPTH_TEST );

  // Now draw the small moving object.


if your driver supports it wgl_buffer_region would be perfect for this. btw i have an example on my site. (url in profile)

Thanks for the pointer. I couldn’t run your example program. I Got
an error like __glutCreateWindowWithExit couldn’t be located in glut32.dll.
I thought I had the latest glut. Oh well. I’ll have to install openIL
to compile it myself. But I don’t think this is exactly what I want anyhow.
I need something portable that works with the least common denominator.
Then maybe I can add code to look for extensions to speed it up. Everything
I’ve seen seems to indicate that glCopyPixels should work, but slowly.

I found the evidence at these locations, but I must be overlooking something.
I just don’t know what…………

I hate to reply to myself, but… I finally got this working (sort
of) but now I have a new question. I was under the impression that
there were two depth buffers, one for the front buffer and one for the
back buffer, and that you could use glCopyPixels between the two depth
buffers. I now believe that there is only one depth buffer shared
between the front and back buffers. Is this true?

Once I reached this conclusion I changed my code to use glReadPixels
to backup the depth buffer into system ram, and then glDrawPixels to
restore the depth buffer before drawing the small moving object. This