Load .OBJ Objects

msi_333's Avatar, Join Date: May 2007
Go4Expert Member
This is the code to read .OBJ MODELS and Print it ,
Code:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.objectfile.*;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;

public class ObjectLoader extends Applet{

   public void init(){
      setLayout(new BorderLayout());
      GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
      canvas3D = new Canvas3D( config );
      add("Center", canvas3D);
      BranchGroup szene = macheSzene();
      szene.compile();
      universe = new SimpleUniverse( canvas3D );
      universe.getViewingPlatform().setNominalViewingTransform();
      universe.addBranchGraph(szene);
   }

   public BranchGroup macheSzene(){
      BranchGroup objWurzel = new BranchGroup();
      Transform3D drehung = new Transform3D();
      Transform3D drehung2 = new Transform3D();
      drehung.rotX(Math.PI / 4.0d);
      drehung2.rotY(Math.PI / 5.0d);
      drehung.mul(drehung2);
      TransformGroup objDreh =
                  new TransformGroup(drehung);
      objDreh.setCapability(17);
      objDreh.setCapability(18);
      ObjectFile file = new ObjectFile (ObjectFile.RESIZE);
      Scene scene = null;
      //new URL("jar:"+getCodeBase()+"ObjectLoader.jar!/teapot.obj")
      try {
         if (getCodeBase()!=null){
            try {
               System.exit(1);
               scene = file.load(ClassLoader.getSystemResource("teapot.obj"));
            } 
            catch (Exception e) {
      System.err.println(e);
      }
         }
         else scene = file.load(ClassLoader.getSystemResource("teapot.obj"));
         
      }
      catch (FileNotFoundException e) {
         System.err.println(e);
         System.exit(1);
      }
      catch (ParsingErrorException e) {
         System.err.println(e);
         System.exit(1);
      }
      catch (IncorrectFormatException e) {
         System.err.println(e);
         System.exit(1);
      }
      objDreh.addChild(scene.getSceneGroup());
      
      DirectionalLight d_Licht = new DirectionalLight(
                   new Color3f (0.7f, 1.5f, 0.3f), new Vector3f (1.0f, -10.0f, 1.0f));
      d_Licht.setInfluencingBounds (new BoundingSphere (new Point3d(0.0d,0.0d,0.0d), 100.0d));
      objDreh.addChild(d_Licht);
      
      BoundingSphere bounds=new BoundingSphere();
      MouseRotate spin=new MouseRotate();
      spin.setTransformGroup(objDreh);
      spin.setSchedulingBounds(bounds);
      
      objWurzel.addChild(spin);
      objWurzel.addChild(objDreh);
      return objWurzel;
   }

   public void destroy(){
      universe.removeAllLocales();
   }

   public static void main(String[] args) {
      frame = new MainFrame(new ObjectLoader(),500,500);
      frame.setTitle("ObjectLoader");
   }

   private SimpleUniverse universe;
   private Canvas3D canvas3D;
   private static Frame frame;
}

Last edited by shabbir; 31May2007 at 10:45.. Reason: Confine links to signature
pradeep's Avatar, Join Date: Apr 2005
Team Leader
What are .OBJ models?
msi_333's Avatar, Join Date: May 2007
Go4Expert Member
Quote:
Originally Posted by pradeep
What are .OBJ models?
IT is the one of the most famous models (3D Models for Objects ) that u can make it using maya ,or 3d max and use it in your Java SW.