State saving for multithreaded application

Hi everyone,
I am working with a 3D modelling application with a GUI that will allow you to export a selected object to a file. I’ve allowed any glBegin/glEnd block to be selectable by the user.

So my problem is, if exporting the geometry to the file at any given time I am missing whatever state information OpenGL is having. So to render this object in a completely fresh OpenGL state, I need to reload all the appropriate matrices and bits.

So far I have saved the Projection, Modelview, Texture, Colour matrix stacks. What else do I need?

example for anywhere in the code


glBegin(...);
   glColor(...);
   glNormal(...);
   glVertex(...);
   ...
glEnd();

if (recording) 
{
   // save opengl state
   // psuedo
   for each matrix type MT
    d = matrix_stack_depth for MT from OGL
    while d > 0
       save current matrix to X
       glPopMatrix();
   // restore the OpenGL state
   for each matrix type MT
    d = matrix_stack_depth for MT from X
    while d > 0
       glLoadMatrixf( from X );
       glPushMatrix();
    
   // what else? glAttrib/glClientAttrib in the same way?

   // re-render but to file.
   write(glBegin); write(...);
      write(glColor); write(...);
      write(glNormal); write(...);
      write(glVertex); write(...);
      ...
   write(glEnd);
}

I can do the object exporting just fine. But it would be nice if OpenGL had something to extract the current state at any given time. Are there any libraries out there that can do this?

Since it is your application that modifies the gl states, you may record them in your modeler for each object as long as you modify the opengl states. Then you won’t have to retrieve all states from opengl.

But I don’t think this is a good idea since you make your format very dependent on opengl and I don’t think it should. You should know when reading the object file, which states to set if the format is well written.

actually its not my app. and the source is quite large enough to warrant a cleaner generic solution, even if it is inefficient. The format based on OpenGL is a design choice on my behalf and beyond contestation :stuck_out_tongue: