Hi,
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
glDrawBuffer(staticbuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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 );
glLoadIdentity();
gluOrtho2D(0, Width, 0, Height);
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
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!!
glPopMatrix();
reshape(Width, Height); // Set up viewport, projection, aspect…
glEnable( GL_DEPTH_TEST );
// Now draw the small moving object.
DrawMovingObject();
}