/
redgpu
/
GDMagArchive
Обзор
Документация
Войти
/
redgpu
/
GDMagArchive
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sep02/lander/MathDefs.cpp
373 строки
11 KB
Don Williamson
first commit
31 окт 2016, 17:03
31 окт 2016, 17:03
c823e7b
Код
Авторство
О чём код?
/////////////////////////////////////////////////////////////////////////////// // // MathDefs.c : implementation file // // Purpose: Implementation of Math Routines // Includes class implementations for the Vector class // // Created: // JL 2/18/98 // Revisions: // JL 8/18/00 Modified for use in physics projects // /////////////////////////////////////////////////////////////////////////////// #include <math.h> #include "mathdefs.h" /////////////////////////////////////////////////////////////////////////////// // Function: IdentityMatrix // Purpose: Creates and Identity 4x4 Matrix in OpenGL Format // Arguments: Matrix // Notes: This routing is tweaked to handle OpenGLs column-major format // This is one obvious place for optimization perhaps asm code /////////////////////////////////////////////////////////////////////////////// void IdentityMatrix(tMatrix *mat) { ///// Local Variables ///////////////////////////////////////////////////////// int loop; /////////////////////////////////////////////////////////////////////////////// for (loop = 0; loop < 16; loop++) mat->m[loop] = 0.0f; mat->m[0] = mat->m[5] = mat->m[10] = mat->m[15] = 1.0f; } //// IdentityMatrix /////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // CVector Class ////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Function: Construction // Purpose: Initialize the vector to 0 /////////////////////////////////////////////////////////////////////////////// CVector::CVector() { x = y = z = 0; } /////////////////////////////////////////////////////////////////////////////// // Function: Destruction // Purpose: Destroys the vector /////////////////////////////////////////////////////////////////////////////// CVector::~CVector() { } /////////////////////////////////////////////////////////////////////////////// // Function: equals operator // Purpose: Sets the current vector to equal CVector // Arguments: CVector /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator=( CVector arg ) { x = arg.x; y = arg.y; z = arg.z; return *this; } /////////////////////////////////////////////////////////////////////////////// // Function: equals operator // Purpose: Sets the current vector to equal list of floats // Arguments: float * /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator=( float *arg ) { x = arg[0]; y = arg[1]; z = arg[2]; return *this; } /////////////////////////////////////////////////////////////////////////////// // Function: addition operator // Purpose: Adds a CVector to the current vector // Arguments: CVector /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator+( CVector arg ) { CVector v; v.x = x + arg.x; v.y = y + arg.y; v.z = z + arg.z; return v; } /////////////////////////////////////////////////////////////////////////////// // Function: addition operator // Purpose: Adds a CVector to the current vector // Arguments: CVector /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator+=( CVector arg ) { x += arg.x; y += arg.y; z += arg.z; return *this; } /////////////////////////////////////////////////////////////////////////////// // Function: subtraction operator // Purpose: Subtracts a CVector from the current vector // Arguments: CVector /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator-( CVector arg ) { CVector v; v.x = x - arg.x; v.y = y - arg.y; v.z = z - arg.z; return v; } /////////////////////////////////////////////////////////////////////////////// // Function: subtraction operator // Purpose: Subtracts a CVector from the current vector // Arguments: CVector /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator-=( CVector v ) { x -= v.x; y -= v.y; z -= v.z; return *this; } /////////////////////////////////////////////////////////////////////////////// // Function: multiplication operator // Purpose: Multiply a CVector by float // Arguments: float /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator*( float arg ) { CVector v; v.x = x * arg; v.y = y * arg; v.z = z * arg; return v; } /////////////////////////////////////////////////////////////////////////////// // Function: division operator // Purpose: Divides a CVector by float // Arguments: float /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator/( float arg ) { CVector v; v.x = x / arg; v.y = y / arg; v.z = z / arg; return v; } /////////////////////////////////////////////////////////////////////////////// // Function: multiplication operator // Purpose: Multiply a CVector by float // Arguments: float /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator*=( float arg ) { CVector v; x *= arg; y *= arg; z *= arg; return *this; } /////////////////////////////////////////////////////////////////////////////// // Function: division operator // Purpose: Divides a CVector by float // Arguments: float /////////////////////////////////////////////////////////////////////////////// CVector CVector::operator/=( float arg ) { CVector v; x /= arg; y /= arg; z /= arg; return *this; } /////////////////////////////////////////////////////////////////////////////// // Function: VectorSquaredDistance // Purpose: Returns the squared distance between two vector // Arguments: CVector * /////////////////////////////////////////////////////////////////////////////// float CVector::VectorSquaredDistance(CVector *v) { return( ((x - v->x) * (x - v->x)) + ((y - v->y) * (y - v->y)) + ((z - v->z) * (z - v->z)) ); } /////////////////////////////////////////////////////////////////////////////// // Function: VectorDistance // Purpose: Returns the distance between two vector // Arguments: CVector * /////////////////////////////////////////////////////////////////////////////// float CVector::VectorDistance(CVector *v) { return((float)sqrt(VectorSquaredDistance(v))); } /////////////////////////////////////////////////////////////////////////////// // Function: SquaredLength // Purpose: Returns the squared length of the vector // Arguments: none /////////////////////////////////////////////////////////////////////////////// float CVector::SquaredLength() { return((x * x) + (y * y) + (z * z)); } /////////////////////////////////////////////////////////////////////////////// // Function: Length // Purpose: Returns the length of the vector // Arguments: none /////////////////////////////////////////////////////////////////////////////// float CVector::Length() { return((float)sqrt(SquaredLength())); } /////////////////////////////////////////////////////////////////////////////// // Function: NormalizeVector // Purpose: Normalizes the class vector // Arguments: none /////////////////////////////////////////////////////////////////////////////// void CVector::NormalizeVector() { float len = Length(); if (len != 0.0) { x /= len; y /= len; z /= len; } } /////////////////////////////////////////////////////////////////////////////// // Function: Lerp // Purpose: Interpolates between vector and another by a factor // Arguments: Vector to lerp to and factor /////////////////////////////////////////////////////////////////////////////// void CVector::Lerp(CVector *v1, float factor) { x = x * (1.0f - factor) + v1->x * factor; y = y * (1.0f - factor) + v1->y * factor; z = z * (1.0f - factor) + v1->z * factor; } /////////////////////////////////////////////////////////////////////////////// // Function: CrossProduct // Purpose: Sets the vector to equal the cross product of two vectors // Arguments: CVector * /////////////////////////////////////////////////////////////////////////////// void CVector::CrossProduct(CVector *v1, CVector *v2) { x = (v1->y * v2->z) - (v1->z * v2->y); y = (v1->z * v2->x) - (v1->x * v2->z); z = (v1->x * v2->y) - (v1->y * v2->x); } /////////////////////////////////////////////////////////////////////////////// // Function: Dot // Purpose: Return the dot product of two vectors // Arguments: CVector * /////////////////////////////////////////////////////////////////////////////// float CVector::Dot(CVector *v) { return ((x * v->x) + (y * v->y) + (z * v->z)); } /////////////////////////////////////////////////////////////////////////////// // Function: Dot // Purpose: Return the dot product of two vectors // Arguments: vector elements /////////////////////////////////////////////////////////////////////////////// float CVector::Dot(float vx, float vy, float vz) { return ((x * vx) + (y * vy) + (z * vz)); } /////////////////////////////////////////////////////////////////////////////// // Function: MultVectorByMatrix // Purpose: Multiplies a vector by a 4x4 Matrix in OpenGL Format // Arguments: Matrix // Notes: This routing is tweaked to handle OpenGLs column-major format // This is one obvious place for optimization perhaps asm code /////////////////////////////////////////////////////////////////////////////// void CVector::MultVectorByMatrix(float *mat) { CVector result; result.x = (mat[0] * x) + (mat[4] * y) + (mat[8] * z) + mat[12]; result.y = (mat[1] * x) + (mat[5] * y) + (mat[9] * z) + mat[13]; result.z = (mat[2] * x) + (mat[6] * y) + (mat[10] * z) + mat[14]; x = result.x; y = result.y; z = result.z; } //// MultVectorByMatrix ////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // Function: MultVectorByRotMatrix // Purpose: Multiplies a vector by a rotation only 4x4 Matrix in OpenGL Format // Arguments: Matrix // Notes: This routing is tweaked to handle OpenGLs column-major format // This is one obvious place for optimization perhaps asm code /////////////////////////////////////////////////////////////////////////////// void CVector::MultVectorByRotMatrix(float *mat) { CVector result; result.x = (mat[0] * x) + (mat[4] * y) + (mat[8] * z); result.y = (mat[1] * x) + (mat[5] * y) + (mat[9] * z); result.z = (mat[2] * x) + (mat[6] * y) + (mat[10] * z); x = result.x; y = result.y; z = result.z; } //// MultVectorByRotMatrix ////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // End of CVector Class //////////////////////////////////////////////////////////////////////