Author Topic: Out of bound exception. Know where it happens not sure how to solve  (Read 9636 times)

Offline hafiz.awang

  • byte
  • *
  • Posts: 3
    • View Profile
Hello,

Allow me to describe my problem. I'm currently having this exception

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 3
   at com.threed.jpct.Object3D.transformVertices(Unknown Source)
   at com.threed.jpct.World.renderScene(Unknown Source)
   at test3D.paint(test3D.java:105)
   at sun.awt.RepaintArea.paintComponent(Unknown Source)
   at sun.awt.RepaintArea.paint(Unknown Source)
   at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
.
.
.

Code: [Select]
import java.applet.*;
import java.awt.*;
import java.net.*;
import com.threed.jpct.*;
import java.awt.event.*;

// coord system for jpct
// +z - in front of you
// +x - to the right
// +y - to the bottom

public class test3D extends Applet implements Runnable, KeyListener
{
public class DemoVertexController extends GenericVertexController
{

int count;

DemoVertexController()
{
count=0;
}

public void apply()
{
SimpleVector[] srcMesh=this.getSourceMesh();
SimpleVector[] dstMesh=this.getDestinationMesh();

int size=this.getMeshSize();

for (int i=0; i<size; i++)
{
float z=srcMesh[i].y;
float y=srcMesh[i].z;
dstMesh[i].z=z;
dstMesh[i].y=-y;
}
count++;
}
}

private Thread loopThread;
private World world=null;
private Object3D obj=null;
private Camera camera=null;
private FrameBuffer buffer=null;
private boolean exit=false;
private final static float MOVE_SPEED=2.5f;
private final static float PLAYER_HEIGHT=30f;
private final static float TURN_SPEED=0.06f;
private Matrix playerDirection=new Matrix();
URL base;
Graphics bufferGraphics;
Image offscreen;
Dimension dim;
Object3D mLevel;

public test3D() {}

public void init()
{
      getAppletContext().showStatus("Initializing...");
      buffer=new FrameBuffer(480, 300, FrameBuffer.SAMPLINGMODE_NORMAL);
      buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
      world=new World();
      world.setAmbientLight(200,200,200);
      obj=Primitives.getCube(3);
      world.addObject(obj);
      obj.translate(0.0f, 30.0f, 0.0f);
      world.getCamera().setPosition(new SimpleVector(0,0,0));
      camera = world.getCamera();
      base = getDocumentBase();
      Image myImage = getImage(base,"data/EarthTex.jpg");
      Texture myTexture = new Texture(myImage);
      TextureManager.getInstance().addTexture("EARTHTEX.JPG", myTexture);

      Object3D[] levelParts=Loader.load3DS(base, "data/testmesh.3ds", 1f);
      mLevel=new Object3D(0);
      for (int i=0; i<levelParts.length; i++)
      {
         Object3D part=levelParts[i];
         mLevel=Object3D.mergeObjects(mLevel, part);
      }
      IVertexController demoControl=new DemoVertexController();
      mLevel.getMesh().setVertexController(demoControl, IVertexController.PRESERVE_SOURCE_MESH);
      mLevel.getMesh().applyVertexController();
      world.addObject(mLevel);

      addKeyListener( this );
      dim = getSize();
      offscreen = createImage(dim.width,dim.height);
      bufferGraphics = offscreen.getGraphics();
   }

   public void destroy() {
      TextureManager.getInstance().flush();
      Object3D.resetNextID();
      exit=true;
      super.destroy();
   }

   public void paint(Graphics g)
   {
        buffer.clear();
        world.renderScene(buffer);
        world.draw(buffer);
        buffer.update();
        buffer.display(bufferGraphics);
        g.drawImage(offscreen,0,0,this);
   }

   public void update(Graphics g)
   {
       paint(g);
   }

   public String getAppletInfo()
   {
     return ("Example Applet ");
   }

   public void run()
   {

   }

   public void start()
   {
   
   }

    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e)
    {
    int code=e.getKeyCode();
        switch (code)
        {
            case (KeyEvent.VK_UP):
            {
            SimpleVector camPos=camera.getPosition();
                camPos.add(playerDirection.getZAxis());
                camera.setPosition(camPos);
                repaint();
                break;
            }
            case (KeyEvent.VK_DOWN):
            {
                SimpleVector camPos=camera.getPosition();
                SimpleVector direction = playerDirection.getZAxis();
                direction.scalarMul(-1f);
                camPos.add(direction);
                camera.setPosition(camPos);
                repaint();
                break;
            }
            case (KeyEvent.VK_LEFT):
            {
                camera.rotateAxis(camera.getBack().getYAxis(), -TURN_SPEED);
                playerDirection.rotateY(-TURN_SPEED);
                repaint();
                break;
            }
            case (KeyEvent.VK_RIGHT):
            {
                camera.rotateAxis(camera.getBack().getYAxis(), TURN_SPEED);
                playerDirection.rotateY(TURN_SPEED);
                repaint();
                break;
            }
        }
    }

    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e)
    {
   
    }

    public void keyTyped(KeyEvent e)
    {

    }

}

As it is, the code runs fine. The problem occurs however when I'm trying NOT to add the object 'obj'. The declaration is on line 44. When I remark line 68 and 69 where line 68 to add 'obj' into the world. The exception occured. This exception however does not occur if i also remark line 86 where a vertex contoller is applied to another Object3D called 'mLevel'. I would appreciate any assistance. The obj primitive was there at the beginning when I was starting to learn. Now I'm trying to get rid of it. I do need the vertex controller though to  help 'correct' my 3ds object that was exported from 3ds max. If anyone out there know how to resolve the exception and explain why it occured, it will really help my situation.

Thanks in advance

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Out of bound exception. Know where it happens not sure how to solve
« Reply #1 on: March 01, 2008, 06:22:48 pm »
I can't verify the problem with your code. Maybe because i don't have to correct files to load. If i'm other some other files, it seems to work fine. Can you make them available somewhere? However, you code is missing a call to build() for both objects. Try to add an world.buildAllObjects() before applying the vertex controller and see if that helps. Omitting the build() isn't a good idea...it shouldn't cause the exception but when combined with a vertex controller, i'm not sure what  may happen.

Offline hafiz.awang

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Out of bound exception. Know where it happens not sure how to solve
« Reply #2 on: March 01, 2008, 08:49:35 pm »
Thanks for the prompt reply Egon.

You are correct. It's because I didn't call buildAllObjects for the world. It seems to work fine now. Just for closure though, you may get the asset file via this link http://hafiz.no-ip.info/data.zip. I would appreciate to know though is this a bug or purely due to my mistake.

Again, thanks for the prompt reply.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Out of bound exception. Know where it happens not sure how to solve
« Reply #3 on: March 02, 2008, 01:22:38 pm »
I would appreciate to know though is this a bug or purely due to my mistake.
I little bit of both. You should not work with unbuild object. However, i don't prevent you from doing so, so  at least it should not crash. That was a small bug. I hope i've corrected it in the latest release candidate 2 (can be found in the News-section).
Thanks for pointing out the problem and for the test case.

Offline hafiz.awang

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Out of bound exception. Know where it happens not sure how to solve
« Reply #4 on: March 02, 2008, 05:08:48 pm »
No problem and again, thanks for the prompt reply.