Simple example with Nifty

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple example with Nifty

elect
Hi, I am trying to get a simple example with Nifty working, but I can't

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import com.jogamp.opengl.util.FPSAnimator;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.batch.BatchRenderDevice;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.jogl.input.JoglInputSystem;
import de.lessvoid.nifty.renderer.jogl.render.batch.JoglBatchRenderBackendCoreProfile;
import de.lessvoid.nifty.spi.render.RenderDevice;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import de.lessvoid.nifty.spi.time.TimeProvider;

/**
 *
 * @author gbarbieri
 */
public class Test2 implements GLEventListener {

    private GLCanvas canvas;
    private int imageWidth;
    private int imageHeight;
    private Nifty nifty;
    private JoglInputSystem inputSystem;
    private RenderDevice renderDevice;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Test2 test2 = new Test2();

        Frame frame = new Frame("Test2");

        frame.add(test2.canvas);

        frame.setSize(test2.canvas.getWidth(), test2.canvas.getHeight());

        final FPSAnimator fPSAnimator = new FPSAnimator(test2.canvas, 30);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                fPSAnimator.stop();
                System.exit(0);
            }
        });

        fPSAnimator.start();

        frame.setVisible(true);
    }

    public Test2() {
       
        initGL();
    }

    private void initGL() {

        imageWidth = 800;
        imageHeight = 600;

        GLProfile profile = GLProfile.get(GLProfile.GL3);

        GLCapabilities capabilities = new GLCapabilities(profile);

        canvas = new GLCanvas(capabilities);

        canvas.setSize(imageWidth, imageHeight);

        canvas.addGLEventListener(this);
    }

    @Override
    public void init(GLAutoDrawable drawable) {

        renderDevice = new BatchRenderDevice(new JoglBatchRenderBackendCoreProfile(), 2048, 2048);
       
        inputSystem = new JoglInputSystem();
       
//        canvas.addMouseListener(inputSystem);
       
        nifty = new Nifty(renderDevice, new NullSoundDevice(), inputSystem, new TimeProvider() {

            @Override
            public long getMsTime() {
               
                return System.currentTimeMillis();
            }
        });

        nifty.fromXmlWithoutStartScreen("test/helloworld.xml");
       
        canvas.setAutoSwapBufferMode(true);
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        System.out.println("display");
       
        GL3 gl3 = drawable.getGL().getGL3();

        gl3.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        gl3.glClearDepthf(1.0f);
        gl3.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
       
        nifty.render(true);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
       
        GL3 gl3 = drawable.getGL().getGL3();

        gl3.glViewport(x, y, width, height);
    }
}


Caused by: java.lang.NoClassDefFoundError: org/jglfont/spi/BitmapFontRenderer
        at test.Test2.init(Test2.java:90)

Am I doing it right? If yes, where can I find the missing jar?

I am trying to figure it out from this hello world example

http://sourceforge.net/apps/mediawiki/nifty-gui/index.php?title=Hello_World_Example

In addition in the "examples for JOGL", he does something like

window.addMouseListener(inputSystem);
    window.addKeyListener(inputSystem);


Where

window = GLWindow.create(new GLCapabilities(getProfile(mode)));

But if I try to do

canvas.addMouseListener(inputSystem);

It returns error..
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

elect
So I managed to fix all the damned dependences, jglfont-core, xpp3_min-1.1.4c and eventbus-1.4..

Now I dont see anything, while I am supposed to see the picture you see in the hello world link..

I guess it might have something to do with the missing mouse listener..

I cant get it, inputSystem extend the mouseListener, but when i try to add it to the canvas

canvas.addMouseListener(inputSystem);

method addMouseListener in class Component cannot be applied to given types;
  required: MouseListener
  found: JoglInputSystem
  reason: actual argument JoglInputSystem cannot be converted to MouseListener by method invocation conversion
----
(Alt-Enter shows hints)


What?
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

elect
Ah, canvas.addMouseListener wants a mouseListener coming from the

