1
\$\begingroup\$

I am trying to create a gui with nifty on top of lwjgl. I've already had some problems during the Nifty setup, however now Nifty is set up and running correctly but for text rendering. Here is my xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.4.xsd" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://niftygui.
sourceforge.net/nifty-1.4.xsd http://nifty-gui.sourceforge.net/nifty-1.4.xsd">
    <screen id="start">
        <layer childLayout="center">
            <panel id="panel" height="25%" width="35%" align="center" valign="center" backgroundColor="#f60f" childLayout="center" visibleToMouse="true">
                <text id="text" font="aurulent-sans-16.fnt" color="#ffff" text="Hello World!"/>
            </panel>
        </layer>
    </screen>
</nifty>

and here the respective java class:

package niftylwjgl;

import de.lessvoid.nifty.Nifty;
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.tools.TimeProvider;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class NiftyLwjgl {

    private static Nifty nifty;
    private static boolean close;
    private static LwjglInputSystem inSys;

    public static void main(String[] args) throws LWJGLException, Exception {
        init();
        initNifty();
        while (!close) {
            update();
            preRender();
            render();
            postRender();
        }
        destroy();
    }

    private static void init() throws LWJGLException {
        Display.setDisplayMode(Display.getDesktopDisplayMode());
        Display.setFullscreen(true);
        Display.create();

        GL11.glOrtho(0, 1920, 0, 1080, 1, -1);
    }

    private static void initNifty() throws Exception {
        inSys = new LwjglInputSystem();
        inSys.startup();

        nifty = new Nifty(new LwjglRenderDevice(),
                new NullSoundDevice(),
                inSys,
                new TimeProvider());
        nifty.fromXml("xml/main.xml", "start");
        nifty.loadStyleFile("nifty-default-styles.xml");
        nifty.loadControlFile("nifty-default-controls.xml");
    }

    private static void preRender() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();

        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glEnable(GL11.GL_TEXTURE_2D);
    }

    private static void postRender() {
        GL11.glDisable(GL11.GL_TEXTURE_2D);
    }

    private static void update() {
        if (Display.isCloseRequested()
                || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)
                || nifty.update()) {
            close = true;
        }
    }

    private static void render() {
        nifty.render(false);
        Display.update();
    }

    private static void destroy() {
        inSys.shutdown();
        Display.destroy();
    }
}

Here is a screenshot of the application running: enter image description here I somehow feel like I'm missing something incredible simple but important, like an glXXXXX call. Any help/suggestions are appreciated.

\$\endgroup\$
3
  • \$\begingroup\$ When you say "but for the text rendering", what exactly do you mean? Is it partly working? Can you post a screenshot? \$\endgroup\$ Aug 13, 2014 at 7:07
  • \$\begingroup\$ Check your font exists. \$\endgroup\$ Aug 13, 2014 at 8:33
  • \$\begingroup\$ I don't think it's a font problem. I just tryed a different font of which I'm sure it exists. Also, aurulent-sans-16 seems to be the standard font for nifty and is included in the nifty files as far as I know. \$\endgroup\$ Aug 13, 2014 at 8:52

1 Answer 1

1
\$\begingroup\$

You're not setting the projection matrix correctly. At the time you call glOrtho() you probably want to have the GL_PROJECTION matrix mode enabled.

Something like that


// set projection matrix
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 1920, 1080, 0, 1, -1);

// set modelview matrix to identity
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

Also note that Nifty assumes the coordinates (0,0) at the top left corner. That's why the third and fourth parameter should be switched.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for your advice, but this doesn't fix the problem. I tried your changes, however, text still isn't appearing. Everything else looks like before. \$\endgroup\$ Aug 15, 2014 at 12:01
  • \$\begingroup\$ The only thing I've additionally modified in your example - besides fixing the projection matrix - was: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - which I've forgot to mention :/ After these changes text rendering worked for me with your example code! If this doesn't help then please update your post with your current source code! \$\endgroup\$
    – void256
    Aug 15, 2014 at 13:02
  • \$\begingroup\$ Thank you very much! With these two statements added it works perfectly. \$\endgroup\$ Aug 15, 2014 at 15:31
  • \$\begingroup\$ @BenSteffan Why don't you mark this as the correct answer then? \$\endgroup\$
    – user46896
    Aug 21, 2014 at 2:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .