Changeset 284 for trunk/src/formats


Ignore:
Timestamp:
07/20/14 23:45:56 (11 years ago)
Author:
epyon
Message:
  • nmd format made more robust
  • full implementation of runtime animation format decoding
Location:
trunk/src/formats
Files:
2 edited

Legend:

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

    r282 r284  
    1515const int MAX_BONES = 64;
    1616
    17 nv::assimp_loader::assimp_loader( const string& a_ext, const mat4& a_rotate_transform, float a_scale, uint32 a_assimp_flags /*= 0 */ ) : m_ext( a_ext ), m_rotate_transform( a_rotate_transform ), m_scale( a_scale ), m_assimp_flags( a_assimp_flags ), m_mesh_count(0), m_scene( nullptr )
    18 {
     17nv::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 )
     18{
     19        initialize( a_ext, a_rotate_transform, a_scale, a_assimp_flags );
     20}
     21
     22nv::assimp_loader::assimp_loader( const string& a_ext, uint32 a_assimp_flags /*= 0 */ ) : m_mesh_count(0), m_scene( nullptr )
     23{
     24        initialize( a_ext, mat3(), 1.0f, a_assimp_flags );
     25}
     26
     27
     28void nv::assimp_loader::initialize( const string& a_ext, const mat3& a_rotate_transform, float a_scale, uint32 a_assimp_flags )
     29{
     30        m_ext   = a_ext;
     31        m_r33   = a_rotate_transform;
     32        m_ri33  = glm::transpose( m_r33 );
     33        m_scale = a_scale;
     34        m_assimp_flags = a_assimp_flags;
    1935        if ( m_assimp_flags == 0 )
    2036        {
     
    3652}
    3753
    38 nv::assimp_loader::assimp_loader( const string& a_ext, uint32 a_assimp_flags /*= 0 */ ) : m_ext( a_ext ), m_rotate_transform(), m_scale( 1.0f ), m_assimp_flags( a_assimp_flags ), m_mesh_count(0), m_scene( nullptr )
    39 {
    40         if ( m_assimp_flags == 0 )
    41         {
    42                 m_assimp_flags = (
    43                         aiProcess_CalcTangentSpace                              | 
    44                         aiProcess_GenSmoothNormals                              | 
    45                         aiProcess_JoinIdenticalVertices                 |   
    46                         aiProcess_ImproveCacheLocality                  | 
    47                         aiProcess_LimitBoneWeights                              | 
    48                         aiProcess_RemoveRedundantMaterials      | 
    49                         aiProcess_SplitLargeMeshes                              | 
    50                         aiProcess_Triangulate                                   | 
    51                         aiProcess_GenUVCoords                   | 
    52                         aiProcess_SortByPType                   | 
    53                         aiProcess_FindDegenerates               | 
    54                         aiProcess_FindInvalidData               | 
    55                         0 );
    56         }
    57 }
     54
    5855bool nv::assimp_loader::load( stream& source )
    5956{
     
    8683        const aiMesh*  mesh  = scene->mMeshes[ index ];
    8784
    88         mat3 scaled_rotatation = glm::mat3( glm::scale( m_scale, m_scale, m_scale ) * m_rotate_transform );
    89         //mat3 scaled_rotatation = glm::mat3( m_rotate_transform );
    9085        vec3 vertex_offset     = glm::vec3();
    91         mat3 vertex_transform  = scaled_rotatation;
    92         mat3 normal_transform  = glm::mat3( m_rotate_transform );
     86        mat3 vertex_transform  = m_scale * m_r33;
     87        mat3 normal_transform  = m_r33;
    9388
    9489        bool skinned = mesh->mNumBones > 0;
     
    170165        if ( mesh->mNumBones == 0 ) return false;
    171166
    172         mat4 bone_transform    = glm::scale( 1.f/m_scale, 1.f/m_scale, 1.f/m_scale ) *  glm::inverse( glm::mat4(m_rotate_transform) );
    173         mat4 bone_pre_transform    = glm::scale( m_scale, m_scale, m_scale );
     167        mat4 bone_transform     = mat4( ( 1.f/m_scale * m_ri33 ) );
     168        mat4 bone_pre_transform = mat4( m_scale * m_r33 );
    174169
    175170        for (unsigned int m=0; m<mesh->mNumBones; m++)
     
    291286                        }
    292287                }
    293                 if ( m > 0 )
     288                if ( m > 0 && bones.size() > 0 )
    294289                {
    295290                        mesh_data* mesh = model->meshes[m];
     
    318313        if ( m_scene == nullptr ) return nullptr;
    319314        const aiScene* scene = (const aiScene*)m_scene;
    320         if ( scene->mRootNode == nullptr || scene->mAnimations[0] == nullptr ) return nullptr;
     315        if ( scene->mRootNode == nullptr || scene->mAnimations == nullptr || scene->mAnimations[0] == nullptr) return nullptr;
    321316        assimp_animation* result = new assimp_animation;
    322317
     
    370365        //       node's without keys
    371366        a_data.transform = nv::assimp_mat4_cast( node->mTransformation );
     367        if (this_id == 0)
     368                a_data.transform = mat4();
    372369        a_data.channel_count = 0;
    373370
     
    405402                size_t pn = glm::min( node->mNumPositionKeys - 1, n );
    406403                size_t rn = glm::min( node->mNumRotationKeys - 1, n );
    407                 nv::vec3 pos = nv::assimp_vec3_cast(node->mPositionKeys[pn].mValue);
    408                 nv::quat rot = nv::assimp_quat_cast(node->mRotationKeys[rn].mValue);
     404                nv::vec3 pos = m_r33 * nv::assimp_vec3_cast(node->mPositionKeys[pn].mValue) * m_scale;
     405                nv::quat rot = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[rn].mValue ) ) * m_ri33 );
    409406                // TODO: only do the calculation when a rotate transform is present!
    410                 nv::transform ptr( vec3(), glm::quat_cast( m_rotate_transform ) );
     407                nv::transform ptr;
    411408                if ( parent )
    412409                {
     
    417414                        }
    418415                }
    419                 nv::transform key( ptr * nv::transform( pos * m_scale, rot ) );
     416
     417                nv::transform key( ptr * nv::transform( pos, rot ) );
    420418                channel[n].tform = key;
    421419        }
     
    425423{
    426424        data->channel_count = 0;
    427         // TODO : support for m_rotate_transform and m_scale! ( should be easy )
    428425        const aiNodeAnim* node = (const aiNodeAnim*)vnode;
    429426        if ( node->mNumPositionKeys == 0 && node->mNumRotationKeys == 0 && node->mNumScalingKeys == 0 )
     
    443440        {
    444441                pchannel[np].time     = (float)node->mPositionKeys[np].mTime;
    445                 pchannel[np].position = assimp_vec3_cast(node->mPositionKeys[np].mValue);
     442                pchannel[np].position = m_r33 * assimp_vec3_cast(node->mPositionKeys[np].mValue) * m_scale;
    446443        }
    447444        for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
    448445        {
    449446                rchannel[np].time     = (float)node->mRotationKeys[np].mTime;
    450                 rchannel[np].rotation = assimp_quat_cast(node->mRotationKeys[np].mValue);
     447                rchannel[np].rotation = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[np].mValue ) ) * m_ri33 );
    451448        }
    452449        if ( node->mNumScalingKeys > 0 )
     
    463460                        }
    464461                }
    465         }
    466 }
    467 
     462                else
     463                {
     464                        schannel[0].time  = (float)node->mScalingKeys[0].mTime;
     465                        schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue);
     466                }
     467        }
     468
     469}
     470
  • trunk/src/formats/nmd_loader.cc

    r283 r284  
    2222                switch ( element_header.type )
    2323                {
    24                 case nmd_type::MESH           : load_mesh( source, element_header.children ); break;
    25                 case nmd_type::ANIMATION      : load_animation( source, element_header.children ); break;
    26                 case nmd_type::BONE_ARRAY     : load_bones( source, element_header.children ); break;
     24                case nmd_type::MESH           : load_mesh( source, element_header ); break;
     25                case nmd_type::ANIMATION      : load_animation( source, element_header ); break;
     26                case nmd_type::BONE_ARRAY     : load_bones( source, element_header ); break;
    2727                case nmd_type::STRING_TABLE   : load_strings( source ); break;
    2828                default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
     
    3232}
    3333
    34 bool nv::nmd_loader::load_mesh( stream& source, uint32 children )
     34bool nv::nmd_loader::load_mesh( stream& source, const nmd_element_header& e )
    3535{
    3636        mesh_data* mesh = new mesh_data();
    37         for ( uint32 s = 0; s < children; ++s )
     37        for ( uint32 s = 0; s < e.children; ++s )
    3838        {
    3939                nmd_element_header element_header;
     
    8282}
    8383
    84 bool nv::nmd_loader::load_bones( stream& source, uint32 children )
     84bool nv::nmd_loader::load_bones( stream& source, const nmd_element_header& e )
    8585{
    8686        NV_ASSERT( m_bone_data == nullptr, "MULTIPLE BONE ENTRIES!" );
    8787        m_bone_data = new nmd_bone_data;
    88         m_bone_data->bones = new nmd_bone[ children ];
    89         m_bone_data->count = (uint16)children;
    90         source.read( m_bone_data->bones, sizeof( nmd_bone ), children );
     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 );
    9191        return true;
    9292}
     
    114114
    115115
    116 bool nv::nmd_loader::load_animation( stream& source, uint32 children )
     116bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
    117117{
    118118        NV_ASSERT( m_animation == nullptr, "MULTIPLE ANIMATION ENTRIES!" );
     
    123123        m_animation->duration   = header.duration;
    124124        m_animation->flat       = header.flat;
    125         m_animation->node_count = (uint16)children;
    126         m_animation->nodes      = new nmd_node[ children ];
    127         for ( uint32 i = 0; i < children; ++i )
     125        m_animation->node_count = (uint16)e.children;
     126        m_animation->nodes      = new nmd_node[ e.children ];
     127        for ( uint32 i = 0; i < e.children; ++i )
    128128        {
    129129                nmd_element_header element_header;
    130130                source.read( &element_header, sizeof( element_header ), 1 );
    131131                NV_ASSERT( element_header.type == nmd_type::ANIMATION_NODE, "ANIMATION_NODE expected!" );
     132                m_animation->nodes[i].name          = element_header.name;
    132133
    133134                uint16 ch_count = element_header.children;
     
    135136                nmd_animation_node_header node_header;
    136137                source.read( &node_header, sizeof( node_header ), 1 );
    137                 m_animation->nodes[i].name          = node_header.name;
    138138                m_animation->nodes[i].parent_id     = node_header.parent_id;
    139139                m_animation->nodes[i].transform     = node_header.transform;
Note: See TracChangeset for help on using the changeset viewer.