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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 }
Note: See TracChangeset for help on using the changeset viewer.