Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Load .OBJ Objects (http://www.go4expert.com/forums/load-obj-objects-t4493/)

msi_333 30May2007 22:17

Load .OBJ Objects
 
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;
}


pradeep 31May2007 14:51

Re: Load .OBJ Objects
 
What are .OBJ models?

msi_333 31May2007 19:05

Re: Load .OBJ Objects
 
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.


All times are GMT +5.5. The time now is 07:20.