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