]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/threedee/Java3DWindow.java
f5b324390139f6f4af96271e19ce702a87ee52af
[GpsPrune.git] / src / tim / prune / threedee / Java3DWindow.java
1 package tim.prune.threedee;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.Font;
6 import java.awt.GraphicsConfiguration;
7 import java.awt.GraphicsEnvironment;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12 import java.awt.geom.GeneralPath;
13
14 import javax.media.j3d.AmbientLight;
15 import javax.media.j3d.Appearance;
16 import javax.media.j3d.Billboard;
17 import javax.media.j3d.BoundingSphere;
18 import javax.media.j3d.BranchGroup;
19 import javax.media.j3d.Canvas3D;
20 import javax.media.j3d.DirectionalLight;
21 import javax.media.j3d.Font3D;
22 import javax.media.j3d.FontExtrusion;
23 import javax.media.j3d.GeometryArray;
24 import javax.media.j3d.GraphicsConfigTemplate3D;
25 import javax.media.j3d.Group;
26 import javax.media.j3d.Material;
27 import javax.media.j3d.PointLight;
28 import javax.media.j3d.QuadArray;
29 import javax.media.j3d.Shape3D;
30 import javax.media.j3d.Text3D;
31 import javax.media.j3d.Texture;
32 import javax.media.j3d.TextureAttributes;
33 import javax.media.j3d.Transform3D;
34 import javax.media.j3d.TransformGroup;
35 import javax.swing.JButton;
36 import javax.swing.JFrame;
37 import javax.swing.JOptionPane;
38 import javax.swing.JPanel;
39 import javax.vecmath.Color3f;
40 import javax.vecmath.Matrix3d;
41 import javax.vecmath.Point3d;
42 import javax.vecmath.Point3f;
43 import javax.vecmath.TexCoord2f;
44 import javax.vecmath.Vector3d;
45 import javax.vecmath.Vector3f;
46
47 import tim.prune.DataStatus;
48 import tim.prune.FunctionLibrary;
49 import tim.prune.I18nManager;
50 import tim.prune.data.Track;
51 import tim.prune.function.Export3dFunction;
52 import tim.prune.function.srtm.LookupSrtmFunction;
53 import tim.prune.gui.map.MapSourceLibrary;
54 import tim.prune.save.GroutedImage;
55 import tim.prune.save.MapGrouter;
56
57 import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
58 import com.sun.j3d.utils.geometry.Box;
59 import com.sun.j3d.utils.geometry.Cylinder;
60 import com.sun.j3d.utils.geometry.GeometryInfo;
61 import com.sun.j3d.utils.geometry.NormalGenerator;
62 import com.sun.j3d.utils.geometry.Sphere;
63 import com.sun.j3d.utils.image.TextureLoader;
64 import com.sun.j3d.utils.universe.SimpleUniverse;
65
66
67 /**
68  * Class to hold main window for java3d view of data
69  */
70 public class Java3DWindow implements ThreeDWindow
71 {
72         private Track _track = null;
73         private JFrame _parentFrame = null;
74         private JFrame _frame = null;
75         private ThreeDModel _model = null;
76         private OrbitBehavior _orbit = null;
77         private double _altFactor = -1.0;
78         private ImageDefinition _imageDefinition = null;
79         private GroutedImage _baseImage = null;
80         private TerrainDefinition _terrainDefinition = null;
81         private DataStatus _dataStatus = null;
82
83         /** only prompt about big track size once */
84         private static boolean TRACK_SIZE_WARNING_GIVEN = false;
85
86         // Constants
87         private static final double INITIAL_Y_ROTATION = -25.0;
88         private static final double INITIAL_X_ROTATION = 15.0;
89         private static final String CARDINALS_FONT = "Arial";
90         private static final int MAX_TRACK_SIZE = 2500; // threshold for warning
91         private static final double MODEL_SCALE_FACTOR = 20.0;
92
93
94         /**
95          * Constructor
96          * @param inFrame parent frame
97          */
98         public Java3DWindow(JFrame inFrame)
99         {
100                 _parentFrame = inFrame;
101         }
102
103
104         /**
105          * Set the track object
106          * @param inTrack Track object
107          */
108         public void setTrack(Track inTrack)
109         {
110                 _track = inTrack;
111         }
112
113         /**
114          * @param inFactor altitude factor to use
115          */
116         public void setAltitudeFactor(double inFactor)
117         {
118                 _altFactor = inFactor;
119         }
120
121         /**
122          * Set the parameters for the base image and do the grouting already
123          * (setTrack should already be called by now)
124          */
125         public void setBaseImageParameters(ImageDefinition inDefinition)
126         {
127                 _imageDefinition = inDefinition;
128                 if (inDefinition != null && inDefinition.getUseImage())
129                 {
130                         _baseImage = new MapGrouter().createMapImage(_track, MapSourceLibrary.getSource(inDefinition.getSourceIndex()),
131                                 inDefinition.getZoom());
132                 }
133                 else _baseImage = null;
134         }
135
136         /**
137          * Set the terrain parameters
138          */
139         public void setTerrainParameters(TerrainDefinition inDefinition)
140         {
141                 _terrainDefinition = inDefinition;
142         }
143
144         /**
145          * Set the current data status
146          */
147         public void setDataStatus(DataStatus inStatus)
148         {
149                 _dataStatus = inStatus;
150         }
151
152         /**
153          * Show the window
154          */
155         public void show() throws ThreeDException
156         {
157                 // Make sure altitude exaggeration is positive
158                 if (_altFactor < 0.0) {_altFactor = 1.0;}
159
160                 // Set up the graphics config
161                 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
162                 if (config == null)
163                 {
164                         // Config shouldn't be null, but we can try to create a new one as a workaround
165                         GraphicsConfigTemplate3D gc = new GraphicsConfigTemplate3D();
166                         gc.setDepthSize(0);
167                         config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(gc);
168                 }
169
170                 if (config == null)
171                 {
172                         // Second attempt also failed, going to have to give up here.
173                         throw new ThreeDException("Couldn't create graphics config");
174                 }
175
176                 // Check number of points in model isn't too big, and suggest compression
177                 Object[] buttonTexts = {I18nManager.getText("button.continue"), I18nManager.getText("button.cancel")};
178                 if (_track.getNumPoints() > MAX_TRACK_SIZE && !TRACK_SIZE_WARNING_GIVEN)
179                 {
180                         if (JOptionPane.showOptionDialog(_parentFrame,
181                                         I18nManager.getText("dialog.3d.warningtracksize"),
182                                         I18nManager.getText("function.show3d"), JOptionPane.OK_CANCEL_OPTION,
183                                         JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
184                                 == JOptionPane.OK_OPTION)
185                         {
186                                 // opted to continue, don't show warning again
187                                 TRACK_SIZE_WARNING_GIVEN = true;
188                         }
189                         else {
190                                 // opted to cancel - show warning again next time
191                                 return;
192                         }
193                 }
194
195                 Canvas3D canvas = new Canvas3D(config);
196                 canvas.setSize(400, 300);
197
198                 // Create the scene and attach it to the virtual universe
199                 BranchGroup scene = createSceneGraph();
200                 SimpleUniverse u = new SimpleUniverse(canvas);
201
202                 // This will move the ViewPlatform back a bit so the
203                 // objects in the scene can be viewed.
204                 u.getViewingPlatform().setNominalViewingTransform();
205
206                 // Add behaviour to rotate using mouse
207                 _orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
208                 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
209                 _orbit.setSchedulingBounds(bounds);
210                 u.getViewingPlatform().setViewPlatformBehavior(_orbit);
211                 u.addBranchGraph(scene);
212
213                 // Don't reuse _frame object from last time, because data and/or scale might be different
214                 // Need to regenerate everything
215                 _frame = new JFrame(I18nManager.getText("dialog.3d.title"));
216                 _frame.getContentPane().setLayout(new BorderLayout());
217                 _frame.getContentPane().add(canvas, BorderLayout.CENTER);
218                 _frame.setIconImage(_parentFrame.getIconImage());
219                 // Make panel for render, close buttons
220                 JPanel panel = new JPanel();
221                 panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
222                 // Add button for exporting pov
223                 JButton povButton = new JButton(I18nManager.getText("function.exportpov"));
224                 povButton.addActionListener(new ActionListener() {
225                         /** Export pov button pressed */
226                         public void actionPerformed(ActionEvent e)
227                         {
228                                 if (_orbit != null) {
229                                         callbackRender(FunctionLibrary.FUNCTION_POVEXPORT);
230                                 }
231                         }});
232                 panel.add(povButton);
233                 // Close button
234                 JButton closeButton = new JButton(I18nManager.getText("button.close"));
235                 closeButton.addActionListener(new ActionListener()
236                 {
237                         /** Close button pressed - clean up */
238                         public void actionPerformed(ActionEvent e) {
239                                 dispose();
240                                 _orbit = null;
241                         }
242                 });
243                 panel.add(closeButton);
244                 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
245                 _frame.setSize(500, 350);
246                 _frame.pack();
247                 // Add a listener to clean up when window closed
248                 _frame.addWindowListener(new WindowAdapter() {
249                         public void windowClosing(WindowEvent e) {
250                                 dispose();
251                         }
252                 });
253
254                 // show frame
255                 _frame.setVisible(true);
256                 if (_frame.getState() == JFrame.ICONIFIED) {
257                         _frame.setState(JFrame.NORMAL);
258                 }
259         }
260
261         /**
262          * Dispose of the frame and its resources
263          */
264         public void dispose()
265         {
266                 if (_frame != null) {
267                         _frame.dispose();
268                         _frame = null;
269                 }
270         }
271
272         /**
273          * Create the whole scenery from the given track
274          * @return all objects in the scene
275          */
276         private BranchGroup createSceneGraph()
277         {
278                 // Create the root of the branch graph
279                 BranchGroup objRoot = new BranchGroup();
280
281                 // Create the transform group node and initialize it.
282                 // Enable the TRANSFORM_WRITE capability so it can be spun by the mouse
283                 TransformGroup objTrans = new TransformGroup();
284                 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
285
286                 // Create a translation
287                 Transform3D shiftz = new Transform3D();
288                 shiftz.setScale(0.055);
289                 TransformGroup shiftTrans = new TransformGroup(shiftz);
290
291                 objRoot.addChild(shiftTrans);
292                 Transform3D rotTrans = new Transform3D();
293                 rotTrans.rotY(Math.toRadians(INITIAL_Y_ROTATION));
294                 Transform3D rot2 = new Transform3D();
295                 rot2.rotX(Math.toRadians(INITIAL_X_ROTATION));
296                 TransformGroup tg2 = new TransformGroup(rot2);
297                 objTrans.setTransform(rotTrans);
298                 shiftTrans.addChild(tg2);
299                 tg2.addChild(objTrans);
300
301                 // Base plane
302                 Appearance planeAppearance = null;
303                 Box plane = null;
304                 planeAppearance = new Appearance();
305                 planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
306                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
307                         new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
308                 plane = new Box(10f, 0.04f, 10f, planeAppearance);
309                 objTrans.addChild(plane);
310
311                 // Image on top of base plane, if specified
312                 final boolean showTerrain = _terrainDefinition != null && _terrainDefinition.getUseTerrain();
313                 if (_baseImage != null && !showTerrain)
314                 {
315                         QuadArray baseSquare = new QuadArray (4, QuadArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
316                         baseSquare.setCoordinate(0, new Point3f(-10f, 0.05f, -10f));
317                         baseSquare.setCoordinate(1, new Point3f(-10f, 0.05f, 10f));
318                         baseSquare.setCoordinate(2, new Point3f( 10f, 0.05f, 10f));
319                         baseSquare.setCoordinate(3, new Point3f( 10f, 0.05f, -10f));
320                         // and set anchor points for the texture
321                         baseSquare.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 1.0f));
322                         baseSquare.setTextureCoordinate(0, 1, new TexCoord2f(0.0f, 0.0f));
323                         baseSquare.setTextureCoordinate(0, 2, new TexCoord2f(1.0f, 0.0f));
324                         baseSquare.setTextureCoordinate(0, 3, new TexCoord2f(1.0f, 1.0f));
325                         // Set appearance including image
326                         Appearance baseAppearance = new Appearance();
327                         Texture mapImage = new TextureLoader(_baseImage.getImage(), _frame).getTexture();
328                         baseAppearance.setTexture(mapImage);
329                         objTrans.addChild(new Shape3D(baseSquare, baseAppearance));
330                 }
331
332                 // Create model containing track information
333                 _model = new ThreeDModel(_track);
334                 _model.setAltitudeFactor(_altFactor);
335
336                 if (showTerrain)
337                 {
338                         TerrainHelper terrainHelper = new TerrainHelper(_terrainDefinition.getGridSize());
339                         // See if there's a previously saved terrain track we can reuse
340                         Track terrainTrack = TerrainCache.getTerrainTrack(_dataStatus, _terrainDefinition);
341                         if (terrainTrack == null)
342                         {
343                                 // Construct the terrain track according to these extents and the grid size
344                                 terrainTrack = terrainHelper.createGridTrack(_track);
345                                 // Get the altitudes from SRTM for all the points in the track
346                                 LookupSrtmFunction srtmLookup = (LookupSrtmFunction) FunctionLibrary.FUNCTION_LOOKUP_SRTM;
347                                 srtmLookup.begin(terrainTrack);
348                                 while (srtmLookup.isRunning())
349                                 {
350                                         try {
351                                                 Thread.sleep(750);  // just polling in a wait loop isn't ideal but simple
352                                         }
353                                         catch (InterruptedException e) {}
354                                 }
355
356                                 // Fix the voids
357                                 terrainHelper.fixVoids(terrainTrack);
358
359                                 // Store this back in the cache, maybe we'll need it again
360                                 TerrainCache.storeTerrainTrack(terrainTrack, _dataStatus, _terrainDefinition);
361                         }
362                         // else System.out.println("Yay - reusing the cached track!");
363
364                         // Give the terrain definition to the _model as well
365                         _model.setTerrain(terrainTrack);
366                         _model.scale();
367
368                         objTrans.addChild(createTerrain(_model, terrainHelper, _baseImage));
369                 }
370                 else
371                 {
372                         // No terrain, so just scale the model as it is
373                         _model.scale();
374                 }
375
376                 // N, S, E, W
377                 GeneralPath bevelPath = new GeneralPath();
378                 bevelPath.moveTo(0.0f, 0.0f);
379                 for (int i=0; i<91; i+= 5)
380                 {
381                         bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
382                           (float) (0.1 * Math.sin(Math.toRadians(i))));
383                 }
384                 for (int i=90; i>0; i-=5)
385                 {
386                         bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
387                           (float) (0.1 * Math.sin(Math.toRadians(i))));
388                 }
389                 Font3D compassFont = new Font3D(
390                         new Font(CARDINALS_FONT, Font.PLAIN, 1),
391                         new FontExtrusion(bevelPath));
392                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.n"), new Point3f(0f, 0f, -11.5f), compassFont));
393                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.s"), new Point3f(0f, 0f, 11.5f), compassFont));
394                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11.5f, 0f, 0f), compassFont));
395                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(11.5f, 0f, 0f), compassFont));
396
397                 // Add points to model
398                 objTrans.addChild(createDataPoints(_model));
399
400                 // Create lights - always add ambient light
401                 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
402                 AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
403                 aLgt.setInfluencingBounds(bounds);
404                 objTrans.addChild(aLgt);
405
406                 // Additional lights depend on whether there's a terrain or not
407                 if (showTerrain)
408                 {
409                         // If there's a terrain, just have directional light from northwest
410                         DirectionalLight dl = new DirectionalLight(true,
411                                 new Color3f(1.0f, 1.0f, 1.0f),
412                                 new Vector3f(1.0f, -1.0f, 1.0f));
413                         dl.setInfluencingBounds(bounds);
414                         objTrans.addChild(dl);
415                 }
416                 else
417                 {
418                         // There is no terrain, so use point lights as before
419                         PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
420                                 new Point3f(0f, 0f, 2f), new Point3f(0.25f, 0.05f, 0.0f) );
421                         pLgt.setInfluencingBounds(bounds);
422                         objTrans.addChild(pLgt);
423
424                         PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
425                                 new Point3f(6f, 1f, 6f), new Point3f(0.2f, 0.1f, 0.05f) );
426                         pl2.setInfluencingBounds(bounds);
427                         objTrans.addChild(pl2);
428
429                         PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
430                                 new Point3f(0.0f, 12f, -2f), new Point3f(0.1f, 0.1f, 0.0f) );
431                         pl3.setInfluencingBounds(bounds);
432                         objTrans.addChild(pl3);
433                 }
434
435                 // Have Java 3D perform optimizations on this scene graph.
436                 objRoot.compile();
437
438                 return objRoot;
439         }
440
441
442         /**
443          * Create a text object for compass point, N S E or W
444          * @param inText text to display
445          * @param inLocn position at which to display
446          * @param inFont 3d font to use
447          * @return compound object
448          */
449         private TransformGroup createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
450         {
451                 Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
452                 Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
453                         new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
454                         new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
455                 mat.setLightingEnable(true);
456                 Appearance app = new Appearance();
457                 app.setMaterial(mat);
458                 Shape3D shape = new Shape3D(txt, app);
459
460                 // Make transform group with billboard behaviour
461                 TransformGroup subGroup = new TransformGroup();
462                 subGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
463                 subGroup.addChild(shape);
464                 Billboard billboard = new Billboard(subGroup, Billboard.ROTATE_ABOUT_POINT, inLocn);
465                 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
466                 billboard.setSchedulingBounds(bounds);
467                 subGroup.addChild(billboard);
468                 return subGroup;
469         }
470
471
472         /**
473          * Make a Group of the data points to be added
474          * @param inModel model containing data
475          * @return Group object containing spheres, rods etc
476          */
477         private static Group createDataPoints(ThreeDModel inModel)
478         {
479                 // Add points to model
480                 Group group = new Group();
481                 int numPoints = inModel.getNumPoints();
482                 for (int i=0; i<numPoints; i++)
483                 {
484                         byte pointType = inModel.getPointType(i);
485                         if (pointType == ThreeDModel.POINT_TYPE_WAYPOINT)
486                         {
487                                 // Add waypoint
488                                 // Note that x, y and z are horiz, altitude, -vert
489                                 group.addChild(createWaypoint(new Point3d(
490                                         inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
491                                         inModel.getScaledAltValue(i)   * MODEL_SCALE_FACTOR,
492                                         -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR)));
493                         }
494                         else
495                         {
496                                 // Add colour-coded track point
497                                 // Note that x, y and z are horiz, altitude, -vert
498                                 group.addChild(createTrackpoint(new Point3d(
499                                         inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
500                                         inModel.getScaledAltValue(i)   * MODEL_SCALE_FACTOR,
501                                         -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR), inModel.getPointHeightCode(i)));
502                         }
503                 }
504                 return group;
505         }
506
507
508         /**
509          * Create a waypoint sphere
510          * @param inPointPos position of point
511          * @return Group object containing sphere
512          */
513         private static Group createWaypoint(Point3d inPointPos)
514         {
515                 Material mat = getWaypointMaterial();
516                 // MAYBE: sort symbol scaling
517                 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
518                 return createBall(inPointPos, dot, mat);
519         }
520
521
522         /**
523          * @return a new Material object to define waypoint colour / shine etc
524          */
525         private static Material getWaypointMaterial()
526         {
527                 return new Material(new Color3f(0.1f, 0.1f, 0.4f),
528                          new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.2f, 0.7f),
529                          new Color3f(1.0f, 0.6f, 0.6f), 40.0f);
530         }
531
532
533         /**
534          * @return track point object
535          */
536         private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
537         {
538                 Material mat = getTrackpointMaterial(inHeightCode);
539                 // MAYBE: sort symbol scaling
540                 Sphere dot = new Sphere(0.2f);
541                 return createBall(inPointPos, dot, mat);
542         }
543
544
545         /**
546          * @return Material object for track points with the appropriate colour for the height
547          */
548         private static Material getTrackpointMaterial(byte inHeightCode)
549         {
550                 // create default material
551                 Material mat = new Material(new Color3f(0.3f, 0.2f, 0.1f),
552                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.6f, 0.0f),
553                         new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
554                 // change colour according to height code
555                 if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
556                 else if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
557                 else if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.3f, 0.6f, 0.4f));
558                 else if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
559                 else if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
560                 // return object
561                 return mat;
562         }
563
564
565         /**
566          * Create a ball at the given point
567          * @param inPosition scaled position of point
568          * @param inSphere sphere object
569          * @param inMaterial material object
570          * @return Group containing sphere
571          */
572         private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
573         {
574                 Group group = new Group();
575                 // Create ball and add to group
576                 Transform3D ballShift = new Transform3D();
577                 ballShift.setTranslation(new Vector3d(inPosition));
578                 TransformGroup ballShiftTrans = new TransformGroup(ballShift);
579                 inMaterial.setLightingEnable(true);
580                 Appearance ballApp = new Appearance();
581                 ballApp.setMaterial(inMaterial);
582                 inSphere.setAppearance(ballApp);
583                 ballShiftTrans.addChild(inSphere);
584                 group.addChild(ballShiftTrans);
585                 // Also create rod for ball to sit on
586                 Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
587                 Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
588                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
589                         new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
590                 rodMat.setLightingEnable(true);
591                 Appearance rodApp = new Appearance();
592                 rodApp.setMaterial(rodMat);
593                 rod.setAppearance(rodApp);
594                 Transform3D rodShift = new Transform3D();
595                 rodShift.setTranslation(new Vector3d(inPosition.x, inPosition.y/2.0, inPosition.z));
596                 TransformGroup rodShiftTrans = new TransformGroup(rodShift);
597                 rodShiftTrans.addChild(rod);
598                 group.addChild(rodShiftTrans);
599                 // return the pair
600                 return group;
601         }
602
603         /**
604          * Create a java3d Shape for the terrain
605          * @param inModel threedModel
606          * @param inHelper terrain helper
607          * @param inBaseImage base image for shape, or null for no image
608          * @return Shape3D object
609          */
610         private static Shape3D createTerrain(ThreeDModel inModel, TerrainHelper inHelper, GroutedImage inBaseImage)
611         {
612                 final int numNodes = inHelper.getGridSize();
613                 final int RESULT_SIZE = numNodes * (numNodes * 2 - 2);
614                 int[] stripData = inHelper.getStripLengths();
615
616                 // Get the scaled terrainTrack coordinates (or just heights) from the model
617                 final int nSquared = numNodes * numNodes;
618                 Point3d[] rawPoints = new Point3d[nSquared];
619                 for (int i=0; i<nSquared; i++)
620                 {
621                         double height = inModel.getScaledTerrainValue(i) * MODEL_SCALE_FACTOR;
622                         rawPoints[i] = new Point3d(inModel.getScaledTerrainHorizValue(i) * MODEL_SCALE_FACTOR,
623                                 Math.max(height, 0.05), // make sure it's above the box
624                                 -inModel.getScaledTerrainVertValue(i) * MODEL_SCALE_FACTOR);
625                 }
626
627                 GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
628                 gi.setCoordinates(inHelper.getTerrainCoordinates(rawPoints));
629                 gi.setStripCounts(stripData);
630
631                 Appearance tAppearance = new Appearance();
632                 if (inBaseImage != null)
633                 {
634                         gi.setTextureCoordinateParams(1, 2); // one coord set of two dimensions
635                         gi.setTextureCoordinates(0, inHelper.getTextureCoordinates());
636                         Texture mapImage = new TextureLoader(inBaseImage.getImage()).getTexture();
637                         tAppearance.setTexture(mapImage);
638                         TextureAttributes texAttr = new TextureAttributes();
639                         texAttr.setTextureMode(TextureAttributes.MODULATE);
640                         tAppearance.setTextureAttributes(texAttr);
641                 }
642                 else
643                 {
644                         Color3f[] colours = new Color3f[RESULT_SIZE];
645                         Color3f terrainColour = new Color3f(0.1f, 0.2f, 0.2f);
646                         for (int i=0; i<RESULT_SIZE; i++) {colours[i] = terrainColour;}
647                         gi.setColors(colours);
648                 }
649                 new NormalGenerator().generateNormals(gi);
650                 Material terrnMat = new Material(new Color3f(0.4f, 0.4f, 0.4f), // ambient colour
651                         new Color3f(0f, 0f, 0f), // emissive (none)
652                         new Color3f(0.8f, 0.8f, 0.8f), // diffuse
653                         new Color3f(0.2f, 0.2f, 0.2f), //specular
654                         30f); // shinyness
655                 tAppearance.setMaterial(terrnMat);
656                 return new Shape3D(gi.getGeometryArray(), tAppearance);
657         }
658
659         /**
660          * Calculate the angles and call them back to the app
661          * @param inFunction function to call for export
662          */
663         private void callbackRender(Export3dFunction inFunction)
664         {
665                 Transform3D trans3d = new Transform3D();
666                 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
667                 Matrix3d matrix = new Matrix3d();
668                 trans3d.get(matrix);
669                 Point3d point = new Point3d(0.0, 0.0, 1.0);
670                 matrix.transform(point);
671                 // Set up initial rotations
672                 Transform3D firstTran = new Transform3D();
673                 firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
674                 Transform3D secondTran = new Transform3D();
675                 secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
676                 // Apply inverse rotations in reverse order to the test point
677                 Point3d result = new Point3d();
678                 secondTran.transform(point, result);
679                 firstTran.transform(result);
680
681                 // Give the settings to the rendering function
682                 inFunction.setCameraCoordinates(result.x, result.y, result.z);
683                 inFunction.setAltitudeExaggeration(_altFactor);
684                 inFunction.setTerrainDefinition(_terrainDefinition);
685                 inFunction.setImageDefinition(_imageDefinition);
686
687                 inFunction.begin();
688         }
689 }