Changeset 287 for trunk/src


Ignore:
Timestamp:
07/23/14 15:24:03 (11 years ago)
Author:
epyon
Message:
  • mesh_data_pack's in every format
  • md5_mesh_data removed, uses standard mesh_data
  • BONE_ARRAY in NMD is now a simple set of animation nodes
  • bone and animation node concepts merged
  • several minor changes
Location:
trunk/src
Files:
8 edited

Legend:

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

    r285 r287  
    7979{
    8080        if ( index >= m_mesh_count ) return nullptr;
    81         mesh_data* result = new mesh_data();
     81        mesh_data* result = new mesh_data;
     82        load_mesh_data( result, index );
     83        return result;
     84}
     85void nv::assimp_loader::load_mesh_data( mesh_data* data, size_t index )
     86{
    8287        const aiScene* scene = (const aiScene*)m_scene;
    8388        const aiMesh*  mesh  = scene->mMeshes[ index ];
     89        data->set_name( mesh->mName.data );
    8490
    8591        vec3 vertex_offset     = glm::vec3();
     
    94100                channel = mesh_raw_channel::create< assimp_plain_vtx >( mesh->mNumVertices );
    95101
    96         result->add_channel( channel );
     102        data->add_channel( channel );
    97103        for (unsigned int i=0; i<mesh->mNumVertices; i++)
    98104        {
     
    139145
    140146        mesh_raw_channel* ichannel = mesh_raw_channel::create_index( USHORT, mesh->mNumFaces * 3 );
    141         result->add_channel( ichannel );
     147        data->add_channel( ichannel );
    142148        uint16* indices = (uint16*)ichannel->data;
    143149        for (unsigned int i=0; i<mesh->mNumFaces; i++)
     
    149155                }
    150156        }
    151 
    152         return result;
    153157}
    154158
     
    322326        const aiAnimation* anim = scene->mAnimations[0]; // if implemented, change in load_node also
    323327
    324         result->fps            = (float)anim->mTicksPerSecond;
     328        result->frame_rate     = (uint16)anim->mTicksPerSecond;
    325329        result->duration       = (float)anim->mDuration;
    326         result->pretransformed = pre_transform;
     330        result->flat           = pre_transform;
     331        result->max_frames     = 0;
    327332
    328333        load_node( result, root, 0, -1 );
     334
     335        // 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
    329342        return result;
    330343}
     
    371384        if (anode)
    372385        {
    373                 if ( data->pretransformed )
     386                if ( data->flat )
    374387                {
    375388                        create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(data->nodes[ parent_id ]) : nullptr );
     
    379392                        create_direct_keys( &a_data, anode );
    380393                }
     394                data->max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, data->max_frames );
    381395        }
    382396
     
    386400                next = load_node( data, node->mChildren[i], next, this_id );
    387401        }
     402
    388403        return next;
    389404}
     
    432447        key_raw_channel* raw_pchannel = key_raw_channel::create<assimp_key_p>( node->mNumPositionKeys );
    433448        key_raw_channel* raw_rchannel = key_raw_channel::create<assimp_key_r>( node->mNumRotationKeys );
    434         key_raw_channel* raw_schannel = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys );
     449        //key_raw_channel* raw_schannel = key_raw_channel::create<assimp_key_s>( node->mNumScalingKeys );
    435450        data->data->add_channel( raw_pchannel );
    436451        data->data->add_channel( raw_rchannel );
    437         data->data->add_channel( raw_schannel );
     452        //data->data->add_channel( raw_schannel );
    438453        assimp_key_p* pchannel = ((assimp_key_p*)(raw_pchannel->data));
    439454        assimp_key_r* rchannel = ((assimp_key_r*)(raw_rchannel->data));
    440         assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data));
     455        //assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data));
    441456
    442457        for ( unsigned np = 0; np < node->mNumPositionKeys; ++np )
     
    450465                rchannel[np].rotation = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[np].mValue ) ) * m_ri33 );
    451466        }
    452         if ( node->mNumScalingKeys > 0 )
    453         {
    454                 nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue );
    455                 float scale_value   = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) );
    456                 if ( node->mNumScalingKeys > 1 || scale_value > 0.001 )
    457                 {
    458                         NV_LOG( nv::LOG_WARNING, "scale key significant!" );
    459                         for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
    460                         {
    461                                 schannel[np].time  = (float)node->mScalingKeys[np].mTime;
    462                                 schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue);
    463                         }
    464                 }
    465                 else
    466                 {
    467                         schannel[0].time  = (float)node->mScalingKeys[0].mTime;
    468                         schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue);
    469                 }
    470         }
    471 
    472 }
    473 
     467//      if ( node->mNumScalingKeys > 0 )
     468//      {
     469//              nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue );
     470//              float scale_value   = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) );
     471//              if ( node->mNumScalingKeys > 1 || scale_value > 0.001 )
     472//              {
     473//                      NV_LOG( nv::LOG_WARNING, "scale key significant!" );
     474//                      for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
     475//                      {
     476//                              schannel[np].time  = (float)node->mScalingKeys[np].mTime;
     477//                              schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue);
     478//                      }
     479//              }
     480//              else
     481//              {
     482//                      schannel[0].time  = (float)node->mScalingKeys[0].mTime;
     483//                      schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue);
     484//              }
     485//      }
     486
     487}
     488
     489mesh_data_pack* nv::assimp_loader::release_mesh_data_pack()
     490{
     491        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
     499mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data()
     500{
     501        // TODO: implement
     502        return nullptr;
     503}
     504
  • trunk/src/formats/md2_loader.cc

    r280 r287  
    315315mesh_data* nv::md2_loader::release_mesh_data( size_t )
    316316{
    317         return release_mesh_frame( -1 );
    318 }
    319 
    320 mesh_data* nv::md2_loader::release_mesh_frame( sint32 frame )
     317        mesh_data* data = new mesh_data( "md2_mesh" );
     318        release_mesh_frame( data, -1 );
     319        return data;
     320}
     321
     322void nv::md2_loader::release_mesh_frame( mesh_data* data, sint32 frame )
    321323{
    322324        md2_t* md2 = (md2_t*)m_md2;
     
    366368        }
    367369
    368         mesh_data* result = new mesh_data();
    369         result->add_channel( mc_pn );
    370         result->add_channel( mc_t );
    371         result->add_channel( ic );
    372         return result;
    373 }
     370        data->add_channel( mc_pn );
     371        data->add_channel( mc_t );
     372        data->add_channel( ic );
     373}
     374
     375mesh_data_pack* nv::md2_loader::release_mesh_data_pack()
     376{
     377        mesh_data* data = new mesh_data[1];
     378        release_mesh_frame( &data[0], -1 );
     379        return new mesh_data_pack( 1, data );
     380}
  • trunk/src/formats/md3_loader.cc

    r285 r287  
    321321mesh_data* nv::md3_loader::release_mesh_data( size_t )
    322322{
    323         return release_mesh_frame( -1 );
    324 }
    325 
    326 mesh_data* nv::md3_loader::release_mesh_frame( sint32 frame )
     323        mesh_data* data = new mesh_data;
     324        release_mesh_frame( data, -1 );
     325        return data;
     326}
     327
     328void nv::md3_loader::release_mesh_frame( mesh_data* data, sint32 frame )
    327329{
    328330        md3_t* md3 = (md3_t*)m_md3;
     
    394396        }
    395397
    396         mesh_data* result = new mesh_data();
    397         result->add_channel( mc_pn );
    398         result->add_channel( mc_t );
    399         result->add_channel( ic );
    400         return result;
    401 }
    402 
    403 tag_map* nv::md3_loader::create_tag_map()
     398        data->set_name( (char*)md3->header.name );
     399        data->add_channel( mc_pn );
     400        data->add_channel( mc_t );
     401        data->add_channel( ic );
     402}
     403
     404mesh_nodes_data* nv::md3_loader::release_mesh_nodes_data()
    404405{
    405406        md3_t* md3 = (md3_t*)m_md3;
    406         tag_map* result = new tag_map();
    407         for ( sint32 i = 0; i < md3->header.num_tags; ++i )
     407        uint32 node_count = md3->header.num_tags;
     408        if ( node_count == 0 ) return nullptr;;
     409        mesh_node_data* nodes = new mesh_node_data[ node_count ];
     410        for ( uint32 i = 0; i < node_count; ++i )
    408411        {
    409412                const md3_tag_t& rtag = md3->tags[i];
    410413                std::string name( (char*)(rtag.name) );
    411414
    412                 key_data* data = new key_data;
    413                 data->add_channel( load_tags( name ) );
    414                 result->insert( name, data );
    415         }
    416         return result;
    417 }
    418 
    419 mesh_node_data* nv::md3_loader::release_mesh_node_data( size_t index )
    420 {
    421         md3_t* md3 = (md3_t*)m_md3;
    422         const md3_tag_t& rtag = md3->tags[index];
    423         std::string name( (char*)(rtag.name) );
    424 
    425         mesh_node_data* result = new mesh_node_data;
    426         result->transform = mat4();
    427         result->name      = name;
    428         result->parent_id = -1;
    429         result->target_id = -1;
    430         result->data = new key_data;
     415                nodes[i].transform = mat4();
     416                nodes[i].name      = name;
     417                nodes[i].parent_id = -1;
     418                nodes[i].target_id = -1;
     419                nodes[i].data = new key_data;
    431420       
    432         key_raw_channel* keys = load_tags( name );
    433         result->data->add_channel( keys );
    434         return result;
    435 }
    436 
    437 size_t nv::md3_loader::get_node_count() const
    438 {
    439         return ((md3_t*)m_md3)->header.num_tags;
     421                key_raw_channel* keys = load_tags( name );
     422                nodes[i].data->add_channel( keys );
     423        }
     424        return new mesh_nodes_data( "tags", node_count, nodes );
     425}
     426
     427mesh_data_pack* nv::md3_loader::release_mesh_data_pack()
     428{
     429        mesh_data* data = new mesh_data[1];
     430        release_mesh_frame( &data[0], -1 );
     431        return new mesh_data_pack( 1, data, release_mesh_nodes_data() );
    440432}
    441433
  • trunk/src/formats/md5_loader.cc

    r282 r287  
    1010#include "nv/logging.hh"
    1111#include "nv/io/std_stream.hh"
    12 #include "nv/profiler.hh"
    1312#include <cstring>
    1413
     
    4544bool md5_loader::load( stream& source )
    4645{
    47         NV_PROFILE( "Load MD5" ); // 16XXms original
    4846        std_stream sstream( &source );
    4947        std::string command;
     
    9694                else if ( command == "mesh" )
    9795                {
    98                         md5_mesh_data* mesh = new md5_mesh_data();
    99 
    100                         int num_verts, num_tris, num_weights;
     96                        mesh_data* mesh = new mesh_data("md5_mesh");
     97
     98                        int num_verts   = 0;
     99                        int num_tris    = 0;
     100                        int num_weights = 0;
    101101
    102102                        discard( sstream, "{" );
     
    106106                                if ( command == "shader" )
    107107                                {
    108                                         sstream >> mesh->m_shader;
    109                                         remove_quotes( mesh->m_shader );
    110                                         // texturePath.replace_extension( ".tga" );
     108                                        std::string shader;
     109                                        sstream >> shader;
     110                                        remove_quotes( shader );
    111111                                        next_line( sstream );
    112112                                }
     
    115115                                        sstream >> num_verts;
    116116
     117                                        md5_vtx_t* tdata = nullptr;
    117118                                        {
    118119                                                mesh_raw_channel* ch_pnt = mesh_raw_channel::create<md5_vtx_pnt>( num_verts );
    119120                                                mesh_raw_channel* ch_t   = mesh_raw_channel::create<md5_vtx_t>( num_verts );
    120                                                 mesh->m_pntdata          = (md5_vtx_pnt*)ch_pnt->data;
    121                                                 mesh->m_tdata            = (md5_vtx_t*)ch_t->data;
     121                                                mesh_raw_channel* ch_pntiw = mesh_raw_channel::create<md5_vtx_pntiw>( num_verts );
     122                                                tdata = (md5_vtx_t*)ch_t->data;
    122123                                                mesh->add_channel( ch_pnt );
    123124                                                mesh->add_channel( ch_t );
     125                                                // TODO: hack to prevent rendering
     126                                                ch_pntiw->count = 0;
     127                                                mesh->add_channel( ch_pntiw );
    124128                                        }
    125                                         mesh->m_vtx_data.resize( num_verts );
    126129                                        weight_info.resize( num_verts );
    127130
     
    138141                                                weight_info[i].start_weight = start_weight;
    139142                                                weight_info[i].weight_count = weight_count;
    140                                                 mesh->m_tdata[i].texcoord = texcoord;
     143                                                tdata[i].texcoord = texcoord;
    141144                                        } 
    142145                                }
     
    147150                                        mesh_raw_channel* ch_i = mesh_raw_channel::create_index<uint32>( num_tris * 3 );
    148151                                        uint32* vtx_i                = (uint32*)ch_i->data;
    149                                         mesh->m_idata                = vtx_i;
    150152                                        uint32 idx = 0;
    151153                                        mesh->add_channel( ch_i );
     
    190192                        }
    191193
    192                         prepare_mesh( mesh, weights.data(), weight_info.data() );
     194                        prepare_mesh( weight_info.size(), mesh, weights.data(), weight_info.data() );
    193195
    194196                        m_meshes[ m_num_meshes ] = mesh;
     
    201203}
    202204
    203 bool md5_loader::prepare_mesh( md5_mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info )
    204 {
    205         uint32 vtx_count = mdata->m_vtx_data.size();
    206         md5_vtx_pnt* vtcs = mdata->m_pntdata;
    207 
    208         mdata->m_bone_offset.resize( m_joints.size() );
    209         for ( uint32 i = 0; i < m_joints.size(); ++i )
    210         {
    211                 transform j( m_joints[i].pos, m_joints[i].orient );
    212                 mdata->m_bone_offset[i] = j.inverse();
    213         }
     205bool md5_loader::prepare_mesh( uint32 vtx_count, mesh_data* mdata, md5_weight* weights, md5_weight_info* weight_info )
     206{
     207        md5_vtx_pnt* vtcs = (md5_vtx_pnt*)mdata->get_channel< md5_vtx_pnt >()->data;
     208        md5_vtx_pntiw* vtx_data = (md5_vtx_pntiw*)mdata->get_channel< md5_vtx_pntiw >()->data;
    214209
    215210        for ( uint32 i = 0; i < vtx_count; ++i )
     
    217212                size_t start_weight = weight_info[i].start_weight;
    218213                size_t weight_count = weight_info[i].weight_count;
    219                 md5_vtx_data& vdata = mdata->m_vtx_data[i];
     214                md5_vtx_pntiw& vdata = vtx_data[i];
    220215                md5_vtx_pnt& vtc = vtcs[i];
    221216
     
    268263        }
    269264
     265        const uint32*    idata = (uint32*)mdata->get_index_channel()->data;
     266        const md5_vtx_t* tdata = mdata->get_channel_data<md5_vtx_t>();
     267
    270268        // Prepare normals
    271269        uint32 tri_count = mdata->get_count() / 3;
    272270        for ( unsigned int i = 0; i < tri_count; ++i )
    273271        {
    274                 uint32 ti0 = mdata->m_idata[ i * 3 ];
    275                 uint32 ti1 = mdata->m_idata[ i * 3 + 1 ];
    276                 uint32 ti2 = mdata->m_idata[ i * 3 + 2 ];
     272                uint32 ti0 = idata[ i * 3 ];
     273                uint32 ti1 = idata[ i * 3 + 1 ];
     274                uint32 ti2 = idata[ i * 3 + 2 ];
    277275 
    278276                glm::vec3 v1 = vtcs[ ti0 ].position;
     
    288286                vtcs[ ti2 ].normal += normal;
    289287
    290                 const vec2& w1 = mdata->m_tdata[ ti0 ].texcoord;
    291                 const vec2& w2 = mdata->m_tdata[ ti1 ].texcoord;
    292                 const vec2& w3 = mdata->m_tdata[ ti2 ].texcoord;
     288                const vec2& w1 = tdata[ ti0 ].texcoord;
     289                const vec2& w2 = tdata[ ti1 ].texcoord;
     290                const vec2& w3 = tdata[ ti2 ].texcoord;
    293291
    294292                vec2 st1 = w3 - w1;
     
    306304        for ( size_t i = 0; i < vtx_count; ++i )
    307305        {
    308                 md5_vtx_data& vdata = mdata->m_vtx_data[i];
     306                md5_vtx_pntiw& vdata = vtx_data[i];
    309307
    310308                glm::vec3 normal  = glm::normalize( vtcs[i].normal );
     
    541539}
    542540
    543 md5_mesh_instance* nv::md5_mesh_data::spawn() const
    544 {
    545         return new md5_mesh_instance( this );
    546 }
     541mesh_nodes_data* nv::md5_loader::release_mesh_nodes_data()
     542{
     543        mesh_node_data* nodes = new mesh_node_data[ m_num_joints ];
     544        for ( uint32 i = 0; i < m_num_joints; ++i )
     545        {
     546                mesh_node_data& node = nodes[i];
     547                node.name      = m_joints[i].name;
     548                node.target_id = -1;
     549                node.parent_id = -1;
     550                node.transform = transform( m_joints[i].pos, m_joints[i].orient ).inverse().extract();
     551                node.data      = nullptr;
     552        }
     553        return new mesh_nodes_data( "nodes", m_num_joints, nodes );
     554}
     555
     556mesh_data_pack* nv::md5_loader::release_mesh_data_pack()
     557{
     558        uint32 size = m_meshes.size();
     559        mesh_data* meshes = new mesh_data[ size ];
     560        for ( uint32 i = 0; i < size; ++i )
     561        {
     562                m_meshes[i]->move_to( meshes[i] );
     563                delete m_meshes[i];
     564                m_meshes[i] = nullptr;
     565        }
     566        return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
     567}
     568
    547569
    548570nv::md5_loader::~md5_loader()
     
    551573}
    552574
    553 nv::md5_mesh_instance::md5_mesh_instance( const md5_mesh_data* a_data )
    554         : m_data( a_data ), m_indices( 0 )
    555 {
    556         m_indices = m_data->get_count();
    557         m_pntdata.assign( m_data->m_pntdata, m_data->m_vtx_data.size() );
    558 }
    559 
    560 void nv::md5_mesh_instance::apply( const transform* skeleton )
    561 {
    562         NV_PROFILE("md5::apply");
    563         size_t skeleton_size = m_data->m_bone_offset.size();
    564         size_t vertex_count  = m_pntdata.size();
    565         m_pos_offset.resize( skeleton_size );
    566         for ( unsigned int i = 0; i < skeleton_size; ++i )
    567         {
    568                 m_pos_offset[i] = skeleton[i] * m_data->m_bone_offset[i];
    569         }
    570 
    571         std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 );
    572         for ( unsigned int i = 0; i < vertex_count; ++i )
    573         {
    574                 const md5_vtx_data& vert = m_data->m_vtx_data[i];
    575                 md5_vtx_pnt& result = m_pntdata[i];
    576 
    577                 for ( size_t j = 0; j < 4; ++j )
    578                 {
    579                         int   index  = vert.boneindex[j];
    580                         float weight = vert.boneweight[j];
    581                         const quat& orient      = skeleton[index].get_orientation();
    582                         const transform& offset = m_pos_offset[index];
    583                         result.position += offset.transformed( vert.position )        * weight;
    584                         result.normal   += ( orient * vert.normal  ) * weight;
    585                         result.tangent  += ( orient * vert.tangent ) * weight;
    586                 }
    587         }
    588 }
  • trunk/src/formats/nmd_loader.cc

    r285 r287  
    77#include "nv/formats/nmd_loader.hh"
    88#include "nv/io/std_stream.hh"
     9#include "nv/string.hh"
    910
    1011using namespace nv;
     
    2425                case nmd_type::MESH           : load_mesh( source, element_header ); break;
    2526                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;
     
    4747                mesh->add_channel( channel );
    4848        }
     49        m_mesh_names.push_back( e.name );
    4950        m_meshes.push_back( mesh );
    5051        return true;
     
    5455{
    5556        mesh_data* result = m_meshes[ index ];
     57        if ( m_strings ) result->set_name( m_strings->get( m_mesh_names[ index ] ) );
    5658        m_meshes[ index ] = nullptr;
    5759        return result;
     60}
     61
     62mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
     63{
     64        uint32 size = m_meshes.size();
     65        mesh_data* meshes = new mesh_data[ size ];
     66        for ( uint32 i = 0; i < size; ++i )
     67        {
     68                m_meshes[i]->move_to( meshes[i] );
     69                delete m_meshes[i];
     70        }
     71        m_meshes.clear();
     72        return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
    5873}
    5974
     
    6176{
    6277        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
    63         if ( m_animation ) delete m_animation;
    6478        if ( m_strings )   delete m_strings;
    65         if ( m_bone_data ) delete m_bone_data;
     79        if ( m_node_data ) delete m_node_data;
    6680        m_meshes.clear();
    67         m_animation = nullptr;
    68         m_bone_data = nullptr;
    69         m_strings   = nullptr;
     81        m_mesh_names.clear();
     82        m_node_names.clear();
     83
     84        m_node_data  = nullptr;
     85        m_node_array = nullptr;
     86        m_strings    = nullptr;
    7087}
    7188
     
    8299}
    83100
    84 bool nv::nmd_loader::load_bones( stream& source, const nmd_element_header& e )
    85 {
    86         NV_ASSERT( m_bone_data == nullptr, "MULTIPLE BONE ENTRIES!" );
    87         m_bone_data = new nmd_bone_data;
    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 );
    91         return true;
    92 }
    93 
    94 nmd_animation* nv::nmd_loader::release_animation()
    95 {
    96         nmd_animation* result = m_animation;
    97         m_animation = nullptr;
    98         return result;
    99 }
    100 
    101 nmd_bone_data* nv::nmd_loader::release_bone_data()
    102 {
    103         nmd_bone_data* result = m_bone_data;
    104         m_bone_data = nullptr;
    105         return result;
    106 }
    107 
    108 string_table* nv::nmd_loader::release_string_table()
    109 {
    110         string_table* result = m_strings;
    111         m_strings = nullptr;
    112         return result;
    113 }
    114 
    115 
    116101bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
    117102{
    118         NV_ASSERT( m_animation == nullptr, "MULTIPLE ANIMATION ENTRIES!" );
    119         nmd_animation_header header;
    120         source.read( &header, sizeof( header ), 1 );
    121         m_animation = new nmd_animation;
    122         m_animation->fps        = header.fps;
    123         m_animation->duration   = header.duration;
    124         m_animation->flat       = header.flat;
    125         m_animation->node_count = (uint16)e.children;
    126         m_animation->nodes      = new nmd_node[ e.children ];
     103        NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
     104        nmd_animation_header animation_header;
     105        source.read( &animation_header, sizeof( animation_header ), 1 );
     106        m_node_array = new mesh_node_data[ e.children ];
    127107        for ( uint32 i = 0; i < e.children; ++i )
    128108        {
    129109                nmd_element_header element_header;
    130110                source.read( &element_header, sizeof( element_header ), 1 );
    131                 NV_ASSERT( element_header.type == nmd_type::ANIMATION_NODE, "ANIMATION_NODE expected!" );
    132                 m_animation->nodes[i].name          = element_header.name;
    133 
     111                NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
     112                m_node_names.push_back( element_header.name );
    134113                uint16 ch_count = element_header.children;
    135114
    136                 nmd_animation_node_header node_header;
     115                nmd_node_header node_header;
    137116                source.read( &node_header, sizeof( node_header ), 1 );
    138                 m_animation->nodes[i].parent_id     = node_header.parent_id;
    139                 m_animation->nodes[i].transform     = node_header.transform;
    140                 m_animation->nodes[i].data          = nullptr;
     117                m_node_array[i].parent_id     = node_header.parent_id;
     118                m_node_array[i].transform     = node_header.transform;
     119                m_node_array[i].data          = nullptr;
    141120                if ( ch_count > 0 )
    142121                {
    143122                        key_data* kdata = new key_data;
    144                         m_animation->nodes[i].data = kdata;
     123                        m_node_array[i].data = kdata;
    145124                        for ( uint32 c = 0; c < ch_count; ++c )
    146125                        {
    147126                                source.read( &element_header, sizeof( element_header ), 1 );
    148                                 NV_ASSERT( element_header.type == nmd_type::ANIMATION_CHANNEL, "ANIMATION_CHANNEL expected!" );
    149                                 nv::nmd_animation_channel_header cheader;
     127                                NV_ASSERT( element_header.type == nmd_type::KEY_CHANNEL, "CHANNEL expected!" );
     128                                nv::nmd_key_channel_header cheader;
    150129                                source.read( &cheader, sizeof( cheader ), 1 );
    151130                                key_raw_channel* channel = key_raw_channel::create( cheader.format, cheader.count );
     
    155134                }
    156135        }
     136        m_node_data = new mesh_nodes_data( "animation", e.children, m_node_array, animation_header.frame_rate, animation_header.duration, animation_header.flat );
    157137        return true;
    158138}
    159139
     140mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data()
     141{
     142        if ( m_node_data )
     143        {
     144                if ( m_strings )
     145                {
     146                        for ( uint32 i = 0; i < m_node_data->get_count(); ++i )
     147                        {
     148                                m_node_array[i].name = m_strings->get( m_node_names[i] );
     149                        }
     150                }
     151                mesh_nodes_data* result = m_node_data;
     152                m_node_data = nullptr;
     153                m_node_array = nullptr;
     154                return result;
     155        }
     156        return nullptr;
     157}
    160158
    161159// TEMPORARY
    162 nv::nmd_temp_animation::nmd_temp_animation( nmd_loader* loader )
    163 {
    164         m_animation = loader->release_animation();
    165         m_strings   = loader->release_string_table();
    166160
    167         for ( uint32 n = 0; n < m_animation->node_count; ++n )
    168         {
    169                 nmd_node& node = m_animation->nodes[n];
    170                 m_data.push_back( node.data );
    171                 node.data = nullptr;
    172         }
    173 
    174         if ( !m_animation->flat )
    175         {
    176                 m_children.resize( m_animation->node_count );
    177                 for ( nv::uint32 n = 0; n < m_animation->node_count; ++n )
    178                 {
    179                         const nmd_node& node = m_animation->nodes[n];
    180                         if ( node.parent_id != -1 )
    181                         {
    182                                 m_children[ node.parent_id ].push_back( n );
    183                         }
    184                 }
    185         }
    186 
    187         m_bone_ids.resize( m_animation->node_count );
    188 }
    189 
    190 nv::nmd_temp_animation::~nmd_temp_animation()
    191 {
    192         for ( auto node : m_data ) delete node;
    193         delete m_animation;
    194         delete m_strings;
    195 }
    196 
    197 void nv::nmd_temp_animation::prepare( const nmd_temp_model* model )
    198 {
    199         m_offsets = model->m_bone_offsets.data();
    200         for ( uint32 n = 0; n < m_animation->node_count; ++n )
    201         {
    202                 const nmd_node& node = m_animation->nodes[n];
    203                 sint16 bone_id = -1;
    204 
    205                 auto bi = model->m_bone_names.find( m_strings->get( node.name ) );
    206                 if ( bi != model->m_bone_names.end() )
    207                 {
    208                         bone_id = bi->second;
    209                 }
    210                 m_bone_ids[n] = bone_id;
    211         }
    212 }
    213 
    214 void nv::nmd_temp_animation::animate( mat4* data, uint32 time )
    215 {
    216         float tick_time = ( time / 1000.0f ) * m_animation->fps;
    217         float anim_time = fmodf( tick_time, m_animation->duration );
    218 
    219         if ( !m_animation->flat )
    220         {
    221                 animate_rec( data, anim_time, 0, mat4() );
    222                 return;
    223         }
    224 
    225         for ( uint32 n = 0; n < m_animation->node_count; ++n )
    226                 if ( m_bone_ids[n] >= 0 )
    227                 {
    228                         const nmd_node* node = &m_animation->nodes[ n ];
    229                         nv::mat4 node_mat( node->transform );
    230 
    231                         if ( m_data[n] )
    232                         {
    233                                 node_mat = m_data[n]->get_matrix( anim_time );
    234                         }
    235 
    236                         sint16 bone_id = m_bone_ids[n];
    237                         data[ bone_id ] = node_mat * m_offsets[ bone_id ];
    238                 }
    239 
    240 }
    241 
    242 void nv::nmd_temp_animation::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat )
    243 {
    244         // TODO: fix transforms, which are now embedded,
    245         //       see note in assimp_loader.cc:load_node
    246         const nmd_node* node = &m_animation->nodes[ node_id ];
    247         mat4 node_mat( node->transform );
    248 
    249         if ( m_data[ node_id ] )
    250         {
    251                 node_mat = m_data[ node_id ]->get_matrix( time );
    252         }
    253 
    254         mat4 global_mat = parent_mat * node_mat;
    255 
    256         sint16 bone_id = m_bone_ids[ node_id ];
    257         if ( bone_id >= 0 )
    258         {
    259                 data[ bone_id ] = global_mat * m_offsets[ bone_id ];
    260         }
    261 
    262         for ( auto child : m_children[ node_id ] )
    263         {
    264                 animate_rec( data, time, child, global_mat );
    265         }
    266 }
    267 
    268 nv::nmd_temp_model::nmd_temp_model( nmd_loader* loader )
     161nv::nmd_temp_model_data::nmd_temp_model_data( nmd_loader* loader )
    269162{
    270163        for ( unsigned m = 0; m < loader->get_mesh_count(); ++m )
     
    272165                m_mesh_data.push_back(loader->release_mesh_data(m));
    273166        }
    274         nmd_bone_data* bone_data = loader->release_bone_data();
    275         string_table*  strings   = loader->release_string_table();
    276 
    277         for ( nv::uint16 bi = 0; bi < bone_data->count; ++bi )
    278         {
    279                 m_bone_names[ strings->get( bone_data->bones[bi].name ) ] = bi;
    280                 m_bone_offsets.push_back( bone_data->bones[bi].offset );
    281         }
    282 
    283         delete bone_data;
    284         delete strings;
     167        m_node_data = loader->release_mesh_nodes_data();
    285168}
    286169
    287 nv::nmd_temp_model::~nmd_temp_model()
     170nv::nmd_temp_model_data::~nmd_temp_model_data()
    288171{
    289172        for ( unsigned m = 0; m < m_mesh_data.size(); ++m )
     
    291174                delete m_mesh_data[m];
    292175        }
     176        delete m_node_data;
    293177}
  • trunk/src/formats/obj_loader.cc

    r280 r287  
    5050        std::string line;
    5151        std::string cmd;
     52        std::string name;
     53        std::string next_name;
    5254
    5355        std::size_t size;
     
    7779bool obj_reader::read_stream( std::istream& stream )
    7880{
     81        name = next_name;
    7982        bool added_faces = false;
    8083        f32 x, y, z;
     
    146149                if ( cmd == "g" )
    147150                {
    148                         if (added_faces) return true;
     151                        ss >> next_name;
     152                        if (added_faces)
     153                                return true;
     154                        name = next_name;
    149155                        continue;
    150156                }
     
    335341                channel->count = reader->size * 3;
    336342
    337                 mesh_data* mesh = new mesh_data();
     343                mesh_data* mesh = new mesh_data(reader->name);
    338344                mesh->add_channel( channel );
    339345                m_meshes.push_back( mesh );
     
    357363        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
    358364}
     365
     366mesh_data_pack* nv::obj_loader::release_mesh_data_pack()
     367{
     368        uint32 size = m_meshes.size();
     369        mesh_data* meshes = new mesh_data[ size ];
     370        for ( uint32 i = 0; i < size; ++i )
     371        {
     372                m_meshes[i]->move_to( meshes[i] );
     373                delete m_meshes[i];
     374        }
     375        m_meshes.clear();
     376        return new mesh_data_pack( size, meshes );
     377}
  • trunk/src/gfx/keyframed_mesh.cc

    r285 r287  
    1515using namespace nv;
    1616
    17 nv::keyframed_mesh::keyframed_mesh( device* a_device, mesh_data* a_data, tag_map* a_tag_map )
     17nv::keyframed_mesh::keyframed_mesh( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
    1818        : animated_mesh()
    1919        , m_mesh_data( a_data )
     
    4040}
    4141
    42 transform keyframed_mesh::get_tag( uint32 tag ) const
     42transform keyframed_mesh::get_node_transform( uint32 node_id ) const
    4343{
    4444        NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
    45         const key_data* data = m_tag_map->get_tag( tag );
     45        NV_ASSERT( node_id < m_tag_map->get_count(), "TAGMAP FAIL" );
     46        const key_data* data = m_tag_map->get_node( node_id )->data;
    4647        NV_ASSERT( data, "TAG FAIL" );
    4748        transform last = data->get_raw_transform( m_last_frame );
    4849        transform next = data->get_raw_transform( m_next_frame );
    4950        return interpolate( last, next, m_interpolation  );
     51}
     52
     53mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const
     54{
     55        return get_node_transform( node_id ).extract();
    5056}
    5157
     
    125131}
    126132
    127 nv::keyframed_mesh_gpu::keyframed_mesh_gpu( device* a_device, mesh_data* a_data, tag_map* a_tag_map, program* a_program )
     133nv::keyframed_mesh_gpu::keyframed_mesh_gpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map, program* a_program )
    128134        : keyframed_mesh( a_device, a_data, a_tag_map )
    129135        , m_loc_next_position( 0 )
     
    159165}
    160166
    161 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( device* a_device, mesh_data* a_data, tag_map* a_tag_map )
     167nv::keyframed_mesh_cpu::keyframed_mesh_cpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
    162168        : keyframed_mesh( a_device, a_data, a_tag_map )
    163169{
  • trunk/src/gfx/skeletal_mesh.cc

    r283 r287  
    1111
    1212
    13 nv::skeletal_mesh::skeletal_mesh( device* a_device, md5_mesh_data* a_mesh_data )
     13nv::skeletal_mesh::skeletal_mesh( device* a_device, const mesh_data* a_mesh_data, const mesh_nodes_data* bones )
    1414        : animated_mesh()
    15         , m_mesh_data( nullptr )
    16 {
    17         m_mesh_data = a_mesh_data->spawn();
     15        , m_data( a_mesh_data )
     16{
     17        const mesh_raw_channel* pnt_chan = a_mesh_data->get_channel<md5_vtx_pnt>();
     18        m_pntdata.assign( (const md5_vtx_pnt*)pnt_chan->data, pnt_chan->count );
     19        m_bone_offset.resize( bones->get_count() );
     20        for ( uint32 i = 0; i < bones->get_count(); ++i )
     21        {
     22                m_bone_offset[i] = transform( bones->get_node(i)->transform );
     23        }
     24        m_vtx_data  = a_mesh_data->get_channel_data<md5_vtx_pntiw>();
     25        m_indices   = a_mesh_data->get_count();
    1826        m_va        = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
    1927}
     
    2836                uint32 new_time = a_anim_time % anim_duration;
    2937                anim->update_skeleton( m_transform.data(), (float)new_time * 0.001f );
    30                 m_mesh_data->apply( m_transform.data() );
     38
     39                //m_mesh_data->apply( m_transform.data() );
     40                {
     41                        size_t skeleton_size = m_bone_offset.size();
     42                        size_t vertex_count  = m_pntdata.size();
     43                        m_pos_offset.resize( skeleton_size );
     44                        for ( unsigned int i = 0; i < skeleton_size; ++i )
     45                        {
     46                                m_pos_offset[i] = m_transform[i] * m_bone_offset[i];
     47                        }
     48
     49                        std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 );
     50                        for ( unsigned int i = 0; i < vertex_count; ++i )
     51                        {
     52                                const md5_vtx_pntiw& vert = m_vtx_data[i];
     53
     54                                for ( size_t j = 0; j < 4; ++j )
     55                                {
     56                                        int   index  = vert.boneindex[j];
     57                                        float weight = vert.boneweight[j];
     58                                        const quat& orient      = m_transform[index].get_orientation();
     59                                        const transform& offset = m_pos_offset[index];
     60                                        m_pntdata[i].position += offset.transformed( vert.position )        * weight;
     61                                        m_pntdata[i].normal   += ( orient * vert.normal  ) * weight;
     62                                        m_pntdata[i].tangent  += ( orient * vert.tangent ) * weight;
     63                                }
     64                        }
     65                }
     66
    3167                vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION );
    3268                vb->bind();
    33                 vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
     69                vb->update( m_pntdata.data(), 0, m_pntdata.raw_size() );
    3470                vb->unbind();
    3571        }
     
    3975{
    4076        delete m_va;
    41         delete m_mesh_data;
    4277}
    4378
     
    5287}
    5388
    54 void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time )
    55 {
    56         m_animation->animate( data, time );
    57 }
    58 
    59 void nv::skeletal_animation_entry_gpu::prepare( const nmd_temp_model* m_model )
    60 {
    61         m_animation->prepare( m_model );
    62 }
    63 
    64 nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const nmd_temp_model* a_model, uint32 index, bool primary )
    65         : animated_mesh(), m_primary( primary ), m_model( a_model )
    66 {
    67         const mesh_data* data = a_model->get_data( index );
    68         m_va          = a_device->create_vertex_array( data, nv::STATIC_DRAW );
    69         m_index_count = data->get_count();
     89
     90nv::skeletal_animation_entry_gpu::skeletal_animation_entry_gpu( const std::string& name, const mesh_nodes_data* anim, bool a_looping )
     91        : animation_entry( name )
     92        , m_node_data( anim )
     93{
     94        uint32 node_count = m_node_data->get_count();
     95
     96        m_prepared  = false;
     97        m_looping   = a_looping;
     98        m_children  = nullptr;
     99        m_offsets   = nullptr;
     100        m_bone_ids  = new sint16[ node_count ];
     101
     102        if ( !m_node_data->is_flat() )
     103        {
     104                m_children = new std::vector< uint32 >[ node_count ];
     105                for ( uint32 n = 0; n < node_count; ++n )
     106                {
     107                        const mesh_node_data* node = m_node_data->get_node(n);
     108                        if ( node->parent_id != -1 )
     109                        {
     110                                m_children[ node->parent_id ].push_back( n );
     111                        }
     112                }
     113        }
     114}
     115
     116void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) const
     117{
     118        float tick_time = ( time * 0.001f ) * m_node_data->get_frame_rate();
     119        float anim_time = fmodf( tick_time, m_node_data->get_duration() );
     120
     121        if ( !m_node_data->is_flat() )
     122        {
     123                animate_rec( data, anim_time, 0, mat4() );
     124                return;
     125        }
     126
     127        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
     128                if ( m_bone_ids[n] >= 0 )
     129                {
     130                        const mesh_node_data* node = m_node_data->get_node(n);
     131                        nv::mat4 node_mat( node->transform );
     132
     133                        if ( node->data )
     134                        {
     135                                node_mat = node->data->get_matrix( anim_time );
     136                        }
     137
     138                        sint16 bone_id = m_bone_ids[n];
     139                        data[ bone_id ] = node_mat * m_offsets[ bone_id ];
     140                }
     141}
     142
     143void nv::skeletal_animation_entry_gpu::prepare( const mesh_nodes_data* bones )
     144{
     145        if ( m_prepared ) return;
     146        std::unordered_map< std::string, nv::uint16 > bone_names;
     147        m_offsets = new mat4[ bones->get_count() ];
     148        for ( nv::uint16 bi = 0; bi < bones->get_count(); ++bi )
     149        {
     150                const mesh_node_data* bone = bones->get_node(bi);
     151                bone_names[ bone->name ] = bi;
     152                m_offsets[bi] = bone->transform;
     153        }
     154
     155        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
     156        {
     157                const mesh_node_data* node = m_node_data->get_node(n);
     158                sint16 bone_id = -1;
     159
     160                auto bi = bone_names.find( node->name );
     161                if ( bi != bone_names.end() )
     162                {
     163                        bone_id = bi->second;
     164                }
     165                m_bone_ids[n] = bone_id;
     166        }
     167        m_prepared = true;
     168}
     169
     170void nv::skeletal_animation_entry_gpu::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const
     171{
     172        // TODO: fix transforms, which are now embedded,
     173        //       see note in assimp_loader.cc:load_node
     174        const mesh_node_data* node = m_node_data->get_node( node_id );
     175        mat4 node_mat( node->transform );
     176
     177        if ( node->data )
     178        {
     179                node_mat = node->data->get_matrix( time );
     180        }
     181
     182        mat4 global_mat = parent_mat * node_mat;
     183
     184        sint16 bone_id = m_bone_ids[ node_id ];
     185        if ( bone_id >= 0 )
     186        {
     187                data[ bone_id ] = global_mat * m_offsets[ bone_id ];
     188        }
     189
     190        for ( auto child : m_children[ node_id ] )
     191        {
     192                animate_rec( data, time, child, global_mat );
     193        }
     194}
     195
     196nv::skeletal_animation_entry_gpu::~skeletal_animation_entry_gpu()
     197{
     198        delete[] m_offsets;
     199        delete[] m_children;
     200        delete[] m_bone_ids;
     201}
     202
     203nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data )
     204        : animated_mesh(), m_bone_data( a_bone_data ), m_transform( nullptr )
     205{
     206        m_va          = a_device->create_vertex_array( a_mesh, nv::STATIC_DRAW );
     207        m_index_count = a_mesh->get_count();
     208        if ( m_bone_data )
     209        {
     210                m_transform = new mat4[ m_bone_data->get_count() ];
     211        }
    70212}
    71213
    72214void nv::skeletal_mesh_gpu::run_animation( animation_entry* a_anim )
    73215{
    74         if ( m_primary && a_anim != nullptr )
     216        if ( m_bone_data && a_anim != nullptr )
    75217        {
    76218                skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)(a_anim);
    77                 m_transform.resize( m_model->get_bone_count() );
    78                 anim->prepare( m_model );
     219                anim->prepare( m_bone_data );
    79220                update_animation( a_anim, 0 );
    80221        }
     
    83224void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
    84225{
    85         if ( m_primary && a_anim )
     226        if ( m_bone_data && a_anim )
    86227        {
    87228                skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim;
    88                 anim->update_skeleton( m_transform.data(), a_anim_time );
     229                anim->update_skeleton( m_transform, a_anim_time );
    89230        }
    90231}
     
    92233void nv::skeletal_mesh_gpu::update( program* a_program ) const
    93234{
    94         if (m_primary)
    95                 a_program->set_uniform_array( "nv_m_bones", m_transform );
    96 }
     235        if ( m_bone_data )
     236                a_program->set_uniform_array( "nv_m_bones", m_transform, m_bone_data->get_count() );
     237}
     238
     239nv::transform nv::skeletal_mesh_gpu::get_node_transform( uint32 node_id ) const
     240{
     241        return transform( m_transform[ node_id ] );
     242}
     243
     244nv::mat4 nv::skeletal_mesh_gpu::get_node_matrix( uint32 node_id ) const
     245{
     246        return m_transform[ node_id ];
     247}
Note: See TracChangeset for help on using the changeset viewer.