Author Topic: Another problem with addTriangle?  (Read 3929 times)

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Another problem with addTriangle?
« on: April 18, 2008, 01:53:47 pm »
Hi,
I am trying to take the triangles from a primitive object (cube, sphere, pyramid)
and add them into another object. I can add the triangles from a cube, sphere, pyramid
just fine, but when the object that receives the triangles is rendererd an ArrayOutOfBounds
exception is thrown whenever I had added more than 60 triangles. Here is the code that adds
the triangles.

Quote
class MoldIn {
   private Object3De in;
   private Object3D from;
   private PolygonManager man;
   private static int index=0;
   public MoldIn(Object3De into, Object3D source){
      from=source;
      in=into;
   }

   public void mold(){
      System.out.println("-----IN MOLD() "+index+"--------");
      man = from.getPolygonManager();
      int id = man.getMaxPolygonID();
      in.build();
      in.unbuild();
      for(int i = 0; i<id; i++){
         System.out.println("Copy " + k + "|" + i);
         //System.out.println("" + man.getTransformedVertex(place,0) + " " + man.getTransformedVertex(place,1) + " " + man.getTransformedVertex(place,2) + " ");
         in.addTriangle(man.getTransformedVertex(i,0),man.getTransformedVertex(i,1),man.getTransformedVertex(i,2));
      }
      in.build();
      System.out.println("_________DONE WITH MOLD()_______");
   }

}

Object3De is just a class that extends Object3D. Any ideas why I can't
add more than 60 in? Actualy I can add a cube's trian. 7 times to the
object but after I add the 8th the exception is thrown??
Any ideas?

Jman

Here is the exception that is thrown during the rendering.

Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 56
        at com.threed.jpct.Object3D.transformVertices(Unknown Source)
        at com.threed.jpct.World.renderScene(Unknown Source)
        at Controller.renderNow(Controller.java:181)
        at Controller.run(Controller.java:141)
        at java.lang.Thread.run(Unknown Source)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Another problem with addTriangle?
« Reply #1 on: April 18, 2008, 02:44:47 pm »
What kind of object is "into"? You can't add more triangles than the object is able to hold. That depends on the value given to the constructor when initializing the object. Maybe you can create a simple test case?

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Another problem with addTriangle?
« Reply #2 on: April 18, 2008, 02:54:25 pm »
Into is just an empty object that was created with the 3000 as the argument. I know that is a huge number, but I wanted to make sure that wasn't the problem. Also, when I add the triangles, no exception is thrown so I know I am not adding more than what the object can hold.

Quote
Maybe you can create a simple test case?

Do you mean like using the MoldIn class?

Also, that exception is the same thing that would happen before there was any unbuild method. When I would add triangles to an object they would add fine, but when the object was rendered then the exception was thrown. So, maybe the unbuild method has a little bug?
« Last Edit: April 18, 2008, 02:59:11 pm by JavaMan »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Another problem with addTriangle?
« Reply #3 on: April 19, 2008, 11:31:59 am »
The unbuild() method may have had a problem when called for an empty object that has been build regardless of its emptyness. Please try this version an see if it helps: http://www.jpct.net/download/beta/jpctapi116rc4.zip

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Another problem with addTriangle?
« Reply #4 on: April 20, 2008, 02:15:09 pm »
Thanks for the download, but it didn't clear the problem up right away.

I did create a test case though using just an Object3De and MoldIn and I got the outofbounds exception during rendering. So, I tried removing the Object3De from the world before calling mold() and then add it after  mold(). This got rid of the exception in the test case and in the editor.

Do you know why this would be? I didn't have to do that when adding a cube's tri..

I will use the rc4 from now on.

Jman

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Another problem with addTriangle?
« Reply #5 on: April 20, 2008, 03:13:14 pm »
Are you adding triangles in another thread than the rendering thread?

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Another problem with addTriangle?
« Reply #6 on: April 21, 2008, 02:34:12 am »
No, the initial response is in the AWT thread, but to make sure I don't add triangles in the rendering thread the final response is sent to the rendering thread.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Another problem with addTriangle?
« Reply #7 on: April 21, 2008, 11:15:39 am »
Can you post that test case, please!?

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Another problem with addTriangle?
« Reply #8 on: April 22, 2008, 12:04:50 am »
Ok, here it is.

Code: [Select]
import com.threed.jpct.*;
import java.util.*;
class Test {
public static void main(String args[])throws Exception{
Object3De ob = new Object3De(6000,"JO");
World w = new World();
w.newCamera();
w.setAmbientLight(200,200,200);
Camera c = w.getCamera();
c.moveCamera(Camera.CAMERA_MOVEOUT,300);
FrameBuffer b = new FrameBuffer(800,600,FrameBuffer.SAMPLINGMODE_NORMAL);
b.enableRenderer(IRenderer.RENDERER_OPENGL,FrameBuffer.SAMPLINGMODE_NORMAL );
b.disableRenderer(IRenderer.RENDERER_SOFTWARE);

ob.build();
w.addObject(ob);
Object3D sphere = Primitives.getSphere(30);
sphere.build();
MoldIn mold = new MoldIn(ob,sphere);
//w.removeObject(ob);
mold.mold();
ob.build();
//w.addObject(ob);

while(true){
b.clear();
//System.out.println("RENDERSCENE");
w.renderScene(b);
//System.out.println("DRAW");
w.draw(b);
//System.out.println("UPDATE");
b.update();
//System.out.println("GLONLY");
b.displayGLOnly();
//Thread.yield();
Thread.sleep(10);
}
}
}

class MoldIn {
private Object3De in;
private Object3D from;
private PolygonManager man;
private Controller controller;
private static int index=0;
public MoldIn(Object3De into, Object3D source){
from=source;
in=into;
}
public MoldIn(Object3De into, Object3D source,Controller c){
from=source;
in=into;
}
public void mold(){
System.out.println("-----IN MOLD() "+index+"--------");
from.build();
man = from.getPolygonManager();
int id = man.getMaxPolygonID();
in.build();
in.unbuild();
try{Thread.sleep(100);}catch(Exception ee){}
int place=0;
System.out.println("NUMBER OF POLYGONS TO COPY " + id + "  NUMBER OF POLYGONS AVAILABLE " + in.tric + " going " + id/4);

for(int i = 0; i<id&&in.currentadd<in.tric; i++){
//System.out.println("Copy " + i);
//System.out.println("" + man.getTransformedVertex(place,0) + " " + man.getTransformedVertex(place,1) + " " + man.getTransformedVertex(place,2) + " ");
in.addTriangle(man.getTransformedVertex(i,0),man.getTransformedVertex(i,1),man.getTransformedVertex(i,2));
in.currentadd++;
}
in.build();
System.out.println("_________DONE WITH MOLD()_______");
}

}
class Controller{}
class TabbedObject{}
class Object3De  extends Object3D{
public transient TabbedObject controlspanel;
public transient ArrayList<SimpleVector> vectors;
public transient int count=0;
public transient static int count2=0;
public transient int tric;
public transient static SimpleVector v1 = new SimpleVector(0,0,0);
public transient static SimpleVector v2 = new SimpleVector(0,0,0);
public transient static SimpleVector v3 = new SimpleVector(0,0,0);
private transient SimpleVector b1;
private transient SimpleVector b2;
private transient SimpleVector b3;

private transient SimpleVector t1;
private transient SimpleVector t2;
private transient SimpleVector t3;

public transient PolygonManager p;
public transient String name;
public transient int transs=-1;
public transient String colors="notset";
public transient String basets="notset";
public transient String texts="notset";
public transient String bumpts="notset";
public transient String bumps="notset";
public transient String blends="notset";
public transient String boards="notset";
public transient String collisions="notset";
public transient String cullings="notset";
public transient String envs="notset";
public transient String ellips="notset";
public transient int index=0;
public static int currentadd=0;
public transient int type = 0;//2==shaper  0=basic 1=terrain
private int index2=0;
private transient boolean haslistener=false;
private transient HashMap<Float,HashMap<Float,HashMap<Float,Integer>>> vertexes;
private transient float[][][] indexes;
private transient HashMap<Float,Integer> z;
private transient HashMap<Float,HashMap<Float,Integer>> y;
/* 0=basic 1=terrian 2=shaper; */
public HashMap<Float,HashMap<Float,HashMap<Float,Integer>>> getVertexes(){
return vertexes;
}

public float[][][] getIndexes(){
return indexes;
}

public Object3De(int trianglenumber, String n){
super(trianglenumber+1);
setName(n);
//Object3Ds.render2=false;
System.out.println("Creating object named " + n + " with " + trianglenumber + " triangles.");
count=0;
tric=trianglenumber;
name=n;
vectors = new ArrayList<SimpleVector>();
int m =1;
currentadd=0;
build();

}
/* Tests to see if this object3de is a type of shaper */
public synchronized boolean isShaper(){
if(type==2){
return true;
}
return false;
}

/* Tests to see if this object3de is a type of basic */
public synchronized boolean isBasic(){
if(type==0)
return true;
return false;
}

/*Tests to see if this object3de is a type of terrian */
public synchronized boolean isTerrain(){
System.out.println("" + type);
if(type==1)
return true;
return false;
}


private void addXRow(float length, float number, float start){
float z = length;
float begin=0;
for(int i = 0; i<number; i++){
b1 = new SimpleVector(begin,0,0+start);
b2 = new SimpleVector(begin+length,0,0+start);
b3 = new SimpleVector(begin,0,z+start);

t1 = new SimpleVector(begin,0,z+start);
t2 = new SimpleVector(begin+length,0,0+start);
t3 = new SimpleVector(begin+length,0,z+start);
addTriangle(b1,b2,b3);
addTriangle(t1,t2,t3);
begin = begin+length;
}

}

public static int getCurrentAdd(){
return currentadd;
}

public int getCount(){
return this.count;
}



public synchronized boolean hasListen(){
return haslistener;
}

public synchronized void addCollisionListener(CollisionListener col){
if(hasListen()!=true){
super.addCollisionListener(col);
haslistener=true;
}
else{
System.out.println("THIS object already has a listener");
}
}


}

I know that is a lot of stuff,but this is what I tested. Also controller and tabbedobject are "real" classes but I just made fake substitutes for them here. It still throws the exception for me.

Jman
« Last Edit: April 22, 2008, 12:26:19 pm by JavaMan »