]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - mathlib.c
changed most #define macros to use (,,,) format instead of {;;;} so they will gobble...
[xonotic/darkplaces.git] / mathlib.c
index 9d847add2d8d3bfd2a7504241c8ba4284df62c26..ab50923c26b6c4c1ba9169839cac44a4d35e90ae 100644 (file)
--- a/mathlib.c
+++ b/mathlib.c
@@ -26,11 +26,10 @@ void Sys_Error (char *error, ...);
 
 vec3_t vec3_origin = {0,0,0};
 int nanmask = 255<<23;
+float ixtable[4096];
 
 /*-----------------------------------------------------------------*/
 
-#define DEG2RAD( a ) ( a * M_PI ) / 180.0F
-
 float m_bytenormals[NUMVERTEXNORMALS][3] =
 {
 {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188}, 
@@ -42,7 +41,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188}, 
 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567}, 
 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191}, 
-{-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856}, 
+{-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325}, 
 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863}, 
 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621}, 
@@ -62,11 +61,11 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866}, 
 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000}, 
 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866}, 
-{0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567}, 
-{0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856}, 
+{0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
+{0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866}, 
 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567}, 
-{0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731}, 
+{0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856}, 
 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718}, 
 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785}, 
@@ -84,7 +83,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863}, 
 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785}, 
 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325}, 
-{0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242}, 
+{0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460}, 
 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242}, 
 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460}, 
@@ -93,9 +92,9 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460}, 
 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621}, 
 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000}, 
-{-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863}, 
+{-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856}, 
-{-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325}, 
+{-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188}, 
 {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017}, 
 {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785}, 
@@ -108,7 +107,7 @@ float m_bytenormals[NUMVERTEXNORMALS][3] =
 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000}, 
 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856}, 
 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000}, 
-{-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866}, 
+{-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567}, 
 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731}, 
 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191}, 
@@ -139,9 +138,35 @@ byte NormalToByte(vec3_t n)
 void ByteToNormal(byte num, vec3_t n)
 {
        if (num < NUMVERTEXNORMALS)
-               VectorCopy(m_bytenormals[num], n)
+               VectorCopy(m_bytenormals[num], n);
        else
-               VectorClear(n) // FIXME: complain?
+               VectorClear(n); // FIXME: complain?
+}
+
+float Q_RSqrt(float number)
+{
+       float y;
+
+       if (number == 0.0f)
+               return 0.0f;
+
+       *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
+       return y * (1.5f - (number * 0.5f * y * y));
+}
+
+void _VectorNormalizeFast(vec3_t v)
+{
+       float y, number;
+
+       number = DotProduct(v, v);
+
+       if (number != 0.0)
+       {
+               *((long *)&y) = 0x5f3759df - ((* (long *) &number) >> 1);
+               y = y * (1.5f - (number * 0.5f * y * y));
+
+               VectorScale(v, y, v);
+       }
 }
 
 #if 0
@@ -272,6 +297,22 @@ void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
        right[1] = -forward[0];
        right[2] = forward[1];
 
+       d = DotProduct(forward, right);
+       right[0] -= d * forward[0];
+       right[1] -= d * forward[1];
+       right[2] -= d * forward[2];
+       VectorNormalizeFast(right);
+       CrossProduct(right, forward, up);
+}
+
+void VectorVectorsDouble(const double *forward, double *right, double *up)
+{
+       double d;
+
+       right[0] = forward[2];
+       right[1] = -forward[0];
+       right[2] = forward[1];
+
        d = DotProduct(forward, right);
        right[0] -= d * forward[0];
        right[1] -= d * forward[1];
@@ -465,18 +506,6 @@ void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
 /*-----------------------------------------------------------------*/
 
 
-float  anglemod(float a)
-{
-#if 0
-       if (a >= 0)
-               a -= 360*(int)(a/360);
-       else
-               a += 360*( 1 + (int)(-a/360) );
-#endif
-       a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
-       return a;
-}
-
 // LordHavoc note 1:
 // BoxOnPlaneSide did a switch on a 'signbits' value and had optimized
 // assembly in an attempt to accelerate it further, very inefficient
@@ -552,60 +581,119 @@ void BoxOnPlaneSideClassify(mplane_t *p)
        }
 }
 
