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