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