1

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();

    }

}
zergylord
  • 4,368
  • 5
  • 38
  • 60
  • 1
    What does `application.draw` do? And I don't mean for you to describe it; *show us*. – Nicol Bolas Nov 16 '12 at 01:03
  • Its a pretty long routine, but the problem persists if I try to simply draw a rectangle, so I've added that to the example code – zergylord Nov 16 '12 at 01:36
  • Why did you remove the correct part from your code? You know, the part where you were setting your matrices each frame. – Nicol Bolas Nov 17 '12 at 01:01
  • Oh, I just wrote up a quick example that replicates the problem, as per one of the other comments. I'm using pixel-space coords here, so I figured it wasn't necessary. – zergylord Nov 17 '12 at 01:04

2 Answers2

3

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.

void256
  • 1,286
  • 10
  • 11
  • thanks for the suggestion! I added a short example that replicates the problem. It should compile fine if you have lwjgl and nifty gui installed. – zergylord Nov 17 '12 at 00:57
  • Works like a charm! Any chance you know of a good opengl reference too prevent me from asking more stupid questions? O:-) – zergylord Nov 18 '12 at 00:43
  • 1
    That really is hard to answer without knowing your level of OpenGL knowledge or what you want to do with it. For reference I'd look directly at the [OpenGL reference pages](http://www.opengl.org/sdk/docs/man/) or the [OpenGL spec pdf](http://www.opengl.org/registry/). For general learning I've found the [OpenGL Super Bible](http://www.opengl.org/documentation/books/#opengl_superbible_4th_edition) helpful. If you're new to OpenGL in general I'd suggest starting with core profile. It might be a bit harder to learn (and Nifty does not support it yet - but will do so soon ;-) but it's the future. – void256 Nov 18 '12 at 12:25
0

NiftyGUI (as a non-shader-based render) will at some point modify the modelview matrix. Do you clean up the damage it does to the modelview matrix, by initializing it with identity? You know, like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLU.gluOrtho2D(0f,(float)VIEWPORT_DIMENSIONS[0],0f,(float)VIEWPORT_DIMENSIONS[1]);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(translation[0],translation[1],0);
glRectf(-1,-1,1,1);
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I tried this, but it didn't seem to help. :-/ Perhaps I'm inserting the identity call in the wrong place, but I think I tried everywhere it seemed reasonable (though this was pretty rote since I'm quite new to opengl). Thanks for the suggestion though! – zergylord Nov 17 '12 at 00:59