ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/util.C
Revision: 1.19
Committed: Thu Oct 7 23:39:43 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.18: +1 -32 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.19 void box::add (const point &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 root 1.7 /////////////////////////////////////////////////////////////////////////////
188 root 1.4
189     struct timer timer;
190     static double base;
191     double timer::now = 0.;
192     double timer::diff;
193    
194     void timer::frame ()
195     {
196     struct timeval tv;
197     gettimeofday (&tv, 0);
198    
199     double next = tv.tv_sec - base + tv.tv_usec / 1.e6;
200    
201     diff = next - now;
202     now = next;
203     }
204    
205     timer::timer ()
206     {
207     struct timeval tv;
208     gettimeofday (&tv, 0);
209     base = tv.tv_sec + tv.tv_usec / 1.e6;
210     }
211    
212 root 1.13 GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord)
213     {
214     GLuint texture;
215     int w, h;
216     SDL_Surface *image;
217     SDL_Rect area;
218     Uint32 saved_flags;
219     Uint8 saved_alpha;
220    
221     /* Use the surface width and height expanded to powers of 2 */
222 root 1.14 //w = power_of_two (surface->w);
223     //h = power_of_two (surface->h);
224 root 1.13 w = power_of_two (surface->w);
225     h = power_of_two (surface->h);
226     texcoord[0] = 0.0f; /* Min X */
227     texcoord[1] = 0.0f; /* Min Y */
228     texcoord[2] = (GLfloat) surface->w / w; /* Max X */
229     texcoord[3] = (GLfloat) surface->h / h; /* Max Y */
230    
231     image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
232     #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
233     0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
234     #else
235     0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
236     #endif
237     );
238     if (image == NULL)
239     {
240     return 0;
241     }
242    
243     /* Save the alpha blending attributes */
244     saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
245     saved_alpha = surface->format->alpha;
246     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
247     {
248     SDL_SetAlpha (surface, 0, 0);
249     }
250    
251     /* Copy the surface into the GL texture image */
252     area.x = 0;
253     area.y = 0;
254     area.w = surface->w;
255     area.h = surface->h;
256     SDL_BlitSurface (surface, &area, image, &area);
257    
258     /* Restore the alpha blending attributes */
259     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
260     {
261     SDL_SetAlpha (surface, saved_flags, saved_alpha);
262     }
263    
264     /* Create an OpenGL texture for the image */
265     glGenTextures (1, &texture);
266     glBindTexture (GL_TEXTURE_2D, texture);
267     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
268     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
269     glTexImage2D (GL_TEXTURE_2D,
270     0,
271     GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
272     SDL_FreeSurface (image); /* No longer needed */
273    
274     return texture;
275 root 1.15 }
276 root 1.13
277 root 1.10 //skedjuhlar main_scheduler;
278 root 1.17