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