Results 1 to 2 of 2
  1. #1
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default A problem with the simple univers

    When I try to load a obj file in the simple univers i get this error:
    Java Code:
    Exception in thread "main" com.sun.j3d.loaders.ParsingErrorException: Unrecognized token, line 4
    	at com.sun.j3d.loaders.objectfile.ObjectFile.readFile(ObjectFile.java:597)
    	at com.sun.j3d.loaders.objectfile.ObjectFile.load(ObjectFile.java:1248)
    	at com.sun.j3d.loaders.objectfile.ObjectFile.load(ObjectFile.java:676)
    	at SceneControl.addModelToUniverse(SceneControl.java:68)
    	at SceneControl.<init>(SceneControl.java:37)
    	at Program.main(Program.java:8)
    Java Code:
    import java.io.IOException;
    
    public class Program {
        
        
        public static void main(String[] args) {
            try {
                SceneControl control = new SceneControl();
                CockroachWindow window = new CockroachWindow(control.getCanvas());
                window.setVisible(true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        
    }
    Java Code:
    import java.awt.event.KeyEvent;
    import java.util.Enumeration;
    import javax.media.j3d.Behavior;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.media.j3d.WakeupOnAWTEvent;
    import javax.vecmath.Vector3f;
    
    
    /**
     * Implements the most silly movement ever for the cockroach body.
     *
     * @author Dalton 
     */
    public class CockroachBehavior extends Behavior {
        
        
        // INSTANCE ****************************************************************
        
        /** Groups that will be animated. */
        private TransformGroup[] groups;
        
        /** Used to transform the groups you will animate. */
        private Transform3D[] transforms;
        
        /** Used to translate the groups you will animate. */
        private Vector3f[] translations;
        
        /** Type of event for which groups will react. */
        private WakeupOnAWTEvent wake;
        
        /** Increments 1 every time the user hits a key. */
        private int hitCount;
        
        /** Decides which group will be animated based on the hitCount. */
        private int bodyPartIndex;
        
        // INITIALIZATION **********************************************************
        
        public CockroachBehavior(TransformGroup... groups) {
            this.groups = groups; // You can add a groups count security check if you will
        
            wake = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); // You decide which key later
    
            translations = new Vector3f[groups.length];
            transforms = new Transform3D[groups.length];
    
            for (int i = 0; i < groups.length; i++) {
                translations[i] = new Vector3f(0F, 0F, 0F);
                transforms[i] = new Transform3D();
            }
        }
    
        public void initialize() {
            wakeupOn(wake); // Inherited method
        }
    
        // ACCESS ******************************************************************
        
        public void processStimulus(Enumeration enumeration) {
            KeyEvent k = (KeyEvent) wake.getAWTEvent()[0];
    
            /* Moves only if the key pressed is the right directional key and 
               if the hit count is a multiple of 4 */
            if ((k.getKeyCode() == KeyEvent.VK_RIGHT) && (hitCount++ % 4 == 0)) {
    
                /* Selects the body part to be moved */
                bodyPartIndex = (bodyPartIndex + 1) % 3; 
            
                /* Moves 0.3 on Z axis */
                translations[bodyPartIndex].set(translations[bodyPartIndex].x,
                                                translations[bodyPartIndex].y,
                                                translations[bodyPartIndex].z + 0.1F); 
            
                transforms[bodyPartIndex].setTranslation(translations[bodyPartIndex]);
                groups[bodyPartIndex].setTransform(transforms[bodyPartIndex]);
            }
        
            /* If you don't put it here, it won't respond the next time you press a key */
            wakeupOn(wake); 
        }
        
        
    }
    Java Code:
    import java.awt.Color;
    import java.util.Map;
    import javax.media.j3d.Behavior;
    import javax.media.j3d.BoundingSphere;
    import javax.media.j3d.Bounds;
    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.DirectionalLight;
    import javax.media.j3d.Shape3D;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.vecmath.Color3f;
    import javax.vecmath.Vector3f;
    import com.sun.j3d.loaders.Scene;
    
    
    /**
     * Creates and configures the roach.
     *
     * @author Dalton Filho
     */
    public class CockroachBuilder {
        
        
        public static final TransformGroup getCockroach(Scene scene, Bounds bounds) {
            
            /* Obtain the scene BranchGroup, from which the components are removed */
            BranchGroup root = scene.getSceneGroup();
            
            Map<String, Shape3D> nameMap = scene.getNamedObjects();
            
            /* Remove all children because (you don't want a MultiParentException) */
            root.removeAllChildren();
            
            /* Construct the groups */
       //     TransformGroup leftLegs = getLeftLegs(nameMap);
        //    TransformGroup rightLegs = getRightLegs(nameMap);
            TransformGroup body = getBody(nameMap);
            TransformGroup roach = getRoach( body);
            
            /** Rotates 90 deg on the Y axis and translates the roach a little */
            transformRoach(roach);
            
            /** Adds a red light over the cockroach. */
            addLights(roach);
            
            /** Necessary step to allow transformation on these parts. */
            enableTransformCapability(body, roach);
            
            /** Allows the roach to move when the right directional key is pressed */
            addBehavior(new TransformGroup[]{body}, roach, bounds);
            
            return roach;
        }
        
        
        // COCKROACH CONSTRUCTION **************************************************
        
        private static final TransformGroup getRightLegs(Map<String, Shape3D> nameMap) {
            TransformGroup rightLegs = new TransformGroup();
            
            rightLegs.addChild(nameMap.get("ruplegf"));
            rightLegs.addChild(nameMap.get("ruplegm"));
            rightLegs.addChild(nameMap.get("ruplegr"));
            rightLegs.addChild(nameMap.get("rmidlegf"));
            rightLegs.addChild(nameMap.get("rmidlegm"));
            rightLegs.addChild(nameMap.get("rmidlegr"));
            rightLegs.addChild(nameMap.get("rlowlegf"));
            rightLegs.addChild(nameMap.get("rlowlegm"));
            rightLegs.addChild(nameMap.get("rlowlegr"));
            rightLegs.addChild(nameMap.get("rfootf"));
            rightLegs.addChild(nameMap.get("rfootm"));
            rightLegs.addChild(nameMap.get("rfootr"));
            
            return rightLegs;
        }
        
        private static final TransformGroup getLeftLegs(Map<String, Shape3D> nameMap) {
            TransformGroup leftLegs = new TransformGroup();
            
            leftLegs.addChild(nameMap.get("luplegf"));
            leftLegs.addChild(nameMap.get("luplegm"));
            leftLegs.addChild(nameMap.get("luplegr"));
            leftLegs.addChild(nameMap.get("lmidlegf"));
            leftLegs.addChild(nameMap.get("lmidlegm"));
            leftLegs.addChild(nameMap.get("lmidlegr"));
            leftLegs.addChild(nameMap.get("llowlegf"));
            leftLegs.addChild(nameMap.get("llowlegm"));
            leftLegs.addChild(nameMap.get("llowlegr"));
            leftLegs.addChild(nameMap.get("lfootf"));
            leftLegs.addChild(nameMap.get("lfootm"));
            leftLegs.addChild(nameMap.get("lfootr"));
            
            return leftLegs;
        }
        
        private static final TransformGroup getBody(Map<String, Shape3D> nameMap) { 
            TransformGroup body = new TransformGroup();
            
            body.addChild(nameMap.get("sf-12"));
        
            return body;
        }
        
        /**
         * Creates a <code>TransformGroup</code> from the given <code>parts</code>. 
         *
         * @param parts the parts of the cockroach
         * @return a <code>TransformGroup</code> with <code>parts</code> as children
         */
        private static final TransformGroup getRoach(TransformGroup... parts) {
            TransformGroup roach = new TransformGroup();
            
            for (TransformGroup part : parts) {
                roach.addChild(part);
            }
            
            return roach;
        }
    
        /**
         * Allows the given <code>parts</code> to be transformed. 
         *
         * @param parts transform groups that need to have the transform write 
         * capability set
         */
        private static final void enableTransformCapability(TransformGroup... parts) {
            for (TransformGroup part : parts) {
                part.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            }
        }
        
        // COCKROACH ADDONS ********************************************************
        
        
        /**
         * Executes necessary transformations on the cockroach to put it in the right
         * position to be viewed. 
         *
         * @param roach the cockroach
         */
        private static final void transformRoach(TransformGroup roach) {
            Transform3D transform = new Transform3D();
            transform.rotY(90D); 
            transform.setTranslation(new Vector3f(0F, 0F, -2F));
            roach.setTransform(transform);
        }
        
        /**
         * Adds a direction light over the cockroach, which guarantees the roach will
         * always be illuminated. 
         *
         * @param roach the cockroach
         */
        private static final void addLights(TransformGroup roach) {
            Color3f lightColor = new Color3f(Color.RED);
            Vector3f lightDirection = new Vector3f(-1F, -1F, -1F);
            DirectionalLight light = new DirectionalLight(lightColor, lightDirection);
            Bounds influenceRegion = new BoundingSphere();
            light.setInfluencingBounds(influenceRegion);
            roach.addChild(light);
        }
        
        /**
         * Adds a simple tripod movement to the given <code>roach</code>. 
         *
         * @param parts parts from the body of the roach (legs, body)
         * @param roach the supernode of <code>parts</code>
         * @param bounds world bounds, the smae used for lighting 
         */
        private static final void addBehavior(TransformGroup[] parts, 
                                              TransformGroup roach, 
                                              Bounds bounds) {
            
            Behavior behavior = new CockroachBehavior(parts);
            
            /* Behavior will not work if you don't set the scheduling bounds! */
            behavior.setSchedulingBounds(bounds);
            
            roach.addChild(behavior);
        }
        
        
    }
    Java Code:
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import java.awt.*;
    import javax.media.j3d.Canvas3D;
    import javax.swing.*;
    
    
    /**
     * A simple resizable window with the 3D canvas on its center.
     *
     * @author Dalton Filho
     */
    public class CockroachWindow extends JFrame {
        
        
        // INSTANCE ****************************************************************
    
        /** The canvas where the object is rendered. */
        private Canvas3D canvas;
    
        
        // INITIALIZATION **********************************************************
    
        /**
         * Creates a window with a 3D canvas on its center.
         *
         * @param canvas a 3D canvas
         */
        public CockroachWindow(Canvas3D canvas) {
            if (canvas != null ) {
                this.canvas = canvas;
            } else {
                throw new IllegalArgumentException("Canvas cannot be null");
            }
            
            configureWindow();
            
            getContentPane().add(canvas, BorderLayout.CENTER);
        }
    
        /**
         * Defines basic properties of this window.
         */
        private void configureWindow() {
            setTitle("Basic Java3D Program");
            setSize(640, 480);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int locationX = (screenSize.width - getWidth()) / 2;
            int locationY = (screenSize.height - getHeight()) / 2;
            setLocation(locationX,locationY);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        
        
    }
    Java Code:
    import java.io.*;
    import com.sun.j3d.loaders.Loader;
    import com.sun.j3d.loaders.Scene; 
    import com.sun.j3d.loaders.objectfile.ObjectFile; 
    import com.sun.j3d.loaders.lw3d.Lw3dLoader; 
    
    
    /**
     * Creates loaders for different model types.
     *
     * @author Dalton Filho
     */
    public class LoaderFactory {
    
        
        
        public static final Loader getLightwaveLoader() {
            return new Lw3dLoader();
        }
        
    
        public static final Loader getWavefrontLoader() {
            return new ObjectFile(ObjectFile.RESIZE); 
        }
        
        /**
         * Returns a loader for the model on the given <code>path</code>.
         *
         * @param path the path of the model
         * @throws UnsupportedOperationException if the model type is not supported
         * @return a loader for the model on the given <code>path</code>
         */
        public static final Loader getLoaderForModel(String path) 
            throws UnsupportedOperationException {
            
            if (path.endsWith("obj")) {
                return new ObjectFile(ObjectFile.RESIZE); 
            } else 
            if (path.endsWith("lwo")) {
                return new Lw3dLoader(); 
            }
            
            throw new UnsupportedOperationException("Unknown model type");
        }
        
        
    }
    Java Code:
    import java.awt.Color;
    import java.io.IOException;
    import javax.media.j3d.*;
    import javax.vecmath.*;
    import com.sun.j3d.loaders.Loader;
    import com.sun.j3d.loaders.Scene;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    
    
    /** 
     * Controls all aspects from the scene and adds a cockroach model to it.
     *
     * @author Dalton Filho
     */
    public class SceneControl {
        
        
        // INSTANCE ****************************************************************
    
        /** Simplifies the configuration of the scene. */
        private SimpleUniverse universe;
    
        /** The root node of the scene. */
        private BranchGroup root;
        
        /** The canvas where the model is rendered. */
        private Canvas3D canvas;
    
        // INITIALIZATION **********************************************************
        
        public SceneControl() throws IOException {
            initCanvas();
            initUniverse();
            Bounds influenceRegion = new BoundingSphere();
            addModelToUniverse(influenceRegion);
            addLightsToUniverse(influenceRegion);
            addBackground(influenceRegion);
            root.compile();
            universe.addBranchGraph(root);
        }
        
        /**
         * Defines basic properties of the canvas. 
         */
        private void initCanvas() {
            canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
            canvas.setDoubleBufferEnable(true);
            canvas.setFocusable(true);
        }
    
        /** 
         * Defines basic properties of the universe.
         */
        private void initUniverse() {
            universe = new SimpleUniverse(canvas);
            universe.getViewingPlatform().setNominalViewingTransform();
        }
    
        /**
         * Loads a model from disk and assign the root node of the scene
         *
         * @throws IOException if it's impossible to find the 3D model
         */
        private void addModelToUniverse(Bounds influenceRegion) throws IOException {
            Loader loader = LoaderFactory.getWavefrontLoader();
            Scene scene = loader.load("untitled.obj");
            TransformGroup cockroach = CockroachBuilder.getCockroach(scene, influenceRegion);
            root = new BranchGroup();        
            root.addChild(cockroach);
        }
    
        /** 
         * Adds a dramatic blue light... 
         */
        private void addLightsToUniverse(Bounds influenceRegion) {
            Color3f lightColor = new Color3f(Color.BLUE);
            Vector3f lightDirection = new Vector3f(-1F, -1F, -1F);
            DirectionalLight light = new DirectionalLight(lightColor, lightDirection);
            light.setInfluencingBounds(influenceRegion);
            root.addChild(light);
        }
        
        private void addBackground(Bounds influenceRegion) {
            Background background = new Background(new Color3f(Color.LIGHT_GRAY)); 
            background.setApplicationBounds(influenceRegion); 
            root.addChild(background); 
        }
        
        // ACCESS ******************************************************************
        
        public Canvas3D getCanvas() {
            return canvas;
        }
        
        
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: A problem with the simple univers

    That's faaaar too much code to go through, and probably mostly irrelevant to the problem at hand.

    The exception implies that whatever you are trying to load on line 68 of SceneControl is wrong in some way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Simple problem
    By beijct in forum New To Java
    Replies: 6
    Last Post: 12-03-2011, 05:26 AM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 01:43 AM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •