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