/* PlanBClock.java 0.0 09/15/01
* This is a program that will be used to illustrate opportunities and
* problems doing physics with the Java 3D API.
*
* It is built using Sun Microsystem's tutorial example
* HelloJava3Dc.java, the Copyright notice of which is
* reproduced below.
*
* modifications Copyright Fred Klingener 2001
* may be reproduced or used for any non-commercial purpose
*
* Same as HelloJava3D except
* 09-17-01 Add the SimulationClock inner class
* fps counter
* Plan B:
* - WakeupOnElapsedFrames(0);
* - SU.getViewer().getView().setMinimumFrameCycleTime(DeltaT);*
*/
/*
* @(#)HelloJava3Dc.java 1.1 00/09/22 13:55
*
* Copyright (c) 1996-2000 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.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import java.util.Enumeration; // 09-17-01
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.*;
// PlanBClock renders a single, rotating cube.
public class PlanBClock extends Applet
{ String ProgramName = "PlanBClock.java 09-27-2001";
SimpleUniverse simpleU; // Part of Plan B:
// This has to be up here to give the
// SimulationClock Behavior access to
// the View to set the minimum frame time
/* SimpleU
* |
* objRoot (BG)
* |
* SimClock (RotationInterpolator with clock that advances only
* | when renderer is running)
* objSpin (TG)
* |
* ColorCube (Shape)
*/
public BranchGroup createSceneGraph()
{// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the
// identity. Add it to the root of the subgraph.
TransformGroup objSpin = new TransformGroup();
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objSpin);
// 09-17-01 add SimClock Behavior
SimulationClock SimClock =
new SimulationClock(10, 200, objSpin);
BoundingSphere ClockBounds = new BoundingSphere();
SimClock.setSchedulingBounds(ClockBounds);
objRoot.addChild(SimClock);
// Create a simple shape leaf node, add it to the scene graph.
// ColorCube is a Convenience Utility class
objSpin.addChild(new ColorCube(0.4));
return objRoot;
}
// Constructor
public PlanBClock()
{ setLayout(new BorderLayout());
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
canvas3D.setSize(256, 256);
add("Center", canvas3D);
// SimpleUniverse is a Convenience Utility class
simpleU = new SimpleUniverse(canvas3D);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
BranchGroup scene = createSceneGraph();
simpleU.addBranchGraph(scene);
}
// The following allows this to be run as an application
// as well as an applet
public static void main(String[] args)
{ Frame frame = new MainFrame(new PlanBClock(), 256, 256);
}
public class SimulationClock extends Behavior
{// 1. alarm
WakeupCriterion yawn;
// 2. clock
long SimTime;
int DeltaT;
// 3. set model
Alpha SimAlpha;
TransformGroup targetTG;
private Transform3D t3d;
// 4. maintain and report frame rate
final static boolean debug = true;
int FrameCount;
int FrameBlock;
long BlockStartTime;
double AvgDelay;
private java.text.NumberFormat Double1;
public SimulationClock(int ts, int fb, TransformGroup tg)
{ DeltaT = ts;
FrameBlock = fb;
SimAlpha = new Alpha(-1, 4000);
targetTG = tg;
t3d = new Transform3D();
/******* Plan B *******************************/
yawn = new WakeupOnElapsedFrames(0);
/**********************************************/
Double1 = java.text.NumberFormat.getInstance();
Double1.setMaximumFractionDigits(1);
Double1.setMinimumFractionDigits(1);
}
public void initialize()
{ java.text.DateFormat DayTime;
DayTime = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.SHORT, java.text.DateFormat.SHORT);
// 1. alarm
wakeupOn(yawn);
/******* Plan B *******************************/
simpleU.getViewer().getView().setMinimumFrameCycleTime(DeltaT);
/**********************************************/
// 2. clock
SimTime = 0;
// 3. set model
SimAlpha.setStartTime(0);
targetTG.setTransform(t3d);
// 4. maintain and report frame rate
if (debug)
{ FrameCount = 0;
BlockStartTime = System.currentTimeMillis();
System.out.println("");
System.out.println("Program: "+ProgramName);
System.out.println("Run "+DayTime.format(new java.util.Date()));
System.out.println("OS Arch.: "+System.getProperty("os.arch"));
System.out.println("OS: "+System.getProperty("os.name")+
" Version: "+System.getProperty("os.version"));
System.out.println("Averaging block = "+FrameBlock+" Frames");
System.out.println("---------------------------------");
System.out.print(" Frame\n");
System.out.print("DeltaT Delay Lag\n");
System.out.print("(ms) (ms) (ms)\n");
System.out.print("------ --------- -----------\n");
}
}
public void processStimulus(Enumeration e)
{// 1. alarm
wakeupOn(yawn);
// 2. clock
SimTime +=DeltaT;
// 3. set model
t3d.rotY(Math.PI*2.0*SimAlpha.value(SimTime));
targetTG.setTransform(t3d);
// 4. maintain and report frame rate
if (debug)
{ if (++FrameCount >= FrameBlock)
{ long Now = System.currentTimeMillis();
long ET = Now - BlockStartTime;
BlockStartTime = Now;
AvgDelay = ET/FrameBlock;
System.out.print(" "+DeltaT+" "+
Double1.format(AvgDelay)+" ");
String s = "";
for (int i=0; i<(int)AvgDelay-DeltaT; i++)
{ s += "*";
}
System.out.print(s+"\n");
FrameCount = 0;
DeltaT += 1;
wakeupOn(yawn);
/******* Plan B *******************************/
simpleU.getViewer().getView().setMinimumFrameCycleTime(DeltaT);
/**********************************************/
} // end if
} // end if
} // end method
} // end class
}