📄 utils3d.cpp
字号:
iRoll = aRoll;
}
// -----------------------------------------------------------------------------
// T3DModel::SetPosition
// -----------------------------------------------------------------------------
//
void T3DModel::SetPosition(TVector aPosition)
{
iPosition = aPosition;
}
// -----------------------------------------------------------------------------
// T3DModel::GetYaw
// -----------------------------------------------------------------------------
//
GLfloat T3DModel::GetYaw()
{
return iYaw;
}
// -----------------------------------------------------------------------------
// T3DModel::GetPitch
// -----------------------------------------------------------------------------
//
GLfloat T3DModel::GetPitch()
{
return iPitch;
}
// -----------------------------------------------------------------------------
// T3DModel::GetRoll
// -----------------------------------------------------------------------------
//
GLfloat T3DModel::GetRoll()
{
return iRoll;
}
// -----------------------------------------------------------------------------
// T3DModel::GetPosition
// -----------------------------------------------------------------------------
//
TVector T3DModel::GetPosition()
{
return iPosition;
}
// =============================================================================
// T3DModelx
// =============================================================================
// ============================= MEMBER FUNCTIONS ==============================
// -----------------------------------------------------------------------------
// T3DModelx::T3DModelx
// C++ default constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a T3DModelx to Position (0,0,0), with orientation
// [Yaw=0, Pitch=0, Roll=0].
// -----------------------------------------------------------------------------
//
T3DModelx::T3DModelx()
{
*this = T3DModelx(TVectorx(INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(0)),
INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(0));
}
// -----------------------------------------------------------------------------
// T3DModelx::T3DModelx
// C++ default constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a T3DModelx to position aPosition, with
// orientation [aYaw, aPitch, aRoll].
// -----------------------------------------------------------------------------
//
T3DModelx::T3DModelx(TVectorx aPosition, GLfixed aYaw, GLfixed aPitch,
GLfixed aRoll)
{
SetPosition(aPosition);
SetYaw(aYaw);
SetPitch(aPitch);
SetRoll(aRoll);
}
// -----------------------------------------------------------------------------
// T3DModelx::MakeWorldViewMatrix
// Sets up a world + a view matrix.
// -----------------------------------------------------------------------------
//
void T3DModelx::MakeWorldViewMatrix(TCamerax & aCamera)
{
MakeWorldViewMatrix( aCamera, GetPosition(), GetYaw(), GetPitch(),
GetRoll());
}
// -----------------------------------------------------------------------------
// T3DModelx::MakeWorldViewMatrix
// Sets up a world + a view matrix.
// -----------------------------------------------------------------------------
//
void T3DModelx::MakeWorldViewMatrix(TCamerax & aCamera, TVectorx aPosition,
GLfixed aYaw, GLfixed aPitch, GLfixed aRoll)
{
glMultMatrixx((GLfixed*)aCamera.GetViewMatrix());
glTranslatex(aPosition.iX-aCamera.iPosition.iX,
aPosition.iY-aCamera.iPosition.iY, aPosition.iZ-aCamera.iPosition.iZ);
if ( aRoll != INT_2_FIXED(0) )
{
glRotatex( aRoll , INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(1));
}
if ( aYaw != INT_2_FIXED(0) )
{
glRotatex( aYaw , INT_2_FIXED(0), INT_2_FIXED(1), INT_2_FIXED(0));
}
if ( aPitch != INT_2_FIXED(0) )
{
glRotatex( aPitch, INT_2_FIXED(1), INT_2_FIXED(0), INT_2_FIXED(0));
}
}
// -----------------------------------------------------------------------------
// T3DModelx::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 T3DModelx::MakeBillboardWorldViewMatrix(TCamerax & aCamera)
{
MakeBillboardWorldViewMatrix( aCamera, GetPosition() );
}
// -----------------------------------------------------------------------------
// T3DModelx::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 T3DModelx::MakeBillboardWorldViewMatrix(TCamerax & aCamera,
TVectorx aPosition)
{
// Set up a rotation matrix to orient the billboard towards the camera.
TVectorx Dir = aCamera.iLookAt - aCamera.iPosition;
GLfloat Angle = atan( FIXED_2_FLOAT(Dir.iZ), FIXED_2_FLOAT(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, FLOAT_2_FIXED( Angle ) );
}
// -----------------------------------------------------------------------------
// T3DModelx::SetYaw
// -----------------------------------------------------------------------------
//
void T3DModelx::SetYaw(GLfixed aYaw)
{
iYaw = aYaw;
}
// -----------------------------------------------------------------------------
// T3DModelx::SetPitch
// -----------------------------------------------------------------------------
//
void T3DModelx::SetPitch(GLfixed aPitch)
{
iPitch = aPitch;
}
// -----------------------------------------------------------------------------
// T3DModelx::SetRoll
// -----------------------------------------------------------------------------
//
void T3DModelx::SetRoll(GLfixed aRoll)
{
iRoll = aRoll;
}
// -----------------------------------------------------------------------------
// T3DModelx::SetPosition
// -----------------------------------------------------------------------------
//
void T3DModelx::SetPosition(TVectorx aPosition)
{
iPosition = aPosition;
}
// -----------------------------------------------------------------------------
// T3DModelx::GetYaw
// -----------------------------------------------------------------------------
//
GLfixed T3DModelx::GetYaw()
{
return iYaw;
}
// -----------------------------------------------------------------------------
// T3DModelx::GetPitch
// -----------------------------------------------------------------------------
//
GLfixed T3DModelx::GetPitch()
{
return iPitch;
}
// -----------------------------------------------------------------------------
// T3DModelx::GetRoll
// -----------------------------------------------------------------------------
//
GLfixed T3DModelx::GetRoll()
{
return iRoll;
}
// -----------------------------------------------------------------------------
// T3DModelx::GetPosition
// -----------------------------------------------------------------------------
//
TVectorx T3DModelx::GetPosition()
{
return iPosition;
}
// =============================================================================
// TCamera
// =============================================================================
// ============================= MEMBER FUNCTIONS ==============================
// -----------------------------------------------------------------------------
// TCamera::TCamera
// C++ default constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a TCamera to Position (0,0,0), LookAt (0,0,-1),
// Up(0,1,0).
// -----------------------------------------------------------------------------
//
TCamera::TCamera()
{
*this = TCamera(TVector(0, 0, 0), TVector(0, 0, -1), TVector(0, 1, 0));
}
// -----------------------------------------------------------------------------
// TCamera::TCamera
// C++ constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a TCamera to aPosition, aLookAt, aUp.
// -----------------------------------------------------------------------------
//
TCamera::TCamera(TVector aPosition, TVector aLookAt, TVector aUp)
{
LookAt(aPosition, aLookAt, aUp);
}
// -----------------------------------------------------------------------------
// TCamera::LookAt
// Initializes a TCamera to aPosition, aLookAt, aUp.
// -----------------------------------------------------------------------------
//
void TCamera::LookAt(TVector aPosition, TVector aLookAt, TVector aUp)
{
TVector XAxis, YAxis, ZAxis;
iPosition = aPosition;
iLookAt = aLookAt;
iUp = aUp;
// Get the z basis vector, which points straight ahead; the
// difference from the position (eye point) to the look-at point.
// This is the direction of the gaze (+z).
ZAxis = (iLookAt - iPosition);
// Normalize the z basis vector.
ZAxis.Normalize();
// Compute the orthogonal axes from the cross product of the gaze
// and the Up vector.
XAxis = TVector::CrossProduct( ZAxis, iUp );
XAxis.Normalize();
YAxis = TVector::CrossProduct( XAxis, ZAxis );
YAxis.Normalize();
// Start building the matrix. The first three rows contain the
// basis vectors used to rotate the view to point at the look-at point.
MakeIdentity( &iViewMatrix[0][0] );
iViewMatrix[0][0] = XAxis.iX;
iViewMatrix[1][0] = XAxis.iY;
iViewMatrix[2][0] = XAxis.iZ;
iViewMatrix[0][1] = YAxis.iX;
iViewMatrix[1][1] = YAxis.iY;
iViewMatrix[2][1] = YAxis.iZ;
iViewMatrix[0][2] = -ZAxis.iX;
iViewMatrix[1][2] = -ZAxis.iY;
iViewMatrix[2][2] = -ZAxis.iZ;
}
// -----------------------------------------------------------------------------
// TCamera::GetPosition
// Get camera position vector.
// -----------------------------------------------------------------------------
//
TVector TCamera::GetPosition()
{
return iPosition;
}
// -----------------------------------------------------------------------------
// TCamera::GetLookAt
// Get camera lookat position vector.
// -----------------------------------------------------------------------------
//
TVector TCamera::GetLookAt()
{
return iLookAt;
}
// -----------------------------------------------------------------------------
// TCamera::GetUp
// Get camera "up" vector.
// -----------------------------------------------------------------------------
//
TVector TCamera::GetUp()
{
return iUp;
}
// -----------------------------------------------------------------------------
// TCamera::GetViewMatrix
// Get camera matrix representation (world -> camera transform).
// -----------------------------------------------------------------------------
//
GLfloat* TCamera::GetViewMatrix()
{
return (GLfloat *)&iViewMatrix[0][0];
}
// -----------------------------------------------------------------------------
// TCamera::MakeIdentity
// Initializes the matrix aMatrix to identity matrix.
// -----------------------------------------------------------------------------
//
void TCamera::MakeIdentity(GLfloat * aMatrix)
{
aMatrix[0 + 4 * 0] = 1.0f; aMatrix[0 + 4 * 1] = 0.0f;
aMatrix[0 + 4 * 2] = 0.0f; aMatrix[0 + 4 * 3] = 0.0f;
aMatrix[1 + 4 * 0] = 0.0f; aMatrix[1 + 4 * 1] = 1.0f;
aMatrix[1 + 4 * 2] = 0.0f; aMatrix[1 + 4 * 3] = 0.0f;
aMatrix[2 + 4 * 0] = 0.0f; aMatrix[2 + 4 * 1] = 0.0f;
aMatrix[2 + 4 * 2] = 1.0f; aMatrix[2 + 4 * 3] = 0.0f;
aMatrix[3 + 4 * 0] = 0.0f; aMatrix[3 + 4 * 1] = 0.0f;
aMatrix[3 + 4 * 2] = 0.0f; aMatrix[3 + 4 * 3] = 1.0f;
}
// =============================================================================
// TCamerax, fixed-point camera implementation.
// =============================================================================
// ============================= MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// TCamerax::TCamerax
// C++ default constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a TCamera to Position (0,0,0), LookAt (0,0,-1),
// Up(0,1,0).
// -----------------------------------------------------------------------------
//
TCamerax::TCamerax()
{
*this = TCamerax(TVectorx(INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(0)),
TVectorx(INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(-1)),
TVectorx(INT_2_FIXED(0), INT_2_FIXED(1), INT_2_FIXED(0)));
}
// -----------------------------------------------------------------------------
// TCamerax::TCamerax
// C++ constructor can NOT contain any code, that
// might leave.
// Constructs and initializes a TCamera to aPosition, aLookAt, aUp.
// -----------------------------------------------------------------------------
//
TCamerax::TCamerax(TVectorx aPosition, TVectorx aLookAt, TVectorx aUp)
{
LookAt(aPosition, aLookAt, aUp);
}
// -----------------------------------------------------------------------------
// TCamerax::LookAt
// Initializes a TCamera to aPosition, aLookAt, aUp.
// -----------------------------------------------------------------------------
//
void TCamerax::LookAt(TVectorx aPosition, TVectorx aLookAt, TVectorx aUp)
{
TVectorx XAxis, YAxis, ZAxis;
iPosition = aPosition;
iLookAt = aLookAt;
iUp = aUp;
// Get the z basis vector, which points straight ahead; the
// difference from the position (eye point) to the look-at point.
// This is the direction of the gaze (+z).
ZAxis = (iLookAt - iPosition);
// Normalize the z basis vector.
ZAxis.Normalize();
// Compute the orthogonal axes from the cross product of the gaze
// and the Up vector.
XAxis = TVectorx::CrossProduct(ZAxis, iUp);
XAxis.Normalize();
YAxis = TVectorx::CrossProduct(XAxis, ZAxis);
YAxis.Normalize();
// Start building the matrix. The first three rows contain the
// basis vectors used to rotate the view to point at the look-at point.
MakeIdentity(&iViewMatrix[0][0]);
iViewMatrix[0][0] = XAxis.iX;
iViewMatrix[1][0] = XAxis.iY;
iViewMatrix[2][0] = XAxis.iZ;
iViewMatrix[0][1] = YAxis.iX;
iViewMatrix[1][1] = YAxis.iY;
iViewMatrix[2][1] = YAxis.iZ;
iViewMatrix[0][2] = -ZAxis.iX;
iViewMatrix[1][2] = -ZAxis.iY;
iViewMatrix[2][2] = -ZAxis.iZ;
}
// -----------------------------------------------------------------------------
// TCamerax::GetPosition
// Get camera position vector.
// -----------------------------------------------------------------------------
//
TVectorx TCamerax::GetPosition()
{
return iPosition;
}
// -----------------------------------------------------------------------------
// TCamerax::GetLookAt
// Get camera lookat position vector.
// -----------------------------------------------------------------------------
//
TVectorx TCamerax::GetLookAt()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -