Author Topic: loading .obj file  (Read 4508 times)

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
loading .obj file
« on: June 20, 2011, 07:37:56 pm »
Hi,
I am new to jPCT and am using this framework to develop a sample game.I am trying yo load .obj file under 3ds folder.I have created .obj file from .blender file using its exporter.But when I run the following code I am getting this exception:

Quote
Text file 3ds\butterfly.obj loaded...287934 bytes
Processing object from OBJ-file: Cube_Cube.001
Object 'Cube_Cube.001_jPCT0' created using 12 polygons and 8 vertices.
Processing object from OBJ-file: Plane.001_Plane
Object 'Plane.001_Plane_jPCT1' created using 10368 polygons and 5329 vertices.
Processing object from OBJ-file: butterfly_logic
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -10000
        at com.threed.jpct.Loader.createOBJObject(Unknown Source)
        at com.threed.jpct.Loader.loadOBJ(Unknown Source)
        at com.threed.jpct.Loader.loadOBJ(Unknown Source)
        at jpctdemo.Main.<init>(Main.java:46)
        at jpctdemo.Main.main(Main.java:83)

Let me paste the code here.This sample I have taken from the forum only.

Code: [Select]
package jpctdemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import com.threed.jpct.*;
import java.io.File;

class Main extends JFrame
{
   private FrameBuffer _buffer = null;
   private World _world = null;
   private TextureManager _texMgr = null;
   private Camera _camera = null;
   private int _width = 800;
   private int _height = 600;
   private Graphics _graphics = null;
   private int _titleBarHeight = 0;
   private int _leftBorderWidth = 0;
   private Object3D[] _model = null;
   private ClassLoader _classLoader = getClass().getClassLoader();

   public Main()
   {
      super("jPCT " + Config.getVersion());
      Config.glFullscreen = false;
      Config.glMipmap = true;
      Config.glColorDepth = 16;
      Config.maxPolysVisible = 64000;
      Config.fadeoutLight = false;
           
      _world = new World();
      _world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
      _world.getLights().setRGBScale(Lights.RGB_SCALE_2X);
      _world.setAmbientLight(25, 30, 30);
      _world.addLight(new SimpleVector(-1000, -150, 1000), 22, 5, 4);
      _world.addLight(new SimpleVector(1000, -150, -1000), 4, 2, 22);     

      _texMgr = TextureManager.getInstance();
   char c = File.separatorChar;
  _texMgr.addTexture("prop_butterfly_diff", new Texture("textures"+c+"prop_butterfly_diff.png"));
      _texMgr.addTexture("fx_sparkles", new Texture("textures"+c+"fx_sparkles.png"));

//      _texMgr.addTexture("tex", new Texture(_classLoader.getResourceAsStream("model/Mat.jpg")));

      _model = Loader.loadOBJ("3ds"+c+"butterfly.obj", "3ds"+c+"butterfly.mtl", 1f);
     
      for (int i = 0; i < _model.length; i++)
      {
         _world.addObject(_model[i]);
         _model[i].setTexture("prop_butterfly_diff");
      }
     
      _camera = _world.getCamera();
      _camera.setPosition(-12, 90, 0);  // ARGGGHH  how can I center the camera over the stick figure?
      _camera.lookAt(_model[0].getTransformedCenter());

      _world.buildAllObjects();
      Config.tuneForOutdoor();

      Insets insets = getInsets();
      _titleBarHeight = insets.top;
      _leftBorderWidth = insets.left;
      setSize(_width + _leftBorderWidth + insets.right, _height + _titleBarHeight + insets.bottom);
      setResizable(false);
      setVisible(true);     
     
      _graphics = getGraphics();

      addMouseMotionListener(new MouseDragListener());

      _buffer = new FrameBuffer(_width, _height, FrameBuffer.SAMPLINGMODE_NORMAL);
      _buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
      _buffer.setBoundingBoxMode(FrameBuffer.BOUNDINGBOX_NOT_USED);
      _buffer.optimizeBufferAccess();
      _buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
     
      drawWorld();     
   }

   public static void main(String[] args)
   {
      new Main();
   }

