Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Terrain.h
1 //
2 // Terrain.h
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef TERRAINH
9 #define TERRAINH
10 
11 #include "vtdata/vtTime.h"
12 #include "vtdata/FilePath.h"
13 
14 #include "AbstractLayer.h"
15 #include "AnimPath.h" // for vtAnimContainer
16 #include "Content3d.h"
17 #include "DynTerrain.h"
18 #include "GeomUtil.h" // for MeshFactory
19 #include "Location.h"
20 #include "Roads.h"
21 #include "Route.h"
22 #include "TextureUnitManager.h"
23 #include "TiledGeom.h"
24 #include "TParams.h"
25 #include "Trees.h" // for vtSpeciesList3d, vtPlantInstanceArray3d
26 #include "vtTin3d.h"
27 
28 #include <memory>
29 
30 // Try to reduce compile-time dependencies with these forward declarations
31 class vtDIB;
32 class vtElevationGrid;
33 class vtFeatureSet;
34 class vtFence3d;
35 class vtLodGrid;
37 class vtSimpleLodGrid;
39 
42 
43 
44 // Terrain Feature Types
45 enum TFType
46 {
47  TFT_TERRAINSURFACE,
48  TFT_HORIZON,
49  TFT_OCEAN,
50  TFT_VEGETATION,
51  TFT_STRUCTURES,
52  TFT_ROADS
53 };
54 
56 {
57 public:
58  virtual void OnCreate(vtTerrain *t, vtStructure *s) {}
59  virtual void OnDelete(vtTerrain *t, vtStructure *s) {}
60 };
61 
63 {
64  float fDarkness;
65  bool bShadowsEveryFrame;
66  bool bShadowLimit;
67  float fShadowRadius;
68 };
69 
71 typedef bool (*ProgFuncPtrType)(int);
72 
127 {
128 public:
129  vtTerrain();
130  virtual ~vtTerrain();
131 
132  /********************** Public Methods ******************/
133 
134  // parameters for terrain creation
135  void SetParamFile(const char *fname);
136  bool LoadParams();
137  vtString GetParamFile() { return m_strParamFile; }
138  void SetParams(const TParams &Params);
139  TParams &GetParams();
140 
141  // each terrain can have a long descriptive name
142  void SetName(const vtString &str) { m_Params.SetValueString(STR_NAME, str); }
143  vtString GetName() { return m_Params.GetValueString(STR_NAME); }
144 
145  // you can alternately give it a grid to use instead of loading a BT
146  void SetLocalGrid(vtElevationGrid *pGrid, bool bPreserve);
147  void SetTin(vtTin3d *pTin);
148  vtTin3d *GetTin() { return m_pTin; }
150 
152  void SetBothSides(bool bFlag) { m_bBothSides = bFlag; }
153 
154  // You can use these methods to build a terrain step by step,
155  // or simply use the method vtTerrainScene::BuildTerrain.
156  void CreateStep0();
157  bool CreateStep1();
158  bool CreateStep2(vtTransform *pSunLight, vtLightSource *pLightSource);
159  bool CreateStep3();
160  bool CreateStep4();
161  bool CreateStep5();
162  vtString GetLastError() { return m_strErrorMsg; }
163 
164  void SetProgressCallback(ProgFuncPtrType progress_callback = NULL);
165  ProgFuncPtrType m_progress_callback;
166  bool ProgressCallback(int i);
167 
169  void SetTextureColors(ColorMap *colors);
170  ColorMap *GetTextureColors() { return m_pTextureColors.get(); }
171 
173  void SetTextureContours(float fInterval, float fSize);
174 
177  virtual void PaintDib(bool progress_callback(int) = NULL);
178 
180  bool IsCreated();
181 
183  void Enable(bool bVisible);
184 
186  vtTransform *LoadModel(const char *filename, bool bAllowCache = true);
187 
189  void addNode(osg::Node *pNode);
190 
192  void removeNode(osg::Node *pNode);
193 
195  void PlantModel(vtTransform *model);
197  void PlantModelAtPoint(vtTransform *model, const DPoint2 &pos);
198 
200  bool PointIsInTerrain(const DPoint2 &p);
201 
202  // set global projection based on this terrain
203  void SetGlobalProjection();
204 
205  // fences
206  bool AddFence(vtFence3d *f);
207  void AddFencepoint(vtFence3d *f, const DPoint2 &epos);
208  void RedrawFence(vtFence3d *f);
209 
210  // Route
211  void AddRoute(vtRoute *f);
212  void add_routepoint_earth(vtRoute *f, const DPoint2 &epos, const char *structname);
213  void RedrawRoute(vtRoute *f);
214  void SaveRoute();
215  vtRoute* GetLastRoute() { return m_Routes.GetSize()>0?m_Routes[m_Routes.GetSize()-1]:0; }
216  vtRouteMap &GetRouteMap() { return m_Routes; }
217 
218  // Layers
220  LayerSet &GetLayers() { return m_Layers; }
221  void RemoveLayer(vtLayer *lay, bool progress_callback(int) = NULL);
222  vtLayer *LoadLayer(const char *fname);
223 
224  // plants
225  bool AddPlant(const DPoint2 &pos, int iSpecies, float fSize);
226  int DeleteSelectedPlants();
227  void SetPlantList(vtSpeciesList3d *pPlantList);
228  vtSpeciesList3d *GetPlantList() { return m_pPlantList; }
231  bool AddNodeToVegGrid(osg::Node *pNode);
232 
233  // structures
235  void SetStructureLayer(vtStructureLayer *slay);
237  vtStructureLayer *LoadStructuresFromXML(const vtString &strFilename);
238  void CreateStructures(vtStructureArray3d *structures);
239  bool CreateStructure(vtStructureArray3d *structures, int index);
241  void DeleteLayer(uint index);
242  bool FindClosestStructure(const DPoint2 &point, double epsilon,
243  int &structure, double &closest, float fMaxInstRadius,
244  float fLinearWidthBuffer);
245 
246  bool AddNodeToStructGrid(osg::Node *pNode);
247  void RemoveNodeFromStructGrid(osg::Node *pNode);
248 
249  vtLodGrid *GetStructureGrid() { return m_pStructGrid; }
250  int DoStructurePaging();
251  vtPagedStructureLodGrid *GetStructureLodGrid() { return m_pPagedStructGrid; }
252  float GetStructurePageOutDistance() { return m_fPagingStructureDist; }
253  void SetStructurePageOutDistance(float f);
254  int GetStructurePageMax() { return m_iPagingStructureMax; }
255 
256  void ExtendStructure(vtStructure *s);
257  void SetStructureExtension(vtStructureExtension *se = NULL) { m_pStructureExtension = se; }
258  vtStructureExtension *m_pStructureExtension;
259 
260  // abstract layers
261  void SetAbstractLayer(vtAbstractLayer *alay);
263  void RemoveFeatureGeometries(vtAbstractLayer *alay);
264  int DeleteSelectedFeatures();
265  void SetFeatureLoader(vtFeatureLoader *loader) { m_pFeatureLoader = loader; }
266 
270  vtTransform *GetScaledFeatures() { return m_pScaledFeatures; }
271 
272  // roads
273  vtRoadMap3d *GetRoadMap() { return m_pRoadMap.get(); }
274 
275  // Terrain-specific content
276  vtContentManager3d m_Content;
277 
278  // overridable by subclasses to extend culture
279  virtual void CreateCustomCulture();
280 
281  // manage engines specific to this terrain
282  void AddEngine(vtEngine *pE);
283  void ActivateEngines(bool bActive);
284  vtEngine *GetEngineGroup() { return m_pEngineGroup; }
285 
286  // reports world coordinates
287  FPoint3 GetCenter();
288  float GetRadius();
289 
290  // turn various features on/off
291  void SetFeatureVisible(TFType ftype, bool bOn);
292  bool GetFeatureVisible(TFType ftype);
293 
294  // control LOD
295  void SetLODDistance(TFType ftype, float fDistance);
296  float GetLODDistance(TFType ftype);
297  void SetVerticalExag(float fExag);
298  float GetVerticalExag() const { return m_fVerticalExag; }
299 
300  // query
301  vtDynTerrainGeom *GetDynTerrain() { return m_pDynGeom; }
302  const vtDynTerrainGeom *GetDynTerrain() const { return m_pDynGeom; }
303  vtTiledGeom *GetTiledGeom() { return m_pTiledGeom.get(); }
304  vtGroup *GetTopGroup() { return m_pContainerGroup; }
305  vtGroup *GetTerrainGroup() { return m_pTerrainGroup; }
308  vtProjection &GetProjection() { return m_proj; }
309  virtual bool FindAltitudeOnCulture(const FPoint3 &p3, float &fAltitude, bool bTrue, int iCultureFlags) const;
310  int GetShadowTextureUnit();
311 
312  // symbols and labels for abstract data
313  float AddSurfaceLineToMesh(vtGeomFactory *pMF, const DLine2 &line,
314  float fOffset, bool bInterp = true, bool bCurve = false, bool bTrue = false);
315 
316  // Access the viewpoint(s) associated with this terrain
317  void SetCamLocation(FMatrix4 &mat) { m_CamLocation = mat; }
318  FMatrix4 &GetCamLocation() { return m_CamLocation; }
319  vtLocationSaver *GetLocSaver() { return &m_LocSaver; }
320  void Visited(bool bVisited) { m_bVisited = bVisited; }
321  bool IsVisited() { return m_bVisited; }
322 
324  vtAnimContainer *GetAnimContainer() { return &m_AnimContainer; }
325 
326  // Ocean, Sky, Fog, Shadows
327  void SetWaterLevel(float fElev);
328  void SetFog(bool fog);
329  bool GetFog() { return m_bFog; }
330  void SetFogColor(const RGBf &color);
331  void SetFogDistance(float fMeters);
332  void SetBgColor(const RGBf &color);
333  RGBf GetBgColor() { return m_background_color; }
334  void SetShadows(bool shadows);
335  bool GetShadows() { return m_bShadows; }
336  void SetShadowOptions(const vtShadowOptions &opt);
337  void GetShadowOptions(vtShadowOptions &opt);
338  void ForceShadowUpdate();
339 
340  // Time
342  void TranslateToGMT(vtTime &time);
343  void TranslateFromGMT(vtTime &time);
344  DPoint2 GetCenterGeoLocation();
345 
346  // Overlay (2D image on HUD)
347  vtGroup *GetOverlay() { return m_pOverlay; }
348 
349  // Scenarios
350  void ActivateScenario(int iScenario);
351 
352  // Dynamic elevation
353  vtElevationGrid *GetInitialGrid() { return m_pElevGrid.get(); }
354  void UpdateElevation();
355  void RedrapeCulture(const DRECT &area);
356 
357  // Texture
358  void RecreateTextures(vtTransform *pSunLight, bool progress_callback(int) = NULL);
359  osg::Image *GetTextureImage();
360  vtMultiTexture *AddMultiTextureOverlay(vtImage *pImage, const DRECT &extents, int TextureMode);
361  osg::Node *GetTerrainSurfaceNode();
362 
363  /********************** Public Data ******************/
364 
365  // polygon containing geo corners of terrain area
366  DLine2 m_Corners_geo;
367 
368 protected:
369  /********************** Protected Methods ******************/
370 
371  // internal creation functions
372  bool CreateFromTIN();
373  bool CreateFromGrid();
374  bool CreateFromTiles();
375  bool CreateFromExternal();
376  void _CreateCulture();
377  void _CreateVegetation();
378  void _CreateStructures();
379  void _CreateRoads();
380  void _SetupVegGrid(float fLODDistance);
381  void _SetupStructGrid(float fLODDistance);
382  void _CreateAbstractLayers();
383  void _CreateImageLayers();
384  void _CreateTextures(const FPoint3 &light_dir, bool progress_callback(int) = NULL);
385  void _CreateDetailTexture();
386  bool _CreateDynamicTerrain();
387  void _CreateErrorMessage(DTErr error, vtElevationGrid *pGrid);
388  void _SetErrorMessage(const vtString &msg);
389  void CreateArtificialHorizon(float fAltitude, bool bWater, bool bHorizon,
390  bool bCenter);
391 
392  void _ApplyPreLight(vtHeightFieldGrid3d *pLocalGrid, vtBitmapBase *dib,
393  const FPoint3 &light_dir, bool progress_callback(int) = NULL);
394  void _ComputeCenterLocation();
395  void GetTerrainBounds();
396  void EnforcePageOut();
397  void ConnectFogShadow(bool bFog, bool bShadow);
398 
399  /********************** Protected Data ******************/
400 
401  // main scene graph outline
402  vtGroupPtr m_pContainerGroup;
403  vtGroupPtr m_pTerrainGroup;
404  vtGroupPtr m_pUnshadowedGroup;
405 
406  // dynamic terrain (CLOD)
407  vtDynTerrainGeomPtr m_pDynGeom;
408  vtTransformPtr m_pDynGeomScale;
409  float m_fVerticalExag;
410 
411  // triangulated irregular network (TIN)
412  vtTin3dPtr m_pTin;
413 
414  // tiled geometry
415  vtTiledGeomPtr m_pTiledGeom;
416 
417  osg::ref_ptr<vtExternalHeightField3d> m_pExternalHeightField;
418 
419  // construction parameters used to create this terrain
420  TParams m_Params;
421 
422  // elevation data
423  vtHeightField3d *m_pHeightField;
424  bool m_bPreserveInputGrid;
425 
426  // if we're switching between multiple terrains, we can remember where
427  // the camera was in each one
428  FMatrix4 m_CamLocation;
429  bool m_bVisited;
430 
431  // Associate a set of saved locations and animation paths with the terrain
432  vtLocationSaver m_LocSaver;
433  vtAnimContainer m_AnimContainer;
434 
435  // horizon, water, and fog
436  vtFogPtr m_pFog;
437  vtShadowPtr m_pShadow;
438  vtMovGeodePtr m_pHorizonGeom;
439  vtMovGeodePtr m_pOceanGeom;
440  bool m_bFog;
441  RGBf m_fog_color;
442  RGBf m_background_color;
443  bool m_bShadows;
444  vtTin3dPtr m_pWaterTin3d;
445 
446  // Layers
447  LayerSet m_Layers;
448 
449  // built structures, e.g. buildings and fences
450  vtStructureLayer *m_pActiveStructLayer;
451  vtLodGrid *m_pStructGrid;
452  vtPagedStructureLodGrid *m_pPagedStructGrid;
453  int m_iPagingStructureMax;
454  float m_fPagingStructureDist;
455 
456  vtMaterialArrayPtr m_pTerrMats; // materials for the LOD terrain
457  vtMaterialArrayPtr m_pDetailMats; // and detail texture
458  vtMaterialArrayPtr m_pEphemMats; // and ephemeris
459  int m_idx_water;
460  int m_idx_horizon;
461  bool m_bBothSides; // show both sides of terrain materials
462 
463  // abstract layers
464  vtAbstractLayer *m_pActiveAbstractLayer;
465  vtTransform *m_pScaledFeatures;
466  vtFeatureLoader *m_pFeatureLoader;
467 
468  // roads
469  vtGroup *m_pRoadGroup;
470  vtRoadMap3dPtr m_pRoadMap;
471 
472  // plants
474  vtSpeciesList3d *m_pPlantList;
475  vtGroup *m_pVegGroup;
476  vtSimpleLodGrid *m_pVegGrid;
477 
478  // routes
479  vtRouteMap m_Routes;
480 
481  // ground texture and shadows
482  ImagePtr m_pUnshadedImage;
483  ImagePtr m_pSingleImage;
484 
485  auto_ptr<ColorMap> m_pTextureColors;
486  bool m_bTextureInitialized;
487  vtTextureUnitManager m_TextureUnits;
488  int m_iShadowTextureUnit;
489  vtLightSource *m_pLightSource;
490 
491  FSphere m_bound_sphere; // bounding sphere of terrain
492  // (without surrounding ocean)
493  RGBf m_ocean_color;
494  vtString m_strParamFile;
495 
496  // contain the engines specific to this terrain
497  vtEnginePtr m_pEngineGroup;
498 
499  // only used during initialization
500  auto_ptr<vtElevationGrid> m_pElevGrid;
501 
502  // A useful value for computing "local time", the location of the
503  // center of the terrain in Geographic coords.
504  DPoint2 m_CenterGeoLocation;
505  int m_iDifferenceFromGMT;
506 
507  // hold an informative message in case anything goes wrong
508  vtString m_strErrorMsg;
509 
510  vtGroup *m_pOverlay;
511 
512  vtProjection m_proj;
513  bool m_bIsCreated;
514 };
515  // Group terrain
517 
518 #endif // TERRAINH