ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/util.C
Revision: 1.8
Committed: Mon Oct 4 16:03:49 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
added some simple scheduler and events

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.2
12 root 1.1 #include "util.h"
13    
14 root 1.3 #define DEG2RAD (M_PI / 180.)
15    
16 root 1.6 void renormalize (sector &s, point &p)
17     {
18     float i;
19    
20     p.x = modff (p.x, &i); s.x += (int)i;
21     p.y = modff (p.y, &i); s.y += (int)i;
22     p.z = modff (p.z, &i); s.z += (int)i;
23     }
24    
25 root 1.7 /////////////////////////////////////////////////////////////////////////////
26    
27 root 1.1 const vec3 normalize (const vec3 &v)
28     {
29 root 1.3 GLfloat s = abs (v);
30    
31     if (!s)
32     return v;
33 root 1.1
34 root 1.3 s = 1. / s;
35 root 1.1 return vec3 (v.x * s, v.y * s, v.z * s);
36     }
37    
38     const vec3 cross (const vec3 &a, const vec3 &b)
39     {
40     return vec3 (
41 root 1.3 a.y * b.z - a.z * b.y,
42     a.z * b.x - a.x * b.z,
43     a.x * b.y - a.y * b.x
44     );
45 root 1.2 }
46    
47 root 1.7 /////////////////////////////////////////////////////////////////////////////
48    
49 root 1.2 void gl_matrix::diagonal (GLfloat v)
50     {
51     for (int i = 4; i--; )
52     for (int j = 4; j--; )
53     data[i][j] = i == j ? v : 0.;
54     }
55    
56     const gl_matrix operator *(const gl_matrix &a, const gl_matrix &b)
57     {
58     gl_matrix r;
59    
60 root 1.3 // taken from mesa
61     for (int i = 0; i < 4; i++)
62     {
63     const GLfloat ai0=a(i,0), ai1=a(i,1), ai2=a(i,2), ai3=a(i,3);
64    
65     r(i,0) = ai0 * b(0,0) + ai1 * b(1,0) + ai2 * b(2,0) + ai3 * b(3,0);
66     r(i,1) = ai0 * b(0,1) + ai1 * b(1,1) + ai2 * b(2,1) + ai3 * b(3,1);
67     r(i,2) = ai0 * b(0,2) + ai1 * b(1,2) + ai2 * b(2,2) + ai3 * b(3,2);
68     r(i,3) = ai0 * b(0,3) + ai1 * b(1,3) + ai2 * b(2,3) + ai3 * b(3,3);
69     }
70 root 1.2
71 root 1.3 return r;
72     }
73 root 1.2
74 root 1.3 void gl_matrix::rotate (GLfloat angle, const vec3 &axis)
75     {
76     gl_matrix m;
77     GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
78 root 1.2
79 root 1.3 s = (GLfloat) sinf (angle * DEG2RAD);
80     c = (GLfloat) cosf (angle * DEG2RAD);
81    
82     const GLfloat mag = abs (axis);
83    
84     if (mag <= 1.0e-4)
85     return; /* no rotation, leave mat as-is */
86    
87     const vec3 n = axis * (1. / mag);
88    
89     /*
90     * Arbitrary axis rotation matrix.
91     *
92     * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
93     * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
94     * (which is about the X-axis), and the two composite transforms
95     * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
96     * from the arbitrary axis to the X-axis then back. They are
97     * all elementary rotations.
98     *
99     * Rz' is a rotation about the Z-axis, to bring the axis vector
100     * into the x-z plane. Then Ry' is applied, rotating about the
101     * Y-axis to bring the axis vector parallel with the X-axis. The
102     * rotation about the X-axis is then performed. Ry and Rz are
103     * simply the respective inverse transforms to bring the arbitrary
104     * axis back to it's original orientation. The first transforms
105     * Rz' and Ry' are considered inverses, since the data from the
106     * arbitrary axis gives you info on how to get to it, not how
107     * to get away from it, and an inverse must be applied.
108     *
109     * The basic calculation used is to recognize that the arbitrary
110     * axis vector (x, y, z), since it is of unit length, actually
111     * represents the sines and cosines of the angles to rotate the
112     * X-axis to the same orientation, with theta being the angle about
113     * Z and phi the angle about Y (in the order described above)
114     * as follows:
115     *
116     * cos ( theta ) = x / sqrt ( 1 - z^2 )
117     * sin ( theta ) = y / sqrt ( 1 - z^2 )
118     *
119     * cos ( phi ) = sqrt ( 1 - z^2 )
120     * sin ( phi ) = z
121     *
122     * Note that cos ( phi ) can further be inserted to the above
123     * formulas:
124     *
125     * cos ( theta ) = x / cos ( phi )
126     * sin ( theta ) = y / sin ( phi )
127     *
128     * ...etc. Because of those relations and the standard trigonometric
129     * relations, it is pssible to reduce the transforms down to what
130     * is used below. It may be that any primary axis chosen will give the
131     * same results (modulo a sign convention) using this method.
132     *
133     * Particularly nice is to notice that all divisions that might
134     * have caused trouble when parallel to certain planes or
135     * axis go away with care paid to reducing the expressions.
136     * After checking, it does perform correctly under all cases, since
137     * in all the cases of division where the denominator would have
138     * been zero, the numerator would have been zero as well, giving
139     * the expected result.
140     */
141    
142     xx = n.x * n.x;
143     yy = n.y * n.y;
144     zz = n.z * n.z;
145     xy = n.x * n.y;
146     yz = n.y * n.z;
147     zx = n.z * n.x;
148     xs = n.x * s;
149     ys = n.y * s;
150     zs = n.z * s;
151     one_c = 1.0F - c;
152    
153     m(0,0) = (one_c * xx) + c;
154     m(0,1) = (one_c * xy) - zs;
155     m(0,2) = (one_c * zx) + ys;
156     m(0,3) = 0;
157    
158     m(1,0) = (one_c * xy) + zs;
159     m(1,1) = (one_c * yy) + c;
160     m(1,2) = (one_c * yz) - xs;
161     m(1,3) = 0;
162    
163     m(2,0) = (one_c * zx) - ys;
164     m(2,1) = (one_c * yz) + xs;
165     m(2,2) = (one_c * zz) + c;
166     m(2,3) = 0;
167    
168     m(3,0) = 0;
169     m(3,1) = 0;
170     m(3,2) = 0;
171     m(3,3) = 1;
172    
173     (*this) = (*this) * m;
174     }
175    
176     const vec3 operator *(const gl_matrix &a, const vec3 &v)
177     {
178     return vec3 (
179     a(0,0) * v.x + a(1,0) * v.y + a(2,0) * v.z + a(3,0),
180     a(0,1) * v.x + a(1,1) * v.y + a(2,1) * v.z + a(3,1),
181     a(0,2) * v.x + a(1,2) * v.y + a(2,2) * v.z + a(3,2)
182     );
183 root 1.2 }
184    
185     void gl_matrix::print ()
186     {
187     printf ("\n");
188     printf ("[ %f, %f, %f, %f ]\n", data[0][0], data[1][0], data[2][0], data[3][0]);
189     printf ("[ %f, %f, %f, %f ]\n", data[0][1], data[1][1], data[2][1], data[3][1]);
190     printf ("[ %f, %f, %f, %f ]\n", data[0][2], data[1][2], data[2][2], data[3][2]);
191     printf ("[ %f, %f, %f, %f ]\n", data[0][3], data[1][3], data[2][3], data[3][3]);
192     }
193    
194     void gl_matrix::translate (const vec3 &v)
195     {
196     gl_matrix m(1);
197    
198     m(3,0) = v.x;
199     m(3,1) = v.y;
200     m(3,2) = v.z;
201     m(3,3) = 1;
202    
203     (*this) = (*this) * m;
204 root 1.1 }
205    
206 root 1.7 /////////////////////////////////////////////////////////////////////////////
207    
208     plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d)
209     : n (vec3 (a,b,c)), d(d)
210     {
211     GLfloat s = 1. / abs (n);
212    
213     n = n * s;
214     d = d * s;
215     }
216    
217     /////////////////////////////////////////////////////////////////////////////
218    
219 root 1.1 void box::add (const box &o)
220     {
221     a.x = min (a.x, o.a.x);
222     a.y = min (a.y, o.a.y);
223     a.z = min (a.z, o.a.z);
224     b.x = max (b.x, o.b.x);
225     b.y = max (b.y, o.b.y);
226     b.z = max (b.z, o.b.z);
227     }
228    
229 root 1.5 void box::add (const sector &p)
230 root 1.1 {
231     a.x = min (a.x, p.x);
232     a.y = min (a.y, p.y);
233     a.z = min (a.z, p.z);
234     b.x = max (b.x, p.x);
235     b.y = max (b.y, p.y);
236     b.z = max (b.z, p.z);
237 root 1.5 }
238    
239     void box::add (const point &p)
240     {
241     a.x = min (a.x, (soffs)floorf (p.x));
242     a.y = min (a.y, (soffs)floorf (p.y));
243     a.z = min (a.z, (soffs)floorf (p.z));
244 root 1.6 b.x = max (b.x, (soffs)ceilf (p.x));
245     b.y = max (b.y, (soffs)ceilf (p.y));
246     b.z = max (b.z, (soffs)ceilf (p.z));
247 root 1.1 }
248 root 1.7
249     /////////////////////////////////////////////////////////////////////////////
250 root 1.4
251     struct timer timer;
252     static double base;
253     double timer::now = 0.;
254     double timer::diff;
255    
256     void timer::frame ()
257     {
258     struct timeval tv;
259     gettimeofday (&tv, 0);
260    
261     double next = tv.tv_sec - base + tv.tv_usec / 1.e6;
262    
263     diff = next - now;
264     now = next;
265     }
266    
267     timer::timer ()
268     {
269     struct timeval tv;
270     gettimeofday (&tv, 0);
271     base = tv.tv_sec + tv.tv_usec / 1.e6;
272     }
273    
274 root 1.8 skedjuhlar main_scheduler;