ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.14
Committed: Tue Oct 5 02:42:12 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +9 -0 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.1 #include <vector>
6    
7     using namespace std;
8    
9 root 1.7 #include <GL/gl.h>
10    
11 root 1.14 #include <sh/sh.hpp>
12    
13     using namespace SH;
14    
15     extern ShColor3f color;
16     extern ShPoint3f lightPos;
17     extern ShMatrix4x4f mvp, mv;
18     extern ShProgram vsh, fsh;
19    
20 root 1.3 typedef int soffs; // 32 bit
21     typedef unsigned int uoffs;
22 root 1.4 #define OFFS_BITS 31
23 root 1.9 #define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 2))
24     #define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2))
25     #define MAXEXTENT (1UL << (OFFS_BITS - 1))
26 root 1.1
27     #define GLFLOAT_MAX 1e30
28     #define GLFLOAT_MIN -1e30
29    
30     struct vec3 {
31     GLfloat x, y, z;
32     vec3 () { };
33     vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
34    
35     const vec3 operator -() const
36     { return vec3 (-x, -y, -z); }
37     };
38    
39     const vec3 normalize (const vec3 &v);
40     const vec3 cross (const vec3 &a, const vec3 &b);
41 root 1.6
42     inline const vec3 operator *(const vec3 &a, GLfloat s)
43     {
44     return vec3 (a.x * s, a.y * s, a.z * s);
45     }
46    
47     inline GLfloat dot (const vec3 &a, const vec3 &b)
48     {
49     return a.x * b.x + a.y * b.y + a.z * b.z;
50     }
51    
52     inline const GLfloat abs (const vec3 &v)
53     {
54     return sqrtf (dot (v, v));
55     }
56 root 1.1
57 root 1.5 struct gl_matrix {
58     GLfloat data[4][4];
59    
60 root 1.12 const GLfloat operator ()(int i, int j) const { return data[j][i]; };
61     GLfloat &operator ()(int i, int j) { return data[j][i]; };
62 root 1.5
63     void diagonal (GLfloat v);
64     void clear () { diagonal (0.); };
65     void identity () { diagonal (1.); };
66    
67     void print (); // ugly
68    
69 root 1.12 static const gl_matrix translation (const vec3 &v);
70     static const gl_matrix rotation (GLfloat degrees, const vec3 &axis);
71 root 1.5
72     gl_matrix () { };
73     gl_matrix (GLfloat diag) { diagonal (diag); };
74     };
75    
76     const gl_matrix operator *(const gl_matrix &a, const gl_matrix &b);
77 root 1.6 const vec3 operator *(const gl_matrix &a, const vec3 &v);
78 root 1.10
79     typedef vec3 point;
80    
81     // a generic plane
82 root 1.12 struct plane {
83 root 1.10 vec3 n;
84     GLfloat d;
85    
86     GLfloat distance (const point &p) const
87     {
88     return dot (n, p) + d;
89     }
90    
91     plane () { };
92     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
93     };
94    
95     struct sector {
96     soffs x, y, z;
97    
98     sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { };
99    
100     void offset (int subindex, uoffs extent)
101     {
102     if (subindex & 1) x += extent;
103     if (subindex & 2) y += extent;
104     if (subindex & 4) z += extent;
105     }
106     };
107    
108     inline const sector translate (const sector &p, const sector &src, const sector &dst)
109     {
110     sector r;
111    
112     r.x = p.x + (dst.x - src.x);
113     r.y = p.y + (dst.y - src.y);
114     r.z = p.z + (dst.z - src.z);
115    
116     return r;
117     }
118    
119     void renormalize (sector &s, point &p);
120    
121     struct colour {
122     GLfloat r, g, b, a;
123     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
124     };
125 root 1.5
126 root 1.1 struct texc {
127     GLfloat s, t;
128     texc () { };
129     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
130     };
131    
132     struct box {
133 root 1.8 sector a, b;
134    
135     box() { };
136 root 1.1
137     void reset ()
138     {
139 root 1.9 a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX);
140     b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN);
141 root 1.1 }
142    
143     void add (const box &o);
144 root 1.8 void add (const sector &p);
145 root 1.1 void add (const point &p);
146     };
147 root 1.5
148     inline const box translate (const box &b, const sector &src, const sector &dst)
149     {
150     box r;
151    
152     r.a = translate (b.a, src, dst);
153     r.b = translate (b.b, src, dst);
154    
155     return r;
156     }
157 root 1.1
158     struct light {
159     point p;
160     colour c;
161     GLfloat intensity;
162     GLfloat radius;
163 root 1.2 };
164    
165     struct material {
166     colour diffuse, specular, emission;
167     GLfloat shininess;
168 root 1.1 };
169    
170 root 1.4 struct entity_base;
171     struct draw_context;
172 root 1.7
173     extern struct timer {
174     static double now;
175     static double diff;
176    
177     static void frame ();
178     timer ();
179     } timer;
180 root 1.11
181 root 1.13 /*
182 root 1.11 #define MAX_EVENT_TYPES 10
183     enum event_type { TIMER_EV };
184     struct event {
185     event_type type;
186     };
187    
188     typedef callback1<void, event&> event_cb;
189    
190     class skedjuhlar {
191    
192     public:
193     // only 10 types for now
194     private:
195     vector <list<event_cb> > event_lists;
196    
197     public:
198     skedjuhlar () {
199     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
200     }
201    
202     void register_event_cb (const event_type &t, const event_cb &e) {
203     event_lists[t].push_back (e);
204     };
205     void send_event (event &e) {
206     list<event_cb> &l = event_lists[e.type];
207     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
208     (*it)(e);
209     }
210     };
211     void check_events () {
212     while (!events.empty ()) {
213     event &e = events.pop_front ();
214     list<event_cb> &l = event_lists[e->name];
215     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
216     (*it)(e);
217     }
218     delete e; // ugly slow? hai hai..... 183G
219     }
220     }
221     };
222    
223     extern skedjuhlar main_scheduler;
224 root 1.13 */
225 root 1.1
226     #endif
227