1 | /** This file contains various macros, templates and constants for the ERS system.
|
---|
2 | @author Jiri Bittner
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifndef __COMMON_H
|
---|
6 | #define __COMMON_H
|
---|
7 |
|
---|
8 |
|
---|
9 | #include <math.h>
|
---|
10 | #include <stdlib.h>
|
---|
11 | #include <iostream>
|
---|
12 | #include <fstream>
|
---|
13 | #include <string>
|
---|
14 | #include <limits.h>
|
---|
15 | #include <vector>
|
---|
16 | #include <queue>
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 | namespace CHCDemoEngine
|
---|
21 | {
|
---|
22 |
|
---|
23 | struct Triangle3;
|
---|
24 | class BvhNode;
|
---|
25 | class BvhLeaf;
|
---|
26 | class OcclusionQuery;
|
---|
27 | class SceneEntity;
|
---|
28 |
|
---|
29 | class Shape;
|
---|
30 | class Polygon3;
|
---|
31 | class Vector3;
|
---|
32 | class Geometry;
|
---|
33 | class GPUProgramParameters;
|
---|
34 | class Transform3;
|
---|
35 | class Texture;
|
---|
36 | class Material;
|
---|
37 | class ShaderProgram;
|
---|
38 | class FrameBufferObject;
|
---|
39 |
|
---|
40 | struct LODLevel;
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | #if defined(_MSC_VER)
|
---|
45 | // use perftimer only on msvc
|
---|
46 | #define USE_PERFTIMER
|
---|
47 |
|
---|
48 | // define __SSE__ macro as it is not defined under MSVC
|
---|
49 | #define __SSE__
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | // This constant should be used for the length of the array char for filenames
|
---|
53 | // etc., for example: char filename[MaxStringLength]
|
---|
54 | const int MaxStringLength = 256;
|
---|
55 |
|
---|
56 | #if defined(_MSC_VER)
|
---|
57 | #pragma warning(disable:4018)
|
---|
58 | #pragma warning(disable:4800)
|
---|
59 | //#pragma warning(disable:4244)
|
---|
60 |
|
---|
61 | #if 0 // Note matt: comment this out because conflicts with definition in qt library!!
|
---|
62 | typedef unsigned int uint;
|
---|
63 | #endif
|
---|
64 | typedef unsigned short ushort;
|
---|
65 | typedef unsigned char uchar;
|
---|
66 | typedef unsigned long ulong;
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | #if defined(__GNUC__) || defined(_MSC_VER)
|
---|
70 | //#define DIRCAT '.'
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #if !defined(__WATCOMC__) && !defined(__CYGWIN32__) && !defined(_MSC_VER)
|
---|
74 | #include <values.h>
|
---|
75 | #else // __WATCOMC__
|
---|
76 | #define M_PI 3.14159265358979323846F
|
---|
77 | #define M_E 2.71828182845904523536F
|
---|
78 | #define MAXFLOAT 3.40282347e+37F
|
---|
79 | #endif // __WATCOMC__
|
---|
80 |
|
---|
81 | // some compilers do not define the bool yet, but it was declared by ANSI
|
---|
82 | #if !defined(__WATCOMC__) && !defined(_MSC_VER)
|
---|
83 | #if defined (__GNUC__) || (_BOOL)
|
---|
84 | //#error "HAS BOOL defined"
|
---|
85 | #define HAS_BOOL
|
---|
86 | #endif
|
---|
87 | #else // __WATCOMC__
|
---|
88 | #if (__WATCOMC__ > 1060)
|
---|
89 | //#error "Watcom HAS BOOL defined"
|
---|
90 | #define HAS_BOOL
|
---|
91 | #endif
|
---|
92 | #endif // __WATCOMC__
|
---|
93 |
|
---|
94 |
|
---|
95 | #if defined(__WATCOMC__) || defined(_MSC_VER)
|
---|
96 | #define strcasecmp stricmp
|
---|
97 | #define strncasecmp strnicmp
|
---|
98 | #endif // __WATCOMC__
|
---|
99 |
|
---|
100 | // matt
|
---|
101 |
|
---|
102 | #define USE_GZLIB 1
|
---|
103 |
|
---|
104 | #if USE_GZLIB
|
---|
105 |
|
---|
106 | #define OUT_BIN_MODE ios::out
|
---|
107 | #define IN_BIN_MODE ios::in
|
---|
108 |
|
---|
109 | #else
|
---|
110 |
|
---|
111 | #ifdef sgi
|
---|
112 | #define OUT_BIN_MODE ios::out
|
---|
113 | #define IN_BIN_MODE ios::in
|
---|
114 | #else // sgi
|
---|
115 | #if defined(__WATCOMC__) || defined(_MSC_VER)
|
---|
116 | #define OUT_BIN_MODE ios::out | ios::binary
|
---|
117 | #define IN_BIN_MODE ios::in | ios::binary
|
---|
118 | #else
|
---|
119 | #define OUT_BIN_MODE ios::out | ios::bin
|
---|
120 | #define IN_BIN_MODE ios::in | ios::bin
|
---|
121 | #endif // __WATCOMC_
|
---|
122 | #endif // sgi
|
---|
123 |
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | // #ifndef HAS_BOOL
|
---|
127 | // //enum bool {
|
---|
128 | // // false = 0,
|
---|
129 | // // true
|
---|
130 | // //};
|
---|
131 | // #define bool int
|
---|
132 | // #define false 0
|
---|
133 | // #define true 1
|
---|
134 | // #endif // HAS_BOOL
|
---|
135 |
|
---|
136 | typedef unsigned long dword;
|
---|
137 |
|
---|
138 | #ifndef NULL
|
---|
139 | #define NULL (void *)0
|
---|
140 | #endif // NULL
|
---|
141 |
|
---|
142 | typedef float Real;
|
---|
143 | typedef unsigned char byte;
|
---|
144 |
|
---|
145 |
|
---|
146 | #ifndef getch
|
---|
147 | #define getch() getchar()
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | #define TRASH 1.0e-5
|
---|
151 |
|
---|
152 | // delete a pointer and set to NULL
|
---|
153 | #ifndef DEL_PTR
|
---|
154 | #define DEL_PTR(ptr) do {if (ptr) { \
|
---|
155 | delete (ptr); \
|
---|
156 | (ptr) = 0;}} \
|
---|
157 | while (0)
|
---|
158 | #endif
|
---|
159 |
|
---|
160 |
|
---|
161 | // delete a pointer and set to NULL
|
---|
162 | #ifndef DEL_ARRAY_PTR
|
---|
163 | #define DEL_ARRAY_PTR(ptr) do {if (ptr) { \
|
---|
164 | delete [] (ptr); \
|
---|
165 | (ptr) = 0;}} \
|
---|
166 | while (0)
|
---|
167 | #endif
|
---|
168 |
|
---|
169 |
|
---|
170 | // Clears a container (i.e., a vector of pointers) and deletes the pointers
|
---|
171 | #ifndef CLEAR_CONTAINER
|
---|
172 | #define CLEAR_CONTAINER(co) do { for (size_t _i = 0; _i < (co).size(); ++ _i) { \
|
---|
173 | DEL_PTR((co)[_i]);} \
|
---|
174 | (co).clear(); } \
|
---|
175 | while (0)
|
---|
176 | #endif
|
---|
177 |
|
---|
178 |
|
---|
179 | inline int signum(const Real a, const Real thresh = TRASH)
|
---|
180 | {
|
---|
181 | if (a>thresh)
|
---|
182 | return 1;
|
---|
183 | else
|
---|
184 | if (a<-thresh)
|
---|
185 | return -1;
|
---|
186 | return 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | inline double Absd(const double a)
|
---|
190 | {
|
---|
191 | return (a >= 0.0) ? a : -a;
|
---|
192 | }
|
---|
193 |
|
---|
194 | inline float Abs(const float a)
|
---|
195 | {
|
---|
196 | return (a >= 0.0f) ? a : -a;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // =======================================================
|
---|
200 | // Comparing things
|
---|
201 | //struct Limits {
|
---|
202 | // const Real thresh=TRASH;
|
---|
203 | // const Real small=0.1;
|
---|
204 | //};
|
---|
205 |
|
---|
206 | template <class T>
|
---|
207 | bool
|
---|
208 | ClipValue(T &v, const T m, const T M)
|
---|
209 | {
|
---|
210 | if (v<m) {
|
---|
211 | v = m;
|
---|
212 | return true;
|
---|
213 | }
|
---|
214 | if (v>M) {
|
---|
215 | v = M;
|
---|
216 | return true;
|
---|
217 | }
|
---|
218 |
|
---|
219 | return false;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | inline int eq(Real a, Real b, Real t=TRASH)
|
---|
224 | {
|
---|
225 | return Abs(a-b)<t;
|
---|
226 | }
|
---|
227 |
|
---|
228 | inline int leq(Real a,Real b,Real t=TRASH)
|
---|
229 | {
|
---|
230 | return a - b < t;
|
---|
231 | }
|
---|
232 |
|
---|
233 | inline int geq(Real a,Real b,Real t=TRASH)
|
---|
234 | {
|
---|
235 | return t > b - a;
|
---|
236 | }
|
---|
237 |
|
---|
238 | inline int le(Real a,Real b,Real t=TRASH)
|
---|
239 | {
|
---|
240 | return !geq(a,b,t);
|
---|
241 | }
|
---|
242 |
|
---|
243 | inline int ge(Real a,Real b,Real t=TRASH)
|
---|
244 | {
|
---|
245 | return !leq(a,b,t);
|
---|
246 | }
|
---|
247 |
|
---|
248 | // ========================================================
|
---|
249 |
|
---|
250 | // -------------------------------------------------------------------
|
---|
251 | // Indents to a given stream by the number of spaces specified.
|
---|
252 | // This routine is located in main.cpp, for lack of a better place.
|
---|
253 | // -------------------------------------------------------------------
|
---|
254 | void indent(std::ostream &app, int ind);
|
---|
255 |
|
---|
256 | // ---------------------------------------------------------
|
---|
257 | // RandomValue
|
---|
258 | // Returns a random Realing-point value between the two
|
---|
259 | // values. Range is inclusive; the function should
|
---|
260 | // occasionally return exactly a or b.
|
---|
261 | // ---------------------------------------------------------
|
---|
262 | inline Real RandomValue(Real a, Real b)
|
---|
263 | {
|
---|
264 | Real range = (Real) Abs(a - b);
|
---|
265 | return ((Real)rand() / RAND_MAX) * range + ((a < b) ? a : b);
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | inline Real sqr(Real a)
|
---|
270 | {
|
---|
271 | return a*a;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | template <class T>
|
---|
276 | void Swap(T &a,T &b)
|
---|
277 | {
|
---|
278 | T c;
|
---|
279 | c = b;
|
---|
280 | b = a;
|
---|
281 | a = c;
|
---|
282 | }
|
---|
283 |
|
---|
284 | template <class T>
|
---|
285 | int eq(T &a, T &b, T &c, T &d) {
|
---|
286 | return a == b && c==d && b==c;
|
---|
287 | }
|
---|
288 |
|
---|
289 | template <class T>
|
---|
290 | T Min(T a,T b)
|
---|
291 | {
|
---|
292 | return a<b ? a : b;
|
---|
293 | }
|
---|
294 |
|
---|
295 | template <class T>
|
---|
296 | T Max(T a,T b)
|
---|
297 | {
|
---|
298 | return a>b ? a : b;
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | Real Random(Real maxValue);
|
---|
303 | int Random(int maxValue);
|
---|
304 | void Randomize();
|
---|
305 | void Randomize(unsigned int seed);
|
---|
306 |
|
---|
307 |
|
---|
308 | void GetKey(char *s=NULL);
|
---|
309 |
|
---|
310 | inline Real Deg2Rad(const Real a)
|
---|
311 | {
|
---|
312 | return a * (M_PI / 180.0f);
|
---|
313 | }
|
---|
314 |
|
---|
315 | inline Real Rad2Deg(const Real a) {
|
---|
316 | return a * (180.0f/M_PI);
|
---|
317 | }
|
---|
318 |
|
---|
319 | inline Real Clamp(float x, float minVal, float maxVal)
|
---|
320 | {
|
---|
321 | if (x < minVal) return minVal;
|
---|
322 | if (x > maxVal) return maxVal;
|
---|
323 |
|
---|
324 | return x;
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | /** Limits.
|
---|
329 | This class encapsulates all the concessions to Realing-point
|
---|
330 | error made by ray tracers.
|
---|
331 | */
|
---|
332 | class Limits
|
---|
333 | {
|
---|
334 | public:
|
---|
335 | // This is the number used to reject too-close intersections.
|
---|
336 | // The default value is 1.0.
|
---|
337 | static Real Threshold;
|
---|
338 |
|
---|
339 | // This is a "small" number. Less than this number is assumed to
|
---|
340 | // be 0, when such things matter.
|
---|
341 | // The default value is 0.1.
|
---|
342 | static Real Small;
|
---|
343 |
|
---|
344 | // This is an impractically "large" number, used for intersection
|
---|
345 | // parameters out to infinity (e.g. the span resulting from an
|
---|
346 | // FindAllIntersections operation on a plane).
|
---|
347 | // The default value is 100000.
|
---|
348 | static Real Infinity;
|
---|
349 | };
|
---|
350 |
|
---|
351 | // ---------------------------------------------------------
|
---|
352 | // EpsilonEqual(x,y)
|
---|
353 | // Returns if two values are equal or not (by epsilon)
|
---|
354 | // ---------------------------------------------------------
|
---|
355 | inline int
|
---|
356 | EpsilonEqual(const Real &x, const Real &y)
|
---|
357 | {
|
---|
358 | return fabs(x-y) < Limits::Small;
|
---|
359 | }
|
---|
360 |
|
---|
361 | // ---------------------------------------------------------
|
---|
362 | // EpsilonEqual(x)
|
---|
363 | // Returns if a value is zero (+/- epsilon)
|
---|
364 | // ---------------------------------------------------------
|
---|
365 | inline int
|
---|
366 | EpsilonEqual(const Real &x)
|
---|
367 | {
|
---|
368 | return fabs(x) < Limits::Small;
|
---|
369 | }
|
---|
370 |
|
---|
371 | // ---------------------------------------------------------
|
---|
372 | // EpsilonEqual(x,y,epsilon)
|
---|
373 | // Returns if two values are equal or not by a given epsilon
|
---|
374 | // ---------------------------------------------------------
|
---|
375 | inline int
|
---|
376 | EpsilonEqual(const Real &x, const Real &y, const Real &epsilon)
|
---|
377 | {
|
---|
378 | return fabs(x-y) < epsilon;
|
---|
379 | }
|
---|
380 |
|
---|
381 | // ---------------------------------------------------------
|
---|
382 | // InRange
|
---|
383 | // Returns nonzero if min <= candidate <= max.
|
---|
384 | // ---------------------------------------------------------
|
---|
385 | template<class T>
|
---|
386 | inline int
|
---|
387 | InRange(T min, T max, T candidate)
|
---|
388 | {
|
---|
389 | return (candidate >= min) && (candidate <= max);
|
---|
390 | }
|
---|
391 |
|
---|
392 | // --------------------------------------------------------------
|
---|
393 | // string function with new operator
|
---|
394 |
|
---|
395 | inline char*
|
---|
396 | StrDup(char *src) {
|
---|
397 | char *p;
|
---|
398 | for (p = src; *p++; );
|
---|
399 | char *dest = new char[p-src];
|
---|
400 | for ( p = dest;(*p++ = *src++) != 0; );
|
---|
401 | return dest;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | inline char *
|
---|
406 | StrToLower(char *src) {
|
---|
407 | char *p;
|
---|
408 | for (p = src; *p; p++)
|
---|
409 | *p = tolower(*p);
|
---|
410 | return src;
|
---|
411 | }
|
---|
412 |
|
---|
413 | // return l = log2(a) if a is a power of two else -ceillog2(a)
|
---|
414 | inline int
|
---|
415 | GetLog2(int a)
|
---|
416 | {
|
---|
417 | int i, x;
|
---|
418 | i = 0;
|
---|
419 | x = 1;
|
---|
420 |
|
---|
421 | while (x < a) {
|
---|
422 | i++;
|
---|
423 | x <<= 1;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return (x==a) ? i: -i;
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | // return ceil(log2(a)) even if a is not a power of two
|
---|
431 | // Example:
|
---|
432 | // GetCeilLog2(15) = 4
|
---|
433 | // GetCeilLog2(16) = 4
|
---|
434 | // GetCeilLog2(17) = 5
|
---|
435 | inline int
|
---|
436 | GetCeilLog2(int a)
|
---|
437 | {
|
---|
438 | int i, x;
|
---|
439 | if (a < 0)
|
---|
440 | return -1;
|
---|
441 |
|
---|
442 | i = 0;
|
---|
443 | x = 1;
|
---|
444 |
|
---|
445 | while (x < a) {
|
---|
446 | i++;
|
---|
447 | x <<= 1;
|
---|
448 | }
|
---|
449 |
|
---|
450 | return i;
|
---|
451 | }
|
---|
452 |
|
---|
453 |
|
---|
454 | char *GetAbsPath(char *path);
|
---|
455 |
|
---|
456 |
|
---|
457 | std::string
|
---|
458 | ReplaceSuffix(const std::string &filename, const std::string &a, const std::string &b);
|
---|
459 |
|
---|
460 |
|
---|
461 | int
|
---|
462 | SplitFilenames(const std::string &str, std::vector<std::string> &filenames);
|
---|
463 |
|
---|
464 | bool
|
---|
465 | FileExists(char *filename);
|
---|
466 |
|
---|
467 | bool
|
---|
468 | CreateDir(char *dir);
|
---|
469 |
|
---|
470 | char *
|
---|
471 | GetPath(const char *s);
|
---|
472 |
|
---|
473 | char *
|
---|
474 | StripPath(const char *s);
|
---|
475 |
|
---|
476 |
|
---|
477 |
|
---|
478 | ///////////
|
---|
479 | //-- typedefs
|
---|
480 |
|
---|
481 | typedef std::vector<BvhNode *> BvhNodeContainer;
|
---|
482 | typedef std::vector<SceneEntity *> SceneEntityContainer;
|
---|
483 | typedef std::vector<BvhLeaf *> BvhLeafContainer;
|
---|
484 | typedef std::vector<Triangle3> TriangleContainer;
|
---|
485 | typedef std::vector<OcclusionQuery *> QueryContainer;
|
---|
486 |
|
---|
487 | typedef std::queue<OcclusionQuery *> QueryQueue;
|
---|
488 | typedef std::queue<BvhNode *> BvhNodeQueue;
|
---|
489 |
|
---|
490 |
|
---|
491 | ///////
|
---|
492 | //-- container definitions
|
---|
493 |
|
---|
494 | typedef std::vector<Shape *> ShapeContainer;
|
---|
495 | typedef std::vector<Geometry *> GeometryContainer;
|
---|
496 | typedef std::vector<LODLevel *> LODLevelContainer;
|
---|
497 | typedef std::vector<Polygon3 *> PolygonContainer;
|
---|
498 | typedef std::vector<LODLevel *> LODContainer;
|
---|
499 | typedef std::vector<Transform3 *> TransformContainer;
|
---|
500 | typedef std::vector<Texture *> TextureContainer;
|
---|
501 | typedef std::vector<Material *> MaterialContainer;
|
---|
502 | typedef std::vector<GPUProgramParameters *> GPUParamContainer;
|
---|
503 | typedef std::vector<FrameBufferObject *> FBOContainer;
|
---|
504 | typedef std::vector<ShaderProgram *> ShaderContainer;
|
---|
505 |
|
---|
506 | typedef std::vector<Vector3> VertexArray;
|
---|
507 |
|
---|
508 | typedef std::pair<float, float> Texcoord2;
|
---|
509 |
|
---|
510 |
|
---|
511 |
|
---|
512 | // default model directory
|
---|
513 | static std::string model_path("data/city/model/");
|
---|
514 | // log output
|
---|
515 | static std::ofstream Debug("debug.log");
|
---|
516 |
|
---|
517 |
|
---|
518 | #define INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES 1000
|
---|
519 |
|
---|
520 | }
|
---|
521 |
|
---|
522 |
|
---|
523 |
|
---|
524 | #endif
|
---|
525 |
|
---|
526 |
|
---|
527 |
|
---|
528 |
|
---|
529 |
|
---|
530 |
|
---|
531 |
|
---|
532 |
|
---|