This discussion is archived
8 Replies Latest reply: Dec 20, 2010 6:04 AM by 825347 RSS

Why can I not see and update on my shape? Please help...

843799 Newbie
Currently Being Moderated
I think I am missing something and could not locate where the problem is. Here is what I am doing: I am loading an .obj file to the program and extending the Behavior class is giving me the oppurtunity to modify the vertices. However, I cannot see the changes on the shape during the animation. At first I thought that the GeometryInfo is giving me the points by value and not by reference, however some print out comments revealed that this is not the case. Long story short, here is my code:
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.util.Enumeration;
import java.util.Random;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Material;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupOnElapsedTime;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class GeometryUpdate extends JFrame {


    private Shape3D shape = null;

    public GeometryUpdate() {
        setSize(800, 800);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 200.0);

        GraphicsConfiguration graphConfig = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas3D = new Canvas3D(graphConfig);
        SimpleUniverse simpleUni = new SimpleUniverse(canvas3D);

        BranchGroup sceneGraph = new BranchGroup();
        sceneGraph.setBoundsAutoCompute(true);


        Background background = new Background(new Color3f(Color.WHITE));
        background.setApplicationBounds(bounds);
        sceneGraph.addChild(background);


        // Set up the global lights
        Color3f lColor1 = new Color3f( 0.7f, 0.7f, 0.7f );
        Vector3f lDir1  = new Vector3f( -1.0f, -1.0f, -1.0f );
        Color3f alColor = new Color3f( 0.2f, 0.2f, 0.2f );

        AmbientLight aLgt = new AmbientLight( alColor );
        aLgt.setInfluencingBounds( bounds );
        DirectionalLight lgt1 = new DirectionalLight( lColor1, lDir1 );
        lgt1.setInfluencingBounds( bounds );

        sceneGraph.addChild( aLgt );
        sceneGraph.addChild( lgt1 );

        Scene scene = null;


        // read in the geometry information from the data file
        ObjectFile objFileloader = new ObjectFile( ObjectFile.RESIZE );

        try
        {
            scene = objFileloader.load( "models/bc07.obj" );
        }
        catch ( Exception e )
        {
            scene = null;
            System.err.println( e );
        }

        if( scene == null )
            System.exit( 1 );

        // retrieve the Shape3D object from the scene
        BranchGroup branchGroup = scene.getSceneGroup( );
        shape = (Shape3D) branchGroup.getChild( 0 );

        // create an Appearance and Material
        Appearance app = new Appearance( );
        Color3f objColor = new Color3f( 1.0f, 0.7f, 0.8f );
        Color3f black = new Color3f( 0.0f, 0.0f, 0.0f );
        app.setMaterial( new Material( objColor, black, objColor, black, 80.0f ) );

        // assign the appearance to the Shape
        shape.setAppearance( app );

        TransformGroup objTrans1 = new TransformGroup( );
        objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D tr = new Transform3D( );
        objTrans1.getTransform( tr );
        tr.rotX( 60.0 * Math.PI / 180.0 );
        tr.setScale( 0.75 );
        objTrans1.setTransform( tr );


        TransformGroup objTrans2 = new TransformGroup();
        objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha( -1, Alpha.INCREASING_ENABLE,
                0, 0,
                4000, 0, 0,
                0, 0, 0 );

        RotationInterpolator rotator = new RotationInterpolator(
                        rotationAlpha, objTrans2, yAxis,
                        0.0f, (float) Math.PI*2.0f);
        rotator.setSchedulingBounds(bounds);
        sceneGraph.addChild(rotator);

        objTrans2.addChild(branchGroup);
        objTrans1.addChild( objTrans2 );

        PoreBehavior poreGeomUpdater = new PoreBehavior(5);
        poreGeomUpdater.setSchedulingBounds(bounds);
        sceneGraph.addChild(poreGeomUpdater);

        sceneGraph.addChild(objTrans1);

        sceneGraph.compile();

        simpleUni.addBranchGraph(sceneGraph);
        simpleUni.getViewingPlatform().setNominalViewingTransform();

        add(canvas3D);
    }
I cannot post the obj file here, since it has too many vertices, however the code should work with any obj file, hence I am providing here a link to some obj files: http://people.sc.fsu.edu/~burkardt/data/obj/obj.html
Before I forget, I made a similar code work with opengl calls using lwjgl libraries (I can see the shape changing throughout the simulation) and although I like OpenGl calls better, I need to make this work with Java3D due to some compatibility reasons with some previous work.

Thank you very much for your help.

Edited by: dr.neubauer on May 4, 2010 7:10 PM

Edited by: dr.neubauer on May 4, 2010 7:18 PM
  • 1. Re: Why can I not see and update on my shape? Please help...
    843799 Newbie
    Currently Being Moderated
    Well, I am getting weird errors with Firefox and this site, so, I am sorry for the delay. Here is the rest of the code, I could not post it in the first message due to 7500 error...
    *private* *void* updateData(GeometryInfo geomInf) {
    
    
    *double* sigma = 1;
    
    
    *double* gamma = 1;
    
    
    *double* alpha = 1;
    
    
    *double* epsilon = 0.7;
    
    
     
    
    
    *int* normalSize = geomInf.getNormals().length; //no of faces * 3
    
    
     
    
    
    Random rand = *new* Random();
    
    
    *int* id = rand.nextInt(normalSize);
    
    
     
    
    
    Point3f vertex = geomInf.getCoordinates()[id];
    
    
     
    
    
    // System.out.println("before vertex x: " +vertex.x +" y: " +vertex.y +" z: " +vertex.z);
    
    
     
    
    
    Vector3f virtual = *new* Vector3f (vertex);
    
    
    virtual.add(geomInf.getNormals()[id]);
    
    
    virtual.negate();
    
    
    virtual.normalize();
    
    
    Vector3f difference = *new* Vector3f(vertex.x-virtual.x, vertex.y-virtual.y, vertex.z-virtual.z);
    
    
    *float* differenceLength = difference.length();
    
    
    *float* coeff = (*float*) (gamma/sigma*Math.+exp+(-Math.+pow+(differenceLength,alpha)/2/epsilon/epsilon ));
    
    
    difference.scale(coeff);
    
    
    vertex.add(difference);
    
    
     
    
    
    // System.out.println("after vertex x: " +vertex.x +" y: " +vertex.y +" z: " +vertex.z);
    
    
    // Point3f vertex2 = geomInf.getCoordinates()[id];
    
    
    // System.out.println("updated?? after vertex x: " +vertex2.x +" y: " +vertex2.y +" z: " +vertex2.z);
    
    
     
    
    
    }
    
    
     
    
    
    // -----------------------------------------------------------------------------
    
    
    *private* *class* PoreBehavior *extends* Behavior {
    
    
     
    
    
    *private* WakeupOnElapsedTime elapsedTime = *null*;
    
    
    *private* GeometryInfo gi;
    
    
     
    
    
     
    
    
    PoreBehavior(*long* milliseconds) {
    
    
    elapsedTime = *new* WakeupOnElapsedTime(milliseconds);
    
    
    *this*.gi = *new* GeometryInfo((GeometryArray)(shape.getGeometry()));
    
    
    }
    
    
     
    
    
    @Override
    
    
    *public* *void* initialize() {
    
    
    *this*.wakeupOn(elapsedTime);
    
    
    }
    
    
     
    
    
    @Override
    
    
    *public* *void* processStimulus(_Enumeration_ criteria) {
    
    
     
    
    
    updateData(*this*.gi);
    
    
     
    
    
    *this*.wakeupOn(elapsedTime);
    
    
    }
    
    
    }
    
    
     
    
    
    *public* *static* *void* main(String[] args) {
    
    
    *new* GeometryUpdate().setVisible(*true*);
    
    
    }
    
    
    }
    I appreciate your valuable comments. Thanks...

    Edited by: dr.neubauer on May 4, 2010 7:25 PM

    Edited by: dr.neubauer on May 4, 2010 7:32 PM

    Edited by: dr.neubauer on May 4, 2010 7:32 PM
  • 2. Re: Why can I not see and update on my shape? Please help...
    843799 Newbie
    Currently Being Moderated
    I am really sorry, but somehow I am having problems with posting the code here. The first message was ok, now something is wrong. Sorry for the bad format in the second message, I could not fix it...
  • 3. Re: Why can I not see and update on my shape? Please help...
    793415 Pro
    Currently Being Moderated
    dr.neubauer wrote:
    ...I could not post it in the first message due to 7500 error...
    You should probably regard that not as an 'error' but a 'flashing lights and screaming sirens' warning that nobody is going to spend the time looking through more than 7500 chars of code, looking for an error.

    For better help sooner, post an SSCCE.

    As to the 'double spaced' problem in the second post. Usually if you flip between the 'Plain Text' and 'Preview' tabs of the message posting form, then back again, it will reveal the double spaced text.
  • 4. Re: Why can I not see and update on my shape? Please help...
    843799 Newbie
    Currently Being Moderated
    Ok, I just wanted to have the code in its current status, but here is a simplified version, which has the same shape update problem.
    import java.awt.Color;
    import java.awt.GraphicsConfiguration;
    import java.util.Enumeration;
    import java.util.Random;
    import javax.media.j3d.Alpha;
    import javax.media.j3d.Background;
    import javax.media.j3d.Behavior;
    import javax.media.j3d.BoundingSphere;
    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.Canvas3D;
    import javax.media.j3d.GeometryArray;
    import javax.media.j3d.RotationInterpolator;
    import javax.media.j3d.Shape3D;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.media.j3d.WakeupOnElapsedTime;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    import javax.vecmath.Color3f;
    import javax.vecmath.Point3d;
    import javax.vecmath.Point3f;
    import javax.vecmath.Vector3f;
    import com.sun.j3d.loaders.Scene;
    import com.sun.j3d.loaders.objectfile.ObjectFile;
    import com.sun.j3d.utils.geometry.GeometryInfo;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    
    public class ShortGeometryUpdate extends JFrame {
    
    
        private Shape3D shape = null;
    
        public ShortGeometryUpdate() {
            setSize(800, 800);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 200.0);
            GraphicsConfiguration graphConfig = SimpleUniverse.getPreferredConfiguration();
            Canvas3D canvas3D = new Canvas3D(graphConfig);
            SimpleUniverse simpleUni = new SimpleUniverse(canvas3D);
            BranchGroup sceneGraph = new BranchGroup();
            sceneGraph.setBoundsAutoCompute(true);
            Background background = new Background(new Color3f(Color.WHITE));
            background.setApplicationBounds(bounds);
            sceneGraph.addChild(background);
            Scene scene = null;
            ObjectFile objFileloader = new ObjectFile( ObjectFile.RESIZE );
            try
            {
                scene = objFileloader.load( "models/bc07.obj" );
            }
            catch ( Exception e )
            {
                scene = null;
                System.err.println( e );
            }
            if( scene == null )
                System.exit( 1 );
    
            // retrieve the Shape3D object from the scene
            BranchGroup branchGroup = scene.getSceneGroup( );
            shape = (Shape3D) branchGroup.getChild( 0 );
    
            TransformGroup objTrans2 = new TransformGroup();
            objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    
            Transform3D yAxis = new Transform3D();
            Alpha rotationAlpha = new Alpha( -1, Alpha.INCREASING_ENABLE,
                    0, 0,
                    4000, 0, 0,
                    0, 0, 0 );
    
            RotationInterpolator rotator = new RotationInterpolator(
                            rotationAlpha, objTrans2, yAxis,
                            0.0f, (float) Math.PI*2.0f);
            rotator.setSchedulingBounds(bounds);
            sceneGraph.addChild(rotator);
    
            objTrans2.addChild(branchGroup);
    
            PoreBehavior poreGeomUpdater = new PoreBehavior(5);
            poreGeomUpdater.setSchedulingBounds(bounds);
            sceneGraph.addChild(poreGeomUpdater);     
            sceneGraph.addChild(objTrans2);
            sceneGraph.compile();
            simpleUni.addBranchGraph(sceneGraph);
            simpleUni.getViewingPlatform().setNominalViewingTransform();
            add(canvas3D);
        }
    
        private void updateData(GeometryInfo geomInf) {    
            int normalSize = geomInf.getNormals().length; 
            Random rand = new Random();
            int id = rand.nextInt(normalSize);
            Point3f vertex = geomInf.getCoordinates()[id];
            Vector3f difference  = new Vector3f(10,10,10);
            vertex.add(difference);
        }
    
        private class PoreBehavior extends Behavior {
            private WakeupOnElapsedTime elapsedTime = null;
            private GeometryInfo gi;
            PoreBehavior(long milliseconds) {
                elapsedTime = new WakeupOnElapsedTime(milliseconds);
                this.gi = new GeometryInfo((GeometryArray)(shape.getGeometry()));
            }
            @Override
            public void initialize() {
                this.wakeupOn(elapsedTime);
            }
            @Override
            public void processStimulus(Enumeration criteria) {
                updateData(this.gi);
                this.wakeupOn(elapsedTime);
            }
        }
    
        public static void main(String[] args) {
            new ShortGeometryUpdate().setVisible(true);
        }
    }
  • 5. Re: Why can I not see and update on my shape? Please help...
    843799 Newbie
    Currently Being Moderated
    I hope this cleaner version will be more helpful. If a moderator sees this, maybe my second post can be deleted as well...

    Thanks again.

    Edited by: dr.neubauer on May 5, 2010 1:10 AM
  • 6. Re: Why can I not see and update on my shape? Please help...
    843799 Newbie
    Currently Being Moderated
    Hmm. I have been doing some reading and it seems like Java3D should be notified of the changes made with the geometry via "GeometryArray.updateData(GeometryUpdater updater)". It seems like I am changing the geometry, but Java3D does not know about that... I will check this further, I guess I better dump the update method in an inner class implementing GeometryUpdater.

    I will update this thread with the results or some further questions :)
  • 7. Re: Why can I not see and update on my shape? Please help...
    843799 Newbie
    Currently Being Moderated
    Problem solved, but I think I like OpenGL bindings better than Java3D...
  • 8. Re: Why can I not see and update on my shape? Please help...
    825347 Newbie
    Currently Being Moderated
    I have met with the same problem while I don't know exactly how to solve this problem,

    could you please tell me the detail of solving this problem,or could you show me your code if you don't mind.