2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
/*
|
|
|
|
* Trig.c
|
|
|
|
*
|
|
|
|
* Trig lookup tables
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allow frame header files to be singly included */
|
|
|
|
#define FRAME_LIB_INCLUDE
|
|
|
|
|
|
|
|
#include <assert.h>
|
2007-04-15 11:48:13 -07:00
|
|
|
#include <stdlib.h>
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "types.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "fractions.h"
|
|
|
|
#include "trig.h"
|
|
|
|
|
|
|
|
/* Number of steps between -1 and 1 for the inverse tables */
|
|
|
|
#define TRIG_ACCURACY 4096
|
|
|
|
#define TRIG_ACCMASK 0x0fff
|
|
|
|
|
|
|
|
/* Number of entries in sqrt table */
|
|
|
|
#define SQRT_ACCURACY 4096
|
|
|
|
#define SQRT_ACCBITS 12
|
|
|
|
|
|
|
|
|
2007-04-27 03:21:17 -07:00
|
|
|
static float aSin[TRIG_DEGREES];
|
|
|
|
static float aCos[TRIG_DEGREES];
|
|
|
|
static float aInvCos[TRIG_ACCURACY];
|
|
|
|
static float aInvSin[TRIG_ACCURACY];
|
|
|
|
static float aSqrt[SQRT_ACCURACY];
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* Initialise the Trig tables */
|
|
|
|
BOOL trigInitialise(void)
|
|
|
|
{
|
2007-04-27 03:21:17 -07:00
|
|
|
float val = 0.0, inc = 2.0 * M_PI / TRIG_DEGREES;
|
|
|
|
int count;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
// Initialise the tables
|
2007-04-27 03:21:17 -07:00
|
|
|
for (count = 0; count < TRIG_DEGREES; count++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-04-27 03:21:17 -07:00
|
|
|
aSin[count] = sinf(val);
|
|
|
|
aCos[count] = cosf(val);
|
2007-06-28 10:47:08 -07:00
|
|
|
val += inc;
|
|
|
|
}
|
2007-04-27 03:21:17 -07:00
|
|
|
inc = 2.0 / (TRIG_ACCURACY-1);
|
|
|
|
val = -1;
|
|
|
|
for (count = 0; count < TRIG_ACCURACY; count++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-04-27 03:21:17 -07:00
|
|
|
aInvSin[count] = asinf(val) * (float)TRIG_DEGREES / (2.0 * M_PI);
|
|
|
|
aInvCos[count] = acosf(val) * (float)TRIG_DEGREES / (2.0 * M_PI);
|
2007-06-28 10:47:08 -07:00
|
|
|
val += inc;
|
|
|
|
}
|
|
|
|
|
2007-04-27 03:21:17 -07:00
|
|
|
for (count = 0; count < SQRT_ACCURACY; count++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-04-27 03:21:17 -07:00
|
|
|
val = (float)count / (SQRT_ACCURACY / 2);
|
|
|
|
aSqrt[count]= sqrtf(val);
|
2006-10-07 09:40:18 -07:00
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Shutdown the trig tables */
|
|
|
|
void trigShutDown(void)
|
2007-04-27 03:21:17 -07:00
|
|
|
{}
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* Access the trig tables */
|
2007-04-27 03:21:17 -07:00
|
|
|
float trigSin(SDWORD angle)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
if (angle < 0)
|
|
|
|
{
|
|
|
|
angle = (-angle) % TRIG_DEGREES;
|
|
|
|
angle = TRIG_DEGREES - angle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
angle = angle % TRIG_DEGREES;
|
|
|
|
}
|
|
|
|
return aSin[angle % TRIG_DEGREES];
|
|
|
|
}
|
|
|
|
|
2007-04-27 03:21:17 -07:00
|
|
|
|
|
|
|
float trigCos(SDWORD angle)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
if (angle < 0)
|
|
|
|
{
|
|
|
|
angle = (-angle) % TRIG_DEGREES;
|
|
|
|
angle = TRIG_DEGREES - angle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
angle = angle % TRIG_DEGREES;
|
|
|
|
}
|
|
|
|
return aCos[angle % TRIG_DEGREES];
|
|
|
|
}
|
|
|
|
|
2007-04-27 03:21:17 -07:00
|
|
|
|
|
|
|
float trigInvSin(float val)
|
|
|
|
{
|
|
|
|
SDWORD index = (val+1) * (TRIG_ACCURACY-1) / 2;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
return aInvSin[index & TRIG_ACCMASK];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-27 03:21:17 -07:00
|
|
|
float trigInvCos(float val)
|
|
|
|
{
|
|
|
|
SDWORD index = (val+1) * (TRIG_ACCURACY-1) / 2;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
return aInvCos[index & TRIG_ACCMASK];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Fast lookup sqrt */
|
2007-04-27 03:21:17 -07:00
|
|
|
float trigIntSqrt(UDWORD val)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
UDWORD exp, mask;
|
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
{
|
2007-04-27 03:21:17 -07:00
|
|
|
return 0.0;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the exponent of the number
|
|
|
|
mask = 0x80000000; // set the msb in the mask
|
2007-04-27 03:21:17 -07:00
|
|
|
for(exp = 32; exp != 0; exp--)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
if (val & mask)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make all exponents even
|
|
|
|
// odd exponents result in a mantissa of [1..2) rather than [0..1)
|
|
|
|
if (exp & 1)
|
|
|
|
{
|
|
|
|
exp -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to shift the top bit to SQRT_BITS - left or right?
|
|
|
|
if (exp >= SQRT_ACCBITS)
|
|
|
|
{
|
|
|
|
val >>= exp - SQRT_ACCBITS + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val <<= SQRT_ACCBITS - 1 - exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now generate the fractional part for the lookup table
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( val < SQRT_ACCURACY,
|
|
|
|
"trigIntSqrt: aargh - table index out of range" );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-04-27 03:21:17 -07:00
|
|
|
return aSqrt[val] * (1 << (exp/2));
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|