/* * math support * most of the more complicated code is taken from mesa. */ #include // ugly #include #include #include #include #include "util.h" #include "entity.h" #define DEG2RAD (M_PI / 180.) void renormalize (sector &s, point &p) { float i; p.x = modff (p.x, &i); s.x += (soffs)i; p.y = modff (p.y, &i); s.y += (soffs)i; p.z = modff (p.z, &i); s.z += (soffs)i; } ///////////////////////////////////////////////////////////////////////////// const vec3 normalize (const vec3 &v) { GLfloat s = abs (v); if (!s) return v; s = 1. / s; return vec3 (v.x * s, v.y * s, v.z * s); } const vec3 cross (const vec3 &a, const vec3 &b) { return vec3 ( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x ); } ///////////////////////////////////////////////////////////////////////////// void matrix::diagonal (GLfloat v) { for (int i = 4; i--; ) for (int j = 4; j--; ) data[i][j] = i == j ? v : 0.; } const matrix operator *(const matrix &a, const matrix &b) { matrix r; // taken from mesa for (int i = 0; i < 4; i++) { const GLfloat ai0=a(i,0), ai1=a(i,1), ai2=a(i,2), ai3=a(i,3); r(i,0) = ai0 * b(0,0) + ai1 * b(1,0) + ai2 * b(2,0) + ai3 * b(3,0); r(i,1) = ai0 * b(0,1) + ai1 * b(1,1) + ai2 * b(2,1) + ai3 * b(3,1); r(i,2) = ai0 * b(0,2) + ai1 * b(1,2) + ai2 * b(2,2) + ai3 * b(3,2); r(i,3) = ai0 * b(0,3) + ai1 * b(1,3) + ai2 * b(2,3) + ai3 * b(3,3); } return r; } const matrix matrix::rotation (GLfloat angle, const vec3 &axis) { GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; s = (GLfloat) sinf (angle * DEG2RAD); c = (GLfloat) cosf (angle * DEG2RAD); const GLfloat mag = abs (axis); if (mag <= 1.0e-4) return matrix (1); matrix m; const vec3 n = axis * (1. / mag); /* * Arbitrary axis rotation matrix. * * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation * (which is about the X-axis), and the two composite transforms * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary * from the arbitrary axis to the X-axis then back. They are * all elementary rotations. * * Rz' is a rotation about the Z-axis, to bring the axis vector * into the x-z plane. Then Ry' is applied, rotating about the * Y-axis to bring the axis vector parallel with the X-axis. The * rotation about the X-axis is then performed. Ry and Rz are * simply the respective inverse transforms to bring the arbitrary * axis back to it's original orientation. The first transforms * Rz' and Ry' are considered inverses, since the data from the * arbitrary axis gives you info on how to get to it, not how * to get away from it, and an inverse must be applied. * * The basic calculation used is to recognize that the arbitrary * axis vector (x, y, z), since it is of unit length, actually * represents the sines and cosines of the angles to rotate the * X-axis to the same orientation, with theta being the angle about * Z and phi the angle about Y (in the order described above) * as follows: * * cos ( theta ) = x / sqrt ( 1 - z^2 ) * sin ( theta ) = y / sqrt ( 1 - z^2 ) * * cos ( phi ) = sqrt ( 1 - z^2 ) * sin ( phi ) = z * * Note that cos ( phi ) can further be inserted to the above * formulas: * * cos ( theta ) = x / cos ( phi ) * sin ( theta ) = y / sin ( phi ) * * ...etc. Because of those relations and the standard trigonometric * relations, it is pssible to reduce the transforms down to what * is used below. It may be that any primary axis chosen will give the * same results (modulo a sign convention) using this method. * * Particularly nice is to notice that all divisions that might * have caused trouble when parallel to certain planes or * axis go away with care paid to reducing the expressions. * After checking, it does perform correctly under all cases, since * in all the cases of division where the denominator would have * been zero, the numerator would have been zero as well, giving * the expected result. */ xx = n.x * n.x; yy = n.y * n.y; zz = n.z * n.z; xy = n.x * n.y; yz = n.y * n.z; zx = n.z * n.x; xs = n.x * s; ys = n.y * s; zs = n.z * s; one_c = 1.0F - c; m(0,0) = (one_c * xx) + c; m(0,1) = (one_c * xy) - zs; m(0,2) = (one_c * zx) + ys; m(0,3) = 0; m(1,0) = (one_c * xy) + zs; m(1,1) = (one_c * yy) + c; m(1,2) = (one_c * yz) - xs; m(1,3) = 0; m(2,0) = (one_c * zx) - ys; m(2,1) = (one_c * yz) + xs; m(2,2) = (one_c * zz) + c; m(2,3) = 0; m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; return m; } const vec3 operator *(const matrix &a, const vec3 &v) { return vec3 ( a(0,0) * v.x + a(0,1) * v.y + a(0,2) * v.z + a(0,3), a(1,0) * v.x + a(1,1) * v.y + a(1,2) * v.z + a(1,3), a(2,0) * v.x + a(2,1) * v.y + a(2,2) * v.z + a(2,3) ); } void matrix::print () { printf ("\n"); printf ("[ %f, %f, %f, %f ]\n", data[0][0], data[1][0], data[2][0], data[3][0]); printf ("[ %f, %f, %f, %f ]\n", data[0][1], data[1][1], data[2][1], data[3][1]); printf ("[ %f, %f, %f, %f ]\n", data[0][2], data[1][2], data[2][2], data[3][2]); printf ("[ %f, %f, %f, %f ]\n", data[0][3], data[1][3], data[2][3], data[3][3]); } const matrix matrix::translation (const vec3 &v) { matrix m(1); m(0,3) = v.x; m(1,3) = v.y; m(2,3) = v.z; return m; } ///////////////////////////////////////////////////////////////////////////// plane::plane (GLfloat a, GLfloat b, GLfloat c, GLfloat d) : n (vec3 (a,b,c)) { GLfloat s = 1. / abs (n); n = n * s; this->d = d * s; } ///////////////////////////////////////////////////////////////////////////// void box::add (const box &o) { a.x = min (a.x, o.a.x); a.y = min (a.y, o.a.y); a.z = min (a.z, o.a.z); b.x = max (b.x, o.b.x); b.y = max (b.y, o.b.y); b.z = max (b.z, o.b.z); } void box::add (const sector &p) { a.x = min (a.x, p.x); a.y = min (a.y, p.y); a.z = min (a.z, p.z); b.x = max (b.x, p.x); b.y = max (b.y, p.y); b.z = max (b.z, p.z); } void box::add (const point &p) { a.x = min (a.x, (soffs)floorf (p.x)); a.y = min (a.y, (soffs)floorf (p.y)); a.z = min (a.z, (soffs)floorf (p.z)); b.x = max (b.x, (soffs)ceilf (p.x)); b.y = max (b.y, (soffs)ceilf (p.y)); b.z = max (b.z, (soffs)ceilf (p.z)); } ///////////////////////////////////////////////////////////////////////////// struct timer timer; static double base; double timer::now = 0.; double timer::diff; void timer::frame () { struct timeval tv; gettimeofday (&tv, 0); double next = tv.tv_sec - base + tv.tv_usec / 1.e6; diff = next - now; now = next; } timer::timer () { struct timeval tv; gettimeofday (&tv, 0); base = tv.tv_sec + tv.tv_usec / 1.e6; } GLuint SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) { GLuint texture; int w, h; SDL_Surface *image; SDL_Rect area; Uint32 saved_flags; Uint8 saved_alpha; /* Use the surface width and height expanded to powers of 2 */ //w = power_of_two (surface->w); //h = power_of_two (surface->h); w = power_of_two (surface->w); h = power_of_two (surface->h); texcoord[0] = 0.0f; /* Min X */ texcoord[1] = 0.0f; /* Min Y */ texcoord[2] = (GLfloat) surface->w / w; /* Max X */ texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #else 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #endif ); if (image == NULL) { return 0; } /* Save the alpha blending attributes */ saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); saved_alpha = surface->format->alpha; if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) { SDL_SetAlpha (surface, 0, 0); } /* Copy the surface into the GL texture image */ area.x = 0; area.y = 0; area.w = surface->w; area.h = surface->h; SDL_BlitSurface (surface, &area, image, &area); /* Restore the alpha blending attributes */ if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) { SDL_SetAlpha (surface, saved_flags, saved_alpha); } /* Create an OpenGL texture for the image */ glGenTextures (1, &texture); glBindTexture (GL_TEXTURE_2D, texture); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); SDL_FreeSurface (image); /* No longer needed */ return texture; } void draw_some_random_funky_floor_dance_music (int size, int dx, int dy, int dz) { int x, z, ry; vector pts; for (x = 0; x < 100; x++) { for (z = 0; z < 100; z++) { pts.push_back (vertex2d (point (dx + (x * size), dy, dz + (z * size)), vec3 (0, 0, 1), texc (0, 0))); pts.push_back (vertex2d (point (dx + ((x + 1) * size), dy, dz + (z * size)), vec3 (0, 0, 1), texc (1, 0))); pts.push_back (vertex2d (point (dx + ((x + 1) * size), dy, dz + ((z + 1) * size)), vec3 (0, 0, 1), texc (1, 1))); pts.push_back (vertex2d (point (dx + (x * size), dy, dz + ((z + 1) * size)), vec3 (0, 0, 1), texc (0, 1))); entity_quads *q = new entity_quads; q->set (pts); pts.clear (); q->show (); } } } //skedjuhlar main_scheduler;