java.​awt.​event

While my inputSystem extend the mouseListener coming from the

com.​jogamp.​newt.​event

How should I do?
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

Xerxes Rånby
There is a NEWT JOGL 2 nifty-gui backend found in the 1.3 nifty gui branch. This branch work on both mobile and desktop devices using the GL2ES2 profile.
https://github.com/void256/nifty-gui/commits/1.3

This gist show how you can setup and test the 1.3 nifty gui branch
This is how we demonstrated nifty gui using JOGL 2 at SIGGRAPH 2013
https://gist.github.com/xranby/e09daa4997cdd19b474e

Video from the SIGGRAPH 2013 BOF:
http://nifty-gui.lessvoid.com/archives/585
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

elect
Xerxes Rånby wrote
There is a NEWT JOGL 2 nifty-gui backend found in the 1.3 nifty gui branch. This branch work on both mobile and desktop devices using the GL2ES2 profile.
https://github.com/void256/nifty-gui/commits/1.3
You mean this one?

https://github.com/void256/nifty-gui/blob/1.4/nifty-examples-jogl/src/main/java/de/lessvoid/nifty/examples/jogl/JOGLNiftyRunner.java

It does be the one I have been starting porting from..

There a GLwindow is used instead of canvas, but isnt canvas compatible too?

Xerxes Rånby wrote
This gist show how you can setup and test the 1.3 nifty gui branch
This is how we demonstrated nifty gui using JOGL 2 at SIGGRAPH 2013
https://gist.github.com/xranby/e09daa4997cdd19b474e

Video from the SIGGRAPH 2013 BOF:
http://nifty-gui.lessvoid.com/archives/585
Yep, those are the sources that made me gave a try to Nifty
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

Xerxes Rånby
elect wrote
Xerxes Rånby wrote
There is a NEWT JOGL 2 nifty-gui backend found in the 1.3 nifty gui branch. This branch work on both mobile and desktop devices using the GL2ES2 profile.
https://github.com/void256/nifty-gui/commits/1.3
You mean this one?

https://github.com/void256/nifty-gui/blob/1.4/nifty-examples-jogl/src/main/java/de/lessvoid/nifty/examples/jogl/JOGLNiftyRunner.java

It does be the one I have been starting porting from..

There a GLwindow is used instead of canvas, but isnt canvas compatible too?


Oh kudos for porting the jogl 2 backend it over to the 1.4 branch!



Try avoid using the old GLCanvas since it depend on AWT and this prevent the code to run on new devices such as Android and Raspberry Pi where AWT cant be found or be hardware accelerated.

If you must use AWT/Swing then place the GLWindow inside the Swing or AWT application using a NewtCanvasAWT . This is the only canvas compatible with NEWT.
Try do somehing like:
NewtCanvasAWT newtCanvas = new NewtCanvasAWT(glWindow);
frame.add(newtCanvas);

http://jogamp.org/jogl/doc/NEWT-Overview.html
http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/awt/NewtCanvasAWT.html
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

elect
This post was updated on .
Xerxes Rånby wrote
Oh kudos for porting the jogl 2 backend it over to the 1.4 branch!



Try avoid using the old GLCanvas since it depend on AWT and this prevent the code to run on new devices such as Android and Raspberry Pi where AWT cant be found or be hardware accelerated.

If you must use AWT/Swing then place the GLWindow inside the Swing or AWT application using a NewtCanvasAWT . This is the only canvas compatible with NEWT.
Try do somehing like:
NewtCanvasAWT newtCanvas = new NewtCanvasAWT(glWindow);
frame.add(newtCanvas);

http://jogamp.org/jogl/doc/NEWT-Overview.html
http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/awt/NewtCanvasAWT.html
No, for the moment I dont have to use it, it was just because I was used to use glcanvas

So GLwindow is better and the future? Ok, I am going to use that then

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

