/* * math support * most of the more complicated code is taken from mesa. */ #include // ugly #include #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); 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 point &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); } ///////////////////////////////////////////////////////////////////////////// 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 render_text (GLint x, GLint y, const char *str) { glRasterPos2i (x, y); while (!*str) glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, *str++); } namespace gl { void draw_box (const view &ctx, const sector &a, const sector &b) { int i; static GLint verts[] = { 0x8000, 0x8004, 0x8006, 0x8002, // -x 0x8001, 0x8003, 0x8007, 0x8005, // +x 0x8000, 0x8001, 0x8005, 0x8004, // -y 0x8007, 0x8003, 0x8002, 0x8006, // +y 0x8000, 0x8002, 0x8003, 0x8001, // -z 0x8004, 0x8005, 0x8007, 0x8006, // +z 0 }; glBegin (GL_LINE_LOOP); for (GLint *v = verts; *v; v++) { GLint mask = *v; glVertex3i (mask & 1 ? b.x : a.x, mask & 2 ? b.y : a.y, mask & 4 ? b.z : a.z); } glEnd (); } } //skedjuhlar main_scheduler;