+void PlaneClassify(mplane_t *p)
+{
+       if (p->normal[0] == 1)
+               p->type = 0;
+       else if (p->normal[1] == 1)
+               p->type = 1;
+       else if (p->normal[2] == 1)
+               p->type = 2;
+       else
+               p->type = 3;
+       BoxOnPlaneSideClassify(p);
+}
+
 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
 {
-       float           angle;
-       float           sr, sp, sy, cr, cp, cy;
-       
+       double angle, sr, sp, sy, cr, cp, cy;
+
        angle = angles[YAW] * (M_PI*2 / 360);
        sy = sin(angle);
        cy = cos(angle);
        angle = angles[PITCH] * (M_PI*2 / 360);
        sp = sin(angle);
        cp = cos(angle);
-       // LordHavoc: this is only to hush up gcc complaining about 'might be used uninitialized' variables
-       // (they are NOT used uninitialized, but oh well)
-       cr = 0;
-       sr = 0;
+       if (forward)
+       {
+               forward[0] = cp*cy;
+               forward[1] = cp*sy;
+               forward[2] = -sp;
+       }
        if (right || up)
        {
                angle = angles[ROLL] * (M_PI*2 / 360);
                sr = sin(angle);
                cr = cos(angle);
+               if (right)
+               {
+                       right[0] = -1*(sr*sp*cy+cr*-sy);
+                       right[1] = -1*(sr*sp*sy+cr*cy);
+                       right[2] = -1*(sr*cp);
+               }
+               if (up)
+               {
+                       up[0] = (cr*sp*cy+-sr*-sy);
+                       up[1] = (cr*sp*sy+-sr*cy);
+                       up[2] = cr*cp;
+               }
        }
+}
+
+void AngleVectorsFLU (vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
+{
+       double angle, sr, sp, sy, cr, cp, cy;
 
+       angle = angles[YAW] * (M_PI*2 / 360);
+       sy = sin(angle);
+       cy = cos(angle);
+       angle = angles[PITCH] * (M_PI*2 / 360);
+       sp = sin(angle);
+       cp = cos(angle);
        if (forward)
        {
                forward[0] = cp*cy;
                forward[1] = cp*sy;
                forward[2] = -sp;
        }
-       if (right)
-       {
-               right[0] = (-1*sr*sp*cy+-1*cr*-sy);
-               right[1] = (-1*sr*sp*sy+-1*cr*cy);
-               right[2] = -1*sr*cp;
-       }
-       if (up)
+       if (left || up)
        {
-               up[0] = (cr*sp*cy+-sr*-sy);
-               up[1] = (cr*sp*sy+-sr*cy);
-               up[2] = cr*cp;
+               angle = angles[ROLL] * (M_PI*2 / 360);
+               sr = sin(angle);
+               cr = cos(angle);
+               if (left)
+               {
+                       left[0] = sr*sp*cy+cr*-sy;
+                       left[1] = sr*sp*sy+cr*cy;
+                       left[2] = sr*cp;
+               }
+               if (up)
+               {
+                       up[0] = cr*sp*cy+-sr*-sy;
+                       up[1] = cr*sp*sy+-sr*cy;
+                       up[2] = cr*cp;
+               }
        }
 }
 
-int VectorCompare (vec3_t v1, vec3_t v2)
+void AngleMatrix (vec3_t angles, vec3_t translate, vec_t matrix[][4])
 {
-       int             i;
-       
-       for (i=0 ; i<3 ; i++)
-               if (v1[i] != v2[i])
-                       return 0;
-                       
-       return 1;
+       double angle, sr, sp, sy, cr, cp, cy;
+
+       angle = angles[YAW] * (M_PI*2 / 360);
+       sy = sin(angle);
+       cy = cos(angle);
+       angle = angles[PITCH] * (M_PI*2 / 360);
+       sp = sin(angle);
+       cp = cos(angle);
+       angle = angles[ROLL] * (M_PI*2 / 360);
+       sr = sin(angle);
+       cr = cos(angle);
+       matrix[0][0] = cp*cy;
+       matrix[0][1] = sr*sp*cy+cr*-sy;
+       matrix[0][2] = cr*sp*cy+-sr*-sy;
+       matrix[0][3] = translate[0];
+       matrix[1][0] = cp*sy;
+       matrix[1][1] = sr*sp*sy+cr*cy;
+       matrix[1][2] = cr*sp*sy+-sr*cy;
+       matrix[1][3] = translate[1];
+       matrix[2][0] = -sp;
+       matrix[2][1] = sr*cp;
+       matrix[2][2] = cr*cp;
+       matrix[2][3] = translate[2];
 }
 
-void VectorMA (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc)
+void VectorMASlow (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc)
 {
        vecc[0] = veca[0] + scale*vecb[0];
        vecc[1] = veca[1] + scale*vecb[1];
@@ -655,7 +743,7 @@ vec_t Length(vec3_t v)
 {
        int             i;
        float   length;
-       
+
        length = 0;
        for (i=0 ; i< 3 ; i++)
                length += v[i]*v[i];
@@ -664,6 +752,27 @@ vec_t Length(vec3_t v)
        return length;
 }
 
+/*
+// LordHavoc: fixme: do more research on gcc assembly so that qftol_minushalf and result will not be considered unused
+static double qftol_minushalf = -0.5;
+
+int qftol(double v)
+{
+       int result;
+#ifdef _MSC_VER
+       __asm
+       {
+               fld             v
+               fadd    qftol_minushalf
+               fistp   result
+       }
+#else // gcc hopefully
+       asm("fldl v\n\tfaddl qftol_minushalf\n\tfistpl result");
+#endif
+       return result;
+}
+*/
+
 // LordHavoc: renamed these to Length, and made the normal ones #define
 float VectorNormalizeLength (vec3_t v)
 {
@@ -705,14 +814,14 @@ float VectorNormalizeLength2 (vec3_t v, vec3_t dest) // LordHavoc: added to allo
 
 }
 
-void VectorInverse (vec3_t v)
+void _VectorInverse (vec3_t v)
 {
        v[0] = -v[0];
        v[1] = -v[1];
        v[2] = -v[2];
 }
 
-void VectorScale (vec3_t in, vec_t scale, vec3_t out)
+void _VectorScale (vec3_t in, vec_t scale, vec3_t out)
 {
        out[0] = in[0]*scale;
        out[1] = in[1]*scale;
@@ -842,3 +951,14 @@ int GreatestCommonDivisor (int i1, int i2)
                return GreatestCommonDivisor (i1, i2 % i1);
        }
 }
+
+
+void Mathlib_Init(void)
+{
+       int a;
+
+       // LordHavoc: setup 1.0f / N table for quick recipricols of integers
+       ixtable[0] = 0;
+       for (a = 1;a < 4096;a++)
+               ixtable[a] = 1.0f / a;
+}