Author Topic: loading md2 model in background.....  (Read 3092 times)

Offline ashunkhs

  • byte
  • *
  • Posts: 24
    • View Profile
loading md2 model in background.....
« on: November 06, 2012, 10:31:41 am »
Hi

   I am trying to load 5-6 md2 model on my GL. But it it taking more time and my app become stuck (Black screen) . So i tried to lode it in background using thread or AsyncTask. But i am getting null pointer error  in onDrow() . when i used any model object . otherwise blank screen (without model). 

class LoadModelAsyncTask extends AsyncTask<Void, Integer, Boolean>
   {
      protected Boolean doInBackground(Void... params)
      {
         System.out.println("i m doInBackground");
         ogro=loadModel(Environment.getExternalStorageDirectory() +"/texture_one.jpg",Environment.getExternalStorageDirectory() + "/logo_one.md2",0.25f);
         ogroA=loadModel(Environment.getExternalStorageDirectory() +"/texture_one.jpg",Environment.getExternalStorageDirectory() + "/circle_one.md2",0.25f);
         
         return null;
      
      }

      protected void onPostExecute(Boolean result)
      {
         System.out.println("i m onPostExecute");
         world.addObject(ogro);
         world.addObject(ogroA);
         world.buildAllObjects();
      }
   }


and call it in
onSurfaceChange() {

mLoadModelAsyncTask = new LoadModelAsyncTask();

         mLoadModelAsyncTask.execute();

cam.lookAt(ogro.getTransformedCenter());  ........1
cam.lookAt(ogroA.getTransformedCenter());.........2
}


here geting null pointer in line 1 and 2..aswell onDrow().


if any solution pls ..reply soon..!!
 8)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: loading md2 model in background.....
« Reply #1 on: November 06, 2012, 10:41:33 am »
You are not allowed to fiddle around with the world in anything but the rendering thread or your setup code (jPCT-AE isn't thread safe). If you add objects while the world is being rendered, everything may happen. A simple solution is to load the model in the thread but add it in onDraw after the loading has finished.

Offline ashunkhs

  • byte
  • *
  • Posts: 24
    • View Profile
Re: loading md2 model in background.....
« Reply #2 on: November 06, 2012, 12:34:44 pm »
hi sir

As u told  i load a model in thread  in serfaceChanged()  and add the model into world  in onDrow().

public void onDrawFrame(GL10 gl)
      {
         
         if(world.getObjects()==null)
         {
            world.addObject(ogro);    ......................1
            world.addObject(ogroA);
            world.buildAllObjects();
            ................
                                 .....

but i m getting  the null pointer on line ..1. and following lines..
another way to solve it../



Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: loading md2 model in background.....
« Reply #3 on: November 06, 2012, 03:03:26 pm »
Please post the exception's stack trace. What you claim that happens actually can't happen for two reasons:

  • That line can cause a nullpointer only if world is null. But world can't be null, because you access it before in the if-clause.
  • That code never gets executed, because world.getObjects() never returns null.

Offline ashunkhs

  • byte
  • *
  • Posts: 24
    • View Profile
Re: loading md2 model in background.....
« Reply #4 on: November 07, 2012, 08:09:57 am »
Hi

  Now i am not getting any exception but now my model is not visible. it shows blank surface .following is my code.....

public void onSurfaceChanged(GL10 gl, int w, int h) {
         if (fb != null) {
            fb.dispose();
         }
         fb = new FrameBuffer(gl, w, h);
         world = new World();
         world.setAmbientLight(100, 100, 100);
         res = getResources();
         sun = new Light(world);
         sun.setIntensity(250, 250, 250);
         tm = TextureManager.getInstance();

         mLoadModelAsyncTask = new LoadModelAsyncTask(); // loading model in thread
         mLoadModelAsyncTask.execute();
      
         cam = world.getCamera();
         world.getCamera().setPosition(0, 0, -20);
         cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
         //cam.lookAt(ogro.getTransformedCenter());
         //cam.lookAt(ogroA.getTransformedCenter());

         SimpleVector sv = new SimpleVector();
         sv.y -= 50;
         sv.z -= 100;
         sun.setPosition(sv);

         MemoryHelper.compact();
      
      }
      public void onDrawFrame(GL10 gl)
      {
         if(ogro!=null&& ogroA!=null)
         {
            System.out.println("world--------------->"+world); //world is not null
            System.out.println("ogro--------------->"+ogro); // model is not null
            
            world.addObject(ogro);
            world.addObject(ogroA);
            world.buildAllObjects();
         }

  but object is not visible on surface ..pls check out the problem....and !!!

Offline ashunkhs

  • byte
  • *
  • Posts: 24
    • View Profile
Re: loading md2 model in background.....
« Reply #5 on: November 08, 2012, 12:28:47 pm »
hi

 thanks for help ..its working fine....