Ignore:
Timestamp:
07/26/14 09:55:54 (11 years ago)
Author:
epyon
Message:
  • mesh_creator class, currently for transforms, later for const safety
  • pre_transform and transformation moved out of assimp_loader
  • pre_transfrom and transformation works on ANY mesh_data/mesh_nodes_data
  • cleanups
File:
1 edited

Legend:

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

    r292 r293  
    99#include <glm/gtx/transform.hpp>
    1010#include "nv/io/std_stream.hh"
     11#include "nv/gfx/mesh_creator.hh"
    1112#include "nv/lib/assimp.hh"
    1213
     
    1415
    1516const int MAX_BONES = 64;
     17
     18struct assimp_plain_vtx
     19{
     20        vec3 position;
     21        vec3 normal;
     22        vec2 texcoord;
     23        vec4 tangent;
     24
     25        assimp_plain_vtx() {}
     26        assimp_plain_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )
     27        {
     28                position = v;
     29                texcoord = t;
     30                normal   = n;
     31                tangent  = g;
     32        }
     33};
     34
     35struct assimp_skinned_vtx
     36{
     37        vec3  position;
     38        vec3  normal;
     39        vec2  texcoord;
     40        vec4  tangent;
     41        ivec4 boneindex;
     42        vec4  boneweight;
     43
     44        assimp_skinned_vtx() {}
     45        assimp_skinned_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )
     46        {
     47                position = v;
     48                texcoord = t;
     49                normal   = n;
     50                tangent  = g;
     51        }
     52};
    1653
    1754struct assimp_key_p  { float time; vec3 position; };
     
    2158
    2259
    23 nv::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)
    25 {
    26         initialize( a_ext, a_rotate_transform, a_scale, a_assimp_flags );
    27 }
    28 
    29 nv::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)
    31 {
    32         initialize( a_ext, mat3(), 1.0f, a_assimp_flags );
    33 }
    34 
    35 
    36 void nv::assimp_loader::initialize( const string& a_ext, const mat3& a_rotate_transform, float a_scale, uint32 a_assimp_flags )
     60nv::assimp_loader::assimp_loader( const string& a_ext, uint32 a_assimp_flags /*= 0 */ )
     61        : m_scene( nullptr ), m_mesh_count(0)
    3762{
    3863        m_ext   = a_ext;
    39         m_r33   = a_rotate_transform;
    40         m_ri33  = glm::transpose( m_r33 );
    41         m_scale = a_scale;
    4264        m_assimp_flags = a_assimp_flags;
    4365        if ( m_assimp_flags == 0 )
     
    97119        data->set_name( mesh->mName.data );
    98120
    99         vec3 vertex_offset     = glm::vec3();
    100         mat3 vertex_transform  = m_scale * m_r33;
    101         mat3 normal_transform  = m_r33;
    102 
    103121        bool skinned = mesh->mNumBones > 0;
    104122        mesh_raw_channel* channel = nullptr;
     
    111129        for (unsigned int i=0; i<mesh->mNumVertices; i++)
    112130        {
    113                 vec3 v = vertex_transform * assimp_vec3_cast( mesh->mVertices[ i ] ) + vertex_offset;
    114                 vec3 n = glm::normalize( normal_transform * assimp_vec3_cast( mesh->mNormals[ i ] ) );
    115                 vec3 t = glm::normalize( normal_transform * assimp_vec3_cast( mesh->mTangents[ i ] ) );
    116                 vec3 b = glm::normalize( normal_transform * assimp_vec3_cast( mesh->mBitangents[ i ] ) );
     131                vec3 v = assimp_vec3_cast( mesh->mVertices[ i ] );
     132                vec3 n = glm::normalize( assimp_vec3_cast( mesh->mNormals[ i ] ) );
     133                vec3 t = glm::normalize( assimp_vec3_cast( mesh->mTangents[ i ] ) );
     134                vec3 b = glm::normalize( assimp_vec3_cast( mesh->mBitangents[ i ] ) );
    117135                vec2 s = assimp_st_cast( mesh->mTextureCoords[ 0 ][ i ] );
    118136
     
    176194        const aiMesh*  mesh  = scene->mMeshes[ index ];
    177195
    178         mat4 bone_transform     = mat4( ( 1.f/m_scale * m_ri33 ) );
    179         mat4 bone_pre_transform = mat4( m_scale * m_r33 );
    180 
    181196        for (unsigned int m=0; m<mesh->mNumBones; m++)
    182197        {
    183198                aiBone* bone   = mesh->mBones[m];
    184                 mat4    offset = bone_pre_transform * assimp_mat4_cast( bone->mOffsetMatrix ) * bone_transform;
     199                mat4    offset = assimp_mat4_cast( bone->mOffsetMatrix );
    185200                bones[m].name = bone->mName.data;
    186201                bones[m].data = nullptr;
     
    338353        uint16 frame_rate     = (uint16)anim->mTicksPerSecond;
    339354        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 );
    344 
    345         // DAE pre_transform hack
    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 ) ;
     355        bool   flat           = false;
     356
     357        load_node( index, data, root, 0, -1 );
     358
     359        return new mesh_nodes_data( anim->mName.data, count, data, frame_rate, duration, flat );
    353360}
    354361
     
    364371}
    365372
    366 nv::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 )
     373nv::sint16 nv::assimp_loader::load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id )
    367374{
    368375        const aiScene* scene = (const aiScene*)m_scene;
     
    393400        a_data.data = nullptr;
    394401
    395         if (anode)
    396         {
    397                 if ( m_flat )
    398                 {
    399                         create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(nodes[ parent_id ]) : nullptr );
    400                 }
    401                 else
    402                 {
    403                         create_direct_keys( &a_data, anode );
    404                 }
    405                 max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, max_frames );
    406         }
     402        if (anode) create_keys( &a_data, anode );
    407403
    408404        nv::sint16 next = this_id + 1;
    409405        for ( unsigned i = 0; i < node->mNumChildren; ++i )
    410406        {
    411                 next = load_node( anim_id, nodes, node->mChildren[i], next, this_id, max_frames );
     407                next = load_node( anim_id, nodes, node->mChildren[i], next, this_id );
    412408        }
    413409
     
    415411}
    416412
    417 void nv::assimp_loader::create_transformed_keys( mesh_node_data* data, const void* vnode, const mesh_node_data* parent )
    418 {
    419         const aiNodeAnim* node = (const aiNodeAnim*)vnode;
    420         size_t max_keys = glm::max( node->mNumPositionKeys, node->mNumRotationKeys );
    421 
    422         key_raw_channel* raw_channel = key_raw_channel::create<assimp_key_tr>( max_keys );
    423         data->data = new key_data;
    424         data->data->add_channel( raw_channel );
    425         assimp_key_tr* channel = ((assimp_key_tr*)(raw_channel->data));
    426 
    427         for ( unsigned n = 0; n < max_keys; ++n )
    428         {
    429                 size_t pn = glm::min( node->mNumPositionKeys - 1, n );
    430                 size_t rn = glm::min( node->mNumRotationKeys - 1, n );
    431                 nv::vec3 pos = m_r33 * nv::assimp_vec3_cast(node->mPositionKeys[pn].mValue) * m_scale;
    432                 nv::quat rot = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[rn].mValue ) ) * m_ri33 );
    433                 // TODO: only do the calculation when a rotate transform is present!
    434                 nv::transform ptr;
    435                 if ( parent && parent->data )
    436                 {
    437                         const key_raw_channel* pchannel = parent->data->get_channel(0);
    438                         if ( pchannel && pchannel->count > 0 )
    439                         {
    440                                 ptr = ((assimp_key_tr*)pchannel->data)[ glm::min( n, pchannel->count-1 ) ].tform;
    441                         }
    442                 }
    443 
    444                 nv::transform key( ptr * nv::transform( pos, rot ) );
    445                 channel[n].tform = key;
    446         }
    447 }
    448 
    449 void nv::assimp_loader::create_direct_keys( mesh_node_data* data, const void* vnode )
     413void nv::assimp_loader::create_keys( mesh_node_data* data, const void* vnode )
    450414{
    451415        const aiNodeAnim* node = (const aiNodeAnim*)vnode;
     
    469433        {
    470434                pchannel[np].time     = (float)node->mPositionKeys[np].mTime;
    471                 pchannel[np].position = m_r33 * assimp_vec3_cast(node->mPositionKeys[np].mValue) * m_scale;
     435                pchannel[np].position = assimp_vec3_cast(node->mPositionKeys[np].mValue);
    472436        }
    473437        for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
    474438        {
    475439                rchannel[np].time     = (float)node->mRotationKeys[np].mTime;
    476                 rchannel[np].rotation = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[np].mValue ) ) * m_ri33 );
     440                rchannel[np].rotation = assimp_quat_cast(node->mRotationKeys[np].mValue );
    477441        }
    478442//      if ( node->mNumScalingKeys > 0 )
Note: See TracChangeset for help on using the changeset viewer.