]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/threedee/Java3DWindow.java
579245db62eb2d07a08d005529f234ea13fa4bb4
[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                 // Close button
231                 JButton closeButton = new JButton(I18nManager.getText("button.close"));
232                 closeButton.addActionListener(new ActionListener()
233                 {
234                         /** Close button pressed - clean up */
235                         public void actionPerformed(ActionEvent e) {
236                                 dispose();
237                                 _orbit = null;
238                         }
239                 });
240                 panel.add(closeButton);
241                 _frame.getContentPane().add(panel, BorderLayout.SOUTH);
242                 _frame.setSize(500, 350);
243                 _frame.pack();
244                 // Add a listener to clean up when window closed
245                 _frame.addWindowListener(new WindowAdapter() {
246                         public void windowClosing(WindowEvent e) {
247                                 dispose();
248                         }
249                 });
250
251                 // show frame
252                 _frame.setVisible(true);
253                 if (_frame.getState() == JFrame.ICONIFIED) {
254                         _frame.setState(JFrame.NORMAL);
255                 }
256         }
257
258         /**
259          * Dispose of the frame and its resources
260          */
261         public void dispose()
262         {
263                 if (_frame != null) {
264                         _frame.dispose();
265                         _frame = null;
266                 }
267         }
268
269         /**
270          * Create the whole scenery from the given track
271          * @return all objects in the scene
272          */
273         private BranchGroup createSceneGraph()
274         {
275                 // Create the root of the branch graph
276                 BranchGroup objRoot = new BranchGroup();
277
278                 // Create the transform group node and initialize it.
279                 // Enable the TRANSFORM_WRITE capability so it can be spun by the mouse
280                 TransformGroup objTrans = new TransformGroup();
281                 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
282
283                 // Create a translation
284                 Transform3D shiftz = new Transform3D();
285                 shiftz.setScale(0.055);
286                 TransformGroup shiftTrans = new TransformGroup(shiftz);
287
288                 objRoot.addChild(shiftTrans);
289                 Transform3D rotTrans = new Transform3D();
290                 rotTrans.rotY(Math.toRadians(INITIAL_Y_ROTATION));
291                 Transform3D rot2 = new Transform3D();
292                 rot2.rotX(Math.toRadians(INITIAL_X_ROTATION));
293                 TransformGroup tg2 = new TransformGroup(rot2);
294                 objTrans.setTransform(rotTrans);
295                 shiftTrans.addChild(tg2);
296                 tg2.addChild(objTrans);
297
298                 // Base plane
299                 Appearance planeAppearance = null;
300                 Box plane = null;
301                 planeAppearance = new Appearance();
302                 planeAppearance.setMaterial(new Material(new Color3f(0.1f, 0.2f, 0.2f),
303                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.3f, 0.4f, 0.4f),
304                         new Color3f(0.3f, 0.3f, 0.3f), 0.0f));
305                 plane = new Box(10f, 0.04f, 10f, planeAppearance);
306                 objTrans.addChild(plane);
307
308                 // Image on top of base plane, if specified
309                 final boolean showTerrain = _terrainDefinition != null && _terrainDefinition.getUseTerrain();
310                 if (_baseImage != null && !showTerrain)
311                 {
312                         QuadArray baseSquare = new QuadArray (4, QuadArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
313                         baseSquare.setCoordinate(0, new Point3f(-10f, 0.05f, -10f));
314                         baseSquare.setCoordinate(1, new Point3f(-10f, 0.05f, 10f));
315                         baseSquare.setCoordinate(2, new Point3f( 10f, 0.05f, 10f));
316                         baseSquare.setCoordinate(3, new Point3f( 10f, 0.05f, -10f));
317                         // and set anchor points for the texture
318                         baseSquare.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 1.0f));
319                         baseSquare.setTextureCoordinate(0, 1, new TexCoord2f(0.0f, 0.0f));
320                         baseSquare.setTextureCoordinate(0, 2, new TexCoord2f(1.0f, 0.0f));
321                         baseSquare.setTextureCoordinate(0, 3, new TexCoord2f(1.0f, 1.0f));
322                         // Set appearance including image
323                         Appearance baseAppearance = new Appearance();
324                         Texture mapImage = new TextureLoader(_baseImage.getImage(), _frame).getTexture();
325                         baseAppearance.setTexture(mapImage);
326                         objTrans.addChild(new Shape3D(baseSquare, baseAppearance));
327                 }
328
329                 // Create model containing track information
330                 _model = new ThreeDModel(_track);
331                 _model.setAltitudeFactor(_altFactor);
332
333                 if (showTerrain)
334                 {
335                         TerrainHelper terrainHelper = new TerrainHelper(_terrainDefinition.getGridSize());
336                         // See if there's a previously saved terrain track we can reuse
337                         Track terrainTrack = TerrainCache.getTerrainTrack(_dataStatus, _terrainDefinition);
338                         if (terrainTrack == null)
339                         {
340                                 // Construct the terrain track according to these extents and the grid size
341                                 terrainTrack = terrainHelper.createGridTrack(_track);
342                                 // Get the altitudes from SRTM for all the points in the track
343                                 LookupSrtmFunction srtmLookup = (LookupSrtmFunction) FunctionLibrary.FUNCTION_LOOKUP_SRTM;
344                                 srtmLookup.begin(terrainTrack);
345                                 while (srtmLookup.isRunning())
346                                 {
347                                         try {
348                                                 Thread.sleep(750);  // just polling in a wait loop isn't ideal but simple
349                                         }
350                                         catch (InterruptedException e) {}
351                                 }
352
353                                 // Fix the voids
354                                 terrainHelper.fixVoids(terrainTrack);
355
356                                 // Store this back in the cache, maybe we'll need it again
357                                 TerrainCache.storeTerrainTrack(terrainTrack, _dataStatus, _terrainDefinition);
358                         }
359                         // else System.out.println("Yay - reusing the cached track!");
360
361                         // Give the terrain definition to the _model as well
362                         _model.setTerrain(terrainTrack);
363                         _model.scale();
364
365                         objTrans.addChild(createTerrain(_model, terrainHelper, _baseImage));
366                 }
367                 else
368                 {
369                         // No terrain, so just scale the model as it is
370                         _model.scale();
371                 }
372
373                 // N, S, E, W
374                 GeneralPath bevelPath = new GeneralPath();
375                 bevelPath.moveTo(0.0f, 0.0f);
376                 for (int i=0; i<91; i+= 5) {
377                         bevelPath.lineTo((float) (0.1 - 0.1 * Math.cos(Math.toRadians(i))),
378                           (float) (0.1 * Math.sin(Math.toRadians(i))));
379                 }
380                 for (int i=90; i>0; i-=5) {
381                         bevelPath.lineTo((float) (0.3 + 0.1 * Math.cos(Math.toRadians(i))),
382                           (float) (0.1 * Math.sin(Math.toRadians(i))));
383                 }
384                 Font3D compassFont = new Font3D(
385                         new Font(CARDINALS_FONT, Font.PLAIN, 1),
386                         new FontExtrusion(bevelPath));
387                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.n"), new Point3f(0f, 0f, -10f), compassFont));
388                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.s"), new Point3f(0f, 0f, 10f), compassFont));
389                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.w"), new Point3f(-11f, 0f, 0f), compassFont));
390                 objTrans.addChild(createCompassPoint(I18nManager.getText("cardinal.e"), new Point3f(10f, 0f, 0f), compassFont));
391
392                 // Add points to model
393                 objTrans.addChild(createDataPoints(_model));
394
395                 // Create lights
396                 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
397                 AmbientLight aLgt = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
398                 aLgt.setInfluencingBounds(bounds);
399                 objTrans.addChild(aLgt);
400
401                 PointLight pLgt = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
402                         new Point3f(0f, 0f, 2f), new Point3f(0.25f, 0.05f, 0.0f) );
403                 pLgt.setInfluencingBounds(bounds);
404                 objTrans.addChild(pLgt);
405
406                 PointLight pl2 = new PointLight(new Color3f(0.8f, 0.9f, 0.4f),
407                         new Point3f(6f, 1f, 6f), new Point3f(0.2f, 0.1f, 0.05f) );
408                 pl2.setInfluencingBounds(bounds);
409                 objTrans.addChild(pl2);
410
411                 PointLight pl3 = new PointLight(new Color3f(0.7f, 0.7f, 0.7f),
412                         new Point3f(0.0f, 12f, -2f), new Point3f(0.1f, 0.1f, 0.0f) );
413                 pl3.setInfluencingBounds(bounds);
414                 objTrans.addChild(pl3);
415
416                 // Have Java 3D perform optimizations on this scene graph.
417                 objRoot.compile();
418
419                 return objRoot;
420         }
421
422
423         /**
424          * Create a text object for compass point, N S E or W
425          * @param text text to display
426          * @param locn position at which to display
427          * @param font 3d font to use
428          * @return Shape3D object
429          */
430         private Shape3D createCompassPoint(String inText, Point3f inLocn, Font3D inFont)
431         {
432                 Text3D txt = new Text3D(inFont, inText, inLocn, Text3D.ALIGN_FIRST, Text3D.PATH_RIGHT);
433                 Material mat = new Material(new Color3f(0.5f, 0.5f, 0.55f),
434                         new Color3f(0.05f, 0.05f, 0.1f), new Color3f(0.3f, 0.4f, 0.5f),
435                         new Color3f(0.4f, 0.5f, 0.7f), 70.0f);
436                 mat.setLightingEnable(true);
437                 Appearance app = new Appearance();
438                 app.setMaterial(mat);
439                 Shape3D shape = new Shape3D(txt, app);
440                 return shape;
441         }
442
443
444         /**
445          * Make a Group of the data points to be added
446          * @param inModel model containing data
447          * @return Group object containing spheres, rods etc
448          */
449         private static Group createDataPoints(ThreeDModel inModel)
450         {
451                 // Add points to model
452                 Group group = new Group();
453                 int numPoints = inModel.getNumPoints();
454                 for (int i=0; i<numPoints; i++)
455                 {
456                         byte pointType = inModel.getPointType(i);
457                         if (pointType == ThreeDModel.POINT_TYPE_WAYPOINT)
458                         {
459                                 // Add waypoint
460                                 // Note that x, y and z are horiz, altitude, -vert
461                                 group.addChild(createWaypoint(new Point3d(
462                                         inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
463                                         inModel.getScaledAltValue(i)   * MODEL_SCALE_FACTOR,
464                                         -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR)));
465                         }
466                         else
467                         {
468                                 // Add colour-coded track point
469                                 // Note that x, y and z are horiz, altitude, -vert
470                                 group.addChild(createTrackpoint(new Point3d(
471                                         inModel.getScaledHorizValue(i) * MODEL_SCALE_FACTOR,
472                                         inModel.getScaledAltValue(i)   * MODEL_SCALE_FACTOR,
473                                         -inModel.getScaledVertValue(i) * MODEL_SCALE_FACTOR), inModel.getPointHeightCode(i)));
474                         }
475                 }
476                 return group;
477         }
478
479
480         /**
481          * Create a waypoint sphere
482          * @param inPointPos position of point
483          * @return Group object containing sphere
484          */
485         private static Group createWaypoint(Point3d inPointPos)
486         {
487                 Material mat = getWaypointMaterial();
488                 // MAYBE: sort symbol scaling
489                 Sphere dot = new Sphere(0.35f); // * symbolScaling / 100f);
490                 return createBall(inPointPos, dot, mat);
491         }
492
493
494         /**
495          * @return a new Material object to define waypoint colour / shine etc
496          */
497         private static Material getWaypointMaterial()
498         {
499                 return new Material(new Color3f(0.1f, 0.1f, 0.4f),
500                          new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.2f, 0.7f),
501                          new Color3f(1.0f, 0.6f, 0.6f), 40.0f);
502         }
503
504
505         /**
506          * @return track point object
507          */
508         private static Group createTrackpoint(Point3d inPointPos, byte inHeightCode)
509         {
510                 Material mat = getTrackpointMaterial(inHeightCode);
511                 // MAYBE: sort symbol scaling
512                 Sphere dot = new Sphere(0.2f);
513                 return createBall(inPointPos, dot, mat);
514         }
515
516
517         /**
518          * @return Material object for track points with the appropriate colour for the height
519          */
520         private static Material getTrackpointMaterial(byte inHeightCode)
521         {
522                 // create default material
523                 Material mat = new Material(new Color3f(0.3f, 0.2f, 0.1f),
524                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.0f, 0.6f, 0.0f),
525                         new Color3f(1.0f, 0.6f, 0.6f), 70.0f);
526                 // change colour according to height code
527                 if (inHeightCode == 1) mat.setDiffuseColor(new Color3f(0.4f, 0.9f, 0.2f));
528                 else if (inHeightCode == 2) mat.setDiffuseColor(new Color3f(0.7f, 0.8f, 0.2f));
529                 else if (inHeightCode == 3) mat.setDiffuseColor(new Color3f(0.3f, 0.6f, 0.4f));
530                 else if (inHeightCode == 4) mat.setDiffuseColor(new Color3f(0.1f, 0.9f, 0.9f));
531                 else if (inHeightCode >= 5) mat.setDiffuseColor(new Color3f(1.0f, 1.0f, 1.0f));
532                 // return object
533                 return mat;
534         }
535
536
537         /**
538          * Create a ball at the given point
539          * @param inPosition scaled position of point
540          * @param inSphere sphere object
541          * @param inMaterial material object
542          * @return Group containing sphere
543          */
544         private static Group createBall(Point3d inPosition, Sphere inSphere, Material inMaterial)
545         {
546                 Group group = new Group();
547                 // Create ball and add to group
548                 Transform3D ballShift = new Transform3D();
549                 ballShift.setTranslation(new Vector3d(inPosition));
550                 TransformGroup ballShiftTrans = new TransformGroup(ballShift);
551                 inMaterial.setLightingEnable(true);
552                 Appearance ballApp = new Appearance();
553                 ballApp.setMaterial(inMaterial);
554                 inSphere.setAppearance(ballApp);
555                 ballShiftTrans.addChild(inSphere);
556                 group.addChild(ballShiftTrans);
557                 // Also create rod for ball to sit on
558                 Cylinder rod = new Cylinder(0.1f, (float) inPosition.y);
559                 Material rodMat = new Material(new Color3f(0.2f, 0.2f, 0.2f),
560                         new Color3f(0.0f, 0.0f, 0.0f), new Color3f(0.2f, 0.2f, 0.2f),
561                         new Color3f(0.05f, 0.05f, 0.05f), 0.4f);
562                 rodMat.setLightingEnable(true);
563                 Appearance rodApp = new Appearance();
564                 rodApp.setMaterial(rodMat);
565                 rod.setAppearance(rodApp);
566                 Transform3D rodShift = new Transform3D();
567                 rodShift.setTranslation(new Vector3d(inPosition.x, inPosition.y/2.0, inPosition.z));
568                 TransformGroup rodShiftTrans = new TransformGroup(rodShift);
569                 rodShiftTrans.addChild(rod);
570                 group.addChild(rodShiftTrans);
571                 // return the pair
572                 return group;
573         }
574
575         /**
576          * Create a java3d Shape for the terrain
577          * @param inModel threedModel
578          * @param inHelper terrain helper
579          * @param inBaseImage base image for shape, or null for no image
580          * @return Shape3D object
581          */
582         private static Shape3D createTerrain(ThreeDModel inModel, TerrainHelper inHelper, GroutedImage inBaseImage)
583         {
584                 final int numNodes = inHelper.getGridSize();
585                 final int RESULT_SIZE = numNodes * (numNodes * 2 - 2);
586                 int[] stripData = inHelper.getStripLengths();
587
588                 // Get the scaled terrainTrack coordinates (or just heights) from the model
589                 final int nSquared = numNodes * numNodes;
590                 Point3d[] rawPoints = new Point3d[nSquared];
591                 for (int i=0; i<nSquared; i++)
592                 {
593                         double height = inModel.getScaledTerrainValue(i) * MODEL_SCALE_FACTOR;
594                         rawPoints[i] = new Point3d(inModel.getScaledTerrainHorizValue(i) * MODEL_SCALE_FACTOR,
595                                 Math.max(height, 0.05), // make sure it's above the box
596                                 -inModel.getScaledTerrainVertValue(i) * MODEL_SCALE_FACTOR);
597                 }
598
599                 GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_STRIP_ARRAY);
600                 gi.setCoordinates(inHelper.getTerrainCoordinates(rawPoints));
601                 gi.setStripCounts(stripData);
602
603                 Appearance tAppearance = new Appearance();
604                 if (inBaseImage != null)
605                 {
606                         gi.setTextureCoordinateParams(1, 2); // one coord set of two dimensions
607                         gi.setTextureCoordinates(0, inHelper.getTextureCoordinates());
608                         Texture mapImage = new TextureLoader(inBaseImage.getImage()).getTexture();
609                         tAppearance.setTexture(mapImage);
610                         TextureAttributes texAttr = new TextureAttributes();
611                         texAttr.setTextureMode(TextureAttributes.MODULATE);
612                         tAppearance.setTextureAttributes(texAttr);
613                 }
614                 else
615                 {
616                         Color3f[] colours = new Color3f[RESULT_SIZE];
617                         Color3f terrainColour = new Color3f(0.1f, 0.2f, 0.2f);
618                         for (int i=0; i<RESULT_SIZE; i++) {colours[i] = terrainColour;}
619                         gi.setColors(colours);
620                 }
621                 new NormalGenerator().generateNormals(gi);
622                 Material terrnMat = new Material(new Color3f(0.4f, 0.4f, 0.4f), // ambient colour
623                         new Color3f(0f, 0f, 0f), // emissive (none)
624                         new Color3f(0.8f, 0.8f, 0.8f), // diffuse
625                         new Color3f(0.2f, 0.2f, 0.2f), //specular
626                         30f); // shinyness
627                 tAppearance.setMaterial(terrnMat);
628                 return new Shape3D(gi.getGeometryArray(), tAppearance);
629         }
630
631         /**
632          * Calculate the angles and call them back to the app
633          * @param inFunction function to call for export
634          */
635         private void callbackRender(Export3dFunction inFunction)
636         {
637                 Transform3D trans3d = new Transform3D();
638                 _orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
639                 Matrix3d matrix = new Matrix3d();
640                 trans3d.get(matrix);
641                 Point3d point = new Point3d(0.0, 0.0, 1.0);
642                 matrix.transform(point);
643                 // Set up initial rotations
644                 Transform3D firstTran = new Transform3D();
645                 firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
646                 Transform3D secondTran = new Transform3D();
647                 secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
648                 // Apply inverse rotations in reverse order to the test point
649                 Point3d result = new Point3d();
650                 secondTran.transform(point, result);
651                 firstTran.transform(result);
652
653                 // Give the settings to the rendering function
654                 inFunction.setCameraCoordinates(result.x, result.y, result.z);
655                 inFunction.setAltitudeExaggeration(_altFactor);
656                 inFunction.setTerrainDefinition(_terrainDefinition);
657                 inFunction.setImageDefinition(_imageDefinition);
658
659                 inFunction.begin();
660         }
661 }