Hi Fred,

  Attach is a test program for setSchedulingInterval.
This can be used to fix the jitter problem when the
View point is set based on some object transformation
matrix (change by another Behavior) in the scenegraph.
Please see the public summary of bug
    4332793 -
    View transform & object transform not synchronize in a single behavior

for more detail.

Thanks.

- Kelvin
---------------
Java 3D Team
Sun Microsystems Inc.

>Delivered-To: [EMAIL PROTECTED]
>MIME-Version: 1.0
>Content-Transfer-Encoding: 7bit
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
>Date: Thu, 29 Nov 2001 12:26:58 -0500
>From: Fred Klingener <[EMAIL PROTECTED]>
>Subject: [JAVA3D] Behavior.setSchedulingInterval()
>To: [EMAIL PROTECTED]
>
>Add my voice to the praise for v.1.3.
>
>I've had a week now to explore setSchedulingInterval(), and I've
>been startled by its appetite for all of the procedural kluges
>that had infected my models.  More and more things can be moved
>onto the scene graph where they belong.  I can even see daylight
>in some places now.
>
>Thanks, Java 3D Team.
>
>Just curious about the name, though.  I suppose "scheduling
>interval" has some significance in the way that Behaviors are
>internally ordered, but to the user / scene builder, it's
>obfuscating.  It's easy to hide, of course, so the comment
>doesn't qualify as a whine - after only a few times having to
>stop and figure out which way it worked, I covered it with a
>setPriority(), but the curiosity remains.
>
>Fred Klingener
>Brock Engineering
>
>===========================================================================
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff JAVA3D-INTEREST".  For general help, send email to
>[EMAIL PROTECTED] and include in the body of the message "help".

/*
 *
 *    "@(#)BehaviorIntervalTest.java 1.1 01/06/04"
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class BehaviorIntervalTest extends Applet implements ItemListener {
    CameraFollowBehavior camera;
    RotationInterpolator rotator;
    Choice rotationChoiceBox = new Choice();
    Choice cameraChoiceBox = new Choice();


    public BranchGroup createSceneGraph(SimpleUniverse u) {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create the transform group node and initialize it to the
        // identity.  Enable the TRANSFORM_WRITE capability so that
        // our behavior code can modify it at runtime.  Add it to the
        // root of the subgraph.
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        // Create a simple shape leaf node, add it to the scene graph.
        objTrans.addChild(new ColorCube(0.4));

        // Create a new Behavior object that will perform the desired
        // operation on the specified transform object and add it into
        // the scene graph.
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
                                        0, 0,
                                        4000, 0, 0,
                                        0, 0, 0);

        rotator =  new RotationInterpolator(rotationAlpha, objTrans, yAxis,
                                            0.0f, (float) Math.PI*2.0f);
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        rotator.setSchedulingInterval(5);
        objTrans.addChild(rotator);

        camera = new CameraFollowBehavior(objTrans,
                                          
u.getViewingPlatform().getMultiTransformGroup().getTransformGroup(0));
        // set this to run AFTER rotation intepolator
        camera.setSchedulingInterval(6);

        camera.setSchedulingBounds(bounds);
        objTrans.addChild(camera);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

        return objRoot;
    }

    public BehaviorIntervalTest() {
        setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
        add("Center", c);

        Panel intervalPanel = new Panel();

        for (int i=0; i < Behavior.getNumSchedulingIntervals(); i++) {
            rotationChoiceBox.add("" + i);
            cameraChoiceBox.add("" + i);
        }
        rotationChoiceBox.addItemListener(this);
        cameraChoiceBox.addItemListener(this);

        intervalPanel.setLayout(new FlowLayout());
        intervalPanel.add(new Label("Rotation behavior"));
        intervalPanel.add(rotationChoiceBox);
        intervalPanel.add(new Label("Camera follow behavior"));
        intervalPanel.add(cameraChoiceBox);
        add("South", intervalPanel);
        add("North", new Label("Behavior scheduling interval test"));

        SimpleUniverse u = new SimpleUniverse(c);
        // Create a simple scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph(u);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        u.addBranchGraph(scene);

        rotationChoiceBox.select(rotator.getSchedulingInterval());
        cameraChoiceBox.select(camera.getSchedulingInterval());
    }

    public void itemStateChanged(ItemEvent e) {

        int interval = Integer.parseInt((String) e.getItem());

        if (e.getSource() == rotationChoiceBox) {
            rotator.setSchedulingInterval(interval);
        } else {
            camera.setSchedulingInterval(interval);
        }
    }

    //
    // The following allows BehaviorIntervalTest to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        System.out.println("This example demonstrate a camera chasing a rotating 
cube.");
        System.out.println("You will see the cube will jitter if rotation interpolator 
behavior scheduling interval level is same or larger than camera behavior.\n");
        new MainFrame(new BehaviorIntervalTest(), 512, 512);
    }
}
/*
 *
 *    "@(#)CameraFollowBehavior.java 1.1 01/06/04"
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */


import javax.vecmath.*;
import javax.media.j3d.*;
import java.util.*;

public class CameraFollowBehavior extends Behavior
{
    WakeupCondition wakeup = new WakeupOnElapsedFrames(0);
    TransformGroup rotTarget;
    TransformGroup camera;
    Transform3D trans = new Transform3D();
    Matrix4d zTrans = new Matrix4d();
    Matrix4d mat = new Matrix4d();

    public CameraFollowBehavior(TransformGroup rotTarget, TransformGroup camera) {
        this.rotTarget = rotTarget;
        this.camera = camera;
        
    }

    public void initialize()  {
        mat.setIdentity();
        zTrans.setIdentity();
        zTrans.setTranslation(new Vector3d(0, 0, 2));
        wakeupOn(wakeup);
    }

    public void processStimulus(Enumeration criteria) {

        if ((camera != null) && (rotTarget != null)) {
            rotTarget.getTransform(trans);
            trans.get(mat);
            mat.mul(zTrans);    
            trans.set(mat);
            camera.setTransform(trans);
        }
        wakeupOn(wakeup);
    }
}

Reply via email to