ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.C
Revision: 1.18
Committed: Wed Oct 6 19:45:29 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +0 -53 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * math support
3 * most of the more complicated code is taken from mesa.
4 */
5
6 #include <cstdio> // ugly
7 #include <cmath>
8
9 #include <sys/time.h>
10 #include <time.h>
11 #include <GL/gl.h>
12
13 #include "util.h"
14 #include "entity.h"
15
16 #define DEG2RAD (M_PI / 180.)
17
18 void renormalize (sector &s, point &p)
19 {
20 float i;
21
22 p.x = modff (p.x, &i); s.x += (soffs)i;
23 p.y = modff (p.y, &i); s.y += (soffs)i;
24 p.z = modff (p.z, &i); s.z += (soffs)i;
25 }
26
27 /////////////////////////////////////////////////////////////////////////////
28
29 const vec3 normalize (const vec3 &v)
30 {
31 GLfloat s = abs (v);
32
33 if (!s)
34 return v;
35
36 s = 1. / s;
37 return vec3 (v.x * s, v.y * s, v.z * s);
38 }
39
40 const vec3 cross (const vec3 &a, const vec3 &b)
41 {
42 return vec3 (
43 a.y * b.z - a.z * b.y,
44 a.z * b.x - a.x * b.z,
45 a.x * b.y - a.y * b.x
46 );
47 }
48
49 /////////////////////////////////////////////////////////////////////////////
50
51 void matrix::diagonal (GLfloat v)
52 {
53 for (int i = 4; i--; )
54 for (int j = 4; j--; )
55 data[i][j] = i == j ? v : 0.;
56 }
57
58 const matrix operator *(const matrix &a, const matrix &b)
59 {
60 matrix r;
61
62 // taken from mesa
63 for (int i = 0; i < 4; i++)
64 {
65 const GLfloat ai0=a(i,0), ai1=a(i,1), ai2=a(i,2), ai3=a(i,3);
66
67 r(i,0) = ai0 * b(0,0) + ai1 * b(1,0) + ai2 * b(2,0) + ai3 * b(3,0);
68 r(i,1) = ai0 * b(0,1) + ai1 * b(1,1) + ai2 * b(2,1) + ai3 * b(3,1);
69 r(i,2) = ai0 * b(0,2) + ai1 * b(1,2) + ai2 * b(2,2) + ai3 * b(3,2);
70 r(i,3) = ai0 * b(0,3) + ai1 * b(1,3) + ai2 * b(2,3) + ai3 * b(3,3);
71 }
72
73 return r;
74 }
75
76 const matrix matrix::rotation (GLfloat angle, const vec3 &axis)
77 {
78 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
79
80 s = (GLfloat) sinf (angle * DEG2RAD);
81 c = (GLfloat) cosf (angle * DEG2RAD);
82
83 const GLfloat mag = abs (axis);
84
85 if (mag <= 1.0e-4)
86 return matrix (1);
87
88 matrix m;
89 const vec3 n = axis * (1. / mag);
90
91 xx = n.x * n.x;
92 yy = n.y * n.y;
93 zz = n.z * n.z;
94 xy = n.x * n.y;
95 yz = n.y * n.z;
96 zx = n.z * n.x;
97 xs = n.x * s;
98 ys = n.y * s;
99 zs = n.z * s;
100 one_c = 1.0F - c;
101
102 m(0,0) = (one_c * xx) + c;
103 m(0,1) = (one_c * xy) - zs;
104 m(0,2) = (one_c * zx) + ys;
105 m(0,3) = 0;
106
107 m(1,0) = (one_c * xy) + zs;
108 m(1,1) = (one_c * yy) + c;
109 m(1,2) = (one_c * yz) - xs;
110 m(1,3) = 0;
111
112 m(2,0) = (one_c * zx) - ys;
113 m(2,1) = (one_c * yz) + xs;
114 m(2,2) = (one_c * zz) + c;
115 m(2,3) = 0;
116
117 m(3,0) = 0;
118 m(3,1) = 0;
119 m(3,2) = 0;
120 m(3,3) = 1;
121
122 return m;
123 }
124
125 const vec3 operator *(const matrix &a, const vec3 &v)
126 {
127 return vec3 (
128 a(0,0) * v.x + a(0,1) * v.y + a(0,2) * v.z + a(0,3),
129 a(1,0) * v.x + a(1,1) * v.y + a(1,2) * v.z + a(1,3),
130 a(2,0) * v.x + a(2,1) * v.y + a(2,2) * v.z + a(2,3)
131 );
132 }
133
134 void matrix::print ()
135 {
136 printf ("\n");
137 printf ("[ %f, %f, %f, %f ]\n", data[0][0], data[1][0], data[2][0], data[3][0]);
138 printf ("[ %f, %f, %f, %f ]\n", data[0][1], data[1][1], data[2][1], data[3][1]);
139 printf ("[ %f, %f, %f, %f ]\n", data[0][2], data[1][2], data[2][2], data[3][2]);
140 printf ("[ %f, %f, %f, %f ]\n", data[0][3], data[1][3], data[2][3], data[3][3]);
141 }
142
143 const matrix matrix::translation (const vec3 &v)
144 {
145 matrix m(1);
146
147 m(0,3) = v.x;
148 m(1,3) = v.y;
149 m(2,3) = v.z;
150
151 return m;
152 }
153
154 /////////////////////////////////////////////////////////////////////////////
155
156 plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d)
157 : n (vec3 (a,b,c))
158 {
159 GLfloat s = 1. / abs (n);
160
161 n = n * s;
162 this->d = d * s;
163 }
164
165 /////////////////////////////////////////////////////////////////////////////
166
167 void box::add (const box &o)
168 {
169 a.x = min (a.x, o.a.x);
170 a.y = min (a.y, o.a.y);
171 a.z = min (a.z, o.a.z);
172 b.x = max (b.x, o.b.x);
173 b.y = max (b.y, o.b.y);
174 b.z = max (b.z, o.b.z);
175 }
176
177 void box::add (const sector &p)
178 {
179 a.x = min (a.x, p.x);
180 a.y = min (a.y, p.y);
181 a.z = min (a.z, p.z);
182 b.x = max (b.x, p.x);
183 b.y = max (b.y, p.y);
184 b.z = max (b.z, p.z);
185 }
186
187 void box::add (const point &p)
188 {
189 a.x = min (a.x, (soffs)floorf (p.x));
190 a.y = min (a.y, (soffs)floorf (p.y));
191 a.z = min (a.z, (soffs)floorf (p.z));
192 b.x = max (b.x, (soffs)ceilf (p.x));
193 b.y = max (b.y, (soffs)ceilf (p.y));
194 b.z = max (b.z, (soffs)ceilf (p.z));
195 }
196
197 /////////////////////////////////////////////////////////////////////////////
198
199 struct timer timer;
200 static double base;
201 double timer::now = 0.;
202 double timer::diff;
203
204 void timer::frame ()
205 {
206 struct timeval tv;
207 gettimeofday (&tv, 0);
208
209 double next = tv.tv_sec - base + tv.tv_usec / 1.e6;
210
211 diff = next - now;
212 now = next;
213 }
214
215 timer::timer ()
216 {
217 struct timeval tv;
218 gettimeofday (&tv, 0);
219 base = tv.tv_sec + tv.tv_usec / 1.e6;
220 }
221
222 GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
223 {
224 GLuint texture;
225 int w, h;
226 SDL_Surface *image;
227 SDL_Rect area;
228 Uint32 saved_flags;
229 Uint8 saved_alpha;
230
231 /* Use the surface width and height expanded to powers of 2 */
232 //w = power_of_two (surface->w);
233 //h = power_of_two (surface->h);
234 w = power_of_two (surface->w);
235 h = power_of_two (surface->h);
236 texcoord[0] = 0.0f; /* Min X */
237 texcoord[1] = 0.0f; /* Min Y */
238 texcoord[2] = (GLfloat) surface->w / w; /* Max X */
239 texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
240
241 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
242 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
243 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
244 #else
245 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
246 #endif
247 );
248 if (image == NULL)
249 {
250 return 0;
251 }
252
253 /* Save the alpha blending attributes */
254 saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
255 saved_alpha = surface->format->alpha;
256 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
257 {
258 SDL_SetAlpha (surface, 0, 0);
259 }
260
261 /* Copy the surface into the GL texture image */
262 area.x = 0;
263 area.y = 0;
264 area.w = surface->w;
265 area.h = surface->h;
266 SDL_BlitSurface (surface, &area, image, &area);
267
268 /* Restore the alpha blending attributes */
269 if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
270 {
271 SDL_SetAlpha (surface, saved_flags, saved_alpha);
272 }
273
274 /* Create an OpenGL texture for the image */
275 glGenTextures (1, &texture);
276 glBindTexture (GL_TEXTURE_2D, texture);
277 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
278 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
279 glTexImage2D (GL_TEXTURE_2D,
280 0,
281 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
282 SDL_FreeSurface (image); /* No longer needed */
283
284 return texture;
285 }
286
287 void draw_some_random_funky_floor_dance_music (int size, int dx, int dy, int dz)
288 {
289 int x, z, ry;
290
291 for (x = 0; x < 100; x++)
292 {
293 for (z = 0; z < 100; z++)
294 {
295 vector<vertex2d> pts;
296 pts.push_back (vertex2d (point (dx + (x * size), dy, dz + (z * size)), vec3 (0, 1, 0), texc (0, 0)));
297 pts.push_back (vertex2d (point (dx + (x * size), dy, dz + ((z + 1) * size)), vec3 (0, 1, 0), texc (0, 1)));
298 pts.push_back (vertex2d (point (dx + ((x + 1) * size), dy, dz + ((z + 1) * size)), vec3 (0, 1, 0), texc (1, 1)));
299 pts.push_back (vertex2d (point (dx + ((x + 1) * size), dy, dz + (z * size)), vec3 (0, 1, 0), texc (1, 0)));
300
301 entity_quads *q = new entity_quads;
302 q->set (pts);
303 q->show ();
304 }
305 }
306 }
307
308 //skedjuhlar main_scheduler;
309