Author Topic: Displaying Objs Problem  (Read 4398 times)

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Displaying Objs Problem
« on: June 16, 2004, 01:46:08 pm »
Hi Egon,

as you know I’m using a config file where an operator can decide how many objs to load and where to place them;  for instance:
Code: [Select]

ferito1=(-591,-74,-90)
ferito2=(-766,-82,246)

my init() is:
Code: [Select]

public void init() {

      theWorld=new World();
 
       String param = getParameter("FileToRead");
       
       if ( param != null){
      FileToRead = new String(param);
       }
       
       // Now read the file.
       readConfigFile();
.
.
.

theWorld.addObject(mondo);
theWorld.addObject(ferito);


where readConfigFile() is method that scans the config file; when it find out a line having the word “ferito”, it passes to a method loadFerito that name (completed with 1 or 2) and his coordinate:
Code: [Select]


public void loadFerito(String name, float x, float y, float z){
     
      texMan=TextureManager.getInstance();
      texMan.addTexture(name+".jpg",new      Texture(getDocumentBase(),"textures/feriti/"+name+".jpg"));
      Object3D[] feritoArray=Loader.load3DS(getDocumentBase(),"3ds/"+name+".3ds", 20f);
      ferito=new Object3D(0);
      ferito=feritoArray[0];
      ferito.setCenter(SimpleVector.ORIGIN);
      ferito.translate (x, y+50, z);
      ferito.rotateX((float)-Math.PI/2);
      ferito.rotateMesh();
      ferito.setRotationMatrix(new Matrix());
     
      ferito.createTriangleStrips(2);
      ferito.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
      ferito.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
     
      ferito.enableLazyTransformations();      
     

  }



Even if the two 3ds object are correctly loaded:

Code: [Select]

Loading Texture...textures/feriti/ferito1.jpg
Loading file 3ds/ferito1.3ds
File 3ds/ferito1.3ds loaded...55321 bytes
Processing new material 1.Mat0Tex1!
Processing object from 3DS-file: polySur
Object 'polySur_jPCT0' created using 694 polygons and 354 vertices.
Created 183 triangle-strips for polySur_jPCT0 in 1 pass(es)
Loading Texture...textures/feriti/ferito2.jpg
Loading file 3ds/ferito2.3ds
File 3ds/ferito2.3ds loaded...54631 bytes
Processing new material 1.Mat0Tex1!
Processing object from 3DS-file: polySur
Object 'polySur_jPCT4' created using 706 polygons and 360 vertices.
Created 178 triangle-strips for polySur_jPCT4 in 1 pass(es)


It’s displayed just the one in the last line of the config file. I guess this behavior is dued by the fact that
Code: [Select]
theWorld.addObject(ferito) is outside loadFerito() and so it passes just the last value of ferito to init(). So I’ve tried to move
Code: [Select]
theWorld.addObject(ferito) in the last line of the loadFerito method (and even
Code: [Select]
theWorld=new World() to avoid a null exception) to try to add an obj to the world for every ferito’s occurance, but nothing changes... Any suggestion.....Bye and thanks  :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Displaying Objs Problem
« Reply #1 on: June 16, 2004, 05:29:34 pm »
I assume that theWorld is global? Moving the call to addObject() to the end of loadFerito() should do the trick. I can't see a reason why this should cause a nullpointer exception (IF theWorld is global). Creating a new world in the loadFerito()-method is bad, because that means that you create two instances of world and only use the second one for rendering...the result is (as mentioned) the same: You can only see the last object.
So:

1.) Make sure theWorld is global
2.) ferito doesn't have to be global IMO. Make it local inside loadFerito.
3.) Don't create more then one instance of World

BTW:

Code: [Select]

ferito=new Object3D(0);
ferito=feritoArray[0];


is unnecessary work in this case. Just do a ferito=feritoArray[0]. The Object3D(0) will be created and throw away right afterwards in your example. There's no point in doing this.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Displaying Objs Problem
« Reply #2 on: June 17, 2004, 12:19:41 pm »
theWorld is definited global:

Code: [Select]

public class ThreeDSimApplet extends Applet implements Runnable, KeyListener, MouseListener {
.
.
.
FrameBuffer buffer=null;  
World theWorld;            
TextureManager texMan=null;  
private Camera camera=null;
.
.
.
public void init() {
theWorld=new World();
.
.
.

I've followed your steps: I've moved ferito's definition and "adding to the world" into loadFerito() as follow:
Code: [Select]

public void loadFerito(String name, float x, float y, float z){
     
      Object3D ferito=null;  
     
      texMan=TextureManager.getInstance();
      texMan.addTexture(name+".jpg",new Texture(getDocumentBase(),"textures/feriti/"+name+".jpg"));
      Object3D[] feritoArray=Loader.load3DS(getDocumentBase(),"3ds/"+name+".3ds", 20f);
      ferito=feritoArray[0];
      ferito.setCenter(SimpleVector.ORIGIN);
      ferito.translate (x, y+50, z);
      ferito.rotateX((float)-Math.PI/2);
      ferito.rotateMesh();
      ferito.setRotationMatrix(new Matrix());
     
      ferito.createTriangleStrips(2);
      ferito.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
      ferito.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
     
      ferito.enableLazyTransformations();
      theWorld.addObject(ferito);
  }

Now I have not a null expection....but none "ferito" is displayed even if correctly loaded.... I'm becoming crazy  :shock:

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Displaying Objs Problem
« Reply #3 on: June 17, 2004, 05:39:32 pm »
Looks fine to me so far...your problem must be located somewhere else. You are obviously not calling build() on the ferito, so you have to have a call to World.buildAllObjects() later in your code. Otherwise, your feritos are not "builded" correctly. If that's the case, just add a ferito.build() right before adding the ferito to the world.
Second, if the feritos move, remove the enableLazyTransformations() call. That's for static objects only and may cause problems on all others.
And last but not least: Are you sure that the first object in the array returned by the loader is your ferito? If a 3DS-file contains other stuff too, this can be anything...

Edit: For debugging purposes, try something like this to see whichs objects actually belong to your world and where they are:

Code: [Select]

for (Enumeration objs=theWorld.getObjects(); objs.hasMoreElements();) {
Object3D obj=(Object3D) objs.nextElement();
System.out.println(obj.getName());
System.out.println(obj.getTransformedCenter());
System.out.println("---------------------");
}

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Displaying Objs Problem
« Reply #4 on: June 21, 2004, 11:14:58 am »
Hi Egen,

I've solved my problem... :mrgreen: The problem was in the order in which things are made; in fact, moving the calling of readConfigFile() (and conseguently the calling of loadFerito) after the constructor of the world, everything works fine.

Bye and thanks