source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h @ 3038

Revision 3038, 10.7 KB checked in by mattausch, 16 years ago (diff)

unified shader stuff, but phreetham sky not working anymore for forward rendering

Line 
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
20namespace CHCDemoEngine
21{
22
23struct Triangle3;
24class BvhNode;
25class BvhLeaf;
26class OcclusionQuery;
27class SceneEntity;
28
29class Shape;
30class Polygon3;
31class Vector3;
32class Geometry;
33class GPUProgramParameters;
34class Transform3;
35class Texture;
36class Material;
37class ShaderProgram;
38class FrameBufferObject;
39
40struct 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]
54const 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!!
62typedef unsigned int uint;
63#endif
64typedef unsigned short ushort;
65typedef unsigned char uchar;
66typedef 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
136typedef unsigned long dword;
137
138#ifndef NULL
139#define NULL (void *)0
140#endif // NULL
141
142typedef float Real;
143typedef 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(); } \
175while (0)
176#endif
177
178
179inline 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
189inline double Absd(const double a)
190{
191  return (a >= 0.0) ? a : -a;
192}
193
194inline 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
206template <class T>
207bool
208ClipValue(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
223inline int eq(Real a, Real b, Real t=TRASH)
224{
225  return Abs(a-b)<t;
226}
227
228inline int leq(Real a,Real b,Real t=TRASH)
229{
230  return a - b < t;
231}
232
233inline int geq(Real a,Real b,Real t=TRASH)
234{
235  return t > b - a;
236}
237
238inline int le(Real a,Real b,Real t=TRASH)
239{
240  return !geq(a,b,t);
241}
242
243inline 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// -------------------------------------------------------------------
254void 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// ---------------------------------------------------------
262inline 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
269inline Real sqr(Real a)
270{
271  return a*a;
272}
273
274
275template <class T>
276void Swap(T &a,T &b)
277{
278  T c;                 
279  c = b;
280  b = a;
281  a = c;
282}
283
284template <class T>
285int eq(T &a, T &b, T &c, T &d) {
286  return a == b && c==d && b==c;
287}
288
289template <class T>
290T Min(T a,T b)
291{
292  return a<b ? a : b;
293}
294
295template <class T>
296T Max(T a,T b)
297{
298  return a>b ? a : b;
299}
300
301
302Real Random(Real maxValue);
303int Random(int maxValue);
304void Randomize();
305void Randomize(unsigned int seed);
306
307
308void GetKey(char *s=NULL);
309
310inline Real Deg2Rad(const Real a)
311{
312   return a * (M_PI / 180.0f);
313}
314
315inline Real Rad2Deg(const Real a) {
316  return a * (180.0f/M_PI);
317}
318 
319inline 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*/
332class Limits
333{
334public:
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// ---------------------------------------------------------
355inline int
356EpsilonEqual(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// ---------------------------------------------------------
365inline int
366EpsilonEqual(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// ---------------------------------------------------------
375inline int
376EpsilonEqual(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// ---------------------------------------------------------
385template<class T>
386inline int
387InRange(T min, T max, T candidate)
388{
389  return (candidate >= min) && (candidate <= max);
390}
391
392// --------------------------------------------------------------
393// string function with new operator
394
395inline char*
396StrDup(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
405inline char *
406StrToLower(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)
414inline int
415GetLog2(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
435inline int
436GetCeilLog2(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
454char *GetAbsPath(char *path);
455
456
457std::string
458ReplaceSuffix(const std::string &filename, const std::string &a, const std::string &b);
459
460
461int
462SplitFilenames(const std::string &str, std::vector<std::string> &filenames);
463
464bool
465FileExists(char *filename);
466
467bool
468CreateDir(char *dir);
469
470char *
471GetPath(const char *s);
472
473char *
474StripPath(const char *s);
475
476
477
478///////////
479//-- typedefs
480
481typedef std::vector<BvhNode *> BvhNodeContainer;
482typedef std::vector<SceneEntity *> SceneEntityContainer;
483typedef std::vector<BvhLeaf *> BvhLeafContainer;
484typedef std::vector<Triangle3> TriangleContainer;
485typedef std::vector<OcclusionQuery *> QueryContainer;
486
487typedef std::queue<OcclusionQuery *> QueryQueue;
488typedef std::queue<BvhNode *> BvhNodeQueue;
489
490
491///////
492//-- container definitions
493
494typedef std::vector<Shape *> ShapeContainer;
495typedef std::vector<Geometry *> GeometryContainer;
496typedef std::vector<LODLevel *> LODLevelContainer;
497typedef std::vector<Polygon3 *> PolygonContainer;
498typedef std::vector<LODLevel *> LODContainer;
499typedef std::vector<Transform3 *> TransformContainer;
500typedef std::vector<Texture *> TextureContainer;
501typedef std::vector<Material *> MaterialContainer;
502typedef std::vector<GPUProgramParameters *> GPUParamContainer;
503typedef  std::vector<FrameBufferObject *> FBOContainer;
504typedef  std::vector<ShaderProgram *> ShaderContainer;
505
506typedef std::vector<Vector3> VertexArray;
507
508typedef std::pair<float, float> Texcoord2;
509
510
511
512// default model directory
513static std::string model_path("data/city/model/");
514// log output
515static 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
Note: See TracBrowser for help on using the repository browser.