📄 utils3d.cpp
字号:
/*
* ==============================================================================
* Name : Utils3d.cpp
*
* Copyright (c) 2004-2006 Nokia Corporation.
* This material, including documentation and any related
* computer programs, is protected by copyright controlled by
* Nokia Corporation.
* ==============================================================================
*/
// INCLUDE FILES
#include "Utils3d.h"
#include <eikapp.h>
#include <eikappui.h>
// CONSTANTS
// =============================================================================
// TVector
// =============================================================================
// ============================= MEMBER FUNCTIONS ==============================
// -----------------------------------------------------------------------------
// TVector::TVector
// C++ default constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
TVector::TVector()
{
*this = TVector(0, 0, 0);
}
// -----------------------------------------------------------------------------
// TVector::TVector
// C++ constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
TVector::TVector(GLfloat aX, GLfloat aY, GLfloat aZ )
: iX(aX), iY(aY), iZ(aZ)
{
}
// -----------------------------------------------------------------------------
// TVector::operator +
// Return a new vector with sum of this and rhs vector
// -----------------------------------------------------------------------------
TVector TVector::operator + (TVector aVector) const
{
return TVector(this->iX + aVector.iX, this->iY + aVector.iY,
this->iZ + aVector.iZ);
}
// -----------------------------------------------------------------------------
// TVector::operator +=
// Component-wise addition of vectors.
// -----------------------------------------------------------------------------
void TVector::operator += (TVector aVector)
{
*this = *this + aVector;
}
// -----------------------------------------------------------------------------
// TVector::operator -
// Calculate the difference of this, and rhs vector.
// -----------------------------------------------------------------------------
TVector TVector::operator - (TVector aVector) const
{
return (*this + (aVector * - 1));
}
// -----------------------------------------------------------------------------
// TVector::operator -
// Unary - == negation operator. Ineffective implementation, uses multiplication
// with -1.
// -----------------------------------------------------------------------------
TVector TVector::operator - () const
{
return ((*this) * (-1));
}
// -----------------------------------------------------------------------------
// TVector::operator *
// Calculate the scalar multiplication, i.e. dot product, of this and rhs
// vector.
// -----------------------------------------------------------------------------
GLfloat TVector::operator * (TVector aVector) const
{
return (this->iX * aVector.iX + this->iY * aVector.iY +
this->iZ * aVector.iZ);
}
// -----------------------------------------------------------------------------
// TVector::operator *
// Calculate the multiplication of this vector and a scalar rhs
// -----------------------------------------------------------------------------
TVector TVector::operator * (GLfloat aScalar) const
{
return TVector(this->iX * aScalar, this->iY * aScalar, this->iZ * aScalar);
}
// -----------------------------------------------------------------------------
// TVector::Magnitude
// Calculate the magnitude of this vector. Standard trigonometric calculation:
// sqrt(x**2 + y**2 + z**2)
// -----------------------------------------------------------------------------
GLfloat TVector::Magnitude()
{
return sqrt( (*this) * (*this) );
}
// -----------------------------------------------------------------------------
// TVector::Normalize
// Normalizes the vector by dividing each component with the length of
// the vector.
// -----------------------------------------------------------------------------
void TVector::Normalize()
{
_LIT(KNormPanic, "TVector::Normalize()");
GLfloat magnitude = Magnitude();
if ( magnitude == 0.0f )
{
User::Panic( KNormPanic, 0 );
}
(*this) = (*this) * (1 / magnitude);
}
// -----------------------------------------------------------------------------
// TVector::MultMatrix4x4
// Computes the multiplication of this vector with the given matrix.
// Basically transforms the this vector with the given transformation matrix.
// -----------------------------------------------------------------------------
void TVector::MultMatrix4x4( const GLfloat aMatrix[] )
{
GLfloat iXOrig = iX;
GLfloat iYOrig = iY;
GLfloat iZOrig = iZ;
iX = ( iXOrig * aMatrix[0] ) + ( iYOrig * aMatrix[1] ) + ( iZOrig * aMatrix[ 2] ) + aMatrix[3];
iY = ( iXOrig * aMatrix[4] ) + ( iYOrig * aMatrix[5] ) + ( iZOrig * aMatrix[ 6] ) + aMatrix[7];
iZ = ( iXOrig * aMatrix[8] ) + ( iYOrig * aMatrix[9] ) + ( iZOrig * aMatrix[10] ) + aMatrix[11];
}
// -----------------------------------------------------------------------------
// TVector::MultMatrix4x4
// Computes the multiplication of given vector with the given matrix.
// Basically transforms the vector with the given transformation matrix.
// -----------------------------------------------------------------------------
TVector TVector::MultMatrix4x4( const TVector aVector, const GLfloat aMatrix[] )
{
TVector vector;
vector.iX = ( aVector.iX * aMatrix[0] ) + ( aVector.iY * aMatrix[1] ) + ( aVector.iZ * aMatrix[ 2] ) + aMatrix[3];
vector.iY = ( aVector.iX * aMatrix[4] ) + ( aVector.iY * aMatrix[5] ) + ( aVector.iZ * aMatrix[ 6] ) + aMatrix[7];
vector.iZ = ( aVector.iX * aMatrix[8] ) + ( aVector.iY * aMatrix[9] ) + ( aVector.iZ * aMatrix[10] ) + aMatrix[11];
return vector;
}
// -----------------------------------------------------------------------------
// TVector::CrossProduct
// Direct implementation of the cross-product calculation.
// -----------------------------------------------------------------------------
TVector TVector::CrossProduct(const TVector aVector1, const TVector aVector2)
{
TVector Vector;
Vector.iX = aVector1.iY * aVector2.iZ - aVector1.iZ * aVector2.iY;
Vector.iY = aVector1.iZ * aVector2.iX - aVector1.iX * aVector2.iZ;
Vector.iZ = aVector1.iX * aVector2.iY - aVector1.iY * aVector2.iX;
return Vector;
}
// =============================================================================
// TVectorx: Fixed-point vector class
// =============================================================================
// ============================= MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// TVectorx::TVectorx
// C++ default constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
//
TVectorx::TVectorx()
{
*this = TVectorx(INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(0) );
}
// -----------------------------------------------------------------------------
// TVectorx::TVectorx
// C++ constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
//
TVectorx::TVectorx(GLfixed aX, GLfixed aY, GLfixed aZ )
: iX(aX), iY(aY), iZ(aZ)
{
}
// -----------------------------------------------------------------------------
// TVectorx::operator +
// Return a new vector with sum of this and rhs vector
// -----------------------------------------------------------------------------
//
TVectorx TVectorx::operator + (TVectorx aVector) const
{
return TVectorx(this->iX + aVector.iX, this->iY + aVector.iY,
this->iZ + aVector.iZ);
}
// -----------------------------------------------------------------------------
// TVectorx::operator +=
// Component-wise addition of vectors.
// -----------------------------------------------------------------------------
void TVectorx::operator += (TVectorx aVector)
{
*this = *this + aVector;
}
// -----------------------------------------------------------------------------
// TVector::operator -
// Calculate the difference of this, and rhs vector.
// -----------------------------------------------------------------------------
TVectorx TVectorx::operator - (TVectorx aVector) const
{
return (*this + (aVector * INT_2_FIXED(-1)));
}
// -----------------------------------------------------------------------------
// TVector::operator -
// Unary - == negation operator. Ineffective implementation, uses multiplication
// with -1.
// -----------------------------------------------------------------------------
TVectorx TVectorx::operator - () const
{
return ((*this) * INT_2_FIXED(-1));
}
// -----------------------------------------------------------------------------
// TVector::operator *
// Calculate the scalar multiplication, i.e. dot product, of this and rhs
// vector.
// -----------------------------------------------------------------------------
GLfixed TVectorx::operator * (TVectorx aVector) const
{
return (fixedMul(this->iX, aVector.iX) + fixedMul(this->iY, aVector.iY) +
fixedMul(this->iZ, aVector.iZ));
}
// -----------------------------------------------------------------------------
// TVector::operator *
// Calculate the multiplication of this vector and a scalar rhs
// -----------------------------------------------------------------------------
//
TVectorx TVectorx::operator * (GLfixed aScalar) const
{
return TVectorx( fixedMul( this->iX, aScalar ),
fixedMul( this->iY, aScalar ),
fixedMul( this->iZ, aScalar ));
}
// -----------------------------------------------------------------------------
// TVector::Magnitude
// Calculate the magnitude of this vector. Standard trigonometric calculation:
// sqrt(x**2 + y**2 + z**2)
// -----------------------------------------------------------------------------
//
GLfixed TVectorx::Magnitude() const
{
return FLOAT_2_FIXED( sqrt( FIXED_2_FLOAT( (*this) * (*this) ) ) );
}
// -----------------------------------------------------------------------------
// TVector::Normalize
// Normalizes the vector by dividing each component with the length of
// the vector.
// -----------------------------------------------------------------------------
//
void TVectorx::Normalize()
{
_LIT(KNormPanic, "TVectorx::Normalize()");
GLfixed magnitude = Magnitude();
if ( magnitude == INT_2_FIXED(0) )
{
User::Panic( KNormPanic, 0 );
}
(*this) = (*this) * FLOAT_2_FIXED(1 / FIXED_2_FLOAT(magnitude));
}
// -----------------------------------------------------------------------------
// TVector::CrossProduct
// Direct implementation of the cross-product calculation.
// -----------------------------------------------------------------------------
//
TVectorx TVectorx::CrossProduct(TVectorx aVector1, TVectorx aVector2)
{
TVectorx Vector;
Vector.iX = fixedMul(aVector1.iY, aVector2.iZ) -
fixedMul(aVector1.iZ, aVector2.iY);
Vector.iY = fixedMul(aVector1.iZ, aVector2.iX) -
fixedMul(aVector1.iX, aVector2.iZ);
Vector.iZ = fixedMul(aVector1.iX, aVector2.iY) -
fixedMul(aVector1.iY, aVector2.iX);
return Vector;
}
// =============================================================================
// T3DModel
// =============================================================================
// ============================= MEMBER FUNCTIONS ==============================
// -----------------------------------------------------------------------------
// T3DModel::T3DModel
// C++ default constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a T3DModel to Position (0,0,0), with orientation
// [Yaw=0, Pitch=0, Roll=0].
// -----------------------------------------------------------------------------
//
T3DModel::T3DModel()
{
*this = T3DModel(TVector(0.0f, 0.0f, 0.0f), 0.0f, 0.0f, 0.0f);
}
// -----------------------------------------------------------------------------
// T3DModel::T3DModel
// C++ constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a T3DModel to position aPosition, with
// orientation [aYaw, aPitch, aRoll].
// -----------------------------------------------------------------------------
//
T3DModel::T3DModel(TVector aPosition, GLfloat aYaw, GLfloat aPitch,
GLfloat aRoll)
{
SetPosition(aPosition);
SetYaw(aYaw);
SetPitch(aPitch);
SetRoll(aRoll);
}
// -----------------------------------------------------------------------------
// T3DModel::MakeWorldViewMatrix
// Sets up a world + a view matrix.
// -----------------------------------------------------------------------------
//
void T3DModel::MakeWorldViewMatrix(TCamera & aCamera)
{
MakeWorldViewMatrix( aCamera, GetPosition(), GetYaw(), GetPitch(),
GetRoll());
}
// -----------------------------------------------------------------------------
// T3DModel::MakeWorldViewMatrix
// Sets up a world + a view matrix.
// -----------------------------------------------------------------------------
//
void T3DModel::MakeWorldViewMatrix(TCamera & aCamera, TVector aPosition,
GLfloat aYaw, GLfloat aPitch, GLfloat aRoll)
{
glMultMatrixf( (GLfloat*)aCamera.GetViewMatrix() );
glTranslatef( aPosition.iX-aCamera.iPosition.iX,
aPosition.iY-aCamera.iPosition.iY, aPosition.iZ-aCamera.iPosition.iZ );
if ( aRoll )
{
glRotatef( aRoll , 0, 0, 1);
}
if ( aYaw )
{
glRotatef( aYaw , 0, 1, 0);
}
if ( aPitch )
{
glRotatef( aPitch, 1, 0, 0);
}
}
// -----------------------------------------------------------------------------
// T3DModel::MakeBillboardWorldViewMatrix
// Sets up a billboard matrix, which is a matrix that rotates objects in such a
// way that they always face the camera.
// Refer to the billboard example to see how this method is used.
// -----------------------------------------------------------------------------
//
void T3DModel::MakeBillboardWorldViewMatrix(TCamera & aCamera)
{
MakeBillboardWorldViewMatrix( aCamera, GetPosition() );
}
// -----------------------------------------------------------------------------
// T3DModel::MakeBillboardWorldViewMatrix
// Sets up a billboard matrix, which is a matrix that rotates objects in such a
// way that they always face the camera.
// Refer to the billboard example to see how this method is used.
// -----------------------------------------------------------------------------
//
void T3DModel::MakeBillboardWorldViewMatrix(TCamera & aCamera, TVector aPosition)
{
// Set up a rotation matrix to orient the billboard towards the camera.
TVector dir = aCamera.iLookAt - aCamera.iPosition;
GLfloat angle = atan( dir.iZ, dir.iX );
// The Yaw angle is computed in such a way that the object always faces the
// camera.
angle = -(RAD_2_DEG( angle ) + 90);
MakeWorldViewMatrix( aCamera, aPosition, angle );
}
// -----------------------------------------------------------------------------
// T3DModel::SetYaw
// -----------------------------------------------------------------------------
//
void T3DModel::SetYaw(GLfloat aYaw)
{
iYaw = aYaw;
}
// -----------------------------------------------------------------------------
// T3DModel::SetPitch
// -----------------------------------------------------------------------------
//
void T3DModel::SetPitch(GLfloat aPitch)
{
iPitch = aPitch;
}
// -----------------------------------------------------------------------------
// T3DModel::SetRoll
// -----------------------------------------------------------------------------
//
void T3DModel::SetRoll(GLfloat aRoll)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -