Loading models

Hello,



I'm having some trouble loading a model. The script i'm using has no errors in it, but if it tries to load the models something goes wrong and the application ends itself.

This is the code i used:



public class ModelInladen extends SimpleGame {



    private static final Logger logger = Logger

            .getLogger(ModelInladen.class.getName());





public static void main(String[] args)

        {

        ModelInladen app = new ModelInladen();

        app.setConfigShowMode(ConfigShowMode.AlwaysShow);

        app.start();

        }



       

        @Override

        protected void simpleInitGame()

        {

        // Point to a URL of the models

        URL model=ModelInladen.class.getClassLoader().getResource("C:/models/truck.obj");

        URL model1=ModelInladen.class.getClassLoader().getResource("C:/models/boot.obj");

        URL model2=ModelInladen.class.getClassLoader().getResource("C:/models/container.obj");

        URL model3=ModelInladen.class.getClassLoader().getResource("C:/models/crane.obj");



        // Create something to convert .obj format to .jme

        FormatConverter converter=new ObjToJme();



        // Point the converter to where it will find the .mtl file from

        converter.setProperty("mtllib",model);

        converter.setProperty("mtllib",model1);

        converter.setProperty("mtllib",model2);

        converter.setProperty("mtllib",model3);



        // This byte array will hold the .jme files

        ByteArrayOutputStream BO=new ByteArrayOutputStream();

        try {

            // Use the format converter to convert .obj to .jme

            converter.convert(model.openStream(), BO);

            converter.convert(model1.openStream(), BO);

            converter.convert(model2.openStream(), BO);

            converter.convert(model3.openStream(), BO);





            Node truck=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

            Node boat=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

            Node container=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));

            Node crane=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));



            // shrink the models

            truck.setLocalScale(.1f);

            truck.setModelBound(new BoundingSphere());

            truck.updateModelBound();



            boat.setLocalScale(.1f);

            boat.setModelBound(new BoundingSphere());

            boat.updateModelBound();



            container.setLocalScale(.1f);

            container.setModelBound(new BoundingSphere());

            container.updateModelBound();





            crane.setLocalScale(.1f);

            crane.setModelBound(new BoundingSphere());

            crane.updateModelBound();



            // Put the models on the scene graph

            rootNode.attachChild(truck);

            rootNode.attachChild(boat);

            rootNode.attachChild(container);

            rootNode.attachChild(crane);

        }



        catch (IOException e) {  // Just in case anything happens

            logger.logp(Level.SEVERE, this.getClass().toString(),

                    "simpleInitGame()", "Exception", e);

            System.exit(0);

        }



I got it from the website, from a tutorial called Hello ModelLoading.



If i run it, i get this in the debug window



3-dec-2009 13:48:40 com.jme.app.BaseGame start

INFO: Application started.

3-dec-2009 13:48:40 com.jme.system.PropertiesGameSettings <init>

INFO: PropertiesGameSettings created

3-dec-2009 13:48:40 com.jme.system.PropertiesGameSettings load

INFO: Read properties

3-dec-2009 13:48:42 com.jme.system.PropertiesGameSettings save

INFO: Saved properties

3-dec-2009 13:48:42 com.jme.app.BaseSimpleGame initSystem

INFO: jME version 2.0 Stable (r4093)

3-dec-2009 13:48:42 com.jme.input.joystick.DummyJoystickInput <init>

INFO: Joystick support is disabled

3-dec-2009 13:48:42 com.jme.system.lwjgl.LWJGLDisplaySystem <init>

INFO: LWJGL Display System created.

3-dec-2009 13:48:43 com.jme.renderer.lwjgl.LWJGLRenderer <init>

INFO: LWJGLRenderer created. W: 640 H: 480        Version: 2.1.0

3-dec-2009 13:48:44 com.jme.app.BaseSimpleGame initSystem

INFO: Running on: nv4_disp

Driver version: 6.14.11.8122

NVIDIA Corporation - GeForce 9800 GT/PCI/SSE2/3DNOW! - 2.1.2

3-dec-2009 13:48:44 com.jme.renderer.AbstractCamera <init>

INFO: Camera created.

3-dec-2009 13:48:44 com.jme.util.lwjgl.LWJGLTimer <init>

INFO: Timer resolution: 1000 ticks per second

3-dec-2009 13:48:44 com.jme.scene.Node <init>

INFO: Node created.

3-dec-2009 13:48:44 com.jme.scene.Node <init>

INFO: Node created.

3-dec-2009 13:48:44 com.jme.scene.Node <init>

INFO: Node created.

3-dec-2009 13:48:44 com.jme.scene.Node attachChild

INFO: Child "Graph node" attached to this node "Stats node"

3-dec-2009 13:48:44 com.jme.renderer.lwjgl.LWJGLTextureRenderer <init>

INFO: FBO support detected.

3-dec-2009 13:48:44 com.jme.renderer.lwjgl.LWJGLTextureRenderer initCamera

INFO: Init RTT camera

3-dec-2009 13:48:44 com.jme.renderer.AbstractCamera <init>

INFO: Camera created.

3-dec-2009 13:48:44 com.jme.scene.Node <init>

INFO: Node created.

3-dec-2009 13:48:44 com.jme.scene.Line <init>

INFO: Line created.

3-dec-2009 13:48:44 com.jme.renderer.lwjgl.LWJGLTextureRenderer setupTexture

INFO: setup fbo tex with id 2: 640,360

3-dec-2009 13:48:44 com.jme.scene.Node attachChild

INFO: Child "lineGraph" attached to this node "Graph node"

3-dec-2009 13:48:45 com.jme.scene.Node attachChild

INFO: Child "f4" attached to this node "Graph node"

3-dec-2009 13:48:45 com.jme.renderer.lwjgl.LWJGLTextureRenderer <init>

INFO: FBO support detected.

3-dec-2009 13:48:45 com.jme.renderer.lwjgl.LWJGLTextureRenderer initCamera

INFO: Init RTT camera

3-dec-2009 13:48:45 com.jme.renderer.AbstractCamera <init>

INFO: Camera created.

3-dec-2009 13:48:45 com.jme.scene.Node <init>

INFO: Node created.

3-dec-2009 13:48:45 com.jme.renderer.lwjgl.LWJGLTextureRenderer setupTexture

INFO: setup fbo tex with id 3: 640,120

3-dec-2009 13:48:45 com.jme.scene.Node attachChild

INFO: Child "labelGraph" attached to this node "Graph node"

3-dec-2009 13:48:45 class Graphics.ModelInladen start()

SEVERE: Exception in game loop

java.lang.NullPointerException

        at Graphics.ModelInladen.simpleInitGame(ModelInladen.java:66)

        at com.jme.app.BaseSimpleGame.initGame(BaseSimpleGame.java:545)

        at com.jme.app.BaseGame.start(BaseGame.java:74)

        at Graphics.ModelInladen.main(ModelInladen.java:40)

3-dec-2009 13:48:45 com.jme.app.BaseSimpleGame cleanup

INFO: Cleaning up resources.

3-dec-2009 13:48:45 com.jme.app.BaseGame start

INFO: Application ending.

BUILD SUCCESSFUL (total time: 14 seconds)



I have no idea what to do against it. I tried to load just 1 model but it still gives the same effect.

Does anyone know what i'm doing wrong? or is the example too old to work with nowadays?



Thank you in advance,



Bakipum

URL model=ModelInladen.class.getClassLoader().getResource("C:/models/truck.obj");



this is the problem, the path you're giving is no classpath but a filesystem path, thus model==null



try something like



URL model=new File("c:\models\truck.obj).toURL();

Looking again, it looks like you did some more mistakes there. Maybe you should read a bit more before trying again or use the examples in the jmetest package of the JME source to understand better.



e.g.

       converter.setProperty("mtllib",model);

       converter.setProperty("mtllib",model1);

       converter.setProperty("mtllib",model2);

       converter.setProperty("mtllib",model3);



you want to load more than one model, but set the same property in just one loader instance, you need to load them one after another.

Hello Normen,



thank you for your help. Ill look around and read some more first then. Where could i find those examples in the jmetest package by the way? :slight_smile:

Would love to take a look at it!

you can get the source incl. examples from http://jmonkeyengine.googlecode.com/

theres tips on how to import to different IDE's here: http://www.jmonkeyengine.com/wiki/doku.php/new_frontpage

thanks alot!