Skip to content

How to save OpenGL state when render with Nifty

void256 edited this page Jan 31, 2015 · 2 revisions

Note: This section applies to Nifty 1.x rendering...

OpenGL immediate mode

This is when you use regular, old, immediate mode OpenGL rendering. You have to save all states that Nifty changes internally. At the moment it's not very well documented so it's probably best to save all the states like in the sample below.

Additionally you want to make sure you are in 2d-Ortho mode when calling Nifty (FIXME: add more example code that shows how to do that right here).

// update nifty
nifty.update();

// save your OpenGL state
// (assuming you are in glMatrixMode(GL_MODELVIEW) all the time)
glPushMatrix();
glPushAttrib(GL_ALL_ATTRIB_BITS);

// set up GL state for Nifty rendering
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, viewportWidth, viewportHeight, 0, -9999, 9999);

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

GL11.glDisable(GL11.GL_DEPTH_TEST);
glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_CULL_FACE);

glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_NOTEQUAL, 0);

GL11.glDisable(GL11.GL_LIGHTING);
glEnable(GL11.GL_TEXTURE_2D);

// render Nifty (this will change OpenGL state)
nifty.render(false);

// restore your OpenGL state
glPopAttrib();
glPopMatrix();

We are away that this is not the best way to do that and Nifty 2.x will probably be smarter about that (hopefully 😉)

OpenGL core profile

TODO: Add example code to explain how to save state when using one of the Core profile renderers.