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

File Contents

# User Rev Content
1 root 1.3 /*
2     * math support
3     * most of the more complicated code is taken from mesa.
4     */
5    
6 root 1.2 #include <cstdio> // ugly
7 root 1.1 #include <cmath>
8    
9 root 1.4 #include <sys/time.h>
10     #include <time.h>
11 root 1.15 #include <GL/gl.h>
12 root 1.2
13 root 1.1 #include "util.h"
14 root 1.15 #include "entity.h"
15 root 1.1
16 root 1.3 #define DEG2RAD (M_PI / 180.)
17    
18 root 1.6 void renormalize (sector &s, point &p)
19     {
20     float i;
21    
22 root 1.11 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 root 1.6 }
26    
27 root 1.7 /////////////////////////////////////////////////////////////////////////////
28    
29 root 1.1 const vec3 normalize (const vec3 &v)
30     {
31 root 1.3 GLfloat s = abs (v);
32    
33     if (!s)
34     return v;
35 root 1.1
36 root 1.3 s = 1. / s;
37 root 1.1 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 root 1.3 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 root 1.2 }
48    
49 root 1.7 /////////////////////////////////////////////////////////////////////////////
50    
51 root 1.12 void matrix::diagonal (GLfloat v)
52 root 1.2 {
53     for (int i = 4; i--; )
54     for (int j = 4; j--; )
55     data[i][j] = i == j ? v : 0.;
56     }
57    
58 root 1.12 const matrix operator *(const matrix &a, const matrix &b)
59 root 1.2 {
60 root 1.12 matrix r;
61 root 1.2
62 root 1.3 // 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 root 1.2
73 root 1.3 return r;
74     }
75 root 1.2
76 root 1.12 const matrix matrix::rotation (GLfloat angle, const vec3 &axis)
77 root 1.3 {
78     GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
79 root 1.2
80 root 1.3 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 root 1.12 return matrix (1);
87 root 1.3
88 root 1.12 matrix m;
89 root 1.3 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 root 1.9 return m;
123 root 1.3 }
124    
125 root 1.12 const vec3 operator *(const matrix &a, const vec3 &v)
126 root 1.3 {
127     return vec3 (
128 root 1.9 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 root 1.3 );
132 root 1.2 }
133    
134 root 1.12 void matrix::print ()
135 root 1.2 {
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 root 1.12 const matrix matrix::translation (const vec3 &v)
144 root 1.2 {
145 root 1.12 matrix m(1);
146 root 1.2
147 root 1.9 m(0,3) = v.x;
148     m(1,3) = v.y;
149     m(2,3) = v.z;
150 root 1.2
151 root 1.9 return m;
152 root 1.1 }
153    
154 root 1.7 /////////////////////////////////////////////////////////////////////////////
155    
156     plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d)
157 root 1.9 : n (vec3 (a,b,c))
158 root 1.7 {
159     GLfloat s = 1. / abs (n);
160    
161     n = n * s;
162 root 1.9 this->d = d * s;
163 root 1.7 }
164    
165     /////////////////////////////////////////////////////////////////////////////
166    
167 root 1.1 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 root 1.5 void box::add (const sector &p)
178 root 1.1 {
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 root 1.5 }
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 root 1.6 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 root 1.1 }
196 root 1.7
197     /////////////////////////////////////////////////////////////////////////////
198 root 1.4
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 root 1.13 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 root 1.14 //w = power_of_two (surface->w);
233     //h = power_of_two (surface->h);
234 root 1.13 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 root 1.17 void draw_some_random_funky_floor_dance_music (int size, int dx, int dy, int dz)
288     {
289     int x, z, ry;
290 root 1.15
291 root 1.17 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 root 1.15
301 root 1.17 entity_quads *q = new entity_quads;
302     q->set (pts);
303     q->show ();
304     }
305     }
306 root 1.15 }
307 root 1.13
308 root 1.10 //skedjuhlar main_scheduler;
309 root 1.17