doron
doron

Reputation: 28882

Restoring OpenGL State

I want to write a general purpose utility function that will use an OpenGL Framebuffer Object to create a texture that can be used by some OpenGL program for whatever purpose a third party programmer would like.

Lets say for argument stake the function looks like

void createSpecialTexture(GLuint textureID)
{
    MyOpenGLState state;
    saveOpenGLState(state);
    setStateToDefault();
    doMyDrawing();
    restoreOpenGLState(state);
}

What should MyOpenGLState, saveOpenGLState(state), setStateToDefault() and restoreOpenGLState(state) look like to ensure that doMyDrawing will behave correctly and that nothing that I do in doMyDrawing will affect anything else that a third party developer might be doing?

The problem that has been biting me is that OpenGL has a lot of implicit state and I am not sure I am capturing it all.

Update: My main concern is OpenGL ES 2.0 but I thought I would ask the question more generally

Upvotes: 6

Views: 2258

Answers (3)

doron
doron

Reputation: 28882

Don't use a framebuffer object to render your texture. Rather create a new EGLContext and EGLSurface. You can then use eglBindTexImage to turn your EGLSurface into a texture. This way you are guaranteed that state from doMyDrawing will not pollute the main gl Context and visa versa.

Upvotes: 2

Dan
Dan

Reputation: 1486

That depends a lot on what yo do on your doMyDrawing();, but basically you have to restore everything (all states) that you change in this function. Without having a look at what is going on inside doMyDrawing(); it is impossible to guess what you have to restore.

If modifications on the projection or modelview matrix are done inside doMyDrawing(), remember to go initially push both GL_PROJECTION and GL_MODELVIEW matrix through glPushMatrix and restore them after the drawing through glPopMatrix. Any other state that you modify can be push and pop though glPushAttrib and the right attribute. Remember also to unbind any texture, FBO, PBO, etc.. that you might bind inside doMyDrawing();

Upvotes: 0

ltjax
ltjax

Reputation: 15997

As for saving and restoring, glPushAttrib and glPopAttrib will get you very far.

You cannot, however, restore GL to a default state. However, since doMyDrawing() uses and/or modifies only state that should be known to you, you can just set that to values that you need.

Upvotes: 1

Related Questions