Changeset 241
- Timestamp:
- 05/22/14 01:19:08 (11 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/formats/md3_loader.hh
r240 r241 36 36 virtual tag_map* create_tag_map(); 37 37 private: 38 void load_tags( std::vector<transform>& t, const std::string& tag );38 void load_tags( transform_vector& t, const std::string& tag ); 39 39 void* m_md3; 40 40 -
trunk/nv/formats/md5_loader.hh
r240 r241 61 61 size_t start_index; 62 62 }; 63 typedef std::vector<md5_joint_info> md5_joint_info_list;64 63 65 struct md5_ bound64 struct md5_joint 66 65 { 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 ); } 69 70 }; 70 typedef std::vector<md5_bound> md5_bound_list;71 72 struct md5_base_frame73 {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_data80 {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_joint87 {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_skeleton98 {99 md5_skeleton_joint_list joints;100 };101 typedef std::vector<md5_frame_skeleton> md5_frame_skeleton_list;102 103 71 104 72 public: … … 107 75 108 76 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; } 141 82 142 83 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 ); 154 86 155 87 private: … … 159 91 size_t m_frame_rate; 160 92 size_t m_num_animated_components; 93 float m_frame_duration; 94 float m_anim_duration; 95 }; 161 96 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; 165 114 }; 166 115 … … 169 118 public: 170 119 friend class md5_loader; 171 172 void apply( const md5_animation& animation );120 friend class md5_mesh_instance; 121 md5_mesh_instance* spawn() const; 173 122 private: 174 123 uint32* m_idata; … … 189 138 virtual mesh_data* release_mesh_data( size_t index = 0 ); 190 139 virtual size_t get_mesh_count() const { return m_meshes.size(); } 191 bool check_animation( const md5_animation& animation ) const;192 140 protected: 193 141 … … 195 143 { 196 144 std::string name; 197 int parent_id;198 145 vec3 pos; 199 146 quat orient; -
trunk/nv/gfx/skeletal_mesh.hh
r239 r241 36 36 public: 37 37 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(); } 39 39 virtual void run_animation( animation_entry* a_anim ); 40 40 virtual void setup_animation( md5_animation* a_anim ); … … 42 42 virtual ~skeletal_mesh(); 43 43 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; 46 49 }; 47 50 -
trunk/nv/interface/mesh_data.hh
r239 r241 43 43 return result; 44 44 } 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 } 45 54 }; 46 55 … … 65 74 result->count = count; 66 75 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; 67 86 result->data = (count > 0 ? ( new uint8[ result->size ] ) : nullptr ); 68 87 return result; -
trunk/nv/interface/mesh_loader.hh
r240 r241 19 19 #include <nv/transform.hh> 20 20 #include <nv/string.hh> 21 #include <nv/gfx/animation.hh> 21 22 #include <nv/interface/mesh_data.hh> 22 23 #include <nv/interface/stream.hh> … … 28 29 { 29 30 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; 32 32 33 33 tag_map () {} 34 34 map& get_map() { return m_map; } 35 35 const map& get_map() const { return m_map; } 36 const transform s* get_tag( const std::string& key ) const36 const transform_vector* get_tag( const std::string& key ) const 37 37 { 38 38 auto it = m_map.find( key ); -
trunk/nv/math.hh
r238 r241 216 216 }; 217 217 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 } 219 229 220 230 } // namespace nv -
trunk/nv/transform.hh
r230 r241 62 62 }; 63 63 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 ) 65 66 { 66 67 return transform( -
trunk/src/formats/md3_loader.cc
r240 r241 285 285 } 286 286 287 void nv::md3_loader::load_tags( std::vector<transform>& t, const std::string& tag )287 void nv::md3_loader::load_tags( transform_vector& t, const std::string& tag ) 288 288 { 289 289 md3_t* md3 = (md3_t*)m_md3; 290 t.clear();291 290 for ( sint32 f = 0; f < md3->header.num_frames; ++f ) 292 291 { … … 301 300 vec3 axisy ( md3_vec3( rtag.axis[2] ) ); 302 301 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 ) ) ) ); 304 303 } 305 304 } -
trunk/src/formats/md5_loader.cc
r240 r241 75 75 for ( size_t i = 0; i < m_num_joints; ++i ) 76 76 { 77 sstream >> joint.name >> joint.parent_id; 77 int parent_id; 78 sstream >> joint.name >> parent_id; 78 79 discard( sstream, "(" ); 79 80 sstream >> joint.pos.x >> joint.pos.y >> joint.pos.z; … … 290 291 , m_anim_duration( 0 ) 291 292 , m_frame_duration( 0 ) 292 , m_anim_time( 0 )293 293 { 294 294 … … 302 302 bool md5_animation::load_animation( stream& source ) 303 303 { 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; 309 306 m_num_frames = 0; 310 307 … … 332 329 { 333 330 sstream >> m_num_joints; 331 m_joints.reserve( m_num_joints ); 334 332 next_line( sstream ); 335 333 } … … 352 350 sstream >> joint.name >> joint.parent_id >> joint.flags >> joint.start_index; 353 351 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 ) ); 355 354 next_line( sstream ); 356 355 } … … 363 362 for ( size_t i = 0; i < m_num_frames; ++i ) 364 363 { 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; 366 388 discard( sstream, "(" ); 367 sstream >> bound.min.x >> bound.min.y >> bound.min.z;389 sstream >> pos.x >> pos.y >> pos.z; 368 390 discard( sstream, ")" ); 369 391 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; 374 393 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 ); 396 396 } 397 397 discard( sstream, "}" ); … … 400 400 else if ( command == "frame" ) 401 401 { 402 md5_frame_data frame; 403 sstream >> frame.frame_id; 402 std::vector<float> frame; 403 int frame_id; 404 sstream >> frame_id; 404 405 discard( sstream, "{" ); 405 406 next_line( sstream ); 406 407 407 frame. frame_data.reserve( m_num_animated_components );408 frame.reserve( m_num_animated_components ); 408 409 char buf[50]; 409 410 for ( size_t i = 0; i < m_num_animated_components; ++i ) 410 411 { 411 412 sstream >> buf; 412 frame. frame_data.push_back((float)atof(buf));413 frame.push_back((float)atof(buf)); 413 414 } 414 415 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 ); 418 417 419 418 discard( sstream, "}" ); … … 424 423 } 425 424 426 m_animated_skeleton.joints.assign( m_num_joints, md5_skeleton_joint() );427 425 428 426 m_frame_duration = 1.0f / (float)m_frame_rate; 429 427 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 );437 428 438 429 return true; 439 430 } 440 431 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 433 void 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; 451 438 size_t frame0 = (size_t)floorf( frame_num ); 452 439 size_t frame1 = (size_t)ceilf( frame_num ); … … 454 441 frame1 = frame1 % m_num_frames; 455 442 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 452 void 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(); 465 455 for ( unsigned int i = 0; i < joint_infos.size(); ++i ) 466 456 { … … 468 458 469 459 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 } 534 489 } 535 490 … … 541 496 } 542 497 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]; 498 md5_mesh_instance* nv::md5_mesh_data::spawn() const 499 { 500 return new md5_mesh_instance( this ); 501 } 502 503 nv::md5_loader::~md5_loader() 504 { 505 for ( auto m : m_meshes ) { if (m) delete m; } 506 } 507 508 nv::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 517 void 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]; 550 522 md5_vtx_pnt& result = m_pntdata[i]; 551 523 … … 556 528 for ( size_t j = 0; j < vert.weight_count; ++j ) 557 529 { 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 44 44 { 45 45 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 ); 47 47 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 ); 49 49 } 50 50 -
trunk/src/gfx/skeletal_mesh.cc
r240 r241 13 13 nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data ) 14 14 : animated_mesh() 15 , m_mesh_data( a_mesh_data)15 , m_mesh_data( nullptr ) 16 16 , m_animation( nullptr ) 17 , m_animation_time( 0 ) 17 18 { 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 ); 19 21 } 20 22 … … 22 24 void nv::skeletal_mesh::setup_animation( md5_animation* a_anim ) 23 25 { 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 } 28 33 } 29 34 30 35 void nv::skeletal_mesh::update( uint32 ms ) 31 36 { 32 if ( m_animation != nullptr)37 if ( m_animation ) 33 38 { 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 ); 36 45 vertex_buffer* vb = m_va->find_buffer( nv::POSITION ); 37 const mesh_raw_channel* pch = m_mesh_data->get_channel_data()[0];38 46 vb->bind(); 39 vb->update( (const void*)pch->data, 0, pch->size);47 vb->update( m_mesh_data->data(), 0, m_mesh_data->size() ); 40 48 vb->unbind(); 41 49 } … … 45 53 { 46 54 delete m_va; 47 // TODO : INSTANCE! 48 // delete m_mesh_data; 55 delete m_mesh_data; 49 56 } 50 57 … … 52 59 { 53 60 skeletal_animation_entry * anim = down_cast<skeletal_animation_entry>(a_anim); 54 // TODO : INSTANCE!55 61 setup_animation( anim->m_animation ); 56 62 }
Note: See TracChangeset
for help on using the changeset viewer.