ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.23
Committed: Wed Oct 6 07:45:20 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.22: +10 -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.22 #include <SDL/SDL.h>
5     #include <SDL/SDL_image.h>
6 root 1.7 #include <cmath>
7 root 1.1 #include <vector>
8 root 1.22 #include <string>
9 root 1.1
10 root 1.18 #include <Cg/cg.h>
11     #include <Cg/cgGL.h>
12    
13 root 1.1 using namespace std;
14 root 1.18
15     extern CGcontext cgc;
16     extern CGprogram vsh;
17     extern CGprogram fsh;
18 root 1.19 extern CGparameter mv, mvp, lightpos;
19 root 1.21 extern CGprofile vsh_profile, fsh_profile;
20 root 1.1
21 root 1.7 #include <GL/gl.h>
22    
23 root 1.3 typedef int soffs; // 32 bit
24     typedef unsigned int uoffs;
25 root 1.4 #define OFFS_BITS 31
26 root 1.9 #define SOFFS_MIN (soffs)-(1 << (OFFS_BITS - 2))
27     #define SOFFS_MAX (soffs)+(1 << (OFFS_BITS - 2))
28     #define MAXEXTENT (1UL << (OFFS_BITS - 1))
29 root 1.1
30     #define GLFLOAT_MAX 1e30
31     #define GLFLOAT_MIN -1e30
32    
33     struct vec3 {
34     GLfloat x, y, z;
35     vec3 () { };
36     vec3 (GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) { };
37    
38     const vec3 operator -() const
39     { return vec3 (-x, -y, -z); }
40     };
41    
42     const vec3 normalize (const vec3 &v);
43     const vec3 cross (const vec3 &a, const vec3 &b);
44 root 1.6
45     inline const vec3 operator *(const vec3 &a, GLfloat s)
46     {
47     return vec3 (a.x * s, a.y * s, a.z * s);
48     }
49    
50     inline GLfloat dot (const vec3 &a, const vec3 &b)
51     {
52     return a.x * b.x + a.y * b.y + a.z * b.z;
53     }
54    
55     inline const GLfloat abs (const vec3 &v)
56     {
57     return sqrtf (dot (v, v));
58     }
59 root 1.1
60 root 1.16 struct matrix {
61 root 1.5 GLfloat data[4][4];
62    
63 root 1.12 const GLfloat operator ()(int i, int j) const { return data[j][i]; };
64     GLfloat &operator ()(int i, int j) { return data[j][i]; };
65 root 1.5
66     void diagonal (GLfloat v);
67     void clear () { diagonal (0.); };
68     void identity () { diagonal (1.); };
69    
70     void print (); // ugly
71    
72 root 1.16 static const matrix translation (const vec3 &v);
73     static const matrix rotation (GLfloat degrees, const vec3 &axis);
74 root 1.5
75 root 1.16 matrix () { };
76     matrix (GLfloat diag) { diagonal (diag); };
77 root 1.5 };
78    
79 root 1.16 const matrix operator *(const matrix &a, const matrix &b);
80     const vec3 operator *(const matrix &a, const vec3 &v);
81 root 1.10
82     typedef vec3 point;
83    
84     // a generic plane
85 root 1.12 struct plane {
86 root 1.10 vec3 n;
87     GLfloat d;
88    
89     GLfloat distance (const point &p) const
90     {
91     return dot (n, p) + d;
92     }
93    
94     plane () { };
95     plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d);
96     };
97    
98     struct sector {
99     soffs x, y, z;
100    
101     sector (soffs x = 0, soffs y = 0, soffs z = 0) : x(x), y(y), z(z) { };
102    
103     void offset (int subindex, uoffs extent)
104     {
105     if (subindex & 1) x += extent;
106     if (subindex & 2) y += extent;
107     if (subindex & 4) z += extent;
108     }
109     };
110    
111 root 1.23 inline const sector operator +(const sector &a, const sector &b)
112     {
113     return sector (a.x + b.x, a.y + b.y, a.z + b.z);
114     }
115    
116     inline const sector operator -(const sector &a, const sector &b)
117     {
118     return sector (a.x - b.x, a.y - b.y, a.z - b.z);
119     }
120    
121 root 1.10 inline const sector translate (const sector &p, const sector &src, const sector &dst)
122     {
123     sector r;
124    
125     r.x = p.x + (dst.x - src.x);
126     r.y = p.y + (dst.y - src.y);
127     r.z = p.z + (dst.z - src.z);
128    
129     return r;
130     }
131    
132     void renormalize (sector &s, point &p);
133    
134     struct colour {
135     GLfloat r, g, b, a;
136     colour (GLfloat r = 1., GLfloat g = 1., GLfloat b = 1., GLfloat a = 1.) : r(r), g(g), b(b), a(a) { };
137     };
138 root 1.5
139 root 1.1 struct texc {
140     GLfloat s, t;
141     texc () { };
142     texc (GLfloat s, GLfloat t) : s(s), t(t) { };
143     };
144    
145     struct box {
146 root 1.8 sector a, b;
147    
148     box() { };
149 root 1.1
150     void reset ()
151     {
152 root 1.9 a = sector (SOFFS_MAX, SOFFS_MAX, SOFFS_MAX);
153     b = sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN);
154 root 1.1 }
155    
156     void add (const box &o);
157 root 1.8 void add (const sector &p);
158 root 1.1 void add (const point &p);
159     };
160 root 1.5
161     inline const box translate (const box &b, const sector &src, const sector &dst)
162     {
163     box r;
164    
165     r.a = translate (b.a, src, dst);
166     r.b = translate (b.b, src, dst);
167    
168     return r;
169     }
170 root 1.1
171     struct light {
172     point p;
173     colour c;
174     GLfloat intensity;
175     GLfloat radius;
176 root 1.2 };
177    
178     struct material {
179     colour diffuse, specular, emission;
180     GLfloat shininess;
181 root 1.1 };
182    
183 root 1.4 struct entity_base;
184 root 1.20 struct view;
185 root 1.7
186     extern struct timer {
187     static double now;
188     static double diff;
189    
190     static void frame ();
191     timer ();
192     } timer;
193 root 1.11
194 root 1.13 /*
195 root 1.11 #define MAX_EVENT_TYPES 10
196     enum event_type { TIMER_EV };
197     struct event {
198     event_type type;
199     };
200    
201     typedef callback1<void, event&> event_cb;
202    
203     class skedjuhlar {
204    
205     public:
206     // only 10 types for now
207     private:
208     vector <list<event_cb> > event_lists;
209    
210     public:
211     skedjuhlar () {
212     event_lists.resize (MAX_EVENT_TYPES, list<event_cb>());
213     }
214    
215     void register_event_cb (const event_type &t, const event_cb &e) {
216     event_lists[t].push_back (e);
217     };
218     void send_event (event &e) {
219     list<event_cb> &l = event_lists[e.type];
220     for (list<event_cb>::iterator it = l.begin (); it != l.end (); it++) {
221     (*it)(e);
222     }
223     };
224     void check_events () {
225     while (!events.empty ()) {
226     event &e = events.pop_front ();
227     list<event_cb> &l = event_lists[e->name];
228     for (list<event_cb>::iterator it = l.begin (); it !+ l.end (); it++) {
229     (*it)(e);
230     }
231     delete e; // ugly slow? hai hai..... 183G
232     }
233     }
234     };
235    
236     extern skedjuhlar main_scheduler;
237 root 1.13 */
238 root 1.22
239     GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord);
240     inline int power_of_two (int input)
241     {
242     int value = 1;
243    
244     while (value < input)
245     {
246     value <<= 1;
247     }
248     return value;
249     }
250    
251    
252    
253     struct TextureExecption {
254     TextureExecption(string s) : msg(s) {}
255     string msg;
256     };
257     struct Texture {
258     SDL_Surface *image;
259     GLuint texture;
260     GLfloat texcoord[4];
261    
262     public:
263     void new_from_file (const char *f) {
264     image = IMG_Load(f);
265     if(!image)
266     throw (TextureExecption("Couldn't load " + (string) f + ": " + (string) IMG_GetError ()));
267     texture = SDL_GL_LoadTexture (image, (GLfloat*)&texcoord);
268     if (texture == 0)
269     throw (TextureExecption("Couldn't make GL Texture, failed to create new RGB SWSurface"));
270     }
271     };
272 root 1.1
273     #endif
274