Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
5.9 KiB

//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
9 years ago
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include <memory.h>
#include "hud.h"
#include "cl_util.h"
#include "const.h"
#include "com_model.h"
#include "studio_util.h"
/*
====================
AngleMatrix
====================
*/
9 years ago
void AngleMatrix( const float *angles, float (*matrix)[4] )
9 years ago
{
9 years ago
float angle;
float sr, sp, sy, cr, cp, cy;
angle = angles[YAW] * ( M_PI_F * 2.0f / 360.0f );
9 years ago
sy = sin( angle );
cy = cos( angle );
angle = angles[PITCH] * ( M_PI_F * 2.0f / 360.0f );
9 years ago
sp = sin( angle );
cp = cos( angle );
angle = angles[ROLL] * ( M_PI_F * 2.0f / 360.0f );
9 years ago
sr = sin( angle );
cr = cos( angle );
9 years ago
// matrix = (YAW * PITCH) * ROLL
9 years ago
matrix[0][0] = cp * cy;
matrix[1][0] = cp * sy;
9 years ago
matrix[2][0] = -sp;
9 years ago
matrix[0][1] = sr * sp * cy + cr * -sy;
matrix[1][1] = sr * sp * sy + cr * cy;
matrix[2][1] = sr * cp;
matrix[0][2] = (cr * sp * cy + -sr * -sy);
matrix[1][2] = (cr * sp * sy + -sr* cy);
matrix[2][2] = cr * cp;
matrix[0][3] = 0.0f;
matrix[1][3] = 0.0f;
matrix[2][3] = 0.0f;
9 years ago
}
/*
====================
VectorCompare
====================
*/
9 years ago
int VectorCompare( const float *v1, const float *v2 )
9 years ago
{
9 years ago
int i;
for( i = 0; i < 3; i++ )
if( v1[i] != v2[i] )
9 years ago
return 0;
9 years ago
9 years ago
return 1;
}
/*
====================
CrossProduct
====================
*/
9 years ago
void CrossProduct( const float *v1, const float *v2, float *cross )
9 years ago
{
cross[0] = v1[1] * v2[2] - v1[2] * v2[1];
cross[1] = v1[2] * v2[0] - v1[0] * v2[2];
cross[2] = v1[0] * v2[1] - v1[1] * v2[0];
9 years ago
}
/*
====================
VectorTransform
====================
*/
9 years ago
void VectorTransform( const float *in1, float in2[3][4], float *out )
9 years ago
{
out[0] = DotProduct(in1, in2[0]) + in2[0][3];
out[1] = DotProduct(in1, in2[1]) + in2[1][3];
out[2] = DotProduct(in1, in2[2]) + in2[2][3];
}
/*
================
ConcatTransforms
================
*/
9 years ago
void ConcatTransforms( float in1[3][4], float in2[3][4], float out[3][4] )
9 years ago
{
out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
in1[0][2] * in2[2][0];
out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
in1[0][2] * in2[2][1];
out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
in1[0][2] * in2[2][2];
out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] +
in1[0][2] * in2[2][3] + in1[0][3];
out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
in1[1][2] * in2[2][0];
out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
in1[1][2] * in2[2][1];
out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
in1[1][2] * in2[2][2];
out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] +
in1[1][2] * in2[2][3] + in1[1][3];
out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
in1[2][2] * in2[2][0];
out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
in1[2][2] * in2[2][1];
out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
in1[2][2] * in2[2][2];
out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] +
in1[2][2] * in2[2][3] + in1[2][3];
}
// angles index are not the same as ROLL, PITCH, YAW
/*
====================
AngleQuaternion
====================
*/
void AngleQuaternion( float *angles, vec4_t quaternion )
{
9 years ago
float angle;
float sr, sp, sy, cr, cp, cy;
9 years ago
// FIXME: rescale the inputs to 1/2 angle
angle = angles[2] * 0.5f;
9 years ago
sy = sin( angle );
cy = cos( angle );
angle = angles[1] * 0.5f;
9 years ago
sp = sin( angle );
cp = cos( angle );
angle = angles[0] * 0.5f;
9 years ago
sr = sin( angle );
cr = cos( angle );
9 years ago
9 years ago
quaternion[0] = sr * cp * cy - cr * sp * sy; // X
quaternion[1] = cr * sp * cy + sr * cp * sy; // Y
quaternion[2] = cr * cp * sy - sr * sp * cy; // Z
quaternion[3] = cr * cp * cy + sr * sp * sy; // W
9 years ago
}
/*
====================
QuaternionSlerp
====================
*/
void QuaternionSlerp( vec4_t p, vec4_t q, float t, vec4_t qt )
{
int i;
9 years ago
float omega, cosom, sinom, sclp, sclq;
9 years ago
// decide if one of the quaternions is backwards
float a = 0;
float b = 0;
9 years ago
for( i = 0; i < 4; i++ )
9 years ago
{
9 years ago
a += ( p[i] - q[i] ) * ( p[i] - q[i] );
b += ( p[i] + q[i] ) * ( p[i] + q[i] );
9 years ago
}
9 years ago
if(a > b)
9 years ago
{
9 years ago
for( i = 0; i < 4; i++ )
9 years ago
{
q[i] = -q[i];
}
}
9 years ago
cosom = p[0] * q[0] + p[1] * q[1] + p[2] * q[2] + p[3] * q[3];
9 years ago
if( ( 1.0f + cosom ) > 0.000001f )
9 years ago
{
if( ( 1.0f - cosom ) > 0.000001f )
9 years ago
{
omega = acos( cosom );
sinom = sin( omega );
sclp = sin( ( 1.0f - t ) * omega ) / sinom;
9 years ago
sclq = sin( t * omega ) / sinom;
9 years ago
}
else
{
sclp = 1.0f - t;
9 years ago
sclq = t;
}
9 years ago
for( i = 0; i < 4; i++ )
{
9 years ago
qt[i] = sclp * p[i] + sclq * q[i];
}
}
else
{
qt[0] = -q[1];
qt[1] = q[0];
qt[2] = -q[3];
qt[3] = q[2];
sclp = sin( ( 1.0f - t ) * ( 0.5f * M_PI_F ) );
sclq = sin( t * ( 0.5f * M_PI_F ) );
9 years ago
for( i = 0; i < 3; i++ )
9 years ago
{
qt[i] = sclp * p[i] + sclq * qt[i];
}
}
}
/*
====================
QuaternionMatrix
====================
*/
void QuaternionMatrix( vec4_t quaternion, float (*matrix)[4] )
{
matrix[0][0] = 1.0f - 2.0f * quaternion[1] * quaternion[1] - 2.0f * quaternion[2] * quaternion[2];
matrix[1][0] = 2.0f * quaternion[0] * quaternion[1] + 2.0f * quaternion[3] * quaternion[2];
matrix[2][0] = 2.0f * quaternion[0] * quaternion[2] - 2.0f * quaternion[3] * quaternion[1];
9 years ago
matrix[0][1] = 2.0f * quaternion[0] * quaternion[1] - 2.0f * quaternion[3] * quaternion[2];
matrix[1][1] = 1.0f - 2.0f * quaternion[0] * quaternion[0] - 2.0f * quaternion[2] * quaternion[2];
matrix[2][1] = 2.0f * quaternion[1] * quaternion[2] + 2.0f * quaternion[3] * quaternion[0];
9 years ago
matrix[0][2] = 2.0f * quaternion[0] * quaternion[2] + 2.0f * quaternion[3] * quaternion[1];
matrix[1][2] = 2.0f * quaternion[1] * quaternion[2] - 2.0f * quaternion[3] * quaternion[0];
matrix[2][2] = 1.0f - 2.0f * quaternion[0] * quaternion[0] - 2.0f * quaternion[1] * quaternion[1];
9 years ago
}
/*
====================
MatrixCopy
====================
*/
void MatrixCopy( float in[3][4], float out[3][4] )
{
memcpy( out, in, sizeof( float ) * 3 * 4 );
9 years ago
}