ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.h
Revision: 1.38
Committed: Sun Oct 10 19:50:37 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.37: +0 -38 lines
Log Message:
first material stuff

File Contents

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