How to import Blender models into JOGL?

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

How to import Blender models into JOGL?

Estajxo
Hi there,

I want to import my Blender models into JOGL, which I suppose to be a fairly standard use case. However, I could not find any readily available solution to this wish.

I tried joglutils, which seems to be a little outdated and even worse, uncomplete: http://sourceforge.net/projects/joglutils/. When I import it into my project, I get 1342 errors stating that some classes could not be found.

I also looked around in the forum here, but the closest thing I found was a tutorial telling me how to program a port for Blender animations into LWJGL: http://forum.jogamp.org/Tutorial-Exporting-Animation-Data-From-Blender-2-5-API-td1755881.html#a1914726. A rather laborous task, which I only want to approach if absolutely necessary.

So, can it really be that for such a basic task no one already wrote an importer, not for any of the file formats Blender can export to? I guess it would be easy enough for me if it was only about vertices, edges and faces, but going into materials and animations probably exceeds my schedule.

Thanks for any hint or solution you can provide!
Estajxo
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Pixelapp
How about I tell you how I do it.

The reason you might not find any tutorials is because you don't need anything to import.

What I usually do is this. I export my object (i.e. Cube, Piramid) to myObject.x3d. Afterwards, myObject.x3d will have the vertices, colors and texture coordinates for you to copy and paste.

Make sure you check the "Triangulate" box if you are going to use only triangles, not quads.

I usually do this in Blender.
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

gouessej
Administrator
In reply to this post by Estajxo
Hi

You should use the version of JOGLUtils that is on Github; if it is not yet supporting JOGL 2.0, I will port it.

I have used JOGL since 2006 and I have imported all sorts of models without any problem, WaveFront OBJ, MD2, MD3, Collada, etc... I use a lot Blender, you can load Blender models with JMonkeyEngine 2.0 + my old renderer based on JOGL 1 + an extension supporting Blender format. Another solution consists in exporting your model to Collada to load it into Ardor3D (you can use it with my renderer based on JOGL 2.0), there is even an example of animated model on its repository.

Keep in mind that JOGL is essentially a low level Java binding for the OpenGL API, not a full scenegraph. You can find such loaders in Java3D, Xith3D, Ardor3D, JMonkeyEngine, Aviatrix3D, 3DzzD, etc...

Anyway, most loaders using plain C/C++ and OpenGL are easy to port to JOGL, I did that to load MD3 models in 2007. Best regards.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Demoscene Passivist
Administrator
In reply to this post by Estajxo
Maybe the Wavefront .obj loader from my repository is what you are looking for. Wavefront objects can be easily exported from blender (I use blender myself for exactly the same purpose).

You can either use the DisplayList based version or the VBO based version of the Wavefront loader.

The usage is quite easy:

mDisplayListID = WavefrontObjectLoader_DisplayList.loadWavefrontObjectAsDisplayList(inGL,"/binaries/geometry/TextureBaking_Normals.wobj.zip");

Works with vertices, polygons, normals and textures. So you can completely export a UV unwrapped model from Blender and simply use it in your JOGL application.
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
In reply to this post by gouessej
Hi, thanks for the quick responses!

I tried JOGLUtils on Github now, which looks much more complete. Still, I am having some problems with it. Some classes from JOGL seem to have been moved since this version, which is easy to fix, most of the time.

But quite some methods require an additional GLProfile parameter now. I am not sure how to correctly handle this. I suppose this is what you meant by
gouessej wrote
You should use the version of JOGLUtils that is on Github; if it is not yet supporting JOGL 2.0, I will port it.
?

Cheers,
Estajxo
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Demoscene Passivist
Administrator
For GL profiles and JOGL take a look here ...
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
In reply to this post by Demoscene Passivist
Thanks, I tried the DisplayList version, but did not succeed so far. When reading the .obj file created by Blender the BufferedReader seems to be created, but throws an uncaught exception. I cannot read into the details in the debugger, so I have no idea what is actually happening.
Will try more the next days...
Good night!
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Demoscene Passivist
Administrator
Did you triangulate the model during export in Blender ? The loader can't handle non triangle polygons ...
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
I did triangulate them now, but with the same result.
Also I am using a .obj file, no zip. But that should be ok as far as I can tell. BTW, is the "w" in .wobj.zip a typo in your example line?
La Estajxo
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
This is the error ouput:

LOADING WAVEFRONT OBJECT MODEL Modelle/Twonky.obj
Exception in thread "Thread-4" java.lang.RuntimeException: java.lang.NullPointerException
        at jogamp.newt.DefaultEDTUtil.invokeImpl(DefaultEDTUtil.java:189)
        at jogamp.newt.DefaultEDTUtil.invoke(DefaultEDTUtil.java:122)
        at jogamp.newt.DisplayImpl.runOnEDTIfAvail(DisplayImpl.java:191)
        at jogamp.newt.WindowImpl.runOnEDTIfAvail(WindowImpl.java:1578)
        at jogamp.newt.WindowImpl.setVisible(WindowImpl.java:784)
        at com.jogamp.newt.opengl.GLWindow.setVisible(GLWindow.java:336)
        at RoboRally.JOGLGUI.zeichneSpielfeld(JOGLGUI.java:373)
        at RoboRally.RoboRally.client(RoboRally.java:269)
        at RoboRally.RoboRally.access$4(RoboRally.java:237)
        at RoboRally.RoboRally$1ServerSpielerThread.run(RoboRally.java:319)
