ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.39
Committed: Mon Oct 11 00:05:48 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.38: +7 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H
2     #define UTIL_H
3    
4 root 1.7 #include <cmath>
5 root 1.27 #include <cfloat>
6 root 1.39 #include <cstdlib>
7 root 1.1 #include <vector>
8 root 1.22 #include <string>
9 root 1.1
10 root 1.37 #include "opengl.h"
11 root 1.18
12 root 1.37 #include <SDL/SDL_image.h>
13 root 1.30
14 root 1.1 using namespace std;
15 root 1.18
16     extern CGcontext cgc;
17     extern CGprogram vsh;
18     extern CGprogram fsh;
19 root 1.19 extern CGparameter mv, mvp, lightpos;
20 root 1.21 extern CGprofile vsh_profile, fsh_profile;
21 root 1.1
22 root 1.36 typedef long long soffs; // 32 bit
23     typedef unsigned long long uoffs;
24 root 1.39 #define OFFS_BITS 63
25 root 1.36 #define SOFFS_MIN (soffs)-(1LL << (OFFS_BITS - 2))
26     #define SOFFS_MAX (soffs)+(1LL << (OFFS_BITS - 2))
27     #define MAXEXTENT (1ULL << (OFFS_BITS - 1))
28 root 1.1
29 root 1.39 #define ABS(n) ((n) < 0 ? -(n) : (n))
30    
31 root 1.37 struct sector
32     {
33 root 1.32 soffs x, y, z;
34    
35     sector (soffs x, soffs y, soffs z) : x(x), y(y), z(z) { };
36     sector (soffs xyz = 0) : x(xyz), y(xyz), z(xyz) { };
37    
38     void offset (int subindex, uoffs extent)
39     {
40     if (subindex & 1) x += extent;
41     if (subindex & 2) y += extent;
42     if (subindex & 4) z += extent;
43     }
44     };
45    
46     inline sector operator +(const sector &a, const sector &b)
47     {
48     return sector (a.x + b.x, a.y + b.y, a.z + b.z);
49     }
50    
51     inline sector operator -(const sector &a, const sector &b)
52     {
53     return sector (a.x - b.x, a.y - b.y, a.z - b.z);
54     }
55    
56     inline sector operator /(const sector &a, soffs b)
57     {
58     return sector (a.x / b, a.y / b, a.z / b);
59     }
60    
61     inline bool operator <=(const sector &a, const sector &b)
62     {
63     return a.x <= b.x && a.y <= b.y && a.z <= b.z;
64     }
65    
66     inline soffs max (const sector &a)
67     {
68     return max (a.x, max (a.y, a.z));
69     }
70    
71     inline sector translate (const sector &p, const sector &src, const sector &dst)
72     {
73     return p + (dst - src);
74     }
75    
76     inline sector abs (const sector &s)
77     {
78 root 1.39 return sector (ABS (s.x), ABS (s.y), ABS (s.z));
79 root 1.32 }
80 root 1.1
81 root 1.37 struct vec3
82     {
83 root 1.1 GLfloat x, y, z;
84     vec3 () { };
85 root 1.32 vec3 (GLfloat s) : x(s), y(s), z(s) { };
86 root 1.1 vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
87 root 1.32 vec3 (const sector &s) : x(s.x), y(s.y), z(s.z) { };
88 root 1.1
89     const vec3 operator -() const
90     { return vec3 (-x, -y, -z); }
91     };
92    
93     const vec3 normalize (const vec3 &v);
94     const vec3 cross (const vec3 &a, const vec3 &b);
95 root 1.6
96     inline const vec3 operator *(const vec3 &a, GLfloat s)
97     {
98     return vec3 (a.x * s, a.y * s, a.z * s);
99     }
100    
101 root 1.27 inline const vec3 operator +(const vec3 &a, const vec3 &b)
102     {
103     return vec3 (a.x + b.x, a.y + b.y, a.z + b.z);
104     }
105    
106     inline const vec3 operator -(const vec3 &a, const vec3 &b)
107     {
108     return vec3 (a.x - b.x, a.y - b.y, a.z - b.z);
109     }
110    
111 root 1.6 inline GLfloat dot (const vec3 &a, const vec3 &b)
112     {
113     return a.x * b.x + a.y * b.y + a.z * b.z;
114     }
115    
116 root 1.39 inline const GLfloat norm (const vec3 &v)
117 root 1.6 {
118     return sqrtf (dot (v, v));
119     }
120 root 1.1
121 root 1.37 struct matrix
122     {
123 root 1.5 GLfloat data[4][4];
124    
125 root 1.12 const GLfloat operator ()(int i, int j) const { return data[j][i]; };
126     GLfloat &operator ()(int i, int j) { return data[j][i]; };
127 root 1.5
128 root 1.33 operator GLfloat *() { return &data[0][0]; }
129    
130 root 1.5 void diagonal (GLfloat v);
131 root 1.32 void clear () { diagonal (0.F); };
132     void identity () { diagonal (1.F); };
133 root 1.5
134     void print (); // ugly
135    
136 root 1.16 static const matrix translation (const vec3 &v);
137     static const matrix rotation (GLfloat degrees, const vec3 &axis);
138 root 1.5
139 root 1.16 matrix () { };
140     matrix (GLfloat diag) { diagonal (diag); };
141 root 1.5 };
142    
143 root 1.16 const matrix operator *(const matrix &a, const matrix &b);
144     const vec3 operator *(const matrix &a, const vec3 &v);
145 root 1.10
146     typedef vec3 point;
147    
148     // a generic plane
149 root 1.37 struct plane
150     {
151 root 1.10 vec3 n;
152     GLfloat d;
153    
154     GLfloat distance (const point &p) const
155     {
156     return dot (n, p) + d;
157     }
158    
159     plane () { };
160     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
161     };
162    
163     void renormalize (sector &s, point &p);
164    
165 root 1.37 struct colour
166     {
167 root 1.10 GLfloat r, g, b, a;
168     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
169     };
170 root 1.5
171 root 1.37 struct texc
172     {
173 root 1.1 GLfloat s, t;
174     texc () { };
175     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
176     };
177    
178 root 1.37 struct box
179     {
180 root 1.27 point a, b;
181 root 1.8
182     box() { };
183 root 1.1
184     void reset ()
185     {
186 root 1.27 a = point ( FLT_MAX, FLT_MAX, FLT_MAX);
187     b = point (-FLT_MAX, -FLT_MAX, -FLT_MAX);
188 root 1.1 }
189    
190     void add (const box &o);
191     void add (const point &p);
192     };
193 root 1.5
194 root 1.27 struct entity;
195     struct geometry;
196 root 1.20 struct view;
197 root 1.32 struct octant;
198 root 1.7
199 root 1.37 extern struct timer
200     {
201 root 1.7 static double now;
202     static double diff;
203    
204     static void frame ();
205     timer ();
206     } timer;
207 root 1.11
208 root 1.13 /*
209 root 1.11 #define MAX_EVENT_TYPES 10
210     enum event_type { TIMER_EV };
211 root 1.37 struct event
212     {
213 root 1.11 event_type type;
214     };
215    
216     typedef callback1<void, event&> event_cb;
217    
218 root 1.37 class skedjuhlar
219     {
220 root 1.11 public:
221     // only 10 types for now
222     private:
223     vector <list<event_cb> > event_lists;
224    
225     public:
226     skedjuhlar () {
227     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
228     }
229    
230     void register_event_cb (const event_type &t, const event_cb &e) {
231     event_lists[t].push_back (e);
232     };
233     void send_event (event &e) {
234     list<event_cb> &l = event_lists[e.type];
235     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
236     (*it)(e);
237     }
238     };
239     void check_events () {
240     while (!events.empty ()) {
241     event &e = events.pop_front ();
242     list<event_cb> &l = event_lists[e->name];
243     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
244     (*it)(e);
245     }
246     delete e; // ugly slow? hai hai..... 183G
247     }
248     }
249     };
250    
251     extern skedjuhlar main_scheduler;
252 root 1.13 */
253 root 1.22
254 root 1.37 namespace gl
255     {
256 root 1.39 void draw_bbox (const view &ctx, const sector &a, const sector &b);
257 root 1.34
258 root 1.35 extern int nesting;
259 root 1.34 void errchk (const char *name, const char *args, const char *file, int line);
260 root 1.31 }
261    
262 root 1.22 GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord);
263 root 1.27
264 root 1.1 #endif
265