   private void drawWorld()
   {
      _buffer.clear();
      _world.renderScene(_buffer);
      _world.draw(_buffer);
      _buffer.update();
      _buffer.display(_graphics, _leftBorderWidth, _titleBarHeight);
   } 
   
   private class MouseDragListener implements MouseMotionListener
   {
      final float speed = 0.05f;
      int lastX = 0;
      int lastY = 0;
     
      public void mouseDragged(MouseEvent me)
      {
         int currentX = me.getX();
         int currentY = me.getY();
         
         if (currentX > lastX)
         {
            // the mouse is going to the right
            _model[0].rotateX(speed);
         }
         else if (currentX < lastX)
         {
            // the moust is going to the left
            _model[0].rotateX(-speed);
         }
                 
         if (currentY > lastY)
         {
            // we are going down on the screen
            _model[0].rotateY(speed);
         }
         else if (currentY < lastY)
         {
            // we are going up on the screen
            _model[0].rotateY(-speed);
         }
         
         lastX = currentX;
         lastY = currentY;
         drawWorld();
      }

      public void mouseMoved(MouseEvent arg0)
      {
      }
   }
}

Can you please look into this issue and tell what I am doing wrong here.

Thanks,
« Last Edit: June 20, 2011, 11:10:20 pm by EgonOlsen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: loading .obj file
« Reply #1 on: June 20, 2011, 11:12:13 pm »
Must be another crazy variation of the OBJ-format. Can i have a look at the file? Without it, it's impossible to track this down. What puzzles me, is the name of the object the loader tries to process when crashing: butterfly_logic ... that doesn't sound like a regular object to me...

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: loading .obj file
« Reply #2 on: June 21, 2011, 04:46:48 am »
Thanks for the reply.
Please find the original model files in blender and the exported models using latest version of the Blender and its textures

http://www.filejumbo.com/Download/BF1A3F5E1E77931F
http://www.filejumbo.com/Download/DAE10C1F50CE5757
http://www.filejumbo.com/Download/4260FAD2276A02E1
http://www.filejumbo.com/Download/358A7FAB77B16B8E
http://www.filejumbo.com/Download/B98DD7E623899626

Please specify should I do any changes in the model files when exporting.I have only little knowledge in Blender.
« Last Edit: June 21, 2011, 01:00:14 pm by EgonOlsen »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: loading .obj file
« Reply #3 on: June 21, 2011, 08:55:21 pm »
I've edited your file links to actually work...the problem with that particular file is, that it contains faces that contain only two vertices. No idea what this is supposed to be used for. I've uploaded a fixed jar her: http://www.jpct.net/download/beta/jpct.jar. It can load the file, but will ignore these "faces".

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: loading .obj file
« Reply #4 on: June 22, 2011, 09:08:45 am »
Thank you very much for the support.I have added your new jpct jar file and ran the same program.But this time I am not getting any error in the console.But nothing displays in the screen.Is it a problem with the code?

Let me ask you one more thing.Why cannot we have a method in jpct that will load the .blend file as well? or can we integrate the with some other programs that is already loading the .blend file?

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: loading .obj file
« Reply #5 on: June 22, 2011, 12:30:10 pm »
Quick Update. I changed the ambient  light to max like this:
World.setAmbientLight(255, 255, 255)
This time it loads!!.But no texture seems to be applied to the model.It displays a white object only.Is it because of the problem in the model file that I had used?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: loading .obj file
« Reply #6 on: June 22, 2011, 06:10:12 pm »
It loads with lower ambient light too, it's just that black on black is a little...black. The loader doesn't load any textures at all. It's your task to do this. What the loader does, is to match your textures with the ones used in the file by their names. Have a look at the wiki for a description on how to use this: http://www.jpct.net/wiki/index.php/Loading_models

Quote
Why cannot we have a method in jpct that will load the .blend file as well?
You are free to code your own loader to load .blend files. Personally, i consider the options that jPCT offers to load files as being sufficient for most cases. Adding more and more importers just consumes time that can be better spend otherwise IMHO. However, if someone comes up with a loader for any unsupported format, i would be happy to add it.

Offline gamerfan

  • int
  • **
  • Posts: 97
    • View Profile
Re: loading .obj file
« Reply #7 on: June 29, 2011, 11:32:35 am »
Thanks for the reply.