Changeset 485 for trunk/src/formats/nmd_loader.cc
- Timestamp:
- 01/26/16 18:59:46 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/formats/nmd_loader.cc
r482 r485 26 26 { 27 27 case nmd_type::MESH : load_mesh( source, element_header ); break; 28 case nmd_type:: ANIMATION : load_animation( source, element_header ); break;28 case nmd_type::BONES : load_bones( source, element_header ); break; 29 29 case nmd_type::STRINGS : load_strings( source ); break; 30 case nmd_type::POSES : load_poses( source, element_header ); break; 30 31 default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break; 31 32 } … … 39 40 data_node_info info; 40 41 load_channel_set( source, mesh, info, e ); 41 // m_mesh_names.push_back( e.name );42 42 m_infos.push_back( info ); 43 43 m_meshes.push_back( mesh ); … … 56 56 { 57 57 for ( auto mesh : m_meshes ) if ( mesh ) delete mesh; 58 if ( m_node_data ) delete m_node_data;59 58 if ( m_bone_data ) delete m_bone_data; 59 if ( m_pose_data_set ) delete m_pose_data_set; 60 60 m_meshes.clear(); 61 61 62 m_node_data = nullptr;63 62 m_bone_data = nullptr; 64 63 } … … 85 84 } 86 85 87 bool nv::nmd_loader::load_ animation( stream& source, const nmd_element_header& e )88 { 89 NV_ASSERT( m_ node_data == nullptr, "MULTIPLE NODE ENTRIES!" );86 bool nv::nmd_loader::load_bones( stream& source, const nmd_element_header& e ) 87 { 88 NV_ASSERT( m_bone_data == nullptr, "MULTIPLE NODE ENTRIES!" ); 90 89 nmd_animation_header animation_header; 91 90 source.read( &animation_header, sizeof( animation_header ), 1 ); 92 m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count );93 91 m_bone_data = new data_node_list( e.name ); 94 92 for ( uint32 i = 0; i < e.children; ++i ) … … 102 100 load_channel_set( source, set, info, element_header ); 103 101 m_bone_data->append( info ); 104 m_node_data->append( set, info ); 105 } 106 m_node_data->initialize(); 102 delete set; 103 } 107 104 return true; 108 105 } … … 131 128 } 132 129 130 bool nv::nmd_loader::load_poses( stream& source, const nmd_element_header& e ) 131 { 132 if ( m_pose_data_set == nullptr ) 133 { 134 NV_ASSERT_ALWAYS( m_bone_data, "POSES WITHOUT BONES!" ); 135 m_pose_data_set = new pose_data_set; 136 m_pose_data_set->initialize( *m_bone_data ); 137 } 138 139 uint32 count = e.children; 140 shash64 name = e.name; 141 142 auto& set = m_pose_data_set->m_sets[ name ]; 143 NV_ASSERT_ALWAYS( !set.name, "SET REWRITE!" ); 144 set.name = name; 145 set.count = count; 146 set.start = m_pose_data_set->size(); 147 148 for ( uint32 i = 0; i < count; ++i ) 149 { 150 uint32 length = 0; 151 source.read( &length, sizeof( length ), 1 ); 152 m_pose_data_set->m_data.push_back( skeleton_transforms() ); 153 auto& data = m_pose_data_set->m_data.back().m_transforms; 154 data.resize( length ); 155 source.read( &data[0], length * sizeof( transform ), 1 ); 156 } 157 return true; 158 } 159 133 160 mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t ) 134 161 { 135 mesh_nodes_data* result = m_node_data; 136 m_node_data = nullptr; 137 return result; 162 return nullptr; 138 163 } 139 164 … … 147 172 bool nv::nmd_loader::is_animated( size_t /*= 0 */ ) 148 173 { 149 if ( !m_node_data ) return false; 150 return m_node_data->is_animated(); 174 return m_pose_data_set != nullptr; 151 175 } 152 176 … … 194 218 } 195 219 196 void nv::nmd_dump_ nodes( stream& stream_out, const mesh_nodes_data& nodes )220 void nv::nmd_dump_bones( stream& stream_out, const data_node_list& nodes ) 197 221 { 198 222 uint32 total = sizeof( nmd_animation_header ); … … 200 224 { 201 225 total += sizeof( nmd_element_header ); 202 for ( uint32 c = 0; c < node->size(); ++c )203 {204 total += sizeof( nmd_channel_header );205 total += node->get_channel( c )->raw_size();206 }207 226 } 208 227 209 228 nmd_element_header header; 210 header.type = nmd_type::ANIMATION; 211 header.children = static_cast<uint16>( nodes.size() ); 212 header.size = total; 213 header.name = nodes.get_name(); 214 header.transform = mat4(); 215 header.parent_id = -1; 216 header.attributes = 0; 217 218 stream_out.write( &header, sizeof( header ), 1 ); 219 220 nmd_animation_header aheader; 221 aheader.frame_rate = nodes.get_fps(); 222 aheader.frame_count = nodes.get_frame_count(); 223 aheader.unused = false; 224 stream_out.write( &aheader, sizeof( aheader ), 1 ); 225 226 for ( uint32 i = 0; i < nodes.size(); ++i ) 227 { 228 nmd_dump_element( stream_out, *nodes[i], nodes.get_info(i), nv::nmd_type::NODE ); 229 } 230 } 231 232 void nv::nmd_dump_bones( stream& stream_out, const data_node_list& nodes ) 233 { 234 uint32 total = sizeof( nmd_animation_header ); 235 for ( auto node : nodes ) 236 { 237 total += sizeof( nmd_element_header ); 238 } 239 240 nmd_element_header header; 241 header.type = nmd_type::ANIMATION; 229 header.type = nmd_type::BONES; 242 230 header.children = static_cast<uint16>( nodes.size() ); 243 231 header.size = total; … … 269 257 } 270 258 259 void nv::nmd_dump_poses( stream& stream_out, const array_view< skeleton_transforms* > poses, shash64 name ) 260 { 261 nmd_element_header pheader; 262 pheader.type = nv::nmd_type::POSES; 263 pheader.children = poses.size(); 264 pheader.size = sizeof( transform ) * poses.size() * ( poses.size() > 0 ? poses[0]->size() : 0 ) 265 + sizeof( uint32 ) * poses.size(); 266 pheader.name = name; 267 pheader.parent_id = -1; 268 pheader.attributes = 0; 269 stream_out.write( &pheader, sizeof( pheader ), 1 ); 270 for ( auto pose : poses ) 271 { 272 uint32 count = pose->size(); 273 stream_out.write( &count, sizeof( count ), 1 ); 274 stream_out.write( pose->xforms(), sizeof( transform ) * count, 1 ); 275 } 276 } 277 278 void nv::nmd_dump_pose( stream& stream_out, const skeleton_transforms& pose, shash64 name ) 279 { 280 nmd_element_header pheader; 281 pheader.type = nv::nmd_type::POSES; 282 pheader.children = 1; 283 pheader.size = sizeof( transform ) * pose.size(); 284 pheader.name = name; 285 pheader.parent_id = -1; 286 pheader.attributes = 0; 287 stream_out.write( &pheader, sizeof( pheader ), 1 ); 288 uint32 count = pose.size(); 289 stream_out.write( &count, sizeof( count ), 1 ); 290 stream_out.write( pose.xforms(), sizeof( transform ) * count, 1 ); 291 } 292 271 293 void nv::nmd_dump_strings( stream& stream_out, const string_table& strings ) 272 294 { … … 282 304 } 283 305 284 void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, array_view< data_node_info > infos, const mesh_nodes_data* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ )306 void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, array_view< data_node_info > infos, const nv::data_node_list* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ ) 285 307 { 286 308 uint32 elements = ( strings ? 1 : 0 ) // +1 string array … … 297 319 if ( nodes && nodes->size() > 0 ) 298 320 { 299 nmd_dump_ nodes( stream_out, *nodes );321 nmd_dump_bones( stream_out, *nodes ); 300 322 } 301 323 … … 305 327 } 306 328 } 307 308 void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, array_view< data_node_info > infos, const nv::data_node_list* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ )309 {310 uint32 elements = ( strings ? 1 : 0 ) // +1 string array311 + meshes.size() // meshes312 + ( nodes && nodes->size() > 0 ? 1 : 0 ); // nodes313 nmd_dump_header( stream_out, elements, name );314 315 for ( uint32 i = 0; i < meshes.size(); ++i )316 {317 NV_ASSERT( meshes[i], "mesh is null!" );318 nmd_dump_element( stream_out, *meshes[i], infos[i], nv::nmd_type::MESH );319 }320 321 if ( nodes && nodes->size() > 0 )322 {323 nmd_dump_bones( stream_out, *nodes );324 }325 326 if ( strings )327 {328 nmd_dump_strings( stream_out, *strings );329 }330 }331 332 void nv::nmd_dump( stream& stream_out, const mesh_nodes_data& animation, const string_table* strings, uint64 name )333 {334 uint32 elements = ( strings ? 1 : 0 ) // +1 string array335 + ( animation.size() > 0 ? 1 : 0 ); // nodes336 nmd_dump_header( stream_out, elements, name );337 338 if ( animation.size() > 0 )339 {340 nmd_dump_nodes( stream_out, animation );341 }342 343 if ( strings )344 {345 nmd_dump_strings( stream_out, *strings );346 }347 }
Note: See TracChangeset
for help on using the changeset viewer.