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