Displaying Objs Problem

Started by Tornado7, June 16, 2004, 01:46:08 PM

Previous topic - Next topic

Tornado7

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:

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

my init() is:

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:


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:


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 theWorld.addObject(ferito) is outside loadFerito() and so it passes just the last value of ferito to init(). So I've tried to move theWorld.addObject(ferito) in the last line of the loadFerito method (and even 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  :)

EgonOlsen

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:


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.

Tornado7

theWorld is definited global:


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:

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:

EgonOlsen

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:


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("---------------------");
}

Tornado7

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