Ignore:
Timestamp:
07/14/15 20:19:52 (10 years ago)
Author:
epyon
Message:
  • animation - key_channel_set simplified to data_channel_set
  • animation - raw_channel_interpolator
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/gfx/animation.hh

    r418 r419  
    2020{
    2121
    22         class key_channel_set : public data_channel_set
     22        class raw_channel_interpolator
    2323        {
    2424        public:
    25                 mat4 get_raw_matrix( uint32 index ) const
    26                 {
    27                         float key[ 16 ];
     25                explicit raw_channel_interpolator( const data_channel_set* set )
     26                        : m_set( set ), m_interpolation_key( set->get_interpolation_key() )
     27                {
     28                }
     29                raw_channel_interpolator( const data_channel_set* set, const data_descriptor& ikey )
     30                        : m_set( set ), m_interpolation_key( ikey )
     31                { }
     32
     33                template < typename T >
     34                T get_raw( uint32 index ) const
     35                {
     36                        float key[16];
    2837                        float* pkey = key;
    29                         for ( uint16 i = 0; i < size(); ++i )
    30                         {
    31                                 pkey += get_raw( m_channels[i], index, pkey );
    32                         }
    33                         return extract_matrix_raw( m_final_key, key );
    34                 }
    35 
    36                 transform get_raw_transform( uint32 index ) const
    37                 {
    38                         float key[ 16 ];
     38                        for ( const auto& channel : *m_set )
     39                        {
     40                                pkey += get_raw( channel, index, pkey );
     41                        }
     42                        return extract_key_raw< T >( m_interpolation_key, key );
     43                }
     44
     45                template < typename T >
     46                T get( float time ) const
     47                {
     48                        float key[16];
    3949                        float* pkey = key;
    40                         for ( uint16 i = 0; i < size(); ++i )
    41                         {
    42                                 pkey += get_raw( m_channels[i], index, pkey );
    43                         }
    44                         return extract_transform_raw( m_final_key, key );
    45                 }
    46 
    47                 mat4 get_matrix( float time ) const
    48                 {
    49                         float key[ 16 ];
    50                         float* pkey = key;
    51                         for ( uint16 i = 0; i < size(); ++i )
    52                         {
    53                                 pkey += interpolate_raw( m_channels[i], time, pkey );
    54                         }
    55                         return extract_matrix_raw( m_final_key, key );
    56                 }
    57 
    58                 transform get_transform( float time ) const
    59                 {
    60                         float key[ 16 ];
    61                         float* pkey = key;
    62                         for ( uint16 i = 0; i < size(); ++i )
    63                         {
    64                                 pkey += interpolate_raw( m_channels[i], time, pkey );
    65                         }
    66                         return extract_transform_raw( m_final_key, key );
    67                 }
    68 
    69                 const data_descriptor& get_final_key() const { return m_final_key; }
     50                        for ( const auto& channel : *m_set )
     51                        {
     52                                pkey += interpolate_raw( channel, time, pkey );
     53                        }
     54                        return extract_key_raw< T >( m_interpolation_key, key );
     55                }
     56
     57                template < typename T >
     58                T  get( uint32 index1, uint32 index2, float interpolation ) const
     59                {
     60                        T t1 = get_raw< T >( index1 );
     61                        T t2 = get_raw< T >( index2 );
     62                        return interpolate( t1, t2, interpolation );
     63                }
    7064
    7165                static uint32 get_raw( const raw_data_channel& channel, uint32 index, float* result )
     
    138132                        return ret;
    139133                }
    140 
    141         private:
    142                 friend class key_channel_set_creator;
    143 
    144                 key_channel_set() {}
    145 
    146                 data_descriptor m_final_key;
     134                const data_descriptor& get_interpolation_key() { return m_interpolation_key; }
     135        protected:
     136                const data_channel_set* m_set;
     137                data_descriptor m_interpolation_key;
    147138        };
    148139
    149         template < typename KEY, bool TIMED >
    150         class key_channel_interpolator;
    151 
    152 
    153         template < typename KEY >
    154         class key_channel_interpolator< KEY, false >
    155         {
    156         public:
    157                 key_channel_interpolator() : m_data( nullptr ) {}
    158                 key_channel_interpolator( const raw_data_channel* data ) : m_data( nullptr ) { set_data( data ); }
    159                 key_channel_interpolator( const raw_data_channel* data, bool ) : m_data( data ) {}
    160                 void set_data( const raw_data_channel* data )
    161                 {
    162                         m_data = data;
    163                         data_descriptor desc;
    164                         desc.initialize<KEY>();
    165                         NV_ASSERT( data->descriptor() == desc, "Bad channel passed!" );
    166                 }
    167                 void get_interpolated( KEY& result, float frame ) const
    168                 {
    169                         NV_ASSERT( m_data, "Data is null!" );
    170                         const KEY* keys = m_data->data_cast<KEY>( );
    171                         uint32 count = m_data->size();
    172                         if ( count == 0 ) return;
    173                         if ( count == 1 )
    174                         {
    175                                 result = keys[0];
    176                                 return;
    177                         }
    178                         size_t index = glm::clamp<size_t>( size_t( frame ), 0, count - 2 );
    179                         float factor = glm::clamp<float> ( frame - index, 0.0f, 1.0f );
    180                         interpolate_key( result, keys[index], keys[index+1], factor );
    181                 }
    182 
    183         private:
    184                 const raw_data_channel* m_data;
    185         };
    186 
    187         template < typename KEY >
    188         class key_channel_interpolator< KEY, true >
    189         {
    190         public:
    191                 key_channel_interpolator() : m_data( nullptr ) {}
    192                 key_channel_interpolator( const raw_data_channel* data ) : m_data( nullptr ) { set_data( data ); }
    193                 void set_data( const raw_data_channel* data )
    194                 {
    195                         m_data = data;
    196                         data_descriptor desc;
    197                         desc.initialize<KEY>();
    198                         NV_ASSERT( data->descriptor() == desc, "Bad channel passed!" );
    199                 }
    200                 void get_interpolated( KEY& result, float time ) const
    201                 {
    202                         // TODO: this probably could be optimized
    203                         NV_ASSERT( m_data, "Data is null!" );
    204                         const KEY* keys = m_data->data_cast<KEY>( );
    205                         uint32 count = m_data->size();
    206                         if ( count == 0 ) return;
    207                         if ( count == 1 )
    208                         {
    209                                 result = keys[0];
    210                                 return;
    211                         }
    212                         int index = -1;
    213                         for ( int i = 0 ; i < int( count ) - 1 ; i++ )
    214                         {
    215                                 if ( time < keys[i + 1].time ) { index = i; break; }
    216                         }
    217                         NV_ASSERT( index >= 0, "animation time fail!");
    218                         float delta  = keys[index + 1].time - keys[index].time;
    219                         float factor = glm::clamp( (time - keys[index].time) / delta, 0.0f, 1.0f );
    220                         interpolate_key( result, keys[index], keys[index+1], factor );
    221                 }
    222 
    223         private:
    224                 const raw_data_channel* m_data;
    225         };
    226 
     140//      template < typename KEY, bool TIMED >
     141//      class key_channel_interpolator;
     142//
     143//
     144//      template < typename KEY >
     145//      class key_channel_interpolator< KEY, false >
     146//      {
     147//      public:
     148//              key_channel_interpolator() : m_data( nullptr ) {}
     149//              key_channel_interpolator( const raw_data_channel* data ) : m_data( nullptr ) { set_data( data ); }
     150//              key_channel_interpolator( const raw_data_channel* data, bool ) : m_data( data ) {}
     151//              void set_data( const raw_data_channel* data )
     152//              {
     153//                      m_data = data;
     154//                      data_descriptor desc;
     155//                      desc.initialize<KEY>();
     156//                      NV_ASSERT( data->descriptor() == desc, "Bad channel passed!" );
     157//              }
     158//              void get_interpolated( KEY& result, float frame ) const
     159//              {
     160//                      NV_ASSERT( m_data, "Data is null!" );
     161//                      const KEY* keys = m_data->data_cast<KEY>( );
     162//                      uint32 count = m_data->size();
     163//                      if ( count == 0 ) return;
     164//                      if ( count == 1 )
     165//                      {
     166//                              result = keys[0];
     167//                              return;
     168//                      }
     169//                      size_t index = glm::clamp<size_t>( size_t( frame ), 0, count - 2 );
     170//                      float factor = glm::clamp<float> ( frame - index, 0.0f, 1.0f );
     171//                      interpolate_key( result, keys[index], keys[index+1], factor );
     172//              }
     173//
     174//      private:
     175//              const raw_data_channel* m_data;
     176//      };
     177//
     178//      template < typename KEY >
     179//      class key_channel_interpolator< KEY, true >
     180//      {
     181//      public:
     182//              key_channel_interpolator() : m_data( nullptr ) {}
     183//              key_channel_interpolator( const raw_data_channel* data ) : m_data( nullptr ) { set_data( data ); }
     184//              void set_data( const raw_data_channel* data )
     185//              {
     186//                      m_data = data;
     187//                      data_descriptor desc;
     188//                      desc.initialize<KEY>();
     189//                      NV_ASSERT( data->descriptor() == desc, "Bad channel passed!" );
     190//              }
     191//              void get_interpolated( KEY& result, float time ) const
     192//              {
     193//                      // TODO: this probably could be optimized
     194//                      NV_ASSERT( m_data, "Data is null!" );
     195//                      const KEY* keys = m_data->data_cast<KEY>( );
     196//                      uint32 count = m_data->size();
     197//                      if ( count == 0 ) return;
     198//                      if ( count == 1 )
     199//                      {
     200//                              result = keys[0];
     201//                              return;
     202//                      }
     203//                      int index = -1;
     204//                      for ( int i = 0 ; i < int( count ) - 1 ; i++ )
     205//                      {
     206//                              if ( time < keys[i + 1].time ) { index = i; break; }
     207//                      }
     208//                      NV_ASSERT( index >= 0, "animation time fail!");
     209//                      float delta  = keys[index + 1].time - keys[index].time;
     210//                      float factor = glm::clamp( (time - keys[index].time) / delta, 0.0f, 1.0f );
     211//                      interpolate_key( result, keys[index], keys[index+1], factor );
     212//              }
     213//
     214//      private:
     215//              const raw_data_channel* m_data;
     216//      };
     217//
    227218//      template < typename KEY1, typename KEY2 = void, typename KEY3 = void >
    228219//      class key_data_interpolator
     
    230221//
    231222//      };
    232 
    233         class key_animation_data
    234         {
    235         public:
    236                 virtual mat4 get_matrix( float time ) const = 0;
    237                 virtual transform get_transform( float time ) const = 0;
    238                 virtual bool empty() const = 0;
    239                 virtual size_t size() const = 0;
    240                 virtual uint32 raw_size() const = 0;
    241                 virtual ~key_animation_data() {}
    242         };
    243 
    244 
    245         class key_vectors_prs : public key_animation_data
    246         {
    247                 struct key_p { float time; vec3 position; };
    248                 struct key_r { float time; quat rotation; };
    249                 struct key_s { float time; vec3 scale; };
    250         public:
    251                 explicit key_vectors_prs( const raw_data_channel* p, const raw_data_channel* r, const raw_data_channel* s )
    252                 {
    253                         m_pchannel = p;
    254                         m_rchannel = r;
    255                         m_schannel = s;
    256                         m_pinter.set_data( m_pchannel );
    257                         m_rinter.set_data( m_rchannel );
    258                         m_sinter.set_data( m_schannel );
    259                 }
    260                 size_t size() const { return 0; } // TODO: remove?
    261                 bool empty() const {
    262                         return m_pchannel->size() == 0
    263                                 && m_rchannel->size() == 0
    264                                 && m_schannel->size() == 0; }
    265                 virtual mat4 get_matrix( float time ) const
    266                 {
    267                         key_p p;
    268                         key_r r;
    269                         key_s s;
    270 
    271                         m_pinter.get_interpolated( p, time );
    272                         m_rinter.get_interpolated( r, time );
    273                         m_sinter.get_interpolated( s, time );
    274 
    275                         return extract_matrix( p ) * extract_matrix( r ) * extract_matrix( s );
    276                 }
    277                 virtual transform get_transform( float time ) const
    278                 {
    279                         key_p p;
    280                         key_r r;
    281 
    282                         m_pinter.get_interpolated( p, time );
    283                         m_rinter.get_interpolated( r, time );
    284 
    285                         return transform( p.position, r.rotation );
    286                 }
    287                 virtual uint32 raw_size() const
    288                 {
    289                         return 3 * sizeof( size_t )
    290                                 + m_pchannel->size() * sizeof( key_p )
    291                                 + m_rchannel->size() * sizeof( key_r )
    292                                 + m_schannel->size() * sizeof( key_s );
    293                 }
    294                 ~key_vectors_prs()
    295                 {
    296                 }
    297         protected:
    298                 const raw_data_channel* m_pchannel;
    299                 const raw_data_channel* m_rchannel;
    300                 const raw_data_channel* m_schannel;
    301                 key_channel_interpolator< key_p, true > m_pinter;
    302                 key_channel_interpolator< key_r, true > m_rinter;
    303                 key_channel_interpolator< key_s, true > m_sinter;
    304         };
    305 
    306         class transform_vector : public key_animation_data
    307         {
    308                 struct key
    309                 {
    310                         transform tform;
    311                 };
    312         public:
    313                 explicit transform_vector( const raw_data_channel* channel )
    314                 {
    315                         data_descriptor kd;
    316                         kd.initialize<key>();
    317                         NV_ASSERT( kd == channel->descriptor(), "bad channel!" );
    318                         m_channel = channel;
    319                         m_interpolator.set_data( m_channel );
    320                 }
    321 
    322                 ~transform_vector()
    323                 {
    324                         delete m_channel;
    325                 }
    326                 bool empty() const { return m_channel->size() == 0; }
    327                 size_t size() const { return m_channel->size(); }
    328                 const transform& get( size_t index ) const { return m_channel->data_cast< key >()[ index ].tform; }
    329                 const transform* data() const { return &(m_channel->data_cast< key >()[0].tform); }
    330 
    331                 virtual uint32 raw_size() const
    332                 {
    333                         return sizeof( size_t ) + m_channel->size() * sizeof( key );
    334                 }
    335 
    336                 virtual mat4 get_matrix( float time ) const
    337                 {
    338                         return get_transform( time ).extract();
    339                 }
    340                 virtual transform get_transform( float time ) const
    341                 {
    342                         key result;
    343                         m_interpolator.get_interpolated( result, time );
    344                         return extract_transform< key >( result );
    345                 }
    346         protected:
    347                 key_channel_interpolator< key, false > m_interpolator;
    348                 const raw_data_channel* m_channel;
    349         };
     223//
     224//      class key_animation_data
     225//      {
     226//      public:
     227//              virtual mat4 get_matrix( float time ) const = 0;
     228//              virtual transform get_transform( float time ) const = 0;
     229//              virtual bool empty() const = 0;
     230//              virtual size_t size() const = 0;
     231//              virtual uint32 raw_size() const = 0;
     232//              virtual ~key_animation_data() {}
     233//      };
     234
     235//      class transform_vector : public key_animation_data
     236//      {
     237//              struct key
     238//              {
     239//                      transform tform;
     240//              };
     241//      public:
     242//              explicit transform_vector( const raw_data_channel* channel )
     243//              {
     244//                      data_descriptor kd;
     245//                      kd.initialize<key>();
     246//                      NV_ASSERT( kd == channel->descriptor(), "bad channel!" );
     247//                      m_channel = channel;
     248//                      m_interpolator.set_data( m_channel );
     249//              }
     250//
     251//              ~transform_vector()
     252//              {
     253//                      delete m_channel;
     254//              }
     255//              bool empty() const { return m_channel->size() == 0; }
     256//              size_t size() const { return m_channel->size(); }
     257//              const transform& get( size_t index ) const { return m_channel->data_cast< key >()[ index ].tform; }
     258//              const transform* data() const { return &(m_channel->data_cast< key >()[0].tform); }
     259//
     260//              virtual uint32 raw_size() const
     261//              {
     262//                      return sizeof( size_t ) + m_channel->size() * sizeof( key );
     263//              }
     264//
     265//              virtual mat4 get_matrix( float time ) const
     266//              {
     267//                      return get_transform( time ).extract();
     268//              }
     269//              virtual transform get_transform( float time ) const
     270//              {
     271//                      key result;
     272//                      m_interpolator.get_interpolated( result, time );
     273//                      return extract_transform< key >( result );
     274//              }
     275//      protected:
     276//              key_channel_interpolator< key, false > m_interpolator;
     277//              const raw_data_channel* m_channel;
     278//      };
    350279
    351280
Note: See TracChangeset for help on using the changeset viewer.