1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Load .OBJ Objects

Discussion in 'Java' started by msi_333, May 30, 2007.

  1. msi_333

    msi_333 New Member

    Joined:
    May 29, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: May 31, 2007
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    What are .OBJ models?
     
  3. msi_333

    msi_333 New Member

    Joined:
    May 29, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page