4 Replies Latest reply: Nov 3, 2010 2:27 AM by tschodt RSS

    How can I open .obj files?

    809845
      Hi!
      I have a little problem,I write a code which can open and draw .obj file.But if the .obj file does't include enter in the end of the file,I get errors(com.sun.j3d.loaders.ParsingErrorException: Expected number on line 24) or the program can't do anything,but when I put the enter in the end of the file the program works correctly.
      Can somebody help me how it would be necessary to modify the program to work correctly?
      Thank a lot:)
      Timmy

      The soure code is:

      import java.applet.Applet;
      import java.awt.BorderLayout;
      import java.awt.Frame;
      import java.awt.GraphicsConfiguration;
      import java.io.FileNotFoundException;
      import java.net.MalformedURLException;
      import java.net.URL;
      import javax.media.j3d.BoundingSphere;
      import javax.media.j3d.BranchGroup;
      import javax.media.j3d.Canvas3D;
      import javax.media.j3d.DirectionalLight;
      import javax.media.j3d.Transform3D;
      import javax.media.j3d.TransformGroup;
      import javax.vecmath.Color3f;
      import javax.vecmath.Point3d;
      import javax.vecmath.Vector3f;
      import com.sun.j3d.loaders.IncorrectFormatException;
      import com.sun.j3d.loaders.ParsingErrorException;
      import com.sun.j3d.loaders.Scene;
      import com.sun.j3d.loaders.objectfile.ObjectFile;
      import com.sun.j3d.utils.applet.MainFrame;
      import com.sun.j3d.utils.universe.SimpleUniverse;

      public class egy 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);

      ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
      Scene scene = null;
      try {
      scene = file.load(ClassLoader.getSystemResource("cube.obj"));

      } catch (Exception e) {
      System.err.println(e);
      System.exit(1);
      }
      objDreh.addChild(scene.getSceneGroup());

      DirectionalLight d_Licht = new DirectionalLight(new Color3f(1.0f, 1.0f,
      1.0f), new Vector3f(-1.0f, -1.0f, -1.0f));
      d_Licht.setInfluencingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d,
      0.0d), 100.0d));
      objDreh.addChild(d_Licht);
      objWurzel.addChild(objDreh);
      return objWurzel;
      }

      /**
      * gibt speicher frei
      */
      public void destroy() {
      universe.removeAllLocales();
      }

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

      private SimpleUniverse universe;

      private Canvas3D canvas3D;

      private static Frame frame;
      }

      The cube.obj includes:
      # cube.obj created by IVREAD.
      # Original data in cube.iv

      g Object001
      v -0.500000 -0.500000 1.00000
      v 0.500000 -0.500000 1.00000
      v 0.500000 0.500000 1.00000
      v -0.500000 0.500000 1.00000
      v 0.500000 -0.500000 0.000000E+00
      v -0.500000 -0.500000 0.000000E+00
      v -0.500000 0.500000 0.000000E+00
      v 0.500000 0.500000 0.000000E+00
      f 1 2 3
      f 1 3 4
      f 5 6 7
      f 5 7 8
      f 6 1 4
      f 6 4 7
      f 2 5 8
      f 2 8 3
      f 6 5 2
      f 6 2 1
      f 4 3 8
      f 4 8 7
        • 1. Re: How can I open .obj files?
          793415
          Please edit your post to use the code tags. That would be to wrap the code in [ code ] & [ code ] without the spaces.
          • 2. Re: How can I open .obj files?
            tschodt
            806842 wrote:
            I have a little problem,I write a code which can open and draw .obj file.But if the .obj file does not include enter line termination in the end of the file,I get errors(com.sun.j3d.loaders.ParsingErrorException: Expected number on line 24) or the program can't do anything, but when I put the enter line termination in the end of the file the program works correctly.
            Can somebody help me how it would be necessary to modify the program to work correctly?
            It is already working correctly.
            In text files lines have to end with line termination.

            If you really want your application to work regardless, you can
            open cube.obj using [url http://download.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemResourceAsStream%28java.lang.String%29]ClassLoader.getSystemResourceAsStream(String name) ^API^ and
            filter it through your own [url http://download.oracle.com/javase/6/docs/api/java/io/FilterInputStream.html]FilterInputStream ^API^ before
            passing it to a [url http://download.oracle.com/javase/6/docs/api/java/io/InputStreamReader.html]InputStreamReader ^API^ which you can
            pass to [url http://download.java.net/media/java3d/javadoc/1.4.0/com/sun/j3d/loaders/objectfile/ObjectFile.html#load%28java.io.Reader%29]ObjectFile.load(Reader reader) ^API^ .

            In your [url http://download.oracle.com/javase/6/docs/api/java/io/FilterInputStream.html]FilterInputStream ^API^ you must supply the missing line termination at the end of the file.
            If the file already ends with line termination you are just adding an extra blank line - you will have to verify that an empty line is allowed at the end of the file,
            or you can opt to only add line termination if the file does not already end with line termination.
            [code]
            import java.applet.Applet;
            import java.awt.BorderLayout;
            import java.awt.Frame;
            import java.awt.GraphicsConfiguration;
            import java.io.FileNotFoundException;
            import java.net.MalformedURLException;
            import java.net.URL;
            import javax.media.j3d.BoundingSphere;
            import javax.media.j3d.BranchGroup;
            import javax.media.j3d.Canvas3D;
            import javax.media.j3d.DirectionalLight;
            import javax.media.j3d.Transform3D;
            import javax.media.j3d.TransformGroup;
            import javax.vecmath.Color3f;
            import javax.vecmath.Point3d;
            import javax.vecmath.Vector3f;
            import com.sun.j3d.loaders.IncorrectFormatException;
            import com.sun.j3d.loaders.ParsingErrorException;
            import com.sun.j3d.loaders.Scene;
            import com.sun.j3d.loaders.objectfile.ObjectFile;
            import com.sun.j3d.utils.applet.MainFrame;
            import com.sun.j3d.utils.universe.SimpleUniverse;
            
            public class egy 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);
            
            ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
            Scene scene = null;
            try {
            scene = file.load(ClassLoader.getSystemResource("cube.obj"));
            
            } catch (Exception e) {
            System.err.println(e);
            System.exit(1);
            }
            objDreh.addChild(scene.getSceneGroup());
            
            DirectionalLight d_Licht = new DirectionalLight(new Color3f(1.0f, 1.0f,
            1.0f), new Vector3f(-1.0f, -1.0f, -1.0f));
            d_Licht.setInfluencingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d,
            0.0d), 100.0d));
            objDreh.addChild(d_Licht);
            objWurzel.addChild(objDreh);
            return objWurzel;
            }
            
            /**
            * gibt speicher frei
            */
            public void destroy() {
            universe.removeAllLocales();
            }
            
            public static void main(String[] args) {
            frame = new MainFrame(new egy(), 500, 500);
            frame.setTitle("Kép");
            }
            
            private SimpleUniverse universe;
            
            private Canvas3D canvas3D;
            
            private static Frame frame;
            }
            [/code]

            The cube.obj includes:
            [code]
            # cube.obj created by IVREAD.
            # Original data in cube.iv
            
            g Object001
            v -0.500000 -0.500000 1.00000
            v 0.500000 -0.500000 1.00000
            v 0.500000 0.500000 1.00000
            v -0.500000 0.500000 1.00000
            v 0.500000 -0.500000 0.000000E+00
            v -0.500000 -0.500000 0.000000E+00
            v -0.500000 0.500000 0.000000E+00
            v 0.500000 0.500000 0.000000E+00
            f 1 2 3
            f 1 3 4
            f 5 6 7
            f 5 7 8
            f 6 1 4
            f 6 4 7
            f 2 5 8
            f 2 8 3
            f 6 5 2
            f 6 2 1
            f 4 3 8
            f 4 8 7
            [/code]
            • 3. Re: How can I open .obj files?
              809845
              Thanks for the help,May I ask a favour yet?If its possibly would you write down the whole code please because I take the function of the FileInputStream incompletely.
              Thanks a lot.
              Timmy
              • 4. Re: How can I open .obj files?
                tschodt
                806842 wrote:
                If its possibly would you write down the whole code please
                Why?
                Try searching the web for examples of how to implement your own FilterInputStream.
                because I take the function of the FileInputStream incompletely.
                I do not understand what you are trying to say there.

                Note that I pointed you to [url http://download.oracle.com/javase/6/docs/api/java/io/FilterInputStream.html]FilterInputStream ^API^
                and you are rambling about [url http://download.oracle.com/javase/6/docs/api/java/io/FileInputStream.html]FileInputStream ^API^

                Edited by: tschodt on Nov 3, 2010 7:26 AM