| 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 |
|
|
/* |
| 92 |
|
|
* Arbitrary axis rotation matrix. |
| 93 |
|
|
* |
| 94 |
|
|
* This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied |
| 95 |
|
|
* like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation |
| 96 |
|
|
* (which is about the X-axis), and the two composite transforms |
| 97 |
|
|
* Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary |
| 98 |
|
|
* from the arbitrary axis to the X-axis then back. They are |
| 99 |
|
|
* all elementary rotations. |
| 100 |
|
|
* |
| 101 |
|
|
* Rz' is a rotation about the Z-axis, to bring the axis vector |
| 102 |
|
|
* into the x-z plane. Then Ry' is applied, rotating about the |
| 103 |
|
|
* Y-axis to bring the axis vector parallel with the X-axis. The |
| 104 |
|
|
* rotation about the X-axis is then performed. Ry and Rz are |
| 105 |
|
|
* simply the respective inverse transforms to bring the arbitrary |
| 106 |
|
|
* axis back to it's original orientation. The first transforms |
| 107 |
|
|
* Rz' and Ry' are considered inverses, since the data from the |
| 108 |
|
|
* arbitrary axis gives you info on how to get to it, not how |
| 109 |
|
|
* to get away from it, and an inverse must be applied. |
| 110 |
|
|
* |
| 111 |
|
|
* The basic calculation used is to recognize that the arbitrary |
| 112 |
|
|
* axis vector (x, y, z), since it is of unit length, actually |
| 113 |
|
|
* represents the sines and cosines of the angles to rotate the |
| 114 |
|
|
* X-axis to the same orientation, with theta being the angle about |
| 115 |
|
|
* Z and phi the angle about Y (in the order described above) |
| 116 |
|
|
* as follows: |
| 117 |
|
|
* |
| 118 |
|
|
* cos ( theta ) = x / sqrt ( 1 - z^2 ) |
| 119 |
|
|
* sin ( theta ) = y / sqrt ( 1 - z^2 ) |
| 120 |
|
|
* |
| 121 |
|
|
* cos ( phi ) = sqrt ( 1 - z^2 ) |
| 122 |
|
|
* sin ( phi ) = z |
| 123 |
|
|
* |
| 124 |
|
|
* Note that cos ( phi ) can further be inserted to the above |
| 125 |
|
|
* formulas: |
| 126 |
|
|
* |
| 127 |
|
|
* cos ( theta ) = x / cos ( phi ) |
| 128 |
|
|
* sin ( theta ) = y / sin ( phi ) |
| 129 |
|
|
* |
| 130 |
|
|
* ...etc. Because of those relations and the standard trigonometric |
| 131 |
|
|
* relations, it is pssible to reduce the transforms down to what |
| 132 |
|
|
* is used below. It may be that any primary axis chosen will give the |
| 133 |
|
|
* same results (modulo a sign convention) using this method. |
| 134 |
|
|
* |
| 135 |
|
|
* Particularly nice is to notice that all divisions that might |
| 136 |
|
|
* have caused trouble when parallel to certain planes or |
| 137 |
|
|
* axis go away with care paid to reducing the expressions. |
| 138 |
|
|
* After checking, it does perform correctly under all cases, since |
| 139 |
|
|
* in all the cases of division where the denominator would have |
| 140 |
|
|
* been zero, the numerator would have been zero as well, giving |
| 141 |
|
|
* the expected result. |
| 142 |
|
|
*/ |
| 143 |
|
|
|
| 144 |
|
|
xx = n.x * n.x; |
| 145 |
|
|
yy = n.y * n.y; |
| 146 |
|
|
zz = n.z * n.z; |
| 147 |
|
|
xy = n.x * n.y; |
| 148 |
|
|
yz = n.y * n.z; |
| 149 |
|
|
zx = n.z * n.x; |
| 150 |
|
|
xs = n.x * s; |
| 151 |
|
|
ys = n.y * s; |
| 152 |
|
|
zs = n.z * s; |
| 153 |
|
|
one_c = 1.0F - c; |
| 154 |
|
|
|
| 155 |
|
|
m(0,0) = (one_c * xx) + c; |
| 156 |
|
|
m(0,1) = (one_c * xy) - zs; |
| 157 |
|
|
m(0,2) = (one_c * zx) + ys; |
| 158 |
|
|
m(0,3) = 0; |
| 159 |
|
|
|
| 160 |
|
|
m(1,0) = (one_c * xy) + zs; |
| 161 |
|
|
m(1,1) = (one_c * yy) + c; |
| 162 |
|
|
m(1,2) = (one_c * yz) - xs; |
| 163 |
|
|
m(1,3) = 0; |
| 164 |
|
|
|
| 165 |
|
|
m(2,0) = (one_c * zx) - ys; |
| 166 |
|
|
m(2,1) = (one_c * yz) + xs; |
| 167 |
|
|
m(2,2) = (one_c * zz) + c; |
| 168 |
|
|
m(2,3) = 0; |
| 169 |
|
|
|
| 170 |
|
|
m(3,0) = 0; |
| 171 |
|
|
m(3,1) = 0; |
| 172 |
|
|
m(3,2) = 0; |
| 173 |
|
|
m(3,3) = 1; |
| 174 |
|
|
|
| 175 |
root |
1.9 |
return m; |
| 176 |
root |
1.3 |
} |
| 177 |
|
|
|
| 178 |
root |
1.12 |
const vec3 operator *(const matrix &a, const vec3 &v) |
| 179 |
root |
1.3 |
{ |
| 180 |
|
|
return vec3 ( |
| 181 |
root |
1.9 |
a(0,0) * v.x + a(0,1) * v.y + a(0,2) * v.z + a(0,3), |
| 182 |
|
|
a(1,0) * v.x + a(1,1) * v.y + a(1,2) * v.z + a(1,3), |
| 183 |
|
|
a(2,0) * v.x + a(2,1) * v.y + a(2,2) * v.z + a(2,3) |
| 184 |
root |
1.3 |
); |
| 185 |
root |
1.2 |
} |
| 186 |
|
|
|
| 187 |
root |
1.12 |
void matrix::print () |
| 188 |
root |
1.2 |
{ |
| 189 |
|
|
printf ("\n"); |
| 190 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][0], data[1][0], data[2][0], data[3][0]); |
| 191 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][1], data[1][1], data[2][1], data[3][1]); |
| 192 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][2], data[1][2], data[2][2], data[3][2]); |
| 193 |
|
|
printf ("[ %f, %f, %f, %f ]\n", data[0][3], data[1][3], data[2][3], data[3][3]); |
| 194 |
|
|
} |
| 195 |
|
|
|
| 196 |
root |
1.12 |
const matrix matrix::translation (const vec3 &v) |
| 197 |
root |
1.2 |
{ |
| 198 |
root |
1.12 |
matrix m(1); |
| 199 |
root |
1.2 |
|
| 200 |
root |
1.9 |
m(0,3) = v.x; |
| 201 |
|
|
m(1,3) = v.y; |
| 202 |
|
|
m(2,3) = v.z; |
| 203 |
root |
1.2 |
|
| 204 |
root |
1.9 |
return m; |
| 205 |
root |
1.1 |
} |
| 206 |
|
|
|
| 207 |
root |
1.7 |
///////////////////////////////////////////////////////////////////////////// |
| 208 |
|
|
|
| 209 |
|
|
plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d) |
| 210 |
root |
1.9 |
: n (vec3 (a,b,c)) |
| 211 |
root |
1.7 |
{ |
| 212 |
|
|
GLfloat s = 1. / abs (n); |
| 213 |
|
|
|
| 214 |
|
|
n = n * s; |
| 215 |
root |
1.9 |
this->d = d * s; |
| 216 |
root |
1.7 |
} |
| 217 |
|
|
|
| 218 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 219 |
|
|
|
| 220 |
root |
1.1 |
void box::add (const box &o) |
| 221 |
|
|
{ |
| 222 |
|
|
a.x = min (a.x, o.a.x); |
| 223 |
|
|
a.y = min (a.y, o.a.y); |
| 224 |
|
|
a.z = min (a.z, o.a.z); |
| 225 |
|
|
b.x = max (b.x, o.b.x); |
| 226 |
|
|
b.y = max (b.y, o.b.y); |
| 227 |
|
|
b.z = max (b.z, o.b.z); |
| 228 |
|
|
} |
| 229 |
|
|
|
| 230 |
root |
1.5 |
void box::add (const sector &p) |
| 231 |
root |
1.1 |
{ |
| 232 |
|
|
a.x = min (a.x, p.x); |
| 233 |
|
|
a.y = min (a.y, p.y); |
| 234 |
|
|
a.z = min (a.z, p.z); |
| 235 |
|
|
b.x = max (b.x, p.x); |
| 236 |
|
|
b.y = max (b.y, p.y); |
| 237 |
|
|
b.z = max (b.z, p.z); |
| 238 |
root |
1.5 |
} |
| 239 |
|
|
|
| 240 |
|
|
void box::add (const point &p) |
| 241 |
|
|
{ |
| 242 |
|
|
a.x = min (a.x, (soffs)floorf (p.x)); |
| 243 |
|
|
a.y = min (a.y, (soffs)floorf (p.y)); |
| 244 |
|
|
a.z = min (a.z, (soffs)floorf (p.z)); |
| 245 |
root |
1.6 |
b.x = max (b.x, (soffs)ceilf (p.x)); |
| 246 |
|
|
b.y = max (b.y, (soffs)ceilf (p.y)); |
| 247 |
|
|
b.z = max (b.z, (soffs)ceilf (p.z)); |
| 248 |
root |
1.1 |
} |
| 249 |
root |
1.7 |
|
| 250 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 251 |
root |
1.4 |
|
| 252 |
|
|
struct timer timer; |
| 253 |
|
|
static double base; |
| 254 |
|
|
double timer::now = 0.; |
| 255 |
|
|
double timer::diff; |
| 256 |
|
|
|
| 257 |
|
|
void timer::frame () |
| 258 |
|
|
{ |
| 259 |
|
|
struct timeval tv; |
| 260 |
|
|
gettimeofday (&tv, 0); |
| 261 |
|
|
|
| 262 |
|
|
double next = tv.tv_sec - base + tv.tv_usec / 1.e6; |
| 263 |
|
|
|
| 264 |
|
|
diff = next - now; |
| 265 |
|
|
now = next; |
| 266 |
|
|
} |
| 267 |
|
|
|
| 268 |
|
|
timer::timer () |
| 269 |
|
|
{ |
| 270 |
|
|
struct timeval tv; |
| 271 |
|
|
gettimeofday (&tv, 0); |
| 272 |
|
|
base = tv.tv_sec + tv.tv_usec / 1.e6; |
| 273 |
|
|
} |
| 274 |
|
|
|
| 275 |
root |
1.13 |
GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
| 276 |
|
|
{ |
| 277 |
|
|
GLuint texture; |
| 278 |
|
|
int w, h; |
| 279 |
|
|
SDL_Surface *image; |
| 280 |
|
|
SDL_Rect area; |
| 281 |
|
|
Uint32 saved_flags; |
| 282 |
|
|
Uint8 saved_alpha; |
| 283 |
|
|
|
| 284 |
|
|
/* Use the surface width and height expanded to powers of 2 */ |
| 285 |
root |
1.14 |
//w = power_of_two (surface->w); |
| 286 |
|
|
//h = power_of_two (surface->h); |
| 287 |
root |
1.13 |
w = power_of_two (surface->w); |
| 288 |
|
|
h = power_of_two (surface->h); |
| 289 |
|
|
texcoord[0] = 0.0f; /* Min X */ |
| 290 |
|
|
texcoord[1] = 0.0f; /* Min Y */ |
| 291 |
|
|
texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
| 292 |
|
|
texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
| 293 |
|
|
|
| 294 |
|
|
image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
| 295 |
|
|
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
| 296 |
|
|
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
| 297 |
|
|
#else |
| 298 |
|
|
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
| 299 |
|
|
#endif |
| 300 |
|
|
); |
| 301 |
|
|
if (image == NULL) |
| 302 |
|
|
{ |
| 303 |
|
|
return 0; |
| 304 |
|
|
} |
| 305 |
|
|
|
| 306 |
|
|
/* Save the alpha blending attributes */ |
| 307 |
|
|
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
| 308 |
|
|
saved_alpha = surface->format->alpha; |
| 309 |
|
|
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
| 310 |
|
|
{ |
| 311 |
|
|
SDL_SetAlpha (surface, 0, 0); |
| 312 |
|
|
} |
| 313 |
|
|
|
| 314 |
|
|
/* Copy the surface into the GL texture image */ |
| 315 |
|
|
area.x = 0; |
| 316 |
|
|
area.y = 0; |
| 317 |
|
|
area.w = surface->w; |
| 318 |
|
|
area.h = surface->h; |
| 319 |
|
|
SDL_BlitSurface (surface, &area, image, &area); |
| 320 |
|
|
|
| 321 |
|
|
/* Restore the alpha blending attributes */ |
| 322 |
|
|
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
| 323 |
|
|
{ |
| 324 |
|
|
SDL_SetAlpha (surface, saved_flags, saved_alpha); |
| 325 |
|
|
} |
| 326 |
|
|
|
| 327 |
|
|
/* Create an OpenGL texture for the image */ |
| 328 |
|
|
glGenTextures (1, &texture); |
| 329 |
|
|
glBindTexture (GL_TEXTURE_2D, texture); |
| 330 |
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 331 |
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 332 |
|
|
glTexImage2D (GL_TEXTURE_2D, |
| 333 |
|
|
0, |
| 334 |
|
|
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
| 335 |
|
|
SDL_FreeSurface (image); /* No longer needed */ |
| 336 |
|
|
|
| 337 |
|
|
return texture; |
| 338 |
|
|
} |
| 339 |
|
|
|
| 340 |
root |
1.17 |
void draw_some_random_funky_floor_dance_music (int size, int dx, int dy, int dz) |
| 341 |
|
|
{ |
| 342 |
|
|
int x, z, ry; |
| 343 |
root |
1.15 |
|
| 344 |
root |
1.17 |
for (x = 0; x < 100; x++) |
| 345 |
|
|
{ |
| 346 |
|
|
for (z = 0; z < 100; z++) |
| 347 |
|
|
{ |
| 348 |
|
|
vector<vertex2d> pts; |
| 349 |
|
|
pts.push_back (vertex2d (point (dx + (x * size), dy, dz + (z * size)), vec3 (0, 1, 0), texc (0, 0))); |
| 350 |
|
|
pts.push_back (vertex2d (point (dx + (x * size), dy, dz + ((z + 1) * size)), vec3 (0, 1, 0), texc (0, 1))); |
| 351 |
|
|
pts.push_back (vertex2d (point (dx + ((x + 1) * size), dy, dz + ((z + 1) * size)), vec3 (0, 1, 0), texc (1, 1))); |
| 352 |
|
|
pts.push_back (vertex2d (point (dx + ((x + 1) * size), dy, dz + (z * size)), vec3 (0, 1, 0), texc (1, 0))); |
| 353 |
root |
1.15 |
|
| 354 |
root |
1.17 |
entity_quads *q = new entity_quads; |
| 355 |
|
|
q->set (pts); |
| 356 |
|
|
q->show (); |
| 357 |
|
|
} |
| 358 |
|
|
} |
| 359 |
root |
1.15 |
} |
| 360 |
root |
1.13 |
|
| 361 |
root |
1.10 |
//skedjuhlar main_scheduler; |
| 362 |
root |
1.17 |
|