Changeset 291 for trunk/src


Ignore:
Timestamp:
07/26/14 02:05:33 (11 years ago)
Author:
epyon
Message:
  • assimp_loader now uses standard mesh_loader structures
  • assimp_loader now supports multiple animation tracks
  • mesh_loader now supports multiple mesh_nodes data sets
Location:
trunk/src/formats
Files:
4 edited

Legend:

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

    r287 r291  
    1515const int MAX_BONES = 64;
    1616
    17 nv::assimp_loader::assimp_loader( const string& a_ext, const mat3& a_rotate_transform, float a_scale, uint32 a_assimp_flags /*= 0 */ ) : m_mesh_count(0), m_scene( nullptr )
     17struct assimp_key_p  { float time; vec3 position; };
     18struct assimp_key_r  { float time; quat rotation; };
     19struct assimp_key_s  { float time; vec3 scale; };
     20struct assimp_key_tr { transform tform; };
     21
     22
     23nv::assimp_loader::assimp_loader( const string& a_ext, const mat3& a_rotate_transform, float a_scale, bool pre_transform, uint32 a_assimp_flags /*= 0 */ )
     24        : m_scene( nullptr ), m_flat( pre_transform ), m_mesh_count(0)
    1825{
    1926        initialize( a_ext, a_rotate_transform, a_scale, a_assimp_flags );
    2027}
    2128
    22 nv::assimp_loader::assimp_loader( const string& a_ext, uint32 a_assimp_flags /*= 0 */ ) : m_mesh_count(0), m_scene( nullptr )
     29nv::assimp_loader::assimp_loader( const string& a_ext, bool pre_transform, uint32 a_assimp_flags /*= 0 */ )
     30        : m_scene( nullptr ), m_flat( pre_transform ), m_mesh_count(0)
    2331{
    2432        initialize( a_ext, mat3(), 1.0f, a_assimp_flags );
     
    162170}
    163171
    164 bool nv::assimp_loader::load_bones( size_t index, std::vector< assimp_bone >& bones )
     172bool nv::assimp_loader::load_bones( size_t index, std::vector< mesh_node_data >& bones )
    165173{
    166174        if ( m_scene == nullptr ) return false;
    167175        const aiScene* scene = (const aiScene*)m_scene;
    168176        const aiMesh*  mesh  = scene->mMeshes[ index ];
    169         if ( mesh->mNumBones == 0 ) return false;
    170177
    171178        mat4 bone_transform     = mat4( ( 1.f/m_scale * m_ri33 ) );
     
    176183                aiBone* bone   = mesh->mBones[m];
    177184                mat4    offset = bone_pre_transform * assimp_mat4_cast( bone->mOffsetMatrix ) * bone_transform;
    178                 bones.emplace_back( bone->mName.data, offset );
     185                bones[m].name = bone->mName.data;
     186                bones[m].data = nullptr;
     187                bones[m].parent_id = -1;
     188                bones[m].target_id = -1;
     189                bones[m].transform = offset;
    179190        }
    180191        return true;
     
    256267}
    257268
    258 assimp_model* nv::assimp_loader::release_merged_model()
    259 {
    260         if ( m_scene == nullptr || m_mesh_count == 0 ) return nullptr;
    261         assimp_model* model = new assimp_model;
    262 
    263         for ( size_t m = 0; m < m_mesh_count; ++m )
    264         {
    265                 model->meshes.push_back( release_mesh_data(m) );
    266         }
    267 
     269mesh_nodes_data* nv::assimp_loader::release_merged_bones( mesh_data* meshes )
     270{
     271        const aiScene* scene = (const aiScene*)m_scene;
     272        std::vector< mesh_node_data > final_bones;
    268273        std::unordered_map< std::string, uint16 > names;
    269         for ( unsigned int m = 0; m < model->meshes.size(); ++m )
     274        for ( unsigned int m = 0; m < m_mesh_count; ++m )
    270275        {
    271276                sint16 translate[MAX_BONES];
    272                 std::vector< assimp_bone > bones;
    273                 load_bones( m, bones );
    274                 for ( unsigned int b = 0; b < bones.size(); ++b )
    275                 {
    276 
    277                         assimp_bone& bone = bones[b];
    278                         auto iname = names.find( bone.name );
    279                         if ( iname == names.end() )
     277                std::vector< mesh_node_data > bones;
     278                const aiMesh*  mesh  = scene->mMeshes[ m ];
     279                if ( mesh->mNumBones != 0 )
     280                {
     281                        bones.resize( mesh->mNumBones );
     282                        load_bones( m, bones );
     283                        for ( unsigned int b = 0; b < mesh->mNumBones; ++b )
    280284                        {
    281                                 NV_ASSERT( model->bones.size() < MAX_BONES, "Too many bones to merge!" );
    282                                 sint16 index = (sint16)model->bones.size();
    283                                 model->bones.push_back( bone );
    284                                 names[ bone.name ] = index;
    285                                 translate[b] = index;
     285
     286                                mesh_node_data& bone = bones[b];
     287                                auto iname = names.find( bone.name );
     288                                if ( iname == names.end() )
     289                                {
     290                                        NV_ASSERT( final_bones.size() < MAX_BONES, "Too many bones to merge!" );
     291                                        sint16 index = (sint16)final_bones.size();
     292                                        final_bones.push_back( bone );
     293                                        names[ bone.name ] = index;
     294                                        translate[b] = index;
     295                                }
     296                                else
     297                                {
     298                                        translate[b] = (sint16)iname->second;
     299                                }
    286300                        }
    287                         else
     301                        if ( m > 0 && bones.size() > 0 )
    288302                        {
    289                                 translate[b] = (sint16)iname->second;
    290                         }
    291                 }
    292                 if ( m > 0 && bones.size() > 0 )
    293                 {
    294                         mesh_data* mesh = model->meshes[m];
    295                         mesh_raw_channel* channel = mesh->get_raw_channels()[0];
    296                         NV_ASSERT( !channel->is_index(), "index channel in release_merged!" );
    297                         assimp_skinned_vtx* va = (assimp_skinned_vtx*)channel->data;
    298                         for ( unsigned v = 0; v < channel->count; ++v )
    299                         {
    300                                 assimp_skinned_vtx& vertex = va[v];
    301 
    302                                 for (uint32 i = 0 ; i < 4; ++i)
     303                                mesh_raw_channel* channel = meshes[m].get_raw_channels()[0];
     304                                NV_ASSERT( !channel->is_index(), "index channel in release_merged!" );
     305                                assimp_skinned_vtx* va = (assimp_skinned_vtx*)channel->data;
     306                                for ( unsigned v = 0; v < channel->count; ++v )
    303307                                {
    304                                         if ( vertex.boneweight[i] > 0.0f )
     308                                        assimp_skinned_vtx& vertex = va[v];
     309
     310                                        for (uint32 i = 0 ; i < 4; ++i)
    305311                                        {
    306                                                 vertex.boneindex[i] = translate[vertex.boneindex[i]];
     312                                                if ( vertex.boneweight[i] > 0.0f )
     313                                                {
     314                                                        vertex.boneindex[i] = translate[vertex.boneindex[i]];
     315                                                }
    307316                                        }
    308317                                }
    309318                        }
    310                 }
    311         }
    312         return model;
    313 }
    314 
    315 assimp_animation* nv::assimp_loader::release_animation( size_t, bool pre_transform )
     319                }       
     320        }
     321        mesh_node_data* bones = new mesh_node_data[ final_bones.size() ];
     322        std::copy( final_bones.begin(), final_bones.end(), bones );
     323        return new mesh_nodes_data( "bones", final_bones.size(), bones );
     324}
     325
     326mesh_nodes_data* nv::assimp_loader::release_animation( size_t index )
    316327{
    317328        if ( m_scene == nullptr ) return nullptr;
    318329        const aiScene* scene = (const aiScene*)m_scene;
    319         if ( scene->mRootNode == nullptr || scene->mAnimations == nullptr || scene->mAnimations[0] == nullptr) return nullptr;
    320         assimp_animation* result = new assimp_animation;
    321 
    322         // need resize not to copy!
    323         result->nodes.resize( count_nodes( scene->mRootNode ) );
     330        if ( scene->mRootNode == nullptr || scene->mAnimations == nullptr || scene->mAnimations[index] == nullptr) return nullptr;
    324331
    325332        const aiNode*      root = scene->mRootNode;
    326         const aiAnimation* anim = scene->mAnimations[0]; // if implemented, change in load_node also
    327 
    328         result->frame_rate     = (uint16)anim->mTicksPerSecond;
    329         result->duration       = (float)anim->mDuration;
    330         result->flat           = pre_transform;
    331         result->max_frames     = 0;
    332 
    333         load_node( result, root, 0, -1 );
     333        const aiAnimation* anim = scene->mAnimations[index];
     334
     335        uint32 count = count_nodes( scene->mRootNode );
     336        mesh_node_data* data    = new mesh_node_data[count];
     337
     338        uint16 frame_rate     = (uint16)anim->mTicksPerSecond;
     339        float  duration       = (float)anim->mDuration;
     340        bool   flat           = m_flat;
     341        uint32 max_frames     = 0;
     342
     343        load_node( index, data, root, 0, -1, max_frames );
    334344
    335345        // 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 
    342         return result;
     346        if ( m_flat && frame_rate == 1 )
     347        {
     348                frame_rate = 32;
     349                duration   = (float)max_frames;
     350        }
     351
     352        return new mesh_nodes_data( anim->mName.data, count, data, frame_rate, duration, flat ) ;
    343353}
    344354
     
    354364}
    355365
    356 nv::uint32 nv::assimp_loader::load_node( assimp_animation* data, const void* vnode, sint32 this_id, sint32 parent_id )
     366nv::sint16 nv::assimp_loader::load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id, uint32& max_frames )
    357367{
    358368        const aiScene* scene = (const aiScene*)m_scene;
    359369        const aiNode*  node  = (const aiNode*)vnode;
    360370        string name( node->mName.data );
    361         const aiAnimation* anim  = scene->mAnimations[0];
     371        const aiAnimation* anim  = scene->mAnimations[anim_id];
    362372        const aiNodeAnim*  anode = nullptr;
    363373
     
    369379        }
    370380
    371         assimp_animated_node_data& a_data = data->nodes[ this_id ];
     381        mesh_node_data& a_data = nodes[ this_id ];
    372382
    373383        a_data.name      = name;
     384        a_data.target_id = -1;
    374385        a_data.parent_id = parent_id;
    375386        // This value is ignored by the create_transformed_keys, but needed by create_direct_keys!
     
    384395        if (anode)
    385396        {
    386                 if ( data->flat )
    387                 {
    388                         create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(data->nodes[ parent_id ]) : nullptr );
     397                if ( m_flat )
     398                {
     399                        create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(nodes[ parent_id ]) : nullptr );
    389400                }
    390401                else
     
    392403                        create_direct_keys( &a_data, anode );
    393404                }
    394                 data->max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, data->max_frames );
    395         }
    396 
    397         nv::uint32 next = this_id + 1;
     405                max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, max_frames );
     406        }
     407
     408        nv::sint16 next = this_id + 1;
    398409        for ( unsigned i = 0; i < node->mNumChildren; ++i )
    399410        {
    400                 next = load_node( data, node->mChildren[i], next, this_id );
     411                next = load_node( anim_id, nodes, node->mChildren[i], next, this_id, max_frames );
    401412        }
    402413
     
    404415}
    405416
    406 void nv::assimp_loader::create_transformed_keys( assimp_animated_node_data* data, const void* vnode, const assimp_animated_node_data* parent )
     417void nv::assimp_loader::create_transformed_keys( mesh_node_data* data, const void* vnode, const mesh_node_data* parent )
    407418{
    408419        const aiNodeAnim* node = (const aiNodeAnim*)vnode;
     
    436447}
    437448
    438 void nv::assimp_loader::create_direct_keys( assimp_animated_node_data* data, const void* vnode )
     449void nv::assimp_loader::create_direct_keys( mesh_node_data* data, const void* vnode )
    439450{
    440451        const aiNodeAnim* node = (const aiNodeAnim*)vnode;
     
    489500mesh_data_pack* nv::assimp_loader::release_mesh_data_pack()
    490501{
     502        if ( m_scene == nullptr || m_mesh_count == 0 ) return nullptr;
     503        const aiScene* scene = (const aiScene*)m_scene;
     504        bool has_bones = false;
    491505        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 
     506        for ( size_t m = 0; m < m_mesh_count; ++m )
     507        {
     508                const aiMesh* mesh = scene->mMeshes[ m ];
     509                meshes[m].set_name( mesh->mName.data );
     510                if ( mesh->mNumBones > 0 ) has_bones = true;
     511                load_mesh_data(&meshes[m],m);
     512        }
     513
     514        mesh_nodes_data* nodes = ( has_bones ? release_merged_bones( meshes ) : release_animation(0) );
     515        return new mesh_data_pack( m_mesh_count, meshes, nodes );
     516}
     517
     518size_t nv::assimp_loader::get_nodes_data_count() const
     519{
     520        if ( m_scene == nullptr ) return 0;
     521        const aiScene* scene = (const aiScene*)m_scene;
     522        return scene->mNumAnimations;   
     523}
     524
     525mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data( size_t index /*= 0 */ )
     526{
     527        return release_animation( index );
     528}
     529
  • trunk/src/formats/md3_loader.cc

    r289 r291  
    402402}
    403403
    404 mesh_nodes_data* nv::md3_loader::release_mesh_nodes_data()
     404mesh_nodes_data* nv::md3_loader::release_mesh_nodes_data( size_t )
    405405{
    406406        md3_t* md3 = (md3_t*)m_md3;
  • trunk/src/formats/md5_loader.cc

    r289 r291  
    504504}
    505505
    506 mesh_nodes_data* nv::md5_loader::release_mesh_nodes_data()
     506mesh_nodes_data* nv::md5_loader::release_mesh_nodes_data( size_t )
    507507{
    508508        mesh_nodes_data* nodes = m_nodes;
  • trunk/src/formats/nmd_loader.cc

    r287 r291  
    138138}
    139139
    140 mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data()
     140mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
    141141{
    142142        if ( m_node_data )
     
    157157}
    158158
    159 // TEMPORARY
    160 
    161 nv::nmd_temp_model_data::nmd_temp_model_data( nmd_loader* loader )
    162 {
    163         for ( unsigned m = 0; m < loader->get_mesh_count(); ++m )
    164         {
    165                 m_mesh_data.push_back(loader->release_mesh_data(m));
    166         }
    167         m_node_data = loader->release_mesh_nodes_data();
    168 }
    169 
    170 nv::nmd_temp_model_data::~nmd_temp_model_data()
    171 {
    172         for ( unsigned m = 0; m < m_mesh_data.size(); ++m )
    173         {
    174                 delete m_mesh_data[m];
    175         }
    176         delete m_node_data;
    177 }
Note: See TracChangeset for help on using the changeset viewer.