TAPs 0.7.7.3
TAPsRigidBodyBase.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsRigidBodyBase.cpp
00003 ******************************************************************************/
00007 /******************************************************************************
00008 SUKITTI PUNAK   (03/25/2010)
00009 UPDATE          (09/26/2010)
00010 ******************************************************************************/
00011 #include "TAPsRigidBodyBase.hpp"
00012 // Using Inclusion Model (i.e. definitions are included in declarations)
00013 //                       (this name.cpp is included in name.hpp)
00014 // Each friend is defined directly inside its declaration.
00015 
00016 BEGIN_NAMESPACE_TAPs
00017 //=============================================================================
00018 // Constructors
00019 //-----------------------------------------------------------------------------
00020 template <typename T>
00021 RigidBodyBase<T>::RigidBodyBase ()
00022     : m_pBV( NULL )
00023 {}
00024 //-----------------------------------------------------------------------------
00025 template <typename T>
00026 RigidBodyBase<T>::RigidBodyBase ( Vector3<T> const & Origin, Quaternion<T> const & Orientation )
00027     : m_Origin( Origin )
00028     , m_Orientation( Orientation )
00029     , m_pBV( NULL )
00030 {
00031     UpdateTransformationSupport();
00032 }
00033 //-----------------------------------------------------------------------------
00034 //template <typename T>
00035 //RigidBodyBase<T>::RigidBodyBase ( RigidBodyBase<T> const &orig )
00036 //  : 
00037 //{}
00038 //-----------------------------------------------------------------------------
00039 template <typename T>
00040 RigidBodyBase<T>::~RigidBodyBase ()
00041 {
00042     if ( m_pBV )    delete m_pBV;
00043 }
00044 //-----------------------------------------------------------------------------
00045 template <typename T>
00046 std::string RigidBodyBase<T>::StrInfo () const
00047 {
00048     std::ostringstream ss;
00049     ss << "RigidBodyBase<" << typeid(T).name() << ">";
00050     ss << " has origin at (" << m_OriginPt[0] << ", " << m_OriginPt[1] << ", " << m_OriginPt[2] << ")";
00051     ss << " with orientation (represented by Quaternion<T>) of " << m_Orientation;
00052     ss << " center at " << m_OrientationCenter;
00053     ss << " Its shape is " << Shape<T>::StrInfo();
00054     ss << " Its bounding volume is ";
00055     if ( m_pBV )    ss << RefToBV();
00056     else            ss << "None";
00057     ss << "\n";
00058     return ss.str();
00059 }
00060 //-----------------------------------------------------------------------------
00061 template <typename T>
00062 void RigidBodyBase<T>::SetBVOfType ( Enum::CD type )
00063 {
00064     if ( m_pBV )    delete m_pBV;
00065 #if defined(__gl_h_) || defined(__GL_H__)
00066     m_pBV = BVSupportFns<T>::CreateBVOfType( type, m_pModel );
00067 #else
00068     m_pBV = BVSupportFns<T>::CreateBVOfType( type );
00069 #endif
00070 }
00071 //-----------------------------------------------------------------------------
00072 template <typename T>
00073 Matrix4x4<T> RigidBodyBase<T>::CalMatrixTransform () const
00074 {
00075     return  CGMath<T>::MatrixTranslation( GetOriginPoint() + GetOrientationCenter() )
00076         *   GetOrientation().GetRotationMatrix4x4()
00077         *   CGMath<T>::MatrixTranslation( -GetOrientationCenter() );
00078 }   
00079 //-----------------------------------------------------------------------------
00080 template <typename T>
00081 Matrix4x4<T> RigidBodyBase<T>::CalInvMatrixTransform () const
00082 {
00083     return CalMatrixTransform().Inversed();
00084 }   
00085 //-----------------------------------------------------------------------------
00086 
00087 
00088 //=============================================================================
00089 // OpenGL
00090 #if defined(__gl_h_) || defined(__GL_H__)
00091 //-----------------------------------------------------------------------------
00092 template <typename T>
00093 void RigidBodyBase<T>::Draw () const
00094 {
00095     if ( m_pModel ) {
00096         glPushMatrix();
00097         Matrix4x4<T> Trx = CalMatrixTransform();
00098         GLfloat m[16] = {
00099             static_cast<GLfloat>(Trx[ 0]), static_cast<GLfloat>(Trx[ 4]), static_cast<GLfloat>(Trx[ 8]), static_cast<GLfloat>(Trx[ 12]), 
00100             static_cast<GLfloat>(Trx[ 1]), static_cast<GLfloat>(Trx[ 5]), static_cast<GLfloat>(Trx[ 9]), static_cast<GLfloat>(Trx[ 13]), 
00101             static_cast<GLfloat>(Trx[ 2]), static_cast<GLfloat>(Trx[ 6]), static_cast<GLfloat>(Trx[10]), static_cast<GLfloat>(Trx[ 14]), 
00102             static_cast<GLfloat>(Trx[ 3]), static_cast<GLfloat>(Trx[ 7]), static_cast<GLfloat>(Trx[11]), static_cast<GLfloat>(Trx[ 15])
00103         };
00104         glMultMatrixf( m );
00105         m_pModel->Draw();
00106         glPopMatrix();
00107     }
00108     else {
00109         glPushAttrib( GL_ALL_ATTRIB_BITS );
00110         glPointSize( 5 );
00111         glBegin( GL_POINTS );
00112         glVertex3f( 0, 0, 0 );
00113         glPopAttrib();
00114         glEnd();
00115     }
00116 }
00117 //-----------------------------------------------------------------------------
00118 template <typename T>
00119 void RigidBodyBase<T>::DrawOrigin () const
00120 {
00121     Matrix4x4<T> Trx( CalMatrixTransform() );
00122     Vector3<T> V( Trx * Vector3<T>() );
00123     glPushMatrix();
00124     glPushAttrib( GL_ALL_ATTRIB_BITS );
00125     glDisable( GL_DEPTH_TEST );
00126     glDisable( GL_LIGHTING );
00127     glColor3f( 0.75, 0.75, 0.75 );
00128     glPointSize( 5 );
00129     glBegin( GL_POINTS );
00130     glVertex3f( V[0], V[1], V[2] );
00131     glEnd();
00132     glPopAttrib();
00133     glPopMatrix();
00134 }
00135 //-----------------------------------------------------------------------------
00136 template <typename T>
00137 void RigidBodyBase<T>::DrawOrientation () const
00138 {
00139     Matrix4x4<T> Trx( CalMatrixTransform() );
00140     Vector3<T> O( Trx * GetOrientationCenter() );
00141     Vector3<T> X( Trx * ( GetOrientationCenter() + Vector3<T>(1,0,0) ) );
00142     Vector3<T> Y( Trx * ( GetOrientationCenter() + Vector3<T>(0,1,0) ) );
00143     Vector3<T> Z( Trx * ( GetOrientationCenter() + Vector3<T>(0,0,1) ) );
00144     glPushMatrix();
00145     glPushAttrib( GL_ALL_ATTRIB_BITS );
00146     glDisable( GL_DEPTH_TEST );
00147     glDisable( GL_LIGHTING );
00148     glPointSize( 3 );
00149     glLineWidth( 1 );
00150     glBegin( GL_POINTS );
00151         glColor3f( 0.5, 0.5, 0.5 );
00152         glVertex3f( O[0], O[1], O[2] );
00153         glColor3f( 1, 0, 0 );
00154         glVertex3f( X[0], X[1], X[2] );
00155         glColor3f( 0, 1, 0 );
00156         glVertex3f( Y[0], Y[1], Y[2] );
00157         glColor3f( 0, 0, 1 );
00158         glVertex3f( Z[0], Z[1], Z[2] );
00159     glEnd();
00160     glBegin( GL_LINES );
00161         glColor3f( 1, 0, 0 );
00162         glVertex3f( O[0], O[1], O[2] );
00163         glVertex3f( X[0], X[1], X[2] );
00164         glColor3f( 0, 1, 0 );
00165         glVertex3f( O[0], O[1], O[2] );
00166         glVertex3f( Y[0], Y[1], Y[2] );
00167         glColor3f( 0, 0, 1 );
00168         glVertex3f( O[0], O[1], O[2] );
00169         glVertex3f( Z[0], Z[1], Z[2] );
00170     glEnd();
00171     glPopAttrib();
00172     glPopMatrix();
00173 }
00174 //-----------------------------------------------------------------------------
00175 template <typename T>
00176 void RigidBodyBase<T>::DrawBV () const
00177 {
00178     if ( m_pBV ) {
00179         glPushMatrix();
00180         Matrix4x4<T> Trx = CalMatrixTransform();
00181         GLfloat m[16] = {
00182             static_cast<GLfloat>(Trx[ 0]), static_cast<GLfloat>(Trx[ 4]), static_cast<GLfloat>(Trx[ 8]), static_cast<GLfloat>(Trx[ 12]), 
00183             static_cast<GLfloat>(Trx[ 1]), static_cast<GLfloat>(Trx[ 5]), static_cast<GLfloat>(Trx[ 9]), static_cast<GLfloat>(Trx[ 13]), 
00184             static_cast<GLfloat>(Trx[ 2]), static_cast<GLfloat>(Trx[ 6]), static_cast<GLfloat>(Trx[10]), static_cast<GLfloat>(Trx[ 14]), 
00185             static_cast<GLfloat>(Trx[ 3]), static_cast<GLfloat>(Trx[ 7]), static_cast<GLfloat>(Trx[11]), static_cast<GLfloat>(Trx[ 15])
00186         };
00187         glMultMatrixf( m );
00188         m_pBV->Draw();
00189         glPopMatrix();
00190     }
00191 }
00192 //-----------------------------------------------------------------------------
00193 #endif  // OpenGL
00194 //=============================================================================
00195 
00196 
00197 //=============================================================================
00198 END_NAMESPACE_TAPs
00199 //34567890123456789012345678901234567890123456789012345678901234567890123456789
00200 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines