1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
/*
* $RCSfile$
*
* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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 MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS 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 THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
* $Revision$
* $Date$
* $State$
*/
import java.io.*;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.behaviors.interpolators.*;
/*
* This program demonstrates the use of KBRotPosScaleSplinePathInterpolator
* in order to to do spline animation paths using Kochanek-Bartels (also
* known as TCB or Tension-Continuity-Bias ) splines. A cone red cone
* is animated along a spline path specified by 5 knot points, which
* are showns as cyan spheres.
*
* Use the left mouse button to changes orientation of scene.
* Use the middle mouse button to zoom in/out
* Use the right mouse button to pan the scene
*/
public class SplineAnim extends Applet implements ActionListener,
AdjustmentListener,
ItemListener {
// 3D Canvas
Canvas3D canvas;
// UI Components
Panel controlPanel;
Panel canvasPanel;
Button animateButton;
Choice interpChoice;
Scrollbar speedSlider;
Label speedLabel;
Label interpLabel;
// Scene Graph
BoundingSphere bounds;
BranchGroup root;
BranchGroup behaviorBranch;
Transform3D sceneTransform;
TransformGroup sceneTransformGroup;
Transform3D objTransform;
TransformGroup objTransformGroup;
Transform3D lightTransform1;
Transform3D lightTransform2;
TransformGroup light1TransformGroup;
TransformGroup light2TransformGroup;
// Key Frames & Interpolator
int duration = 5000;
Alpha animAlpha;
Transform3D yAxis;
KBKeyFrame[] linearKeyFrames = new KBKeyFrame[6];
KBKeyFrame[] splineKeyFrames = new KBKeyFrame[6];
KBRotPosScaleSplinePathInterpolator splineInterpolator;
KBRotPosScaleSplinePathInterpolator linearInterpolator;
// Data: Knot positions & transform groups
Vector3f pos0 = new Vector3f(-5.0f, -5.0f, 0.0f);
Vector3f pos1 = new Vector3f(-5.0f, 5.0f, 0.0f);
Vector3f pos2 = new Vector3f( 0.0f, 5.0f, 0.0f);
Vector3f pos3 = new Vector3f( 0.0f, -5.0f, 0.0f);
Vector3f pos4 = new Vector3f( 5.0f, -5.0f, 0.0f);
Vector3f pos5 = new Vector3f( 5.0f, 5.0f, 0.0f);
TransformGroup k0TransformGroup;
TransformGroup k1TransformGroup;
TransformGroup k2TransformGroup;
TransformGroup k3TransformGroup;
TransformGroup k4TransformGroup;
TransformGroup k5TransformGroup;
// Flags
boolean animationOn = true;
boolean linear = false;
private SimpleUniverse u = null;
public SplineAnim() {
}
public void init() {
this.setLayout(new FlowLayout());
// Create the canvas and the UI
canvasPanel = new Panel();
controlPanel = new Panel();
createCanvasPanel(canvasPanel);
this.add(canvasPanel);
createControlPanel(controlPanel);
this.add(controlPanel);
// Create the scene.
BranchGroup scene = createSceneGraph();
// Setup keyframe data for our animation
setupSplineKeyFrames ();
setupLinearKeyFrames ();
// Setup alpha, create the interpolators and start them. We
// create both a linear and a spline interpolator and turn on
// one depending on user selection. The default is spline.
setupAnimationData ();
createInterpolators();
startInterpolator();
// Add viewing platform
u = new SimpleUniverse(canvas);
// add mouse behaviors to ViewingPlatform
ViewingPlatform viewingPlatform = u.getViewingPlatform();
viewingPlatform.setNominalViewingTransform();
// add orbit behavior to the ViewingPlatform
OrbitBehavior orbit = new OrbitBehavior(canvas,
OrbitBehavior.REVERSE_ALL);
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
orbit.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(orbit);
u.addBranchGraph(scene);
}
public void destroy() {
u.cleanup();
}
/*
* This creates the control panel which contains a choice menu to
* toggle between spline and linear interpolation, a slider to
* adjust the speed of the animation and a animation start/stop
* button.
*/
private void createControlPanel(Panel p) {
GridBagLayout gl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
p.setLayout (gl);
gbc.weightx = 100; gbc.weighty = 100;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 1;
interpLabel = new Label("Interpolation Type", Label.LEFT);
p.add(interpLabel, gbc);
gbc.gridx = 1; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 1;
interpChoice = new Choice();
interpChoice.add("Spline");
interpChoice.add("Linear");
p.add(interpChoice, gbc);
interpChoice.addItemListener (this);
gbc.gridx = 0; gbc.gridy = 2;
gbc.gridwidth = 2; gbc.gridheight = 1;
speedSlider = new Scrollbar(Scrollbar.HORIZONTAL, 2, 1, 0, 11);
speedSlider.setUnitIncrement (1);
p.add(speedSlider, gbc);
speedSlider.addAdjustmentListener(this);
gbc.gridx = 0; gbc.gridy = 3;
gbc.gridwidth = 2; gbc.gridheight = 1;
speedLabel = new Label(" - Animation Speed +", Label.CENTER);
p.add(speedLabel, gbc);
gbc.gridx = 0; gbc.gridy = 5;
gbc.gridwidth = 2; gbc.gridheight = 1;
animateButton = new Button("Stop Animation");
p.add(animateButton, gbc);
animateButton.addActionListener (this);
}
/*
* This creates the Java3D canvas
*/
private void createCanvasPanel(Panel p) {
GridBagLayout gl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
p.setLayout(gl);
gbc.gridx = 0; gbc.gridy = 0;
gbc.gridwidth = 5; gbc.gridheight = 5;
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config);
canvas.setSize(490,490);
p.add(canvas,gbc);
}
/*
* This creates the scene with 5 knot points represented by cyan
* spheres, a cone obejct that will be transformed, and two directional
* lights + and ambient light.
*/
public BranchGroup createSceneGraph() {
// Colors for lights and objects
Color3f aColor = new Color3f(0.2f, 0.2f, 0.2f);
Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
Color3f coneColor = new Color3f(0.9f, 0.1f, 0.1f);
Color3f sphereColor= new Color3f(0.1f, 0.7f, 0.9f);
Color3f bgColor = new Color3f(0.0f, 0.0f, 0.0f);
Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f);
// Root of the branch grsph
BranchGroup root = new BranchGroup();
// Create transforms such that all objects appears in the scene
sceneTransform = new Transform3D();
sceneTransform.setScale(0.14f);
Transform3D yrot = new Transform3D();
yrot.rotY(-Math.PI/5.0d);
sceneTransform.mul(yrot);
sceneTransformGroup = new TransformGroup(sceneTransform);
sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
root.addChild(sceneTransformGroup);
// Create bounds for the background and lights
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0f);
// Set up the background
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
sceneTransformGroup.addChild(bg);
// Create the transform group node for the lights
lightTransform1 = new Transform3D();
lightTransform2 = new Transform3D();
Vector3d lightPos1 = new Vector3d(0.0, 0.0, 2.0);
Vector3d lightPos2 = new Vector3d(1.0, 0.0, -2.0);
lightTransform1.set(lightPos1);
lightTransform2.set(lightPos2);
light1TransformGroup = new TransformGroup(lightTransform1);
light2TransformGroup = new TransformGroup(lightTransform2);
sceneTransformGroup.addChild(light1TransformGroup);
sceneTransformGroup.addChild(light2TransformGroup);
// Create lights
AmbientLight ambLight = new AmbientLight(aColor);
Light dirLight1;
Light dirLight2;
Vector3f lightDir1 = new Vector3f(lightPos1);
Vector3f lightDir2 = new Vector3f(lightPos2);
lightDir1.negate();
lightDir2.negate();
dirLight1 = new DirectionalLight(lightColor, lightDir1);
dirLight2 = new DirectionalLight(lightColor, lightDir2);
// Set the influencing bounds
ambLight.setInfluencingBounds(bounds);
dirLight1.setInfluencingBounds(bounds);
dirLight2.setInfluencingBounds(bounds);
// Add the lights into the scene graph
sceneTransformGroup.addChild(ambLight);
sceneTransformGroup.addChild(dirLight1);
sceneTransformGroup.addChild(dirLight2);
// Create a cone and add it to the scene graph.
objTransform = new Transform3D();
objTransformGroup = new TransformGroup(objTransform);
objTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
sceneTransformGroup.addChild(objTransformGroup);
Material m = new Material(coneColor, eColor, coneColor, sColor, 100.0f);
Appearance a = new Appearance();
m.setLightingEnable(true);
a.setMaterial(m);
Cone cone = new Cone(0.4f, 1.0f);
cone.setAppearance(a);
objTransformGroup.addChild(cone);
// Create transform groups for each knot point
// knot point 0
Transform3D t3dKnot = new Transform3D();
t3dKnot.set (pos0);
TransformGroup k0TransformGroup = new TransformGroup(t3dKnot);
sceneTransformGroup.addChild(k0TransformGroup);
// knot point 1
t3dKnot = new Transform3D();
t3dKnot.set (pos1);
TransformGroup k1TransformGroup = new TransformGroup(t3dKnot);
sceneTransformGroup.addChild(k1TransformGroup);
// knot point 2
t3dKnot = new Transform3D();
t3dKnot.set (pos2);
TransformGroup k2TransformGroup = new TransformGroup(t3dKnot);
sceneTransformGroup.addChild(k2TransformGroup);
// knot point 3
t3dKnot = new Transform3D();
t3dKnot.set (pos3);
TransformGroup k3TransformGroup = new TransformGroup(t3dKnot);
sceneTransformGroup.addChild(k3TransformGroup);
// knot point 4
t3dKnot = new Transform3D();
t3dKnot.set (pos4);
TransformGroup k4TransformGroup = new TransformGroup(t3dKnot);
sceneTransformGroup.addChild(k4TransformGroup);
// knot point 5
t3dKnot = new Transform3D();
t3dKnot.set (pos5);
TransformGroup k5TransformGroup = new TransformGroup(t3dKnot);
sceneTransformGroup.addChild(k5TransformGroup);
// Create spheres for each knot point's transform group
ColoringAttributes sphereColorAttr = new ColoringAttributes();
sphereColorAttr.setColor(sphereColor);
Appearance sphereAppearance = new Appearance();
sphereAppearance.setColoringAttributes(sphereColorAttr);
k0TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
k1TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
k2TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
k3TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
k4TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
k5TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
return root;
}
/*
* This sets up the key frame data for the spline interpolator. Each knot
* point has a scale and rotation component specified. The second argument
* to KBKeyFrame (in this case 0) tells the interpolator that this is
* to be interpolated using splines. The last three arguments to
* KBKeyFrame are Tension, Continuity, and Bias components for each
* key frame.
*/
private void setupSplineKeyFrames () {
// Prepare spline keyframe data
Point3f p = new Point3f (pos0); // position
float head = (float)Math.PI/2.0f; // heading
float pitch = 0.0f; // pitch
float bank = 0.0f; // bank
Point3f s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale
splineKeyFrames[0] =
new KBKeyFrame(0.0f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos1);
head = 0.0f; // heading
pitch = 0.0f; // pitch
bank = (float)-Math.PI/2.0f; // bank
s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale
splineKeyFrames[1] =
new KBKeyFrame(0.2f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos2);
head = 0.0f; // heading
pitch = 0.0f; // pitch
bank = 0.0f; // bank
s = new Point3f(0.7f, 0.7f, 0.7f); // uniform scale
splineKeyFrames[2] =
new KBKeyFrame(0.4f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos3);
head = (float)Math.PI/2.0f; // heading
pitch = 0.0f; // pitch
bank = (float)Math.PI/2.0f; // bank
s = new Point3f(0.5f, 0.5f, 0.5f); // uniform scale
splineKeyFrames[3] =
new KBKeyFrame(0.6f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos4);
head = (float)-Math.PI/2.0f; // heading
pitch = (float)-Math.PI/2.0f; // pitch
bank = (float)Math.PI/2.0f; // bank
s = new Point3f(0.4f, 0.4f, 0.4f); // uniform scale
splineKeyFrames[4] =
new KBKeyFrame(0.8f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos5);
head = 0.0f; // heading
pitch = 0.0f; // pitch
bank = 0.0f; // bank
s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale
splineKeyFrames[5] =
new KBKeyFrame(1.0f, 0, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
}
/*
* This sets up the key frame data for the linear interpolator. Each knot
* point has a scale and rotation component specified. The second argument
* to KBKeyFrame (in this case 1) tells the interpolator that this is
* to be interpolated linearly. The last three arguments to TCBKeyFrame
* are Tension, Continuity, and Bias components for each key frame.
*/
private void setupLinearKeyFrames () {
// Prepare linear keyframe data
Point3f p = new Point3f (pos0);
float head = 0.0f; // heading
float pitch = 0.0f; // pitch
float bank = 0.0f; // bank
Point3f s = new Point3f(1.0f, 1.0f, 1.0f); // uniform scale
linearKeyFrames[0] =
new KBKeyFrame(0.0f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos1);
linearKeyFrames[1] =
new KBKeyFrame(0.2f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos2);
linearKeyFrames[2] =
new KBKeyFrame(0.4f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos3);
linearKeyFrames[3] =
new KBKeyFrame(0.6f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos4);
linearKeyFrames[4] =
new KBKeyFrame(0.8f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
p = new Point3f (pos5);
linearKeyFrames[5] =
new KBKeyFrame(1.0f, 1, p, head, pitch, bank, s, 0.0f, 0.0f, 0.0f);
}
/*
* This sets up alpha for the interpolator
*/
private void setupAnimationData () {
yAxis = new Transform3D();
animAlpha = new Alpha (-1,Alpha.INCREASING_ENABLE,0,0,duration,0,0,0,0,0);
}
/*
* create a spline and a linear interpolator, but we will activate only
* one in startInterpolator()
*/
private void createInterpolators () {
behaviorBranch = new BranchGroup();
// create spline interpolator
splineInterpolator =
new KBRotPosScaleSplinePathInterpolator(animAlpha, objTransformGroup,
yAxis, splineKeyFrames);
splineInterpolator.setSchedulingBounds(bounds);
behaviorBranch.addChild(splineInterpolator);
// create linear interpolator
linearInterpolator =
new KBRotPosScaleSplinePathInterpolator(animAlpha, objTransformGroup,
yAxis, linearKeyFrames);
linearInterpolator.setSchedulingBounds(bounds);
behaviorBranch.addChild(linearInterpolator);
objTransformGroup.addChild(behaviorBranch);
}
/*
* This activates one of the interpolators depending on the state of the
* linear boolean flag which may be toggled by the user using the choice
* menu.
*/
public void startInterpolator () {
if (animationOn) {
if (linear) {
splineInterpolator.setEnable(false);
linearInterpolator.setEnable(true);
} else {
linearInterpolator.setEnable(false);
splineInterpolator.setEnable(true);
}
}
}
/*
* Toggle animation
*/
public void actionPerformed (ActionEvent event) {
Object source = event.getSource();
if (source == animateButton) {
try {
// toggle animation
if (animationOn) {
animationOn = false;
splineInterpolator.setEnable(false);
linearInterpolator.setEnable(false);
animateButton.setLabel("Start Animation");
} else {
animationOn = true;
startInterpolator();
animateButton.setLabel("Stop Animation");
}
} catch (Exception e) {
System.err.println ("Exception " + e);
}
}
}
/*
* Toggle the interpolators
*/
public void itemStateChanged (ItemEvent event) {
Object source = event.getSource();
ItemSelectable ie = event.getItemSelectable();
if (source == interpChoice) {
try {
if (ie.getSelectedObjects()[0] == "Spline") {
linear = false;
}
if (ie.getSelectedObjects()[0] == "Linear") {
linear = true;
}
startInterpolator();
} catch (Exception e) {
System.err.println ("Exception " + e);
}
}
}
/*
* Adjust the speed of the animations
*/
public void adjustmentValueChanged (AdjustmentEvent e) {
int value = e.getValue();
duration = 6000 - (500 * value);
animAlpha.setIncreasingAlphaDuration(duration);
}
public static void main(String[] args) {
Frame frame = new MainFrame(new SplineAnim(), 500, 600);
}
}
|