Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Enviro.h
1 //
2 // class Enviro: Main functionality of the Enviro application
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef ENVIROH
9 #define ENVIROH
10 
11 #include "vtlib/core/TerrainScene.h"
12 
13 #include "vtdata/Fence.h"
14 #include "vtdata/Projections.h"
15 #include "vtlib/core/AnimPath.h"
16 #include "vtlib/core/Elastic.h"
17 #include "vtlib/core/NavEngines.h"
18 #include "vtlib/core/Vehicles.h"
19 #include "EnviroEnum.h"
20 #include "PlantingOptions.h"
21 #include <pthread.h>
22 //#include "RTSP_lib.h"
23 
24 
25 
26 
27 #define INITIAL_SPACE_DIST 3.1f
28 
29 // Use forward declarations to minimize rebuild dependencies
30 class vtTerrainScene;
31 class vtTerrain;
32 class TerrainPicker;
33 class vtIcoGlobe;
34 class vtRoute;
35 class vtUtilNode;
36 class vtFence3d;
37 class vtAbstractLayer;
38 class GlobeLayer;
39 
40 // Engines
41 class GlobePicker;
42 class GrabFlyer;
43 class FlatFlyer;
44 class MapOverviewEngine;
45 class CarEngine;
46 class vtSpriteSizer;
47 
48 // Plants
49 class vtSpeciesList3d;
50 
51 class ControlEngine : public vtEngine
52 {
53 public:
54  void Eval();
55 };
56 
66 class Enviro : public vtTerrainScene
67 {
68 public:
69  Enviro();
70  virtual ~Enviro();
71 
72  // methods
73  void Startup();
74  void Shutdown();
75  virtual void StartupArgument(int i, const char *str);
76 
77  void LoadAllTerrainDescriptions();
78  void LoadGlobalContent();
79  void StartControlEngine();
80  void DoControl();
81  void DoControlOrbit();
82  void DoControlTerrain();
83  void SetTerrain(vtTerrain *pTerrain);
84  vtGroup *GetRoot() { return m_pRoot; }
85  void StoreTerrainParameters();
86  osgText::Font *GetArial() { return m_pArial; }
87 
88  void ShowElevationLegend(bool bShow);
89  bool GetShowElevationLegend();
90  void ShowCompass(bool bShow);
91  bool GetShowCompass();
92  void ShowMapOverview(bool bShow);
93  bool GetShowMapOverview();
94  void TextureHasChanged();
95 
96  // navigation and camera
97  virtual void SetFlightSpeed(float speed);
98  float GetFlightSpeed();
99  void SetFlightAccel(bool bAccel);
100  bool GetFlightAccel();
101  void SetTopDown(bool bTopDown);
102  void SetCurrentNavigator(vtTerrainFlyer *pE);
103  void EnableFlyerEngine(bool bEnable);
104  void SetNavType(NavType nav);
105  NavType GetNavType() {return m_nav; }
106  void SetMaintain(bool bOn);
107  bool GetMaintain();
108  void ResetCamera();
109 
110  void SetMode(MouseMode mode);
111  void DumpCameraInfo();
112  void SetSpeed(float x);
113  float GetSpeed();
114  vtString GetStatusString(int which);
115 
116  // go to space or a terrain
117  void FlyToSpace();
118  bool SwitchToTerrain(const char *name);
119  void SwitchToTerrain(vtTerrain *pTerr);
120 
121  // these work in space
122  vtIcoGlobe *GetGlobe() { return m_pIcoGlobe; }
123  vtIcoGlobe *GetOverlayGlobe() { return m_pOverlayGlobe; }
124  void SetEarthShading(bool bShade);
125  bool GetEarthShading();
126  void SetEarthTilt(bool bTilt);
127  bool GetEarthTilt();
128  void SetEarthShape(bool Flat);
129  bool GetEarthShape() { return m_bGlobeFlat; }
130  void SetEarthUnfold(bool Flat);
131  bool GetEarthUnfold() { return m_bGlobeUnfolded; }
132  void SetSpaceAxes(bool bShow);
133  bool GetSpaceAxes();
134  int AddGlobeAbstractLayer(const char *fname);
135  void RemoveGlobeAbstractLayer(GlobeLayer *glay);
136  void UpdateEarthArc();
137  void SetDisplayedArc(const DPoint2 &g1, const DPoint2 &g2);
138  void SetDisplayedArc(const DLine2 &path);
139  void SetEarthLines(double lon, double lat);
140  void ShowEarthLines(bool bShow);
141  vtTerrain *FindTerrainOnEarth(const DPoint2 &p);
142 
143  vtString GetMessage() { return m_strMessage; }
144  void SetMessage(const vtString &msg, float time = 0.0f);
145  void FormatCoordString(vtString &str, const DPoint3 &coord, LinearUnits units, bool seconds = false);
146  void DescribeCoordinatesEarth(vtString &str);
147  void DescribeCoordinatesTerrain(vtString &str);
148  void DescribeCLOD(vtString &str);
149 
150  // location of 3d cursor on the ground, in earth coordinates
151  DPoint3 m_EarthPos;
152  TerrainPicker *m_pTerrainPicker;
153  GlobePicker *m_pGlobePicker;
154  vtMovGeode *m_pCursorMGeom;
155 
156  // navigation engines
157  vtTerrainFlyer *m_pTFlyer;
158  VFlyer *m_pVFlyer;
159  vtOrthoFlyer *m_pOrthoFlyer;
160  QuakeFlyer *m_pQuakeFlyer;
161  vtTerrainFlyer *m_pCurrentFlyer;
162  GrabFlyer *m_pGFlyer;
163  FlatFlyer *m_pFlatFlyer;
164  vtPanoFlyer *m_pPanoFlyer;
165  // engine to keep the camera above the terrain
166  vtHeightConstrain *m_pHeightEngine;
167 
168  // event handlers
169  bool OnMouse(vtMouseEvent &event);
170  void OnMouseLeftDownTerrain(vtMouseEvent &event);
171  void OnMouseLeftDownTerrainSelect(vtMouseEvent &event);
172  void OnMouseLeftDownTerrainMove(vtMouseEvent &event);
173  void OnMouseLeftDownOrbit(vtMouseEvent &event);
174  void OnMouseLeftUp(vtMouseEvent &event);
175  void OnMouseLeftUpBox(vtMouseEvent &event);
176  void OnMouseRightDown(vtMouseEvent &event);
177  void OnMouseRightUp(vtMouseEvent &event);
178  void OnMouseMove(vtMouseEvent &event);
179  void OnMouseMoveTerrain(vtMouseEvent &event);
180  void OnMouseSelectRayPick(vtMouseEvent &event);
181  void OnMouseSelectCursorPick(vtMouseEvent &event);
182  bool OnMouseCompass(vtMouseEvent &event);
183 
184  // fence methods
185  void start_new_fence();
186  void finish_fence();
187  void close_fence();
188  void SetFenceOptions(const vtLinearParams &param, bool bProfileChanged = false);
189 
190  // route methods
191  void start_new_route();
192  void finish_route();
193  void close_route();
194  void SetRouteOptions(const vtString &sStructType);
195 
196  // plants
197  void LoadSpeciesList();
198  vtSpeciesList3d *GetPlantList() { return m_pPlantList; }
199  PlantingOptions &GetPlantOptions() { return m_PlantOpt; }
200  bool PlantATree(const DPoint2 &epos);
201  void SetPlantOptions(const PlantingOptions &opt);
202 
203  // vehicles
204  void SetVehicleOptions(const VehicleOptions &opt);
205  void CreateVirtualCamera(const Camera3kOptions &opt);
206  VehicleManager m_VehicleManager;
207  VehicleSet m_Vehicles;
208 
209  // import
210  bool ImportModelFromKML(const char *kmlfile);
211 
212  // abstract layers
213  vtAbstractLayer *GetLabelLayer();
214  int NumSelectedAbstractFeatures();
215 
216  // distance tool
217  void SetDistanceToolMode(bool bPath);
218  void SetDistanceTool(const DLine2 &line);
219  void ResetDistanceTool();
220  void UpdateDistanceTool();
221 
222  // Wind
223  void SetWind(int iDirection, float fSpeed);
224 
225  // UI
226  void UpdateCompass();
227 
228  // global state
229  AppState m_state;
230  MouseMode m_mode;
231  NavType m_nav;
232  bool m_bOnTerrain;
233  vtString m_strMessage;
234 
235  // used for mouse interaction
236  bool m_bDragging;
237  bool m_bDragUpDown;
238  bool m_bRotating;
239  bool m_bSelectedStruct;
240  bool m_bSelectedPlant;
241  bool m_bSelectedUtil;
242  bool m_bSelectedVehicle;
243  vtFence3d *m_pDraggingFence;
244  int m_iDraggingFencePoint;
245  DPoint3 m_EarthPosDown;
246  DPoint3 m_EarthPosLast;
247  vtRoute *m_pCurRoute;
248  vtUtilNode *m_pSelUtilNode;
249  vtRoute *m_pSelRoute;
250  IPoint2 m_MouseDown, m_MouseLast;
251  float m_StartRotation;
252  bool m_bConstrainAngles;
253 
254  Camera3kOptions m_camera3kOpt; // ADDED BY NcB holds camera3k options
255  std::vector <Camera3kb *> cam3kb; // ADDED BY NcB holds added camera instances to the enviro
256  int selectedcam; // ADDED BY NcB holds selected camera number
257  std::vector <models3d *> building_models;
258  int selectedmodel;
259  bool editCam;
260  //void* threadFunction(void *threadid);
261  void getViewer();
262 
263  pthread_t thid;
264 
265  // handle to the singleton
266  static Enviro *s_pEnviro;
267 
268  // The following can be overridden by GUI code, to handle situations
269  // in which the GUI may need to be informed of what happens.
270  virtual void ShowPopupMenu(const IPoint2 &pos) {}
271  virtual void SetTerrainToGUI(vtTerrain *pTerrain) {}
272  virtual void ShowLayerView() {}
273  virtual void RefreshLayerView() {}
274  virtual void UpdateLayerView() {}
275  virtual void CameraChanged() {}
276  virtual void EarthPosUpdated() {}
277  virtual void ShowDistance(const DPoint2 &p1, const DPoint2 &p2,
278  double fGround, double fVertical) {}
279  virtual void ShowDistance(const DLine2 &path,
280  double fGround, double fVertical) {}
281  virtual vtTagArray *GetInstanceFromGUI() { return NULL; }
282  virtual bool OnMouseEvent(vtMouseEvent &event) { return false; }
283  virtual void SetTimeEngineToGUI(vtTimeEngine *pEngine) {}
284  virtual bool IsAcceptable(vtTerrain *pTerr) { return true; }
285  virtual void OnCreateTerrain(vtTerrain *pTerr) {}
286  virtual void ShowMessage(const vtString &str) {}
287  virtual void SetState(AppState s) { m_state = s; }
288  virtual vtString GetStringFromUser(const vtString &title, const vtString &msg) = 0;
289  virtual void ShowProgress(bool bShow) {}
290  virtual void SetProgressTerrain(vtTerrain *pTerr) {}
291  virtual void UpdateProgress(const char *msg, int amount1, int amount2) {}
292  virtual void ExtendStructure(vtStructInstance *si) {}
293  virtual void AddVehicle(CarEngine *eng) {}
294  virtual void RemoveVehicle(CarEngine *eng) {}
295 
296  // temporary for demo use
297  void MakeDemoGlobe();
298  void ToggleDemo();
299  vtGroup *m_pDemoGroup;
300  vtGeode *m_pDemoTrails;
301  void CreateSomeTestVehicles(vtTerrain *pTerrain);
302  void MakeOverlayGlobe(vtImage *image, bool progress_callback(int) = NULL);
303 
304 
305 
306 protected:
307  // startup methods
308  void LoadTerrainDescriptions(const vtString &path);
309  void SetupScene1();
310  void SetupScene2();
311  virtual void SetupScene3() {}
312  // other implementation methods
313  void DoCursorOnEarth();
314  void DoCursorOnTerrain();
315  void MakeGlobe();
316  void SetupGlobe();
317  void LookUpTerrainLocations();
318  void SetupTerrain(vtTerrain *pTerr);
319  void CreateInstance();
320  void SetupArcMesh();
321  void FreeArc();
322  void FreeArcMesh();
323  void SetTerrainMeasure(const DPoint2 &g1, const DPoint2 &g2);
324  void SetTerrainMeasure(const DLine2 &path);
325  void CreateElevationLegend();
326  void CreateCompass();
327  void CreateMapOverview();
328  void StartFlyIn();
329  void FlyInStage1();
330  void FlyInStage2();
331  void SetWindowBox(const IPoint2 &ul, const IPoint2 &lr);
332 
333  // plants
334  vtSpeciesList3d *m_pPlantList;
335  PlantingOptions m_PlantOpt;
336  bool m_bPlantsLoaded;
337 
338  // vehicles
339  VehicleOptions m_VehicleOpt;
340 
341 
342  // fence members
343  bool m_bActiveFence, m_bFenceClosed;
344  vtFence3d *m_pCurFence;
345  vtLinearParams m_FenceParams;
346 
347  // line for making buildings and other uses
348  ElasticPolyline m_Elastic;
349 
350  // route members
351  bool m_bActiveRoute;
352  vtString m_sStructType;
353 
354  // linear arc on Earth (or Distance Tool on the Terrain)
355  vtGeode *m_pArc;
356  vtMaterialArray *m_pArcMats;
357  double m_fArcLength;
358  float m_fDistToolHeight;
359  bool m_bMeasurePath;
360  DLine2 m_distance_path;
361 
362  // view and navigation
363  vtCameraPtr m_pNormalCamera;
364  vtCameraPtr m_pTopDownCamera;
365  bool m_bTopDown;
366  vtEngine *m_pNavEngines;
367 
368  vtGroup *m_pRoot; // top of the scene graph
369 
370  // globe
371  vtTimeEngine *m_pGlobeTime;
372  vtGroup *m_pGlobeContainer;
373  vtIcoGlobe *m_pIcoGlobe;
374  vtIcoGlobe *m_pOverlayGlobe;
375  bool m_bEarthShade;
376  bool m_bGlobeFlat;
377  float m_fFlattening, m_fFlattenDir;
378  bool m_bGlobeUnfolded;
379  float m_fFolding, m_fFoldDir;
380  vtTrackball *m_pTrackball;
381  vtGeode *m_pSpaceAxes;
382  vtGeode *m_pEarthLines;
383  vtMesh *m_pLineMesh;
384 
385  // flattened globe view
386  FPQ m_SpaceLoc;
387  FPQ m_FlatLoc;
388 
389  float m_fMessageStart, m_fMessageTime;
390  ControlEngine *m_pControlEng;
391  vtHUD *m_pHUD;
392  vtTextMesh *m_pHUDMessage;
393  vtFontPtr m_pArial;
394 
395  int m_iInitStep; // initialization stage
396  vtTerrain *m_pTargetTerrain; // terrain we are switching to
397  bool m_bFlyIn;
398  int m_iFlightStage; // 1, 2
399  int m_iFlightStep; // 0-100
400  FPoint3 m_TrackStart[3], m_TrackPosDiff;
401  FPoint3 m_SpaceTrackballState[3];
402  vtAnimPath m_FlyInAnim;
403  DPoint2 m_FlyInCenter;
404  float m_fTransitionHeight; // in meters
405 
406  // HUD UI
407  vtMaterialArrayPtr m_pHUDMaterials;
408 
409  vtGeode *m_pLegendGeom;
410  bool m_bCreatedLegend;
411 
412  vtSpriteSizer *m_pCompassSizer;
413  vtGeode *m_pCompassGeom;
414  bool m_bCreatedCompass;
415  bool m_bDragCompass;
416  float m_fDragAngle;
417 
418  vtMesh *m_pWindowBoxMesh;
419 
420  // mapoverviewengine
421  MapOverviewEngine *m_pMapOverview;
422 };
423 
424 // global helper functions
425 vtTerrain *GetCurrentTerrain();
426 
427 #endif // ENVIROH
428