Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 744
doron Avatar asked Sep 18 '12 12:09

doron


2 Answers

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.

like image 58
doron Avatar answered Nov 20 '22 19:11

doron


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.

like image 1
ltjax Avatar answered Nov 20 '22 21:11

ltjax