ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.11
Committed: Mon Oct 4 16:03:49 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +48 -0 lines
Log Message:
added some simple scheduler and events

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     const GLfloat operator ()(int i, int j) const { return data[i][j]; };
55     GLfloat &operator ()(int i, int j) { return data[i][j]; };
56    
57     void diagonal (GLfloat v);
58     void clear () { diagonal (0.); };
59     void identity () { diagonal (1.); };
60    
61     void print (); // ugly
62    
63     void translate (const vec3 &v);
64 root 1.6 void rotate (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     class plane {
77     vec3 n;
78     GLfloat d;
79     public:
80    
81     GLfloat distance (const point &p) const
82     {
83     return dot (n, p) + d;
84     }
85    
86     plane () { };
87     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
88     };
89    
90     struct sector {
91     soffs x, y, z;
92    
93     sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { };
94    
95     void offset (int subindex, uoffs extent)
96     {
97     if (subindex & 1) x += extent;
98     if (subindex & 2) y += extent;
99     if (subindex & 4) z += extent;
100     }
101     };
102    
103     inline const sector translate (const sector &p, const sector &src, const sector &dst)
104     {
105     sector r;
106    
107     r.x = p.x + (dst.x - src.x);
108     r.y = p.y + (dst.y - src.y);
109     r.z = p.z + (dst.z - src.z);
110    
111     return r;
112     }
113    
114     void renormalize (sector &s, point &p);
115    
116     struct colour {
117     GLfloat r, g, b, a;
118     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
119     };
120 root 1.5
121 root 1.1 struct texc {
122     GLfloat s, t;
123     texc () { };
124     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
125     };
126    
127     struct box {
128 root 1.8 sector a, b;
129    
130     box() { };
131 root 1.1
132     void reset ()
133     {
134 root 1.9 a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX);
135     b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN);
136 root 1.1 }
137    
138     void add (const box &o);
139 root 1.8 void add (const sector &p);
140 root 1.1 void add (const point &p);
141     };
142 root 1.5
143     inline const box translate (const box &b, const sector &src, const sector &dst)
144     {
145     box r;
146    
147     r.a = translate (b.a, src, dst);
148     r.b = translate (b.b, src, dst);
149    
150     return r;
151     }
152 root 1.1
153     struct light {
154     point p;
155     colour c;
156     GLfloat intensity;
157     GLfloat radius;
158 root 1.2 };
159    
160     struct material {
161     colour diffuse, specular, emission;
162     GLfloat shininess;
163 root 1.1 };
164    
165 root 1.4 struct entity_base;
166     struct draw_context;
167 root 1.7
168     extern struct timer {
169     static double now;
170     static double diff;
171    
172     static void frame ();
173     timer ();
174     } timer;
175 root 1.11
176     #define MAX_EVENT_TYPES 10
177     enum event_type { TIMER_EV };
178     struct event {
179     event_type type;
180     };
181    
182     typedef callback1<void, event&> event_cb;
183    
184     class skedjuhlar {
185    
186     public:
187     // only 10 types for now
188     private:
189     vector <list<event_cb> > event_lists;
190    
191     public:
192     skedjuhlar () {
193     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
194     }
195    
196     void register_event_cb (const event_type &t, const event_cb &e) {
197     event_lists[t].push_back (e);
198     };
199     void send_event (event &e) {
200     list<event_cb> &l = event_lists[e.type];
201     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
202     (*it)(e);
203     }
204     };
205     /*
206     void check_events () {
207     while (!events.empty ()) {
208     event &e = events.pop_front ();
209     list<event_cb> &l = event_lists[e->name];
210     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
211     (*it)(e);
212     }
213     delete e; // ugly slow? hai hai..... 183G
214     }
215     }
216     */
217     };
218    
219     extern skedjuhlar main_scheduler;
220 root 1.1
221     #endif
222