Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

SkinnedTriangles.h

Go to the documentation of this file.
00001 // Copyright NVIDIA Corporation 2002-2004
00002 // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
00003 // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
00004 // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
00005 // AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
00006 // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
00007 // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00008 // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
00009 // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
00010 // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
00011 
00012 #pragma once
00013 
00015 #include "nvsgcommon.h"
00016 
00017 #include "nvmath/Trafo.h"
00018 #include "nvsg/Animation.h"
00019 #include "nvsg/Triangles.h"
00020 
00021 #ifdef _DEBUG
00022 # ifndef new
00023 #  define new new(__FILE__, __LINE__)
00024 #  define _DEFINED_DBGNEW // restrict the 'new' macro to this header file only
00025 # endif
00026 #endif
00027 
00028 
00029 namespace nvsg
00030 {
00032 
00036   class Bone
00037   {
00038     public:
00040       Bone( void );
00041 
00043       Bone( const Bone &rhs );
00044 
00046       ~Bone( void );
00047 
00048     public:
00050 
00053       nvmath::Vec4f operator()( size_t frame, const nvmath::Vec4f &v )  const;
00054 
00056 
00057       const Animation<nvmath::Trafo> * getAnimation( void ) const;
00058 
00060 
00061       const nvmath::Mat44f & getMatrix( size_t frame   
00062                                       ) const;
00063 
00065 
00066       float getWeight( void ) const;
00067 
00069 
00070       NVSG_API void setAnimation( const Animation<nvmath::Trafo> *pAnimation   
00071                                 );
00072 
00074 
00076       void setInverse( const nvmath::Mat44f &inverse    
00077                      );
00078 
00080 
00081       void setMatrix( const nvmath::Mat44f& trafo    
00082                     );
00083 
00085 
00086       void setWeight( float weight    
00087                     );
00088 
00089     private:
00090       nvmath::Mat44f              m_baseInverse;
00091       const Animation<nvmath::Trafo>  * m_animation;
00092       float               m_weight;
00093 
00094       mutable size_t  m_currFrame;
00095       mutable nvmath::Mat44f  m_currTrafo;
00096   };
00097 
00098   inline Bone::Bone( void )
00099   : m_animation(NULL)
00100   , m_currFrame(0xFFFFFFFF)
00101   {
00102     __TRACE();
00103   }
00104 
00105   inline Bone::Bone( const Bone &rhs )
00106   : m_baseInverse( rhs.m_baseInverse )
00107   , m_weight( rhs.m_weight )
00108   , m_currFrame( rhs.m_currFrame )
00109   , m_currTrafo( rhs.m_currTrafo )
00110   {
00111     __TRACE();
00112     m_animation = rhs.m_animation;
00113     if ( m_animation )
00114     {
00115       m_animation->addRef();
00116     }
00117   }
00118 
00119   inline Bone::~Bone( void )
00120   {
00121     __TRACE();
00122     if ( m_animation )
00123     {
00124       m_animation->removeRef();
00125     }
00126   }
00127 
00128   inline nvmath::Vec4f Bone::operator()( size_t frame, const nvmath::Vec4f &vIn ) const
00129   {
00130     __TRACE();
00131     nvmath::Vec4f vOut;
00132     if ( m_animation )
00133     {
00134       vOut = (*m_animation)[frame].getMatrix() * m_baseInverse * vIn * m_weight;
00135     }
00136     else
00137     {
00138       vOut = vIn * m_weight;
00139     }
00140     return( vOut );
00141   }
00142 
00143   inline const Animation<nvmath::Trafo> * Bone::getAnimation( void ) const
00144   {
00145     __TRACE();
00146     return( m_animation );
00147   }
00148 
00149   inline const nvmath::Mat44f & Bone::getMatrix( size_t frame ) const
00150   {
00151     __TRACE();
00152     if ( m_animation )
00153     {
00154       if ( m_currFrame != frame )
00155       {
00156         m_currTrafo = (*m_animation)[frame].getMatrix();
00157         m_currFrame = frame;
00158       }
00159     }
00160     return( m_currTrafo );
00161   }
00162 
00163   inline float Bone::getWeight( void ) const
00164   {
00165     __TRACE();
00166     return( m_weight );
00167   }
00168 
00169   inline void Bone::setAnimation( const Animation<nvmath::Trafo> *pAnimation )
00170   {
00171     __TRACE();
00172     if ( m_animation != pAnimation )
00173     {
00174       if ( m_animation )
00175       {
00176         m_animation->removeRef();
00177       }
00178       m_animation = pAnimation;
00179       if ( m_animation )
00180       {
00181         m_animation->addRef();
00182 
00183         const nvmath::Trafo trafo = (*m_animation)[0];
00184         nvmath::Quatf rot = trafo.getOrientation();
00185         nvmath::Vec3f pos = trafo.getTranslation();
00186         m_baseInverse = nvmath::Mat44f( nvmath::Vec3f( 0.0f, 0.0f, 0.0f ), nvmath::Quatf( rot[0], rot[1], rot[2], -rot[3] ) )
00187 
00188                       * nvmath::Mat44f( nvmath::Vec4f( 1.0f, 0.0f, 0.0f, -pos[0] ),
00189                                 nvmath::Vec4f( 0.0f, 1.0f, 0.0f, -pos[1] ),
00190                                 nvmath::Vec4f( 0.0f, 0.0f, 1.0f, -pos[2] ),
00191                                 nvmath::Vec4f( 0.0f, 0.0f, 0.0f, 1.0f ) );
00192         m_currFrame   = 0;
00193         m_currTrafo   = nvmath::Mat44f( pos, rot );
00194       }
00195       else
00196       {
00197         m_currFrame = 0xFFFFFFFF;
00198       }
00199     }
00200   }
00201 
00202   inline void Bone::setInverse( const nvmath::Mat44f &inverse )
00203   {
00204     __TRACE();
00205     m_baseInverse = inverse;
00206   }
00207 
00208   inline void Bone::setMatrix( const nvmath::Mat44f &trafo )
00209   {
00210     __TRACE();
00211     m_currTrafo = trafo;
00212     m_currFrame = 0xFFFFFFFF;
00213   }
00214 
00215   inline void Bone::setWeight( float weight )
00216   {
00217     __TRACE();
00218     m_weight = weight;
00219   }
00220 
00222 
00229   typedef struct
00230   {
00231     std::vector<Bone>  m_bones;    
00232     nvmath::Vec3f         m_normal;   
00233     nvmath::Vec3f         m_vertex;   
00234   } Skin;
00235 
00237 
00239   class SkinnedTriangles : public Triangles
00240   {
00241     public:
00243 
00244       NVSG_API static const SkinnedTriangles * create( void );
00245 
00247 
00248       NVSG_API static const SkinnedTriangles * createFromBase( const Triangles &rhs );
00249 
00251 
00252       NVSG_API virtual const SkinnedTriangles * clone( void ) const;
00253 
00255 
00256       NVSG_API virtual bool isDataShared( void ) const;
00257 
00259 
00260       NVSG_API virtual DataID getDataID( void ) const;
00261 
00263 
00264       NVSG_API virtual size_t getNumberOfFrames( void ) const;
00265 
00267 
00269       NVSG_API size_t getNumberOfSkins( void ) const;
00270 
00272 
00273       NVSG_API const Skin * getSkins( void ) const;
00274 
00276 
00278       NVSG_API void setAnimationFrame( size_t frame             
00279                                      , bool bCalculateTangentSpace    
00280                                      );
00281 
00283 
00284       NVSG_API void setNormals( const nvmath::Vec3f * pNor    
00285                               , size_t numNor         
00286                               );
00287 
00289 
00290       NVSG_API void setNormals( size_t pos // Start position
00291                               , const nvmath::Vec3f * pNor    
00292                               , size_t numNor         
00293                               );
00295 
00296       NVSG_API void setSkins( const Skin *pSkins    
00297                             , size_t numSkins       
00298                             );
00299 
00301 
00313       NVSG_API void setSkins( size_t pos
00314                             , const Skin *pSkins
00315                             , size_t numSkins );
00316 
00317     protected:
00319       NVSG_API SkinnedTriangles( void );
00320 
00322       NVSG_API SkinnedTriangles( const Triangles &rhs );
00323 
00325       NVSG_API SkinnedTriangles( const SkinnedTriangles &rhs );
00326 
00328       NVSG_API virtual ~SkinnedTriangles( void );
00329 
00330     private:
00331       nvutil::RCPtr<nvutil::RCVector<Skin> >  m_skins; // sharable skins
00332   };
00333 
00334   // - - - - - - - - - - - - - - - - - - - - - - - - - 
00335   // inlines
00336   // - - - - - - - - - - - - - - - - - - - - - - - - - 
00337 
00338   inline size_t SkinnedTriangles::getNumberOfSkins( void )  const
00339   { 
00340     __TRACE();
00341     return( m_skins->size() ); 
00342   }
00343 
00344   inline const Skin * SkinnedTriangles::getSkins( void )  const 
00345   { 
00346     __TRACE();
00347     return( &(*m_skins)[0] ); 
00348   }
00349 
00350 } //  namespace nvsg
00351 
00352 #ifdef _DEFINED_DBGNEW
00353 # undef new
00354 #endif

Generated on Tue Mar 1 13:19:19 2005 for NVSGSDK by NVIDIA