Is it possible load obj or other file format models in run-time?

Hi ,

I made one test to load obj model in run-time, but failed, could anybody help me to work this out?

Detail:



[java]



public class LoadTest extends SimpleApplication{



@Override

public void simpleInitApp() {

Spatial myobj = assetManager.loadModel("Models/bangongshi/bangongshi.obj");

cam.setLocation(new Vector3f(10, 20, 5));

myobj.rotate(0, 6, 0);

myobj.setLocalScale(0.5f);

//loadModel(); //Can be load successfully.

rootNode.attachChild(myobj);

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());

rootNode.addLight(sun);

}





public void loadModel(String str){

Spatial myobj1 = assetManager.loadModel(str);

myobj1.setLocalTranslation(new Vector3f(5,5,5));

myobj1.scale(0.01f);

rootNode.attachChild(myobj1);

}



public void loadBox(){

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom = new Geometry("Box", b);

rootNode.attachChild(geom);

}

/**

  • @param args

    */

    public static void main(String[] args) {

    LoadTest lt = new LoadTest();

    lt.start();

    System.out.println(“Load obj model!!!”);

    lt.loadModel(“Models/chuangtougui/chuangtougui.obj”); //Can not be load,exception like below.

    // System.out.println(“Load box!!!”);

    // lt.loadBox(); //Can be load successfully.



    }

    }



    [/java]



    Log and exception from the console:

    Load obj model!!!

    Exception in thread “main” java.lang.NullPointerException

    at com.longyg.jmonkeyengine.LoadTest.loadModel(LoadTest.java:39)

    at com.longyg.jmonkeyengine.LoadTest.main(LoadTest.java:57)



    Seems it can not find the model file, but I had tryed all kinds of directory still same exception.

I have met the same problem when trying to load a model in run-time. Need somebody help.

I think that after lt.start(); you are forbidden to have more code in main.



So put the lt.loadModel(“myModel”); in the simpleUpdate();

but you dont want each frame to call lt.loadModel, because it would be slow.

Instead have a boolean that if it is loaded it wont load it again.

Also you can also use a if (totalTime > tpf) loadModel(); in order to delay when loading happens.

This can be usefull because you wont wait init() to load models which may take 5 minutes if you have 200 models.

you can load models at run time fine. Must be the way you are calling it

You MAY call the load model thing in simpleInitApp() as you did in your snippet. You cannot call it from main(), which makes little sense to do anyway as it gives you nothing extra, so stay in simpleInitApp() or call the loadModel function from an update loop using any trigger to keep it from spawning thousands of instances, as stated above.



I suspect the nullPointerException is not complaining about the model obj but about the assetManager, judging by the fact you are trying to use that while the app is not fully started yet and it being the big difference between loadBox and loadModel. You cannot use that assetManager before it is initialized.

Thanks baalgarnaal, wezrule and tralala,

I had tryed as tralala said, it works now.

But I still don’t understand what is the usage or purpose of the parameter “tpf” in the simpleUpdate(float tpf) method.

tpf is time per frame.



[java]

float totalTime = 0 ; //voila now holds totalTime from when application started.



simpleUpdate(tpf)

{

totalTime += tpf;

}[/java]



You need tpf, because some computers run slow e.g 30 frames per seconds, while others run 60000 frames per second. If u dont use tpf, then fast computers will move at omega speed and make game unplayable.

Yes, that make sence.

Thanks again:)