elect
So I modified it in order to use the GLwindows, I could now add the mouse and keylistener, I also modified the nifty initialization, now it is working, but I see the written ugly



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.batch.BatchRenderDevice;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.renderer.jogl.input.JoglInputSystem;
import de.lessvoid.nifty.renderer.jogl.render.batch.JoglBatchRenderBackendCoreProfile;
import de.lessvoid.nifty.spi.render.RenderDevice;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import de.lessvoid.nifty.spi.time.TimeProvider;

/**
 *
 * @author gbarbieri
 */
public class Test2 implements GLEventListener {

    private GLWindow glWindow;
    private int imageWidth;
    private int imageHeight;
    private Nifty nifty;
    private JoglInputSystem inputSystem;
    private RenderDevice renderDevice;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Test2 test2 = new Test2();
    }

    public Test2() {

        initGL();
    }

    private void initGL() {

        imageWidth = 800;
        imageHeight = 600;

        GLProfile profile = GLProfile.get(GLProfile.GL3);

        GLCapabilities capabilities = new GLCapabilities(profile);

        glWindow = GLWindow.create(capabilities);
       
        glWindow.setAutoSwapBufferMode(true);
       
        glWindow.setSize(imageWidth, imageHeight);
       
        glWindow.addGLEventListener(this);
       
        FPSAnimator animator = new FPSAnimator(glWindow, 60, false);
       
        glWindow.setTitle("Test2");
       
        glWindow.setVisible(true);
       
        animator.start();
    }

    @Override
    public void init(GLAutoDrawable drawable) {

        renderDevice = new BatchRenderDevice(new JoglBatchRenderBackendCoreProfile(), 2048, 2048);

        inputSystem = new JoglInputSystem();

        glWindow.addMouseListener(inputSystem);
        glWindow.addKeyListener(inputSystem);

        nifty = new Nifty(renderDevice, new NullSoundDevice(), inputSystem, new TimeProvider() {
            @Override
            public long getMsTime() {

                return System.currentTimeMillis();
            }
        });

        nifty.fromXml("test/helloworld.xml", "start");
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void display(GLAutoDrawable drawable) {
       
        nifty.update();
        nifty.render(true);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

        GL3 gl3 = drawable.getGL().getGL3();

        gl3.glViewport(x, y, width, height);
    }
}


But I guess it is just now a matter of playing with it..

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

gouessej
Administrator
Rather use JOGLNiftyRunner and NiftyTutorialJOGL:
https://github.com/void256/nifty-gui/tree/1.4/nifty-examples-jogl/src/main/java/de/lessvoid/nifty/examples/jogl

I ported NiftyGUI to JOGL 1 several years ago, I don't advise you to get outside of the "straight road". This API is easy to use when you start from existing examples but it becomes hard when you start from scratch. If you miss some initialization code, you end up with a black screen or worse.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Simple example with Nifty

elect
gouessej wrote
Rather use JOGLNiftyRunner and NiftyTutorialJOGL:
https://github.com/void256/nifty-gui/tree/1.4/nifty-examples-jogl/src/main/java/de/lessvoid/nifty/examples/jogl

I ported NiftyGUI to JOGL 1 several years ago, I don't advise you to get outside of the "straight road". This API is easy to use when you start from existing examples but it becomes hard when you start from scratch. If you miss some initialization code, you end up with a black screen or worse.
I think void256 is really behind with the documentation, I am not blaming, I cannot even image how much work doing such a tool requires, so I totally understand the lack of updated documentation (really boring duty after you spent a lot of effort on cool stuffs)

Based on his own words, although the stable version seems to be the 1.4, he says the best source of documentation is the 1.3.2 manual

http://stackoverflow.com/questions/19092658/most-recent-nifty-gui-initialization

Said that, copying the helloword.xml from there works like a charm

<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://niftygui.
sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">
    <screen id="start">
        <layer childLayout="center">
            <text font="aurulent-sans-16.fnt" color="#ffff"
      text="Hello World!" />
        </layer>
    </screen>
</nifty>

Then I think I will go digging that manual ^^