javaopengllwjglnifty-gui

Nifty GUI control prevents the rest of my application from rendering


I've been trying to add some basic gui elements to a openGl (via LWJGL) application using nifty gui, and while I've been successful in rendering panels and static text on top of the the applications graphics, using the built-in nifty controls (e.g. an editable text field) causes the rest of the application to not render. The strange part is that I don't even have to render the gui control, merely declaring it appears to cause this problem.

Compiler ready code showing the basic layout of the problem:

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.lwjgl.input.LwjglInputSystem;
import de.lessvoid.nifty.renderer.lwjgl.render.LwjglRenderDevice;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.TimeProvider;

public class NiftyBreaksRendering {
    private Nifty nifty;
    private Screen screen;
    public NiftyBreaksRendering() throws Exception{
            //init display
            Display.setDisplayMode(new DisplayMode(640,480));
            Display.create();

            //init nifty gui
              LwjglInputSystem inputSystem = new LwjglInputSystem();
              inputSystem.startup();
              nifty = new Nifty(
                new LwjglRenderDevice(),
              new NullSoundDevice(),
                inputSystem,
              new TimeProvider());
            // load default styles
            nifty.loadStyleFile("nifty-default-styles.xml");
            // load standard controls
            nifty.loadControlFile("nifty-default-controls.xml");
            screen = new ScreenBuilder("start") {{
                  layer(new LayerBuilder("baseLayer") {{
                    childLayoutHorizontal();
                    text(new TextBuilder("test"){{
                        font("aurulent-sans-16.fnt");
                        color("#f00f");
                        backgroundColor("#33af");
                        text("l33t");
                    }});

                    //begin: lines that break the rendering
                    control(new TextFieldBuilder("input","asdf") {{
                        width("200px");
                    }});
                    //end: lines that break the rendering
                  }});
                }}.build(nifty);
                nifty.gotoScreen("start");

        //init opengl
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,640,480,0,1,-1);
        glMatrixMode(GL_MODELVIEW);

        while(!Display.isCloseRequested())
        {
            //render

            glClear(GL_COLOR_BUFFER_BIT);

            glBegin(GL_QUADS);
                glVertex2i(400,400); //Upper left
                glVertex2i(450,400);//upper right
                glVertex2i(450,450);//bottom right
                glVertex2i(400,450);//bottom left
            glEnd();

            glBegin(GL_LINES);
                glVertex2i(100,100);
                glVertex2i(200,200);
            glEnd();

            nifty.render(false);
            Display.update();
            Display.sync(60);
        }
        Display.destroy();
    }

    public static void main(String[] args) throws Exception {
        new NiftyBreaksRendering();

    }

}

Solution

  • What would really help to diagnose this kind of problems is a link to a http://sscce.org/

    I guess this is related to OpenGL states changed by Nifty OR to textures being loaded by the Nifty controls which might mess up the rest of your application.

    If you could provide more or the complete code I'm pretty sure we can find the problem.

    Modified answer after complete example code was provided:

    Thanks for providing a complete example!

    As expected the problem is, that Nifty changes OpenGL state and leaves you with OpenGL basically in undefined state. The solution is to save your OpenGL states before you call Nifty and restore it afterwards. Here is some code that does exactly that. I've added the call to nifty.update() as well so that Nifty actually updates the GUI (and makes keyboard and mouse events work):

            // update nifty
            nifty.update();
    
            // save your OpenGL state
            // (assuming you are in glMatrixMode(GL_MODELVIEW) all the time)
            glPushMatrix();
            glPushAttrib(GL_ALL_ATTRIB_BITS);
    
            // render Nifty (this will change OpenGL state)
            nifty.render(false);
    
            // restore your OpenGL state
            glPopAttrib();
            glPopMatrix();
    

    With this change to your original code your example works for me now.