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