Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
TVTerrain.h
1 //
2 // TVTerrain class : Dynamically rendering terrain
3 //
4 // Original authors: Will Evans and Gregg Townsend of the University of Arizona
5 //
6 // Copyright (c) 2001-2011 Virtual Terrain Project
7 // Free for all uses, see license.txt for details.
8 //
9 
10 #ifndef TVTERRAINH
11 #define TVTERRAINH
12 
15 
16 #include <limits.h>
17 #include "DynTerrain.h"
18 
19 /*
20  * =========== Common Constants =============
21  */
22 #ifndef PI
23 #define PI 3.1415926535897932385
24 #endif
25 
26 /*
27  * =========== Common Macros =============
28  */
29 #ifndef ABS
30 #define ABS(x) (((x)<0)?(-(x)):(x))
31 #endif
32 
33 #ifndef ROUND
34 #define ROUND(a) ((int)((a)+0.5))
35 #endif
36 
37 /*
38  * ============ Common Structures ============
39  */
40 
41 typedef int Coord2d[2];
42 
43 /*
44  * ============== Heirarchy ===============
45  */
46 #define MAXERR USHRT_MAX /* maximum scaled triangle error */
47 
48 /* triangle info flag bits */
49 #define SMALLER1 1 /* Is neighbor 1 smaller? */
50 #define SMALLER2 2 /* Is neighbor 2 smaller? */
51 #define BIGGER3 4 /* Is neighbor 3 larger? */
52 #define SURFACE 8 /* on surface? */
53 #define ALLINFOV 16 /* completely in field of view? */
54 #define PARTINFOV 32 /* partially in field of view? */
55 #define ALLINROD 64 /* completely in rectangle of definition? */
56 #define PARTINROD 128 /* partially in rectangle of definition? */
57 
58 /* info field access macros */
59 #define BORDER(t) (m_info[(t)->s] & PARTINROD)
60 #define INROD(t) (m_info[(t)->s] & ALLINROD)
61 #define PUTONSURFACE(t) \
62  m_info[(t)->s] = (m_info[(t)->s]&(ALLINROD|PARTINROD))|SURFACE
63 #define ONSURFACE(t) (m_info[(t)->s] & SURFACE)
64 #define INFOV(t) (m_info[(t)->s] & (ALLINFOV | PARTINFOV))
65 #define SMALLN1(t) (m_info[(t)->s] & SMALLER1)
66 #define SMALLN2(t) (m_info[(t)->s] & SMALLER2)
67 #define BIGN3(t) (m_info[(t)->s] & BIGGER3)
68 
69 /* these macros also take TriIndex arguments */
70 #define ISLEAF(t) ((t)->depth >= m_depth)
71 #define IAMLEFTCHILD(t) ((((t)->depth&1)&&(((t)->orient^(t)->x)&1)) || \
72  ((((t)->depth&1)==0)&&(((t)->orient^(t)->x^(t)->y)&1)))
73 #define TRIEQUATE(a,b) (a)->x=(b)->x; \
74  (a)->y=(b)->y; \
75  (a)->depth=(b)->depth; \
76  (a)->orient=(b)->orient; \
77  (a)->s=(b)->s
78 
79 typedef struct { /* triangle index */
80  int x,y; /* coords of square containing tri at depth */
81  int depth; /* depth of triangle */
82  int orient; /* orientation */
83  int s; /* physical address */
84 } TriIndex;
85 
86 /* initial values */
87 #define IERRTHRESH 0.001
88 #define IERRTHRESHINC 0.00005
89 #define IDISTTHRESH 700.0
90 #define IDISTTHRESHINC 50.0
91 #define IVEXAGGER 1.0
92 #define IVEXAGGERINC 0.1
93 
94 
120 {
121 public:
122  TVTerrain();
123 
125  DTErr Init(const vtElevationGrid *pGrid, float fZScale);
126  static int MemoryRequired(int iDimension);
127 
128  // overrides
129  void DoRender();
130  void DoCulling(const vtCamera *pCam);
131  float GetElevation(int iX, int iZ, bool bTrue = false) const;
132  void GetWorldLocation(int iX, int iZ, FPoint3 &p, bool bTrue = false) const;
133  virtual float GetVerticalExag() const { return m_fZScale; }
134 
135 protected:
136  void mkscale(const vtElevationGrid *pGrid);
137  int calcErr(const vtElevationGrid *pGrid, Coord2d p1, Coord2d p2, Coord2d p3);
138  int inFOV(Coord2d p1, Coord2d p2, Coord2d p3);
139  int triInFOV(TriIndex *t);
140  int inROD(Coord2d p1, Coord2d p2, Coord2d p3);
141  void makeDFS(const vtElevationGrid *pGrid, TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
142  int init_ntriabove(int depth);
143  void getVerts(TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
144 
145  void parent(TriIndex *t, TriIndex *p);
146  void bro1(TriIndex *t, TriIndex *b1);
147  void bro2(TriIndex *t, TriIndex *b2);
148  void bro3(TriIndex *t, TriIndex *b3);
149  void nbr1(TriIndex *t, TriIndex *n1);
150  void nbr2(TriIndex *t, TriIndex *n2);
151  void nbr3(TriIndex *t, TriIndex *n3);
152  void kidsOnSurf(TriIndex *t);
153  void split(TriIndex *t);
154  void rodSplit(TriIndex *t);
155  void eyeSplit(TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
156  void baseSurface();
157 
158  int tooCoarse(TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
159  void errSplit(TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
160  void errSurface();
161  void buildSurface(FPoint3 &eyepos_ogl);
162  void emitSurface();
163  void emitDFS(TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
164  void emitTri(TriIndex *t, Coord2d p1, Coord2d p2, Coord2d p3);
165  void getNormal(int,int,double,int,int,double,int,int,double,double*,double*,double*);
166 
167 protected:
168  // cleanup
169  virtual ~TVTerrain();
170 
171 private:
172  float m_fXScale, m_fYScale, m_fZScale;
173 
174 /* heirarchy data */
175  int m_depth; /* maximum depth */
176  int m_size; /* number of triangles in hierarchy */
177  int m_numSurfTri; /* number of triangles on surface */
178  unsigned short *m_err; /* error of triangles */
179  uchar *m_info; /* surface,neighbor,infov of triangles */
180  int m_iLevels;
181 
182  double x_per_y, y_per_x, errPerEm;
183 
184 /* terrain data */
185  float *m_pVertex;
186 
187  double xmeters, ymeters; /* dimensions of data in meters */
188  double xscale, yscale; /* scales grid distance to meters */
189  double m2grid; /* scales vertical meters to grid units */
190  double err2grid; /* scales 0..MAXERR to grid units */
191  Coord2d se, sw, ne, nw;
192  TriIndex rootL, rootR; /* initial Triangles */
193 
194 /* Triangle err/distance from eye must be < errThresh */
195  double errThresh;
196 
197 /* Triangles < k*distThresh meters from eye must have size < 2^k */
198  double distThresh;
199 
200 /* error parameters, derived from errThresh (above) */
201  double etGrid, dtGrid;
202 
203 /* current viewpoint */
204  double eyeEmx, eyeEmy, eyeEmz; /* eye location */
205  int numDisplayTri; /* number of displayed triangles */
206 
207 // quick conversion from x,y index to output X,Y coordinates
208 // float *fXLookup, *fYLookup;
209 
210  Coord2d eyeP;
211 };
212  // Group dynterr
214 
215 #endif // TVTERRAINH
216