Changeset 285


Ignore:
Timestamp:
07/21/14 02:19:34 (11 years ago)
Author:
epyon
Message:
  • full pure data model for animation
  • all loaders now use pure data model instead of the template/virtual one
Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/formats/assimp_loader.hh

    r284 r285  
    4242                sint32                parent_id;
    4343                mat4                  transform;
    44                 uint16                channel_count;
    45                 key_raw_channel*      channels[4];
     44                key_data*             data;
    4645
    4746                assimp_animated_node_data()
    48                         : name(), parent_id( -1 )
     47                        : name(), parent_id( -1 ), data( nullptr )
    4948                {
    50                         channels[0] = nullptr; channels[1] = nullptr; channels[2] = nullptr; channels[3] = nullptr;
    5149                }
    5250                ~assimp_animated_node_data()
    5351                {
    54                         if ( channels[0] ) delete channels[0];
    55                         if ( channels[1] ) delete channels[1];
    56                         if ( channels[2] ) delete channels[2];
    57                         if ( channels[3] ) delete channels[3];
     52                        if ( data ) delete data;
    5853                }
    5954        };
  • trunk/nv/formats/md2_loader.hh

    r240 r285  
    2929                virtual bool load( stream& source );
    3030                virtual mesh_data* release_mesh_data( size_t index = 0 );
     31                virtual size_t get_mesh_count() const { return 1; }
     32        private:
    3133                virtual mesh_data* release_mesh_frame( sint32 frame );
    32                 virtual size_t get_mesh_count() const { return 1; }
    3334                size_t get_max_frames() const;
    34         private:
    3535                void reindex();
    3636        private:
  • trunk/nv/formats/md3_loader.hh

    r282 r285  
    4040                size_t get_max_frames() const;
    4141                virtual tag_map* create_tag_map();
     42                virtual mesh_node_data* release_mesh_node_data( size_t index );
     43                virtual size_t get_node_count() const;
    4244        private:
    4345                key_raw_channel* load_tags( const std::string& tag );
  • trunk/nv/formats/nmd_loader.hh

    r284 r285  
    7878                sint16 parent_id;
    7979                mat4   transform;
    80                 uint16 channel_count;
    81                 key_raw_channel** channels;
     80                key_data* data;
    8281        };
    8382
     
    9190
    9291                nmd_animation() : node_count(0), nodes( nullptr ) {}
    93                 void clear_node( nmd_node* node )
    94                 {
    95                         if ( node->channel_count > 0 )
    96                         {
    97                                 for ( uint16 c = 0; c < node->channel_count; ++c )
    98                                         delete node->channels[c];
    99                                 delete[] node->channels;
    100                         }
    101                         node->channels = nullptr;
    102                         node->channel_count = 0;
    103                 }
    10492                ~nmd_animation()
    10593                {
    10694                        for ( uint16 n = 0; n < node_count; ++n )
    107                                 clear_node( &nodes[n] );
     95                        {
     96                                if ( nodes[n].data ) delete nodes[n].data;
     97                        }
    10898                        delete[] nodes;
    10999                }
     
    149139
    150140        // TODO: Temporary, find a better way and remove!
    151         struct nmd_temp_node_data
    152         {
    153                 key_animation_data* keys;
    154                 mat4                transform;
    155                 sint32              bone_id;
    156                 std::vector< nmd_temp_node_data* > children;
    157         };
    158141
    159142        class nmd_temp_model;
     
    171154                void animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat );
    172155
    173                 std::vector< key_animation_data* > m_data;
     156                std::vector< key_data* > m_data;
    174157                std::vector< sint16 > m_bone_ids;
    175158                std::vector< std::vector< uint32 > > m_children;
  • trunk/nv/gfx/animation.hh

    r284 r285  
    5252                }
    5353
     54                uint32 get_raw( uint32 index, float* result ) const
     55                {
     56                        if ( count == 0 ) return 0;
     57                        uint32 keyfsize   = desc.size / 4;
     58                        const float* fdata = ((const float*)data) + keyfsize * index;
     59                        uint32 mod        = 0;
     60                        if ( desc.slots[0].vslot == animation_slot::TIME ) mod = 1;
     61                        std::copy_n( fdata + mod, keyfsize - mod, result );
     62                        return keyfsize - mod;
     63                }
     64
    5465                uint32 interpolate_raw( float time, float* result ) const
    5566                {
     67                        if ( count == 0 ) return 0;
    5668                        uint32 keyfsize   = desc.size / 4;
    5769                        uint32 keyfresult = keyfsize;
    5870                        const float* fdata = (const float*)data;
    59                         if ( count == 0 ) return 0;
    6071
    6172                        uint32 slot = 0;
     
    129140                }
    130141
    131                 mat4 get_matrix( float time ) const
     142                mat4 get_raw_matrix( uint32 index ) const
    132143                {
    133144                        float key[ 16 ];
     
    135146                        for ( uint16 i = 0; i < m_channels.size(); ++i )
    136147                        {
    137                                 pkey += m_channels[i]->interpolate_raw( time, pkey );
     148                                pkey += m_channels[i]->get_raw( index, pkey );
    138149                        }
    139150                        return extract_matrix_raw( final_key, key );
    140151                };
    141152
    142                 transform get_transform( float time ) const
     153                transform get_raw_transform( uint32 index ) const
    143154                {
    144155                        float key[ 16 ];
     
    146157                        for ( uint16 i = 0; i < m_channels.size(); ++i )
    147158                        {
     159                                pkey += m_channels[i]->get_raw( index, pkey );
     160                        }
     161                        return extract_transform_raw( final_key, key );
     162                };
     163
     164                mat4 get_matrix( float time ) const
     165                {
     166                        float key[ 16 ];
     167                        float* pkey = key;
     168                        for ( uint16 i = 0; i < m_channels.size(); ++i )
     169                        {
    148170                                pkey += m_channels[i]->interpolate_raw( time, pkey );
    149171                        }
     172                        return extract_matrix_raw( final_key, key );
     173                };
     174
     175                transform get_transform( float time ) const
     176                {
     177                        float key[ 16 ];
     178                        float* pkey = key;
     179                        for ( uint16 i = 0; i < m_channels.size(); ++i )
     180                        {
     181                                pkey += m_channels[i]->interpolate_raw( time, pkey );
     182                        }
    150183                        return extract_transform_raw( final_key, key );
    151184                };
     185
     186                size_t get_channel_count() const { return m_channels.size(); }
     187                const key_raw_channel* get_channel( size_t index ) const { return m_channels[ index ]; }
    152188
    153189                virtual ~key_data()
     
    169205        public:
    170206                key_channel_interpolator() : m_data( nullptr ) {}
    171                 key_channel_interpolator( key_raw_channel* data ) : m_data( nullptr ) { set_data( data ); }
    172                 key_channel_interpolator( key_raw_channel* data, bool ) : m_data( data ) {}
    173                 void set_data( key_raw_channel* data )
     207                key_channel_interpolator( const key_raw_channel* data ) : m_data( nullptr ) { set_data( data ); }
     208                key_channel_interpolator( const key_raw_channel* data, bool ) : m_data( data ) {}
     209                void set_data( const key_raw_channel* data )
    174210                {
    175211                        m_data = data;
     
    194230
    195231        private:
    196                 key_raw_channel* m_data;
     232                const key_raw_channel* m_data;
    197233        };
    198234 
     
    202238        public:
    203239                key_channel_interpolator() : m_data( nullptr ) {}
    204                 key_channel_interpolator( key_raw_channel* data ) : m_data( nullptr ) { set_data( data ); }
    205                 void set_data( key_raw_channel* data )
     240                key_channel_interpolator( const key_raw_channel* data ) : m_data( nullptr ) { set_data( data ); }
     241                void set_data( const key_raw_channel* data )
    206242                {
    207243                        m_data = data;
     
    233269
    234270        private:
    235                 key_raw_channel* m_data;
     271                const key_raw_channel* m_data;
    236272        };
    237273 
     
    260296                struct key_s { float time; vec3 scale; };
    261297        public:
    262                 explicit key_vectors_prs( key_raw_channel* p, key_raw_channel* r, key_raw_channel* s )
     298                explicit key_vectors_prs( const key_raw_channel* p, const key_raw_channel* r, const key_raw_channel* s )
    263299                {
    264300                        m_pchannel = p;
     
    304340                }
    305341        protected:
    306                 key_raw_channel* m_pchannel;
    307                 key_raw_channel* m_rchannel;
    308                 key_raw_channel* m_schannel;
     342                const key_raw_channel* m_pchannel;
     343                const key_raw_channel* m_rchannel;
     344                const key_raw_channel* m_schannel;
    309345                key_channel_interpolator< key_p, true > m_pinter;
    310346                key_channel_interpolator< key_r, true > m_rinter;
     
    319355                };
    320356        public:
    321                 explicit transform_vector( key_raw_channel* channel )
     357                explicit transform_vector( const key_raw_channel* channel )
    322358                {
    323359                        key_descriptor kd;
     
    354390        protected:
    355391                key_channel_interpolator< key, false > m_interpolator;
    356                 key_raw_channel* m_channel;
     392                const key_raw_channel* m_channel;
    357393        };
    358394
  • trunk/nv/gfx/keyframed_mesh.hh

    r283 r285  
    4141                virtual void run_animation( animation_entry* a_anim );
    4242                size_t get_max_frames() const;
    43                 transform get_tag( const std::string& tag ) const;
     43                transform get_tag( uint32 tag_id ) const;
    4444                virtual void setup_animation( uint32 start, uint32 count, uint32 fps, bool loop );
    4545                virtual void set_frame( uint32 frame );
  • trunk/nv/interface/animated_mesh.hh

    r283 r285  
    4141                virtual void update_animation( animation_entry*, uint32 ) {}
    4242                virtual void run_animation( animation_entry* ) {}
    43                 virtual transform get_tag( const std::string& ) const { return transform(); }
     43                virtual transform get_tag( uint32 ) const { return transform(); }
     44
    4445        };
    4546
  • trunk/nv/interface/mesh_loader.hh

    r282 r285  
    3030        {
    3131        public:
    32                 typedef std::unordered_map< std::string, key_raw_channel* > map;
     32                typedef std::unordered_map< std::string, uint32 > map;
    3333
    34                 tag_map () {}
     34                tag_map() {}
    3535
    36                 const transform* get_tag( const std::string& key ) const
     36                const key_data* get_tag( uint32 id ) const
    3737                {
    38                         key_raw_channel* channel = m_map.at( key );
    39                         return ((transform*)(channel->data));
     38                        return m_data[id];
    4039                }
    41                 void insert( const std::string& key, key_raw_channel* chan )
     40                uint32 get_tag_id( const std::string& key ) const
    4241                {
    43                         m_map[ key ] = chan;
     42                        return m_map.at( key );
     43                }
     44                void insert( const std::string& key, key_data* chan )
     45                {
     46                        m_map[ key ] = m_data.size();
     47                        m_data.push_back( chan );
    4448                }
    4549                ~tag_map()
    4650                {
    47                         for ( auto t : m_map )
    48                                 delete t.second;
     51                        for ( auto t : m_data )
     52                                delete t;
    4953                }
    5054        private:
     55                std::vector< key_data* > m_data;
    5156                map m_map;
    5257        };
    5358
     59        struct mesh_node_data
     60        {
     61                string    name;
     62                sint16    target_id;
     63                sint16    parent_id;
     64                mat4      transform;
     65                key_data* data;
     66        };
    5467
    5568        class mesh_loader
     
    6275                virtual mesh_data* release_mesh_data( size_t index = 0 ) = 0;
    6376                virtual size_t get_mesh_count() const = 0;
     77                virtual mesh_node_data* release_mesh_node_data( size_t ) { return nullptr; }
     78                virtual size_t get_node_count() const { return 0; }
    6479        };
    6580
  • trunk/nv/interface/vertex.hh

    r284 r285  
    479479                }
    480480
    481                 bool operator==( const key_descriptor& rhs )
     481                bool operator==( const key_descriptor& rhs ) const
    482482                {
    483483                        if ( size  != rhs.size )  return false;
  • trunk/src/formats/assimp_loader.cc

    r284 r285  
    367367        if (this_id == 0)
    368368                a_data.transform = mat4();
    369         a_data.channel_count = 0;
     369        a_data.data = nullptr;
    370370
    371371        if (anode)
     
    394394        size_t max_keys = glm::max( node->mNumPositionKeys, node->mNumRotationKeys );
    395395
    396         data->channel_count = 1;
    397         data->channels[0] = key_raw_channel::create<assimp_key_tr>( max_keys );
    398         assimp_key_tr* channel = ((assimp_key_tr*)(data->channels[0]->data));
     396        key_raw_channel* raw_channel = key_raw_channel::create<assimp_key_tr>( max_keys );
     397        data->data = new key_data;
     398        data->data->add_channel( raw_channel );
     399        assimp_key_tr* channel = ((assimp_key_tr*)(raw_channel->data));
    399400
    400401        for ( unsigned n = 0; n < max_keys; ++n )
     
    406407                // TODO: only do the calculation when a rotate transform is present!
    407408                nv::transform ptr;
    408                 if ( parent )
    409                 {
    410                         key_raw_channel* pchannel = parent->channels[0];
    411                         if ( parent->channels[0] && parent->channels[0]->count > 0 )
    412                         {
    413                                 ptr = ((assimp_key_tr*)pchannel->data)[ glm::min( n, parent->channels[0]->count-1 ) ].tform;
     409                if ( parent && parent->data )
     410                {
     411                        const key_raw_channel* pchannel = parent->data->get_channel(0);
     412                        if ( pchannel && pchannel->count > 0 )
     413                        {
     414                                ptr = ((assimp_key_tr*)pchannel->data)[ glm::min( n, pchannel->count-1 ) ].tform;
    414415                        }
    415416                }
     
    422423void nv::assimp_loader::create_direct_keys( assimp_animated_node_data* data, const void* vnode )
    423424{
    424         data->channel_count = 0;
    425425        const aiNodeAnim* node = (const aiNodeAnim*)vnode;
    426426        if ( node->mNumPositionKeys == 0 && node->mNumRotationKeys == 0 && node->mNumScalingKeys == 0 )
     
    429429        }
    430430
    431         data->channel_count = 3;
    432         data->channels[0] = key_raw_channel::create<assimp_key_p>( node->mNumPositionKeys );
    433         data->channels[1] = key_raw_channel::create<assimp_key_r>( node->mNumRotationKeys );
    434         data->channels[2] = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys );
    435         assimp_key_p* pchannel = ((assimp_key_p*)(data->channels[0]->data));
    436         assimp_key_r* rchannel = ((assimp_key_r*)(data->channels[1]->data));
    437         assimp_key_s* schannel = ((assimp_key_s*)(data->channels[2]->data));
     431        data->data = new key_data;
     432        key_raw_channel* raw_pchannel = key_raw_channel::create<assimp_key_p>( node->mNumPositionKeys );
     433        key_raw_channel* raw_rchannel = key_raw_channel::create<assimp_key_r>( node->mNumRotationKeys );
     434        key_raw_channel* raw_schannel = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys );
     435        data->data->add_channel( raw_pchannel );
     436        data->data->add_channel( raw_rchannel );
     437        data->data->add_channel( raw_schannel );
     438        assimp_key_p* pchannel = ((assimp_key_p*)(raw_pchannel->data));
     439        assimp_key_r* rchannel = ((assimp_key_r*)(raw_rchannel->data));
     440        assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data));
    438441
    439442        for ( unsigned np = 0; np < node->mNumPositionKeys; ++np )
  • trunk/src/formats/md3_loader.cc

    r282 r285  
    407407        for ( sint32 i = 0; i < md3->header.num_tags; ++i )
    408408        {
    409                 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags];
     409                const md3_tag_t& rtag = md3->tags[i];
    410410                std::string name( (char*)(rtag.name) );
    411                 nv::key_raw_channel* keys = load_tags( name );
    412                 result->insert( name, keys );
     411
     412                key_data* data = new key_data;
     413                data->add_channel( load_tags( name ) );
     414                result->insert( name, data );
    413415        }
    414416        return result;
    415417}
    416418
     419mesh_node_data* nv::md3_loader::release_mesh_node_data( size_t index )
     420{
     421        md3_t* md3 = (md3_t*)m_md3;
     422        const md3_tag_t& rtag = md3->tags[index];
     423        std::string name( (char*)(rtag.name) );
     424
     425        mesh_node_data* result = new mesh_node_data;
     426        result->transform = mat4();
     427        result->name      = name;
     428        result->parent_id = -1;
     429        result->target_id = -1;
     430        result->data = new key_data;
     431       
     432        key_raw_channel* keys = load_tags( name );
     433        result->data->add_channel( keys );
     434        return result;
     435}
     436
     437size_t nv::md3_loader::get_node_count() const
     438{
     439        return ((md3_t*)m_md3)->header.num_tags;
     440}
     441
    417442size_t md3_loader::get_max_frames() const
    418443{
  • trunk/src/formats/nmd_loader.cc

    r284 r285  
    138138                m_animation->nodes[i].parent_id     = node_header.parent_id;
    139139                m_animation->nodes[i].transform     = node_header.transform;
    140                 m_animation->nodes[i].channel_count = ch_count;
    141                 m_animation->nodes[i].channels      = nullptr;
     140                m_animation->nodes[i].data          = nullptr;
    142141                if ( ch_count > 0 )
    143142                {
    144                         m_animation->nodes[i].channels      = new key_raw_channel* [ch_count];
     143                        key_data* kdata = new key_data;
     144                        m_animation->nodes[i].data = kdata;
    145145                        for ( uint32 c = 0; c < ch_count; ++c )
    146146                        {
     
    151151                                key_raw_channel* channel = key_raw_channel::create( cheader.format, cheader.count );
    152152                                source.read( channel->data, channel->desc.size, channel->count );
    153                                 m_animation->nodes[i].channels[c] = channel;
     153                                kdata->add_channel( channel );
    154154                        }
    155155                }
     
    168168        {
    169169                nmd_node& node = m_animation->nodes[n];
    170                 key_animation_data* keys = nullptr;
    171                 if ( node.channel_count > 1 )
    172                 {
    173                         keys = new nv::key_vectors_prs( node.channels[0], node.channels[1], node.channels[2] );
    174                 }
    175                 else if ( node.channel_count == 1 )
    176                 {
    177                         keys      =  new nv::transform_vector( node.channels[0] );
    178                         node.channels[0] = nullptr;
    179                         node.channel_count = 0;
    180                 }
    181                 m_data.push_back( keys );
     170                m_data.push_back( node.data );
     171                node.data = nullptr;
    182172        }
    183173
     
    239229                        nv::mat4 node_mat( node->transform );
    240230
    241                         if ( m_data[n] && !m_data[n]->empty() )
     231                        if ( m_data[n] )
    242232                        {
    243233                                node_mat = m_data[n]->get_matrix( anim_time );
     
    257247        mat4 node_mat( node->transform );
    258248
    259         if ( m_data[ node_id ] && !m_data[ node_id ]->empty() )
     249        if ( m_data[ node_id ] )
    260250        {
    261251                node_mat = m_data[ node_id ]->get_matrix( time );
  • trunk/src/gfx/keyframed_mesh.cc

    r283 r285  
    4040}
    4141
    42 transform keyframed_mesh::get_tag( const std::string& tag ) const
     42transform keyframed_mesh::get_tag( uint32 tag ) const
    4343{
    4444        NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
    45         const transform* transforms = m_tag_map->get_tag( tag );
    46         NV_ASSERT( transforms, "TAG FAIL" );
    47         return interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation  );
     45        const key_data* data = m_tag_map->get_tag( tag );
     46        NV_ASSERT( data, "TAG FAIL" );
     47        transform last = data->get_raw_transform( m_last_frame );
     48        transform next = data->get_raw_transform( m_next_frame );
     49        return interpolate( last, next, m_interpolation  );
    4850}
    4951
Note: See TracChangeset for help on using the changeset viewer.