189 lines
6.0 KiB
C
189 lines
6.0 KiB
C
/*
|
|
Copyright (C) 2002-2007 Victor Luchits
|
|
|
|
This program 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.
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
// r_math.c
|
|
|
|
#include "r_local.h"
|
|
#include "r_math.h"
|
|
#include "mathlib.h"
|
|
|
|
/*
|
|
====================
|
|
CalcFov
|
|
====================
|
|
*/
|
|
float CalcFov( float fov_x, float width, float height )
|
|
{
|
|
float x, half_fov_y;
|
|
|
|
if( fov_x < 1 || fov_x > 179 )
|
|
{
|
|
MsgDev( D_ERROR, "CalcFov: bad fov %g!\n", fov_x );
|
|
fov_x = 90;
|
|
}
|
|
|
|
x = width / tan( DEG2RAD( fov_x ) * 0.5f );
|
|
half_fov_y = atan( height / x );
|
|
return RAD2DEG( half_fov_y ) * 2;
|
|
}
|
|
|
|
/*
|
|
====================
|
|
AdjustFov
|
|
====================
|
|
*/
|
|
void AdjustFov( float *fov_x, float *fov_y, float width, float height, bool lock_x )
|
|
{
|
|
float x, y;
|
|
|
|
if( width * 3 == 4 * height || width * 4 == height * 5 )
|
|
{
|
|
// 4:3 or 5:4 ratio
|
|
return;
|
|
}
|
|
|
|
if( lock_x )
|
|
{
|
|
*fov_y = 2 * atan((width * 3) / (height * 4) * tan (*fov_y * M_PI / 360.0 * 0.5) ) * 360 / M_PI;
|
|
return;
|
|
}
|
|
|
|
y = CalcFov( *fov_x, 640, 480 );
|
|
x = *fov_x;
|
|
|
|
*fov_x = CalcFov( y, height, width );
|
|
if( *fov_x < x ) *fov_x = x;
|
|
else *fov_y = y;
|
|
}
|
|
|
|
/*
|
|
=================
|
|
CategorizePlane
|
|
|
|
A slightly more complex version of SignbitsForPlane and PlaneTypeForNormal,
|
|
which also tries to fix possible floating point glitches (like -0.00000 cases)
|
|
=================
|
|
*/
|
|
void CategorizePlane( cplane_t *plane )
|
|
{
|
|
int i;
|
|
|
|
plane->signbits = 0;
|
|
plane->type = PLANE_NONAXIAL;
|
|
|
|
for( i = 0; i < 3; i++ )
|
|
{
|
|
if( plane->normal[i] < 0 )
|
|
{
|
|
plane->signbits |= 1<<i;
|
|
if( plane->normal[i] == -1.0f )
|
|
{
|
|
plane->signbits = (1<<i);
|
|
plane->normal[0] = plane->normal[1] = plane->normal[2] = 0;
|
|
plane->normal[i] = -1.0f;
|
|
break;
|
|
}
|
|
}
|
|
else if( plane->normal[i] == 1.0f )
|
|
{
|
|
plane->type = i;
|
|
plane->signbits = 0;
|
|
plane->normal[0] = plane->normal[1] = plane->normal[2] = 0;
|
|
plane->normal[i] = 1.0f;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
==============
|
|
BoxOnPlaneSide (engine fast version)
|
|
|
|
Returns SIDE_FRONT, SIDE_BACK, or SIDE_ON
|
|
==============
|
|
*/
|
|
int BoxOnPlaneSide( const vec3_t emins, const vec3_t emaxs, const cplane_t *p )
|
|
{
|
|
if( p->type < 3 ) return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
|
|
switch( p->signbits )
|
|
{
|
|
default:
|
|
case 0: return (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) < p->dist) << 1));
|
|
case 1: return (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) < p->dist) << 1));
|
|
case 2: return (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) < p->dist) << 1));
|
|
case 3: return (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) < p->dist) << 1));
|
|
case 4: return (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
|
|
case 5: return (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
|
|
case 6: return (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
|
|
case 7: return (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
|
|
}
|
|
}
|
|
|
|
/*
|
|
====================
|
|
RotatePointAroundVector
|
|
====================
|
|
*/
|
|
void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
|
|
{
|
|
float t0, t1;
|
|
float angle, c, s;
|
|
vec3_t vr, vu, vf;
|
|
|
|
angle = DEG2RAD( degrees );
|
|
com.sincos( angle, &s, &c );
|
|
VectorCopy( dir, vf );
|
|
VectorVectors( vf, vr, vu );
|
|
|
|
t0 = vr[0] * c + vu[0] * -s;
|
|
t1 = vr[0] * s + vu[0] * c;
|
|
dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
|
|
+ (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
|
|
+ (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
|
|
|
|
t0 = vr[1] * c + vu[1] * -s;
|
|
t1 = vr[1] * s + vu[1] * c;
|
|
dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
|
|
+ (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
|
|
+ (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
|
|
|
|
t0 = vr[2] * c + vu[2] * -s;
|
|
t1 = vr[2] * s + vu[2] * c;
|
|
dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
|
|
+ (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
|
|
+ (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
|
|
}
|
|
|
|
|
|
/*
|
|
=================
|
|
PlaneFromPoints
|
|
=================
|
|
*/
|
|
void PlaneFromPoints( vec3_t verts[3], cplane_t *plane )
|
|
{
|
|
vec3_t v1, v2;
|
|
|
|
VectorSubtract( verts[1], verts[0], v1 );
|
|
VectorSubtract( verts[2], verts[0], v2 );
|
|
CrossProduct( v2, v1, plane->normal );
|
|
VectorNormalize( plane->normal );
|
|
plane->dist = DotProduct( verts[0], plane->normal );
|
|
} |