Caused by: java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:61)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
        at ModelLoader.WavefrontObjectLoader_DisplayList.LoadOBJModel(WavefrontObjectLoader_DisplayList.java:89)
        at ModelLoader.WavefrontObjectLoader_DisplayList.<init>(WavefrontObjectLoader_DisplayList.java:62)
        at ModelLoader.WavefrontObjectLoader_DisplayList.loadWavefrontObjectAsDisplayList(WavefrontObjectLoader_DisplayList.java:312)
        at RoboRally.JOGLGUI.init(JOGLGUI.java:1001)
        at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:150)
        at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:169)
        at com.jogamp.newt.opengl.GLWindow$InitAction.run(GLWindow.java:616)
        at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:423)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:364)
        at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:560)
        at com.jogamp.newt.opengl.GLWindow$1.windowResized(GLWindow.java:91)
        at jogamp.newt.WindowImpl.consumeWindowEvent(WindowImpl.java:2314)
        at jogamp.newt.WindowImpl.sendWindowEvent(WindowImpl.java:2257)
        at jogamp.newt.WindowImpl$FullScreenAction.run(WindowImpl.java:1787)
        at jogamp.newt.WindowImpl.createNative(WindowImpl.java:308)
        at jogamp.newt.WindowImpl.setVisibleActionImpl(WindowImpl.java:731)
        at jogamp.newt.WindowImpl$VisibleAction.run(WindowImpl.java:776)
        at com.jogamp.common.util.RunnableTask.run(RunnableTask.java:128)
        at jogamp.newt.DefaultEDTUtil$EventDispatchThread.run(DefaultEDTUtil.java:295)
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

gouessej
Administrator
Something is wrong in the path you supply, the resource is not found. Use Class.getResource() or Class.getResourceAsStream with the path "/Modelle/Twonky.obj". You may have to modify his loader if it does not support input streams but it is quite trivial.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
Hm, I used

System.err.println(Class.class.getResource("/Modelle/Twonky.obj"));
System.err.println(Class.class.getResourceAsStream("/Modelle/Twonky.obj"));
System.err.println(Class.class.getResource("Modelle/Twonky.obj"));
System.err.println(Class.class.getResourceAsStream("Modelle/Twonky.obj"));

and all yielded null, although the file is readily available (see screenshot):

What am I missing?
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

gouessej
Administrator
Rather do JOGLGUI.class.getResource("/Modelle/Twonky.obj"));

If it doesn't work, don't put the source code into a separate directory and allow binaries to be put directly into the root of the project, that's what I do for my game and it works.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
Still null...
Also I cannot enter the root directory as the output directory. Eclipse says, it is not possible to have the "src" folder inside the output folder, which it would be. Now I remember why I quit trying this a few years ago... :(
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
I suppose the path to the file is not the problem, because I can read out the file like this:

                try {
                        FileReader datei = new FileReader("Modelle/Twonky.obj");
                        BufferedReader br = new BufferedReader(datei);
                        String zeile;
                        while ((zeile = br.readLine()) != null) {
                                System.err.println(zeile);
                        }
                        br.close();
                        datei.close();
                } // try
                catch (IOException io) {
                        System.out.println("Sprachdatei nicht gefunden\n" + io.getMessage());
                        System.exit(1);
                }

The problem must be the getResourceAsStream() method, which is unable to find the obvious...
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

gouessej
Administrator
In reply to this post by Estajxo
Actually, you can modify the parameters directly in the .classpath file:
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>

If there is a "bin" entry, remove it. Maybe you should restart Eclipse after this modification.

Don't give up, you're close to the solution.

Edit.: You have to use getResource() and getResourceAsStream() or you will be unable to use JARs to package your game :s
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
In the output entry there is indeed "bin". But when I remove it and restart Eclipse, I cannot start the application because of a NoClassDefFoundError, same as when I remove it from within Eclipse (which Eclipse does not allow, in fact).
I read in another forum that getResource works well in Linux, but not quite (or not always) in other OSs. As my screenshot reveals, I use Windows...

gouessej wrote
Don't give up, you're close to the solution.
I really liked this sentence, thanks a lot! Now I only need to live up to it...

gouessej wrote
Edit.: You have to use getResource() and getResourceAsStream() or you will be unable to use JARs to package your game :s
Thanks for the hint, at some point this is what I will need to do. I already open files at another point with a FileReader, so I will have to change this, too, once I figured out how.
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
Hm, after a hint from some forum I tried to copy the "Modelle" directory into the "src" directory where my source code is. Et voilà, c'est ça! :)
So now let's try this model loader...
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Estajxo
This one works:
this.getClass().getClassLoader().getResourceAsStream("Modelle/Twonky.obj")

The one from the model loader does not work, yields null:
(new Object()).getClass().getResourceAsStream("Modelle/Twonky.obj")

This one does not work either, also null:
this.getClass().getResourceAsStream("Modelle/Twonky.obj")

Surprisingly, this one does not work at all and yields a NullPointException when i set the window visible:
(new Object()).getClass().getClassLoader().getResourceAsStream("Modelle/Twonky.obj")

If anyone understands the logic behind this, please tell me! :)
Reply | Threaded
Open this post in threaded view
|

Re: How to import Blender models into JOGL?

Pixelapp
To answer your question. The ones you show don't work because you either skip the getClassLoader() method or you don't get the correct present parameter which is "this". And that's the logic behind it.

This is what I do.

ClassLoader cl = this.getClass().getClassLoader();



12