Changeset 241


Ignore:
Timestamp:
05/22/14 01:19:08 (11 years ago)
Author:
epyon
Message:
  • significant simplification of the md5 code
  • proper instancing for both md5 animations and meshes
  • transform_vectors
Location:
trunk
Files:
1 added
11 edited

Legend:

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

    r240 r241  
    3636                virtual tag_map* create_tag_map();
    3737        private:
    38                 void load_tags( std::vector<transform>& t, const std::string& tag );
     38                void load_tags( transform_vector& t, const std::string& tag );
    3939                void* m_md3;
    4040
  • trunk/nv/formats/md5_loader.hh

    r240 r241  
    6161                        size_t      start_index;
    6262                };
    63                 typedef std::vector<md5_joint_info> md5_joint_info_list;
    6463
    65                 struct md5_bound
     64                struct md5_joint
    6665                {
    67                         glm::vec3 min;
    68                         glm::vec3 max;
     66                        int              parent;
     67                        transform_vector keys;
     68
     69                        md5_joint( int a_parent, size_t reserve ) : parent( a_parent ) { keys.reserve( reserve ); }
    6970                };
    70                 typedef std::vector<md5_bound> md5_bound_list;
    71 
    72                 struct md5_base_frame
    73                 {
    74                         glm::vec3 pos;
    75                         glm::quat orient;
    76                 };
    77                 typedef std::vector<md5_base_frame> md5_base_frame_list;
    78 
    79                 struct md5_frame_data
    80                 {
    81                         int frame_id;
    82                         std::vector<float> frame_data;
    83                 };
    84                 typedef std::vector<md5_frame_data> md5_frame_data_list;
    85 
    86                 struct md5_skeleton_joint
    87                 {
    88                         md5_skeleton_joint() : parent(-1), pos(0) {}
    89                         md5_skeleton_joint( const md5_base_frame& copy ) : pos( copy.pos ), orient( copy.orient ) {}
    90 
    91                         int       parent;
    92                         glm::vec3 pos;
    93                         glm::quat orient;
    94                 };
    95                 typedef std::vector<md5_skeleton_joint> md5_skeleton_joint_list;
    96 
    97                 struct md5_frame_skeleton
    98                 {
    99                         md5_skeleton_joint_list joints;
    100                 };
    101                 typedef std::vector<md5_frame_skeleton> md5_frame_skeleton_list;
    102 
    10371
    10472        public:
     
    10775
    10876                bool load_animation( stream& source );
    109                 void update( float delta_time );
    110                 void reset_animation()
    111                 {
    112                         m_anim_time = 0;
    113                 }
    114 
    115                 const md5_frame_skeleton& get_skeleton() const
    116                 {
    117                         return m_animated_skeleton;
    118                 }
    119 
    120                 size_t get_num_joints() const
    121                 {
    122                         return m_num_joints;
    123                 }
    124 
    125                 size_t get_frame_rate() const
    126                 {
    127                         return m_frame_rate;
    128                 }
    129 
    130                 size_t get_frame_count() const
    131                 {
    132                         return m_num_frames;
    133                 }
    134 
    135 
    136                 const md5_joint_info& get_joint_info( uint32 index ) const
    137                 {
    138                         assert( index < m_joint_infos.size() );
    139                         return m_joint_infos[index];
    140                 }
     77                void update_skeleton( std::vector<transform>& skeleton, float anim_time ) const;
     78               
     79                size_t get_num_joints() const { return m_num_joints; }
     80                size_t get_frame_rate() const { return m_frame_rate; }
     81                size_t get_frame_count() const { return m_num_frames; }
    14182
    14283        protected:
    143 
    144                 md5_joint_info_list     m_joint_infos;
    145                 md5_bound_list          m_bounds;
    146                 md5_base_frame_list     m_base_frames;
    147                 md5_frame_data_list     m_frames;
    148                 md5_frame_skeleton_list m_skeletons;
    149 
    150                 md5_frame_skeleton      m_animated_skeleton;
    151 
    152                 void build_frame_skeleton( md5_frame_skeleton_list& skeletons, const md5_joint_info_list& joint_info, const md5_base_frame_list& base_frames, const md5_frame_data& frame_data );
    153                 void interpolate_skeletons( md5_frame_skeleton& final_skeleton, const md5_frame_skeleton& skeleton0, const md5_frame_skeleton& skeleton1, float interpolate );
     84                std::vector<md5_joint>  m_joints;
     85                void build_frame_skeleton( const std::vector<md5_joint_info>& joint_info, const std::vector<transform>& base_frames, const std::vector<float>& frame_data );
    15486
    15587        private:
     
    15991                size_t m_frame_rate;
    16092                size_t m_num_animated_components;
     93                float m_frame_duration;
     94                float m_anim_duration;
     95        };
    16196
    162                 float m_anim_duration;
    163                 float m_frame_duration;
    164                 float m_anim_time;
     97        class md5_mesh_instance
     98        {
     99                friend class md5_mesh_data;
     100        public:
     101                const void* data() const { return m_pntdata; }
     102                uint32 get_index_count() const { return m_indices; }
     103                size_t size() const { return m_size * sizeof( md5_vtx_pnt ); }
     104                void apply( const std::vector< transform >& skeleton );
     105                const md5_mesh_data* get_mesh_data() const { return m_data; }
     106                ~md5_mesh_instance() { delete[] m_pntdata; }
     107        private:
     108                md5_mesh_instance( const md5_mesh_data* a_data );
     109
     110                uint32               m_size;
     111                uint32               m_indices;
     112                md5_vtx_pnt*         m_pntdata;
     113                const md5_mesh_data* m_data;
    165114        };
    166115
     
    169118        public:
    170119                friend class md5_loader;
    171 
    172                 void apply( const md5_animation& animation );
     120                friend class md5_mesh_instance;
     121                md5_mesh_instance* spawn() const;
    173122        private:
    174123                uint32*                     m_idata;
     
    189138                virtual mesh_data* release_mesh_data( size_t index = 0 );
    190139                virtual size_t get_mesh_count() const { return m_meshes.size(); }
    191                 bool check_animation( const md5_animation& animation ) const;
    192140        protected:
    193141
     
    195143                {
    196144                        std::string name;
    197                         int   parent_id;
    198145                        vec3  pos;
    199146                        quat  orient;
  • trunk/nv/gfx/skeletal_mesh.hh

    r239 r241  
    3636        public:
    3737                skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data );
    38                 virtual size_t get_index_count() const { return m_mesh_data->get_count(); }
     38                virtual size_t get_index_count() const { return m_mesh_data->get_index_count(); }
    3939                virtual void run_animation( animation_entry* a_anim );
    4040                virtual void setup_animation( md5_animation* a_anim );
     
    4242                virtual ~skeletal_mesh();
    4343        protected:
    44                 md5_mesh_data* m_mesh_data;
    45                 md5_animation* m_animation;
     44                md5_mesh_instance* m_mesh_data;
     45                md5_animation*     m_animation;
     46
     47                std::vector< transform > m_transform;
     48                uint32                   m_animation_time;
    4649        };
    4750
  • trunk/nv/interface/mesh_data.hh

    r239 r241  
    4343                        return result;
    4444                }
     45                static mesh_raw_channel* create( const vertex_descriptor& vtxdesc, uint32 count = 0 )
     46                {
     47                        mesh_raw_channel* result = new mesh_raw_channel();
     48                        result->desc  = vtxdesc;
     49                        result->count = count;
     50                        result->size  = count * sizeof( vtxdesc.size );
     51                        result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     52                        return result;
     53                }
    4554        };
    4655
     
    6574                        result->count = count;
    6675                        result->size  = count * sizeof( ITYPE );
     76                        result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
     77                        return result;
     78                }
     79
     80                static mesh_raw_index_channel* create( datatype etype, uint32 count = 0 )
     81                {
     82                        mesh_raw_index_channel* result = new mesh_raw_index_channel();
     83                        result->etype = etype;
     84                        result->count = count;
     85                        result->size  = count * get_datatype_info( etype ).size;
    6786                        result->data  = (count > 0 ? ( new uint8[ result->size ] ) : nullptr );
    6887                        return result;
  • trunk/nv/interface/mesh_loader.hh

    r240 r241  
    1919#include <nv/transform.hh>
    2020#include <nv/string.hh>
     21#include <nv/gfx/animation.hh>
    2122#include <nv/interface/mesh_data.hh>
    2223#include <nv/interface/stream.hh>
     
    2829        {
    2930        public:
    30                 typedef std::vector< transform > transforms;
    31                 typedef std::unordered_map< std::string, transforms > map;
     31                typedef std::unordered_map< std::string, transform_vector > map;
    3232
    3333                tag_map () {}
    3434                map& get_map()             { return m_map; }
    3535                const map& get_map() const { return m_map; }
    36                 const transforms* get_tag( const std::string& key ) const
     36                const transform_vector* get_tag( const std::string& key ) const
    3737                {
    3838                        auto it = m_map.find( key );
  • trunk/nv/math.hh

    r238 r241  
    216216        };
    217217
    218 
     218        template < typename T >
     219        T interpolate( const T& lhs, const T& rhs, float f )
     220        {
     221                return glm::mix( lhs, rhs, f );
     222        }
     223
     224        template <>
     225        inline quat interpolate( const quat& lhs, const quat& rhs, float f )
     226        {
     227                return glm::slerp( lhs, rhs, f );
     228        }
    219229
    220230} // namespace nv
  • trunk/nv/transform.hh

    r230 r241  
    6262        };
    6363
    64         inline transform interpolate( const transform& a, const transform& b, f32 value )
     64        template<>
     65        inline transform interpolate( const transform& a, const transform& b, float value )
    6566        {
    6667                return transform(
  • trunk/src/formats/md3_loader.cc

    r240 r241  
    285285}
    286286
    287 void nv::md3_loader::load_tags( std::vector<transform>& t, const std::string& tag )
     287void nv::md3_loader::load_tags( transform_vector& t, const std::string& tag )
    288288{
    289289        md3_t* md3 = (md3_t*)m_md3;
    290         t.clear();
    291290        for ( sint32 f = 0; f < md3->header.num_frames; ++f )
    292291        {
     
    301300                                vec3 axisy  ( md3_vec3( rtag.axis[2] ) );
    302301                                vec3 origin ( md3_vec3( rtag.origin )  );
    303                                 t.emplace_back( origin, quat( mat3( axisx, axisy, axisz ) ) );
     302                                t.insert( transform( origin, quat( mat3( axisx, axisy, axisz ) ) ) );
    304303                        }
    305304                }
  • trunk/src/formats/md5_loader.cc

    r240 r241  
    7575                        for ( size_t i = 0; i < m_num_joints; ++i )
    7676                        {
    77                                 sstream >> joint.name >> joint.parent_id;
     77                                int parent_id;
     78                                sstream >> joint.name >> parent_id;
    7879                                discard( sstream, "(" );
    7980                                sstream >> joint.pos.x >> joint.pos.y >> joint.pos.z;
     
    290291        , m_anim_duration( 0 )
    291292        , m_frame_duration( 0 )
    292         , m_anim_time( 0 )
    293293{
    294294
     
    302302bool md5_animation::load_animation( stream& source )
    303303{
    304         m_joint_infos.clear();
    305         m_bounds.clear();
    306         m_base_frames.clear();
    307         m_frames.clear();
    308         m_animated_skeleton.joints.clear();
     304        std::vector<md5_joint_info> joint_infos;
     305        std::vector<transform>      base_frames;
    309306        m_num_frames = 0;
    310307
     
    332329                {
    333330                        sstream >> m_num_joints;
     331                        m_joints.reserve( m_num_joints );
    334332                        next_line( sstream );
    335333                }
     
    352350                                sstream >> joint.name >> joint.parent_id >> joint.flags >> joint.start_index;
    353351                                remove_quotes( joint.name );
    354                                 m_joint_infos.push_back( joint );
     352                                joint_infos.push_back( joint );
     353                                m_joints.push_back( md5_joint( joint.parent_id, m_num_frames ) );
    355354                                next_line( sstream );
    356355                        }
     
    363362                        for ( size_t i = 0; i < m_num_frames; ++i )
    364363                        {
    365                                 md5_bound bound;
     364//                              vec3 min;
     365//                              vec3 max;
     366//                              discard( sstream, "(" );
     367//                              sstream >> min.x >> min.y >> min.z;
     368//                              discard( sstream, ")" );
     369//                              discard( sstream, "(" );
     370//                              sstream >> max.x >> max.y >> max.z;
     371//                              m_bounds.push_back( bound );
     372                                next_line( sstream );
     373                        }
     374
     375                        discard( sstream, "}" );
     376                        next_line( sstream );
     377                }
     378                else if ( command == "baseframe" )
     379                {
     380                        discard( sstream, "{" );
     381                        next_line( sstream );
     382
     383                        for ( size_t i = 0; i < m_num_joints; ++i )
     384                        {
     385                                transform base_frame;
     386                                vec3 pos;
     387                                quat orient;
    366388                                discard( sstream, "(" );
    367                                 sstream >> bound.min.x >> bound.min.y >> bound.min.z;
     389                                sstream >> pos.x >> pos.y >> pos.z;
    368390                                discard( sstream, ")" );
    369391                                discard( sstream, "(" );
    370                                 sstream >> bound.max.x >> bound.max.y >> bound.max.z;
    371 
    372                                 m_bounds.push_back( bound );
    373 
     392                                sstream >> orient.x >> orient.y >> orient.z;
    374393                                next_line( sstream );
    375                         }
    376 
    377                         discard( sstream, "}" );
    378                         next_line( sstream );
    379                 }
    380                 else if ( command == "baseframe" )
    381                 {
    382                         discard( sstream, "{" );
    383                         next_line( sstream );
    384 
    385                         for ( size_t i = 0; i < m_num_joints; ++i )
    386                         {
    387                                 md5_base_frame base_frame;
    388                                 discard( sstream, "(" );
    389                                 sstream >> base_frame.pos.x >> base_frame.pos.y >> base_frame.pos.z;
    390                                 discard( sstream, ")" );
    391                                 discard( sstream, "(" );
    392                                 sstream >> base_frame.orient.x >> base_frame.orient.y >> base_frame.orient.z;
    393                                 next_line( sstream );
    394 
    395                                 m_base_frames.push_back( base_frame );
     394
     395                                base_frames.emplace_back( pos, orient );
    396396                        }
    397397                        discard( sstream, "}" );
     
    400400                else if ( command == "frame" )
    401401                {
    402                         md5_frame_data frame;
    403                         sstream >> frame.frame_id;
     402                        std::vector<float> frame;
     403                        int frame_id;
     404                        sstream >> frame_id;
    404405                        discard( sstream, "{" );
    405406                        next_line( sstream );
    406407
    407                         frame.frame_data.reserve( m_num_animated_components );
     408                        frame.reserve( m_num_animated_components );
    408409                        char buf[50];
    409410                        for ( size_t i = 0; i < m_num_animated_components; ++i )
    410411                        {
    411412                                sstream >> buf;
    412                                 frame.frame_data.push_back((float)atof(buf));
     413                                frame.push_back((float)atof(buf));
    413414                        }
    414415
    415                         m_frames.push_back(frame);
    416 
    417                         build_frame_skeleton( m_skeletons, m_joint_infos, m_base_frames, frame );
     416                        build_frame_skeleton( joint_infos, base_frames, frame );
    418417
    419418                        discard( sstream, "}" );
     
    424423        }
    425424
    426         m_animated_skeleton.joints.assign( m_num_joints, md5_skeleton_joint() );
    427425
    428426        m_frame_duration = 1.0f / (float)m_frame_rate;
    429427        m_anim_duration = ( m_frame_duration * (float)m_num_frames );
    430         m_anim_time = 0.0f;
    431 
    432         assert( m_joint_infos.size() == m_num_joints );
    433         assert( m_bounds.size()      == m_num_frames );
    434         assert( m_base_frames.size() == m_num_joints );
    435         assert( m_frames.size()      == m_num_frames );
    436         assert( m_skeletons.size()   == m_num_frames );
    437428
    438429        return true;
    439430}
    440431
    441 void md5_animation::update( float delta_time )
    442 {
    443         if ( m_num_frames < 1 ) return;
    444 
    445         m_anim_time += delta_time;
    446 
    447         while ( m_anim_time > m_anim_duration ) m_anim_time -= m_anim_duration;
    448         while ( m_anim_time < 0.0f ) m_anim_time += m_anim_duration;
    449 
    450         float frame_num = m_anim_time * (float)m_frame_rate;
     432
     433void nv::md5_animation::update_skeleton( std::vector<transform>& skeleton, float anim_time ) const
     434{
     435        NV_ASSERT( skeleton.size() == m_num_joints, "Incompatible skeleton passed!" );
     436        anim_time = glm::clamp( anim_time, 0.0f, m_anim_duration );
     437        float frame_num = anim_time * (float)m_frame_rate;
    451438        size_t frame0 = (size_t)floorf( frame_num );
    452439        size_t frame1 = (size_t)ceilf( frame_num );
     
    454441        frame1 = frame1 % m_num_frames;
    455442
    456         float interpolate = fmodf( m_anim_time, m_frame_duration ) / m_frame_duration;
    457 
    458         interpolate_skeletons( m_animated_skeleton, m_skeletons[frame0], m_skeletons[frame1], interpolate );
    459 }
    460 
    461 void md5_animation::build_frame_skeleton( md5_frame_skeleton_list& skeletons, const md5_joint_info_list& joint_infos, const md5_base_frame_list& base_frames, const md5_frame_data& frame_data )
    462 {
    463         md5_frame_skeleton skeleton;
    464 
     443        float interpolation = fmodf( anim_time, m_frame_duration ) / m_frame_duration;
     444
     445        for ( size_t i = 0; i < m_num_joints; ++i )
     446        {
     447                const transform_vector& keys = m_joints[i].keys;
     448                skeleton[i] = interpolate( keys.get(frame0), keys.get(frame1), interpolation );
     449        }
     450}
     451
     452void md5_animation::build_frame_skeleton( const std::vector<md5_joint_info>& joint_infos, const std::vector<transform>& base_frames, const std::vector<float>& frame_data )
     453{
     454        size_t index = m_joints[0].keys.size();
    465455        for ( unsigned int i = 0; i < joint_infos.size(); ++i )
    466456        {
     
    468458
    469459                const md5_joint_info& jinfo = joint_infos[i];
    470                 md5_skeleton_joint animated_joint = base_frames[i];
    471 
    472                 animated_joint.parent = jinfo.parent_id;
    473 
    474                 if ( jinfo.flags & 1 )  animated_joint.pos.x    = frame_data.frame_data[ jinfo.start_index + j++ ];
    475                 if ( jinfo.flags & 2 )  animated_joint.pos.y    = frame_data.frame_data[ jinfo.start_index + j++ ];
    476                 if ( jinfo.flags & 4 )  animated_joint.pos.z    = frame_data.frame_data[ jinfo.start_index + j++ ];
    477                 if ( jinfo.flags & 8 )  animated_joint.orient.x = frame_data.frame_data[ jinfo.start_index + j++ ];
    478                 if ( jinfo.flags & 16 ) animated_joint.orient.y = frame_data.frame_data[ jinfo.start_index + j++ ];
    479                 if ( jinfo.flags & 32 ) animated_joint.orient.z = frame_data.frame_data[ jinfo.start_index + j++ ];
    480 
    481                 unit_quat_w( animated_joint.orient );
    482 
    483                 if ( animated_joint.parent >= 0 ) // Has a parent joint
    484                 {
    485                         md5_skeleton_joint& pjoint = skeleton.joints[static_cast< size_t >( animated_joint.parent ) ];
    486                         glm::vec3 rot_pos = pjoint.orient * animated_joint.pos;
    487 
    488                         animated_joint.pos    = pjoint.pos + rot_pos;
    489                         animated_joint.orient = pjoint.orient * animated_joint.orient;
    490 
    491                         animated_joint.orient = glm::normalize( animated_joint.orient );
    492                 }
    493 
    494                 skeleton.joints.push_back( animated_joint );
    495         }
    496 
    497         skeletons.push_back( skeleton );
    498 }
    499 
    500 void md5_animation::interpolate_skeletons( md5_frame_skeleton& final_skeleton, const md5_frame_skeleton& skeleton0, const md5_frame_skeleton& skeleton1, float interpolate )
    501 {
    502         for ( size_t i = 0; i < m_num_joints; ++i )
    503         {
    504                 md5_skeleton_joint& final_joint = final_skeleton.joints[i];
    505                 const md5_skeleton_joint& joint0 = skeleton0.joints[i];
    506                 const md5_skeleton_joint& joint1 = skeleton1.joints[i];
    507 
    508                 final_joint.parent = joint0.parent;
    509 
    510                 final_joint.orient = glm::slerp( joint0.orient, joint1.orient, interpolate );
    511                 final_joint.pos    = glm::mix( joint0.pos, joint1.pos, interpolate );
    512         }
    513 }
    514 
    515 bool md5_loader::check_animation( const md5_animation& animation ) const
    516 {
    517         if ( m_num_joints != animation.get_num_joints() )
    518         {
    519                 return false;
    520         }
    521 
    522         for ( uint32 i = 0; i < m_joints.size(); ++i )
    523         {
    524                 const md5_joint& mjoint = m_joints[i];
    525                 const md5_animation::md5_joint_info& ajoint = animation.get_joint_info( i );
    526 
    527                 if ( mjoint.name != ajoint.name || mjoint.parent_id != ajoint.parent_id )
    528                 {
    529                         return false;
    530                 }
    531         }
    532 
    533         return true;
     460
     461
     462                int parent_id = jinfo.parent_id;
     463
     464                vec3 pos    = base_frames[i].get_position();
     465                quat orient = base_frames[i].get_orientation();
     466                if ( jinfo.flags & 1 )  pos.x    = frame_data[ jinfo.start_index + j++ ];
     467                if ( jinfo.flags & 2 )  pos.y    = frame_data[ jinfo.start_index + j++ ];
     468                if ( jinfo.flags & 4 )  pos.z    = frame_data[ jinfo.start_index + j++ ];
     469                if ( jinfo.flags & 8 )  orient.x = frame_data[ jinfo.start_index + j++ ];
     470                if ( jinfo.flags & 16 ) orient.y = frame_data[ jinfo.start_index + j++ ];
     471                if ( jinfo.flags & 32 ) orient.z = frame_data[ jinfo.start_index + j++ ];
     472                unit_quat_w( orient );
     473
     474                if ( parent_id >= 0 ) // Has a parent joint
     475                {
     476                        const transform_vector& ptv = m_joints[ size_t( parent_id ) ].keys;
     477                        transform ptr;
     478                        if ( ptv.size() > index ) ptr = ptv.get( index );
     479                        glm::vec3 rot_pos = ptr.get_orientation() * pos;
     480
     481                        pos    = ptr.get_position() + rot_pos;
     482                        orient = ptr.get_orientation() * orient;
     483
     484                        orient = glm::normalize( orient );
     485                }
     486
     487                m_joints[i].keys.insert( transform( pos, orient ) );
     488        }
    534489}
    535490
     
    541496}
    542497
    543 void nv::md5_mesh_data::apply( const md5_animation& animation )
    544 {
    545         const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton();
    546 
    547         for ( unsigned int i = 0; i < m_vtx_data.size(); ++i )
    548         {
    549                 const md5_vtx_data& vert = m_vtx_data[i];
     498md5_mesh_instance* nv::md5_mesh_data::spawn() const
     499{
     500        return new md5_mesh_instance( this );
     501}
     502
     503nv::md5_loader::~md5_loader()
     504{
     505        for ( auto m : m_meshes ) { if (m) delete m; }
     506}
     507
     508nv::md5_mesh_instance::md5_mesh_instance( const md5_mesh_data* a_data )
     509        : m_data( a_data ), m_size( 0 ), m_indices( 0 ), m_pntdata( nullptr )
     510{
     511        m_size = m_data->m_vtx_data.size();
     512        m_indices = m_data->get_count();
     513        m_pntdata = new md5_vtx_pnt[ m_size ];
     514        std::copy_n( m_data->m_pntdata, m_size, m_pntdata );
     515}
     516
     517void nv::md5_mesh_instance::apply( const std::vector< transform >& skeleton )
     518{
     519        for ( unsigned int i = 0; i < m_size; ++i )
     520        {
     521                const md5_vtx_data& vert = m_data->m_vtx_data[i];
    550522                md5_vtx_pnt& result = m_pntdata[i];
    551523
     
    556528                for ( size_t j = 0; j < vert.weight_count; ++j )
    557529                {
    558                         const md5_weight& weight = m_weights[vert.start_weight + j];
    559                         const md5_animation::md5_skeleton_joint& joint = skeleton.joints[weight.joint_id];
    560 
    561                         glm::vec3 rot_pos = joint.orient * weight.pos;
    562                         result.position += ( joint.pos + rot_pos ) * weight.bias;
    563 
    564                         result.normal  += ( joint.orient * vert.normal  ) * weight.bias;
    565                         result.tangent += ( joint.orient * vert.tangent ) * weight.bias;
    566                 }
    567         }
    568 }
    569 
    570 nv::md5_loader::~md5_loader()
    571 {
    572         for ( auto m : m_meshes ) { if (m) delete m; }
    573 }
     530                        const md5_weight& weight = m_data->m_weights[vert.start_weight + j];
     531                        const transform& joint = skeleton[weight.joint_id];
     532
     533                        glm::vec3 rot_pos = joint.get_orientation() * weight.pos;
     534                        result.position += ( joint.get_position() + rot_pos ) * weight.bias;
     535
     536                        result.normal  += ( joint.get_orientation() * vert.normal  ) * weight.bias;
     537                        result.tangent += ( joint.get_orientation() * vert.tangent ) * weight.bias;
     538                }
     539        }
     540}
  • trunk/src/gfx/keyframed_mesh.cc

    r239 r241  
    4444{
    4545        NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
    46         const std::vector< transform >* transforms = m_tag_map->get_tag( tag );
     46        const transform_vector* transforms = m_tag_map->get_tag( tag );
    4747        NV_ASSERT( transforms, "TAG FAIL" );
    48         return interpolate( (*transforms)[ m_last_frame ], (*transforms)[ m_next_frame ], m_interpolation );
     48        return interpolate( transforms->get( m_last_frame ), transforms->get( m_next_frame ), m_interpolation );
    4949}
    5050
  • trunk/src/gfx/skeletal_mesh.cc

    r240 r241  
    1313nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data )
    1414        : animated_mesh()
    15         , m_mesh_data( a_mesh_data )
     15        , m_mesh_data( nullptr )
    1616        , m_animation( nullptr )
     17        , m_animation_time( 0 )
    1718{
    18         m_va = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
     19        m_mesh_data = a_mesh_data->spawn();
     20        m_va        = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
    1921}
    2022
     
    2224void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
    2325{
    24         if ( m_animation ) m_animation->reset_animation();
    25         m_animation = a_anim;
    26         // TODO : INSTANCE!
    27         m_animation->reset_animation();
     26        m_animation      = a_anim;
     27        m_animation_time = 0;
     28        if ( m_animation )
     29        {
     30                m_transform.resize( m_animation->get_num_joints() );
     31                update( 0 );
     32        }
    2833}
    2934
    3035void nv::skeletal_mesh::update( uint32 ms )
    3136{
    32         if (m_animation != nullptr)
     37        if ( m_animation )
    3338        {
    34                 m_animation->update( ms * 0.001f );
    35                 m_mesh_data->apply( *m_animation );
     39                m_animation_time += ms;
     40                float frame_duration = 1000.f / (float)m_animation->get_frame_rate();
     41                uint32 anim_duration = uint32( frame_duration * (float)m_animation->get_frame_count() );
     42                while ( m_animation_time >= anim_duration ) m_animation_time -= anim_duration;
     43                m_animation->update_skeleton( m_transform, (float)m_animation_time * 0.001f );
     44                m_mesh_data->apply( m_transform );
    3645                vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
    37                 const mesh_raw_channel* pch = m_mesh_data->get_channel_data()[0];
    3846                vb->bind();
    39                 vb->update( (const void*)pch->data, 0, pch->size );
     47                vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
    4048                vb->unbind();
    4149        }
     
    4553{
    4654        delete m_va;
    47         // TODO : INSTANCE!
    48         //      delete m_mesh_data;
     55        delete m_mesh_data;
    4956}
    5057
     
    5259{
    5360        skeletal_animation_entry * anim = down_cast<skeletal_animation_entry>(a_anim);
    54         // TODO : INSTANCE!
    5561        setup_animation( anim->m_animation );
    5662}
Note: See TracChangeset for help on using the changeset viewer.