Ignore:
Timestamp:
01/26/16 18:59:46 (9 years ago)
Author:
epyon
Message:
  • massive update (need to start doing atomics again) :/
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/formats/nmd_loader.cc

    r482 r485  
    2626                {
    2727                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;
    2929                case nmd_type::STRINGS        : load_strings( source ); break;
     30                case nmd_type::POSES          : load_poses( source, element_header ); break;
    3031                default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
    3132                }
     
    3940        data_node_info info;
    4041        load_channel_set( source, mesh, info, e );
    41 //      m_mesh_names.push_back( e.name );
    4242        m_infos.push_back( info );
    4343        m_meshes.push_back( mesh );
     
    5656{
    5757        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
    58         if ( m_node_data ) delete m_node_data;
    5958        if ( m_bone_data ) delete m_bone_data;
     59        if ( m_pose_data_set ) delete m_pose_data_set;
    6060        m_meshes.clear();
    6161
    62         m_node_data = nullptr;
    6362        m_bone_data = nullptr;
    6463}
     
    8584}
    8685
    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!" );
     86bool nv::nmd_loader::load_bones( stream& source, const nmd_element_header& e )
     87{
     88        NV_ASSERT( m_bone_data == nullptr, "MULTIPLE NODE ENTRIES!" );
    9089        nmd_animation_header animation_header;
    9190        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 );
    9391        m_bone_data = new data_node_list( e.name );
    9492        for ( uint32 i = 0; i < e.children; ++i )
     
    102100                load_channel_set( source, set, info, element_header );
    103101                m_bone_data->append( info );
    104                 m_node_data->append( set, info );
    105         }
    106         m_node_data->initialize();
     102                delete set;
     103        }
    107104        return true;
    108105}
     
    131128}
    132129
     130bool 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
    133160mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
    134161{
    135         mesh_nodes_data* result = m_node_data;
    136         m_node_data = nullptr;
    137         return result;
     162        return nullptr;
    138163}
    139164
     
    147172bool nv::nmd_loader::is_animated( size_t /*= 0 */ )
    148173{
    149         if ( !m_node_data ) return false;
    150         return m_node_data->is_animated();
     174        return m_pose_data_set != nullptr;
    151175}
    152176
     
    194218}
    195219
    196 void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
     220void nv::nmd_dump_bones( stream& stream_out, const data_node_list& nodes )
    197221{
    198222        uint32 total = sizeof( nmd_animation_header );
     
    200224        {
    201225                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                 }
    207226        }
    208227
    209228        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;
    242230        header.children = static_cast<uint16>( nodes.size() );
    243231        header.size = total;
     
    269257}
    270258
     259void 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
     278void 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
    271293void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
    272294{
     
    282304}
    283305
    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 */ )
     306void 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 */ )
    285307{
    286308        uint32 elements = ( strings ? 1 : 0 ) // +1 string array
     
    297319        if ( nodes && nodes->size() > 0 )
    298320        {
    299                 nmd_dump_nodes( stream_out, *nodes );
     321                nmd_dump_bones( stream_out, *nodes );
    300322        }
    301323
     
    305327        }
    306328}
    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 array
    311                 + meshes.size() // meshes
    312                 + ( nodes && nodes->size() > 0 ? 1 : 0 ); // nodes
    313         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 array
    335                 + ( animation.size() > 0 ? 1 : 0 ); // nodes
    336         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.