Changeset 287 for trunk/src/formats
- Timestamp:
- 07/23/14 15:24:03 (11 years ago)
- Location:
- trunk/src/formats
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/formats/assimp_loader.cc
r285 r287 79 79 { 80 80 if ( index >= m_mesh_count ) return nullptr; 81 mesh_data* result = new mesh_data(); 81 mesh_data* result = new mesh_data; 82 load_mesh_data( result, index ); 83 return result; 84 } 85 void nv::assimp_loader::load_mesh_data( mesh_data* data, size_t index ) 86 { 82 87 const aiScene* scene = (const aiScene*)m_scene; 83 88 const aiMesh* mesh = scene->mMeshes[ index ]; 89 data->set_name( mesh->mName.data ); 84 90 85 91 vec3 vertex_offset = glm::vec3(); … … 94 100 channel = mesh_raw_channel::create< assimp_plain_vtx >( mesh->mNumVertices ); 95 101 96 result->add_channel( channel );102 data->add_channel( channel ); 97 103 for (unsigned int i=0; i<mesh->mNumVertices; i++) 98 104 { … … 139 145 140 146 mesh_raw_channel* ichannel = mesh_raw_channel::create_index( USHORT, mesh->mNumFaces * 3 ); 141 result->add_channel( ichannel );147 data->add_channel( ichannel ); 142 148 uint16* indices = (uint16*)ichannel->data; 143 149 for (unsigned int i=0; i<mesh->mNumFaces; i++) … … 149 155 } 150 156 } 151 152 return result;153 157 } 154 158 … … 322 326 const aiAnimation* anim = scene->mAnimations[0]; // if implemented, change in load_node also 323 327 324 result->f ps = (float)anim->mTicksPerSecond;328 result->frame_rate = (uint16)anim->mTicksPerSecond; 325 329 result->duration = (float)anim->mDuration; 326 result->pretransformed = pre_transform; 330 result->flat = pre_transform; 331 result->max_frames = 0; 327 332 328 333 load_node( result, root, 0, -1 ); 334 335 // DAE pre_transform hack 336 if ( result->flat && result->frame_rate == 1 ) 337 { 338 result->frame_rate = 32; 339 result->duration = (float)result->max_frames; 340 } 341 329 342 return result; 330 343 } … … 371 384 if (anode) 372 385 { 373 if ( data-> pretransformed)386 if ( data->flat ) 374 387 { 375 388 create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(data->nodes[ parent_id ]) : nullptr ); … … 379 392 create_direct_keys( &a_data, anode ); 380 393 } 394 data->max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, data->max_frames ); 381 395 } 382 396 … … 386 400 next = load_node( data, node->mChildren[i], next, this_id ); 387 401 } 402 388 403 return next; 389 404 } … … 432 447 key_raw_channel* raw_pchannel = key_raw_channel::create<assimp_key_p>( node->mNumPositionKeys ); 433 448 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 );449 //key_raw_channel* raw_schannel = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys ); 435 450 data->data->add_channel( raw_pchannel ); 436 451 data->data->add_channel( raw_rchannel ); 437 data->data->add_channel( raw_schannel );452 //data->data->add_channel( raw_schannel ); 438 453 assimp_key_p* pchannel = ((assimp_key_p*)(raw_pchannel->data)); 439 454 assimp_key_r* rchannel = ((assimp_key_r*)(raw_rchannel->data)); 440 assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data));455 //assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data)); 441 456 442 457 for ( unsigned np = 0; np < node->mNumPositionKeys; ++np ) … … 450 465 rchannel[np].rotation = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[np].mValue ) ) * m_ri33 ); 451 466 } 452 if ( node->mNumScalingKeys > 0 ) 453 { 454 nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue ); 455 float scale_value = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) ); 456 if ( node->mNumScalingKeys > 1 || scale_value > 0.001 ) 457 { 458 NV_LOG( nv::LOG_WARNING, "scale key significant!" ); 459 for ( unsigned np = 0; np < node->mNumRotationKeys; ++np ) 460 { 461 schannel[np].time = (float)node->mScalingKeys[np].mTime; 462 schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue); 463 } 464 } 465 else 466 { 467 schannel[0].time = (float)node->mScalingKeys[0].mTime; 468 schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue); 469 } 470 } 471 472 } 473 467 // if ( node->mNumScalingKeys > 0 ) 468 // { 469 // nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue ); 470 // float scale_value = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) ); 471 // if ( node->mNumScalingKeys > 1 || scale_value > 0.001 ) 472 // { 473 // NV_LOG( nv::LOG_WARNING, "scale key significant!" ); 474 // for ( unsigned np = 0; np < node->mNumRotationKeys; ++np ) 475 // { 476 // schannel[np].time = (float)node->mScalingKeys[np].mTime; 477 // schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue); 478 // } 479 // } 480 // else 481 // { 482 // schannel[0].time = (float)node->mScalingKeys[0].mTime; 483 // schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue); 484 // } 485 // } 486 487 } 488 489 mesh_data_pack* nv::assimp_loader::release_mesh_data_pack() 490 { 491 mesh_data* meshes = new mesh_data[ m_mesh_count ]; 492 for ( uint32 i = 0; i < m_mesh_count; ++i ) 493 { 494 load_mesh_data(&meshes[i],i); 495 } 496 return new mesh_data_pack( m_mesh_count, meshes, release_mesh_nodes_data() ); 497 } 498 499 mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data() 500 { 501 // TODO: implement 502 return nullptr; 503 } 504 -
trunk/src/formats/md2_loader.cc
r280 r287 315 315 mesh_data* nv::md2_loader::release_mesh_data( size_t ) 316 316 { 317 return release_mesh_frame( -1 ); 318 } 319 320 mesh_data* nv::md2_loader::release_mesh_frame( sint32 frame ) 317 mesh_data* data = new mesh_data( "md2_mesh" ); 318 release_mesh_frame( data, -1 ); 319 return data; 320 } 321 322 void nv::md2_loader::release_mesh_frame( mesh_data* data, sint32 frame ) 321 323 { 322 324 md2_t* md2 = (md2_t*)m_md2; … … 366 368 } 367 369 368 mesh_data* result = new mesh_data(); 369 result->add_channel( mc_pn ); 370 result->add_channel( mc_t ); 371 result->add_channel( ic ); 372 return result; 373 } 370 data->add_channel( mc_pn ); 371 data->add_channel( mc_t ); 372 data->add_channel( ic ); 373 } 374 375 mesh_data_pack* nv::md2_loader::release_mesh_data_pack() 376 { 377 mesh_data* data = new mesh_data[1]; 378 release_mesh_frame( &data[0], -1 ); 379 return new mesh_data_pack( 1, data ); 380 } -
trunk/src/formats/md3_loader.cc
r285 r287 321 321 mesh_data* nv::md3_loader::release_mesh_data( size_t ) 322 322 { 323 return release_mesh_frame( -1 ); 324 } 325 326 mesh_data* nv::md3_loader::release_mesh_frame( sint32 frame ) 323 mesh_data* data = new mesh_data; 324 release_mesh_frame( data, -1 ); 325 return data; 326 } 327 328 void nv::md3_loader::release_mesh_frame( mesh_data* data, sint32 frame ) 327 329 { 328 330 md3_t* md3 = (md3_t*)m_md3; … … 394 396 } 395 397 396 mesh_data* result = new mesh_data(); 397 result->add_channel( mc_pn ); 398 result->add_channel( mc_t ); 399 result->add_channel( ic ); 400 return result; 401 } 402 403 tag_map* nv::md3_loader::create_tag_map() 398 data->set_name( (char*)md3->header.name ); 399 data->add_channel( mc_pn ); 400 data->add_channel( mc_t ); 401 data->add_channel( ic ); 402 } 403 404 mesh_nodes_data* nv::md3_loader::release_mesh_nodes_data() 404 405 { 405 406 md3_t* md3 = (md3_t*)m_md3; 406 tag_map* result = new tag_map(); 407 for ( sint32 i = 0; i < md3->header.num_tags; ++i ) 407 uint32 node_count = md3->header.num_tags; 408 if ( node_count == 0 ) return nullptr;; 409 mesh_node_data* nodes = new mesh_node_data[ node_count ]; 410 for ( uint32 i = 0; i < node_count; ++i ) 408 411 { 409 412 const md3_tag_t& rtag = md3->tags[i]; 410 413 std::string name( (char*)(rtag.name) ); 411 414 412 key_data* data = new key_data; 413 data->add_channel( load_tags( name ) ); 414 result->insert( name, data ); 415 } 416 return result; 417 } 418 419 mesh_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; 415 nodes[i].transform = mat4(); 416 nodes[i].name = name; 417 nodes[i].parent_id = -1; 418 nodes[i].target_id = -1; 419 nodes[i].data = new key_data; 431 420 432 key_raw_channel* keys = load_tags( name ); 433 result->data->add_channel( keys ); 434 return result; 435 } 436 437 size_t nv::md3_loader::get_node_count() const 438 { 439 return ((md3_t*)m_md3)->header.num_tags; 421 key_raw_channel* keys = load_tags( name ); 422 nodes[i].data->add_channel( keys ); 423 } 424 return new mesh_nodes_data( "tags", node_count, nodes ); 425 } 426 427 mesh_data_pack* nv::md3_loader::release_mesh_data_pack() 428 { 429 mesh_data* data = new mesh_data[1]; 430 release_mesh_frame( &data[0], -1 ); 431 return new mesh_data_pack( 1, data, release_mesh_nodes_data() ); 440 432 } 441 433 -
trunk/src/formats/md5_loader.cc
r282 r287 10 10 #include "nv/logging.hh" 11 11 #include "nv/io/std_stream.hh" 12 #include "nv/profiler.hh"13 12 #include <cstring> 14 13 … … 45 44 bool md5_loader::load( stream& source ) 46 45 { 47 NV_PROFILE( "Load MD5" ); // 16XXms original48 46 std_stream sstream( &source ); 49 47 std::string command; … … 96 94 else if ( command == "mesh" ) 97 95 { 98 md5_mesh_data* mesh = new md5_mesh_data(); 99 100 int num_verts, num_tris, num_weights; 96 mesh_data* mesh = new mesh_data("md5_mesh"); 97 98 int num_verts = 0; 99 int num_tris = 0; 100 int num_weights = 0; 101 101 102 102 discard( sstream, "{" ); … … 106 106 if ( command == "shader" ) 107 107 { 108 s stream >> mesh->m_shader;109 remove_quotes( mesh->m_shader );110 // texturePath.replace_extension( ".tga");108 std::string shader; 109 sstream >> shader; 110 remove_quotes( shader ); 111 111 next_line( sstream ); 112 112 } … … 115 115 sstream >> num_verts; 116 116 117 md5_vtx_t* tdata = nullptr; 117 118 { 118 119 mesh_raw_channel* ch_pnt = mesh_raw_channel::create<md5_vtx_pnt>( num_verts ); 119 120 mesh_raw_channel* ch_t = mesh_raw_channel::create<md5_vtx_t>( num_verts ); 120 mesh ->m_pntdata = (md5_vtx_pnt*)ch_pnt->data;121 mesh->m_tdata= (md5_vtx_t*)ch_t->data;121 mesh_raw_channel* ch_pntiw = mesh_raw_channel::create<md5_vtx_pntiw>( num_verts ); 122 tdata = (md5_vtx_t*)ch_t->data; 122 123 mesh->add_channel( ch_pnt ); 123 124 mesh->add_channel( ch_t ); 125 // TODO: hack to prevent rendering 126 ch_pntiw->count = 0; 127 mesh->add_channel( ch_pntiw ); 124 128 } 125 mesh->m_vtx_data.resize( num_verts );126 129 weight_info.resize( num_verts ); 127 130 … … 138 141 weight_info[i].start_weight = start_weight; 139 142 weight_info[i].weight_count = weight_count; 140 mesh->m_tdata[i].texcoord = texcoord;143 tdata[i].texcoord = texcoord; 141 144 } 142 145 } … … 147 150 mesh_raw_channel* ch_i = mesh_raw_channel::create_index<uint32>( num_tris * 3 ); 148 151 uint32* vtx_i = (uint32*)ch_i->data; 149 mesh->m_idata = vtx_i;150 152 uint32 idx = 0; 151 153 mesh->add_channel( ch_i ); … … 190 192 } 191 193 192 prepare_mesh( mesh, weights.data(), weight_info.data() );194 prepare_mesh( weight_info.size(), mesh, weights.data(), weight_info.data() ); 193 195 194 196 m_meshes[ m_num_meshes ] = mesh; … … 201 203 } 202 204 203 bool md5_loader::prepare_mesh( md5_mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info ) 204 { 205 uint32 vtx_count = mdata->m_vtx_data.size(); 206 md5_vtx_pnt* vtcs = mdata->m_pntdata; 207 208 mdata->m_bone_offset.resize( m_joints.size() ); 209 for ( uint32 i = 0; i < m_joints.size(); ++i ) 210 { 211 transform j( m_joints[i].pos, m_joints[i].orient ); 212 mdata->m_bone_offset[i] = j.inverse(); 213 } 205 bool md5_loader::prepare_mesh( uint32 vtx_count, mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info ) 206 { 207 md5_vtx_pnt* vtcs = (md5_vtx_pnt*)mdata->get_channel< md5_vtx_pnt >()->data; 208 md5_vtx_pntiw* vtx_data = (md5_vtx_pntiw*)mdata->get_channel< md5_vtx_pntiw >()->data; 214 209 215 210 for ( uint32 i = 0; i < vtx_count; ++i ) … … 217 212 size_t start_weight = weight_info[i].start_weight; 218 213 size_t weight_count = weight_info[i].weight_count; 219 md5_vtx_ data& vdata = mdata->m_vtx_data[i];214 md5_vtx_pntiw& vdata = vtx_data[i]; 220 215 md5_vtx_pnt& vtc = vtcs[i]; 221 216 … … 268 263 } 269 264 265 const uint32* idata = (uint32*)mdata->get_index_channel()->data; 266 const md5_vtx_t* tdata = mdata->get_channel_data<md5_vtx_t>(); 267 270 268 // Prepare normals 271 269 uint32 tri_count = mdata->get_count() / 3; 272 270 for ( unsigned int i = 0; i < tri_count; ++i ) 273 271 { 274 uint32 ti0 = mdata->m_idata[ i * 3 ];275 uint32 ti1 = mdata->m_idata[ i * 3 + 1 ];276 uint32 ti2 = mdata->m_idata[ i * 3 + 2 ];272 uint32 ti0 = idata[ i * 3 ]; 273 uint32 ti1 = idata[ i * 3 + 1 ]; 274 uint32 ti2 = idata[ i * 3 + 2 ]; 277 275 278 276 glm::vec3 v1 = vtcs[ ti0 ].position; … … 288 286 vtcs[ ti2 ].normal += normal; 289 287 290 const vec2& w1 = mdata->m_tdata[ ti0 ].texcoord;291 const vec2& w2 = mdata->m_tdata[ ti1 ].texcoord;292 const vec2& w3 = mdata->m_tdata[ ti2 ].texcoord;288 const vec2& w1 = tdata[ ti0 ].texcoord; 289 const vec2& w2 = tdata[ ti1 ].texcoord; 290 const vec2& w3 = tdata[ ti2 ].texcoord; 293 291 294 292 vec2 st1 = w3 - w1; … … 306 304 for ( size_t i = 0; i < vtx_count; ++i ) 307 305 { 308 md5_vtx_ data& vdata = mdata->m_vtx_data[i];306 md5_vtx_pntiw& vdata = vtx_data[i]; 309 307 310 308 glm::vec3 normal = glm::normalize( vtcs[i].normal ); … … 541 539 } 542 540 543 md5_mesh_instance* nv::md5_mesh_data::spawn() const 544 { 545 return new md5_mesh_instance( this ); 546 } 541 mesh_nodes_data* nv::md5_loader::release_mesh_nodes_data() 542 { 543 mesh_node_data* nodes = new mesh_node_data[ m_num_joints ]; 544 for ( uint32 i = 0; i < m_num_joints; ++i ) 545 { 546 mesh_node_data& node = nodes[i]; 547 node.name = m_joints[i].name; 548 node.target_id = -1; 549 node.parent_id = -1; 550 node.transform = transform( m_joints[i].pos, m_joints[i].orient ).inverse().extract(); 551 node.data = nullptr; 552 } 553 return new mesh_nodes_data( "nodes", m_num_joints, nodes ); 554 } 555 556 mesh_data_pack* nv::md5_loader::release_mesh_data_pack() 557 { 558 uint32 size = m_meshes.size(); 559 mesh_data* meshes = new mesh_data[ size ]; 560 for ( uint32 i = 0; i < size; ++i ) 561 { 562 m_meshes[i]->move_to( meshes[i] ); 563 delete m_meshes[i]; 564 m_meshes[i] = nullptr; 565 } 566 return new mesh_data_pack( size, meshes, release_mesh_nodes_data() ); 567 } 568 547 569 548 570 nv::md5_loader::~md5_loader() … … 551 573 } 552 574 553 nv::md5_mesh_instance::md5_mesh_instance( const md5_mesh_data* a_data )554 : m_data( a_data ), m_indices( 0 )555 {556 m_indices = m_data->get_count();557 m_pntdata.assign( m_data->m_pntdata, m_data->m_vtx_data.size() );558 }559 560 void nv::md5_mesh_instance::apply( const transform* skeleton )561 {562 NV_PROFILE("md5::apply");563 size_t skeleton_size = m_data->m_bone_offset.size();564 size_t vertex_count = m_pntdata.size();565 m_pos_offset.resize( skeleton_size );566 for ( unsigned int i = 0; i < skeleton_size; ++i )567 {568 m_pos_offset[i] = skeleton[i] * m_data->m_bone_offset[i];569 }570 571 std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 );572 for ( unsigned int i = 0; i < vertex_count; ++i )573 {574 const md5_vtx_data& vert = m_data->m_vtx_data[i];575 md5_vtx_pnt& result = m_pntdata[i];576 577 for ( size_t j = 0; j < 4; ++j )578 {579 int index = vert.boneindex[j];580 float weight = vert.boneweight[j];581 const quat& orient = skeleton[index].get_orientation();582 const transform& offset = m_pos_offset[index];583 result.position += offset.transformed( vert.position ) * weight;584 result.normal += ( orient * vert.normal ) * weight;585 result.tangent += ( orient * vert.tangent ) * weight;586 }587 }588 } -
trunk/src/formats/nmd_loader.cc
r285 r287 7 7 #include "nv/formats/nmd_loader.hh" 8 8 #include "nv/io/std_stream.hh" 9 #include "nv/string.hh" 9 10 10 11 using namespace nv; … … 24 25 case nmd_type::MESH : load_mesh( source, element_header ); break; 25 26 case nmd_type::ANIMATION : load_animation( source, element_header ); break; 26 case nmd_type::BONE_ARRAY : load_bones( source, element_header ); break;27 27 case nmd_type::STRING_TABLE : load_strings( source ); break; 28 28 default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break; … … 47 47 mesh->add_channel( channel ); 48 48 } 49 m_mesh_names.push_back( e.name ); 49 50 m_meshes.push_back( mesh ); 50 51 return true; … … 54 55 { 55 56 mesh_data* result = m_meshes[ index ]; 57 if ( m_strings ) result->set_name( m_strings->get( m_mesh_names[ index ] ) ); 56 58 m_meshes[ index ] = nullptr; 57 59 return result; 60 } 61 62 mesh_data_pack* nv::nmd_loader::release_mesh_data_pack() 63 { 64 uint32 size = m_meshes.size(); 65 mesh_data* meshes = new mesh_data[ size ]; 66 for ( uint32 i = 0; i < size; ++i ) 67 { 68 m_meshes[i]->move_to( meshes[i] ); 69 delete m_meshes[i]; 70 } 71 m_meshes.clear(); 72 return new mesh_data_pack( size, meshes, release_mesh_nodes_data() ); 58 73 } 59 74 … … 61 76 { 62 77 for ( auto mesh : m_meshes ) if ( mesh ) delete mesh; 63 if ( m_animation ) delete m_animation;64 78 if ( m_strings ) delete m_strings; 65 if ( m_ bone_data ) delete m_bone_data;79 if ( m_node_data ) delete m_node_data; 66 80 m_meshes.clear(); 67 m_animation = nullptr; 68 m_bone_data = nullptr; 69 m_strings = nullptr; 81 m_mesh_names.clear(); 82 m_node_names.clear(); 83 84 m_node_data = nullptr; 85 m_node_array = nullptr; 86 m_strings = nullptr; 70 87 } 71 88 … … 82 99 } 83 100 84 bool nv::nmd_loader::load_bones( stream& source, const nmd_element_header& e )85 {86 NV_ASSERT( m_bone_data == nullptr, "MULTIPLE BONE ENTRIES!" );87 m_bone_data = new nmd_bone_data;88 m_bone_data->bones = new nmd_bone[ e.children ];89 m_bone_data->count = (uint16)e.children;90 source.read( m_bone_data->bones, sizeof( nmd_bone ), e.children );91 return true;92 }93 94 nmd_animation* nv::nmd_loader::release_animation()95 {96 nmd_animation* result = m_animation;97 m_animation = nullptr;98 return result;99 }100 101 nmd_bone_data* nv::nmd_loader::release_bone_data()102 {103 nmd_bone_data* result = m_bone_data;104 m_bone_data = nullptr;105 return result;106 }107 108 string_table* nv::nmd_loader::release_string_table()109 {110 string_table* result = m_strings;111 m_strings = nullptr;112 return result;113 }114 115 116 101 bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e ) 117 102 { 118 NV_ASSERT( m_animation == nullptr, "MULTIPLE ANIMATION ENTRIES!" ); 119 nmd_animation_header header; 120 source.read( &header, sizeof( header ), 1 ); 121 m_animation = new nmd_animation; 122 m_animation->fps = header.fps; 123 m_animation->duration = header.duration; 124 m_animation->flat = header.flat; 125 m_animation->node_count = (uint16)e.children; 126 m_animation->nodes = new nmd_node[ e.children ]; 103 NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" ); 104 nmd_animation_header animation_header; 105 source.read( &animation_header, sizeof( animation_header ), 1 ); 106 m_node_array = new mesh_node_data[ e.children ]; 127 107 for ( uint32 i = 0; i < e.children; ++i ) 128 108 { 129 109 nmd_element_header element_header; 130 110 source.read( &element_header, sizeof( element_header ), 1 ); 131 NV_ASSERT( element_header.type == nmd_type::ANIMATION_NODE, "ANIMATION_NODE expected!" ); 132 m_animation->nodes[i].name = element_header.name; 133 111 NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" ); 112 m_node_names.push_back( element_header.name ); 134 113 uint16 ch_count = element_header.children; 135 114 136 nmd_ animation_node_header node_header;115 nmd_node_header node_header; 137 116 source.read( &node_header, sizeof( node_header ), 1 ); 138 m_ animation->nodes[i].parent_id = node_header.parent_id;139 m_ animation->nodes[i].transform = node_header.transform;140 m_ animation->nodes[i].data = nullptr;117 m_node_array[i].parent_id = node_header.parent_id; 118 m_node_array[i].transform = node_header.transform; 119 m_node_array[i].data = nullptr; 141 120 if ( ch_count > 0 ) 142 121 { 143 122 key_data* kdata = new key_data; 144 m_ animation->nodes[i].data = kdata;123 m_node_array[i].data = kdata; 145 124 for ( uint32 c = 0; c < ch_count; ++c ) 146 125 { 147 126 source.read( &element_header, sizeof( element_header ), 1 ); 148 NV_ASSERT( element_header.type == nmd_type:: ANIMATION_CHANNEL, "ANIMATION_CHANNEL expected!" );149 nv::nmd_ animation_channel_header cheader;127 NV_ASSERT( element_header.type == nmd_type::KEY_CHANNEL, "CHANNEL expected!" ); 128 nv::nmd_key_channel_header cheader; 150 129 source.read( &cheader, sizeof( cheader ), 1 ); 151 130 key_raw_channel* channel = key_raw_channel::create( cheader.format, cheader.count ); … … 155 134 } 156 135 } 136 m_node_data = new mesh_nodes_data( "animation", e.children, m_node_array, animation_header.frame_rate, animation_header.duration, animation_header.flat ); 157 137 return true; 158 138 } 159 139 140 mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data() 141 { 142 if ( m_node_data ) 143 { 144 if ( m_strings ) 145 { 146 for ( uint32 i = 0; i < m_node_data->get_count(); ++i ) 147 { 148 m_node_array[i].name = m_strings->get( m_node_names[i] ); 149 } 150 } 151 mesh_nodes_data* result = m_node_data; 152 m_node_data = nullptr; 153 m_node_array = nullptr; 154 return result; 155 } 156 return nullptr; 157 } 160 158 161 159 // TEMPORARY 162 nv::nmd_temp_animation::nmd_temp_animation( nmd_loader* loader )163 {164 m_animation = loader->release_animation();165 m_strings = loader->release_string_table();166 160 167 for ( uint32 n = 0; n < m_animation->node_count; ++n ) 168 { 169 nmd_node& node = m_animation->nodes[n]; 170 m_data.push_back( node.data ); 171 node.data = nullptr; 172 } 173 174 if ( !m_animation->flat ) 175 { 176 m_children.resize( m_animation->node_count ); 177 for ( nv::uint32 n = 0; n < m_animation->node_count; ++n ) 178 { 179 const nmd_node& node = m_animation->nodes[n]; 180 if ( node.parent_id != -1 ) 181 { 182 m_children[ node.parent_id ].push_back( n ); 183 } 184 } 185 } 186 187 m_bone_ids.resize( m_animation->node_count ); 188 } 189 190 nv::nmd_temp_animation::~nmd_temp_animation() 191 { 192 for ( auto node : m_data ) delete node; 193 delete m_animation; 194 delete m_strings; 195 } 196 197 void nv::nmd_temp_animation::prepare( const nmd_temp_model* model ) 198 { 199 m_offsets = model->m_bone_offsets.data(); 200 for ( uint32 n = 0; n < m_animation->node_count; ++n ) 201 { 202 const nmd_node& node = m_animation->nodes[n]; 203 sint16 bone_id = -1; 204 205 auto bi = model->m_bone_names.find( m_strings->get( node.name ) ); 206 if ( bi != model->m_bone_names.end() ) 207 { 208 bone_id = bi->second; 209 } 210 m_bone_ids[n] = bone_id; 211 } 212 } 213 214 void nv::nmd_temp_animation::animate( mat4* data, uint32 time ) 215 { 216 float tick_time = ( time / 1000.0f ) * m_animation->fps; 217 float anim_time = fmodf( tick_time, m_animation->duration ); 218 219 if ( !m_animation->flat ) 220 { 221 animate_rec( data, anim_time, 0, mat4() ); 222 return; 223 } 224 225 for ( uint32 n = 0; n < m_animation->node_count; ++n ) 226 if ( m_bone_ids[n] >= 0 ) 227 { 228 const nmd_node* node = &m_animation->nodes[ n ]; 229 nv::mat4 node_mat( node->transform ); 230 231 if ( m_data[n] ) 232 { 233 node_mat = m_data[n]->get_matrix( anim_time ); 234 } 235 236 sint16 bone_id = m_bone_ids[n]; 237 data[ bone_id ] = node_mat * m_offsets[ bone_id ]; 238 } 239 240 } 241 242 void nv::nmd_temp_animation::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) 243 { 244 // TODO: fix transforms, which are now embedded, 245 // see note in assimp_loader.cc:load_node 246 const nmd_node* node = &m_animation->nodes[ node_id ]; 247 mat4 node_mat( node->transform ); 248 249 if ( m_data[ node_id ] ) 250 { 251 node_mat = m_data[ node_id ]->get_matrix( time ); 252 } 253 254 mat4 global_mat = parent_mat * node_mat; 255 256 sint16 bone_id = m_bone_ids[ node_id ]; 257 if ( bone_id >= 0 ) 258 { 259 data[ bone_id ] = global_mat * m_offsets[ bone_id ]; 260 } 261 262 for ( auto child : m_children[ node_id ] ) 263 { 264 animate_rec( data, time, child, global_mat ); 265 } 266 } 267 268 nv::nmd_temp_model::nmd_temp_model( nmd_loader* loader ) 161 nv::nmd_temp_model_data::nmd_temp_model_data( nmd_loader* loader ) 269 162 { 270 163 for ( unsigned m = 0; m < loader->get_mesh_count(); ++m ) … … 272 165 m_mesh_data.push_back(loader->release_mesh_data(m)); 273 166 } 274 nmd_bone_data* bone_data = loader->release_bone_data(); 275 string_table* strings = loader->release_string_table(); 276 277 for ( nv::uint16 bi = 0; bi < bone_data->count; ++bi ) 278 { 279 m_bone_names[ strings->get( bone_data->bones[bi].name ) ] = bi; 280 m_bone_offsets.push_back( bone_data->bones[bi].offset ); 281 } 282 283 delete bone_data; 284 delete strings; 167 m_node_data = loader->release_mesh_nodes_data(); 285 168 } 286 169 287 nv::nmd_temp_model ::~nmd_temp_model()170 nv::nmd_temp_model_data::~nmd_temp_model_data() 288 171 { 289 172 for ( unsigned m = 0; m < m_mesh_data.size(); ++m ) … … 291 174 delete m_mesh_data[m]; 292 175 } 176 delete m_node_data; 293 177 } -
trunk/src/formats/obj_loader.cc
r280 r287 50 50 std::string line; 51 51 std::string cmd; 52 std::string name; 53 std::string next_name; 52 54 53 55 std::size_t size; … … 77 79 bool obj_reader::read_stream( std::istream& stream ) 78 80 { 81 name = next_name; 79 82 bool added_faces = false; 80 83 f32 x, y, z; … … 146 149 if ( cmd == "g" ) 147 150 { 148 if (added_faces) return true; 151 ss >> next_name; 152 if (added_faces) 153 return true; 154 name = next_name; 149 155 continue; 150 156 } … … 335 341 channel->count = reader->size * 3; 336 342 337 mesh_data* mesh = new mesh_data( );343 mesh_data* mesh = new mesh_data(reader->name); 338 344 mesh->add_channel( channel ); 339 345 m_meshes.push_back( mesh ); … … 357 363 for ( auto mesh : m_meshes ) if ( mesh ) delete mesh; 358 364 } 365 366 mesh_data_pack* nv::obj_loader::release_mesh_data_pack() 367 { 368 uint32 size = m_meshes.size(); 369 mesh_data* meshes = new mesh_data[ size ]; 370 for ( uint32 i = 0; i < size; ++i ) 371 { 372 m_meshes[i]->move_to( meshes[i] ); 373 delete m_meshes[i]; 374 } 375 m_meshes.clear(); 376 return new mesh_data_pack( size, meshes ); 377 }
Note: See TracChangeset
for help on using the changeset viewer.