2009-07-12 22:00:00 +02:00
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( fov_x < 1 || fov_x > 179 )
|
|
|
|
Host_Error( "bad fov: %f\n", fov_x );
|
|
|
|
|
|
|
|
x = width / tan( fov_x / 360 * M_PI );
|
|
|
|
|
|
|
|
return atan( height / x ) * 360 / M_PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-02 22:00:00 +02:00
|
|
|
/*
|
|
|
|
=================
|
2009-10-28 22:00:00 +01:00
|
|
|
CategorizePlane
|
2009-08-02 22:00:00 +02:00
|
|
|
|
2009-10-28 22:00:00 +01:00
|
|
|
A slightly more complex version of SignbitsForPlane and PlaneTypeForNormal,
|
|
|
|
which also tries to fix possible floating point glitches (like -0.00000 cases)
|
2009-08-02 22:00:00 +02:00
|
|
|
=================
|
|
|
|
*/
|
2009-10-28 22:00:00 +01:00
|
|
|
void CategorizePlane( cplane_t *plane )
|
2009-08-02 22:00:00 +02:00
|
|
|
{
|
2009-10-28 22:00:00 +01:00
|
|
|
int i;
|
2009-08-02 22:00:00 +02:00
|
|
|
|
2009-10-28 22:00:00 +01:00
|
|
|
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));
|
|
|
|
}
|
2009-08-02 22:00:00 +02:00
|
|
|
}
|
|
|
|
|
2009-07-12 22:00:00 +02:00
|
|
|
/*
|
|
|
|
=================
|
|
|
|
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 );
|
|
|
|
}
|