Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
PagedLodGrid.h
1 //
2 // PagedLodGrid.h
3 //
4 // Copyright (c) 2007-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef PAGEDLODGRIDH
9 #define PAGEDLODGRIDH
10 
11 #include "LodGrid.h"
12 
13 class vtStructure;
14 class vtStructure3d;
15 class vtStructureArray3d;
16 
17 /*
18  Implementation scene graph:
19  A
20  / \
21  B B
22  /|\ |\
23  C C C C C
24 
25  A = vtPagedStructureLodGrid, contans an array of cells consisting of:
26  B = vtPagedStructureLOD, which has any number of:
27  C = vtStructure/vtStructure3d, which produces a vtTransform when built.
28  */
30 
31 struct StructureRef {
32  vtStructureArray3d *pArray;
33  uint iIndex;
34 };
35 typedef std::vector<StructureRef> StructureRefVector;
36 
43 class vtPagedStructureLOD : public vtLOD
44 {
45 public:
47 
48  void SetRange(float range);
49  bool TestVisible(float fDistance, bool bLoad);
50 
51  void Add(vtStructureArray3d *pArray, int iIndex);
52  void Remove(vtStructureArray3d *pArray, int iIndex);
53  void SetGrid(vtPagedStructureLodGrid *g) { m_pGrid = g; }
54  void AppendToQueue();
55 
56  StructureRefVector m_StructureRefs;
57  int m_iNumConstructed;
58  bool m_bAddedToQueue;
59 
60  // Implement OSG's traversal with our own logic
61  virtual void traverse(osg::NodeVisitor& nv)
62  {
63  switch(nv.getTraversalMode())
64  {
65  case(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN):
66  std::for_each(_children.begin(),_children.end(),osg::NodeAcceptOp(nv));
67  break;
68  case(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN):
69  {
70  // 'Active' children are those within the given distance
71  float distance = nv.getDistanceToEyePoint(getCenter(),true);
72 
73  // _visitorType might be NODE_VISITOR (in cases such as
74  // intersection testing) or CULL_VISITOR (during rendering).
75  // We only want do visibility testing / page loading during
76  // rendering.
77 
78  // Test distance and contruct geometry if needed
79  if (TestVisible(distance,
80  nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR))
81  {
82  // Tell OSG to traverse all children
83  std::for_each(_children.begin(),_children.end(),osg::NodeAcceptOp(nv));
84  }
85  break;
86  }
87  default:
88  break;
89  }
90  }
91 
92 protected:
93  float m_fRange;
94  virtual ~vtPagedStructureLOD() {}
95 
96  // Pointer up to container
97  vtPagedStructureLodGrid *m_pGrid;
98 };
99 
100 struct QueueEntry {
101  vtPagedStructureLOD *pLOD;
102  vtStructureArray3d *pStructureArray;
103  uint iStructIndex;
104  float fDistance;
105 };
106 typedef std::vector<QueueEntry> QueueVector;
107 
118 {
119 public:
121  void Setup(const FPoint3 &origin, const FPoint3 &size,
122  int iDimension, float fLODDistance, vtHeightField3d *pHF = NULL);
123  void Cleanup();
124 
125  // methods
126  void SetDistance(float fLODDistance);
127  bool AppendToGrid(vtStructureArray3d *sa, int iIndex);
128  void RemoveFromGrid(vtStructureArray3d *sa, int iIndex);
129 
130  vtPagedStructureLOD *GetPagedCell(int a, int b);
131 
132  void DoPaging(const FPoint3 &CamPos, int iMaxStructures, float fDeleteDistance);
133  bool AddToQueue(vtPagedStructureLOD *pLOD, vtStructureArray3d *pArray, int iIndex);
134  bool RemoveFromQueue(vtStructureArray3d *pArray, int iIndex);
135  uint GetQueueSize() { return m_Queue.size(); }
136  void SortQueue();
137  void ClearQueue(vtStructureArray3d *pArray);
138  void RefreshPaging(vtStructureArray3d *pArray);
139 
140  void EnableLoading(bool b) { m_LoadingEnabled = b; }
141  bool m_LoadingEnabled;
142 
143  int GetLoadCount() { return m_iLoadCount; }
144  void ResetLoadCount() { m_iLoadCount = 0; }
145  int GetTotalConstructed() { return m_iTotalConstructed; }
146 
148  void ConstructByIndex(vtPagedStructureLOD *pLOD, vtStructureArray3d *pArray,
149  uint iStructIndex);
150 
151 protected:
152  void CullFarawayStructures(const FPoint3 &CamPos,
153  int iMaxStructures, float fDistance);
154  void DeconstructCell(vtPagedStructureLOD *pLOD);
155  void RemoveCellFromQueue(vtPagedStructureLOD *pLOD);
156 
157  vtPagedStructureLOD **m_pCells;
158  int m_iLoadCount, m_iTotalConstructed;
159 
160  osg::Group *FindCellParent(const FPoint3 &point);
161  vtPagedStructureLOD *FindPagedCellParent(const FPoint3 &point);
162  void AllocateCell(int a, int b);
163  osg::Group *GetCell(int a, int b);
164 
165  QueueVector m_Queue;
166 };
167 
168 #endif // PAGEDLODGRIDH
169