source: trunk/src/formats/md5_loader.cc @ 430

Last change on this file since 430 was 428, checked in by epyon, 10 years ago
  • renaming mesh_nodes_data members to more compliant
  • moved string_table to stl
File size: 15.3 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/formats/md5_loader.hh"
8
9#include "nv/core/logging.hh"
10#include "nv/stl/vector.hh"
11#include "nv/io/std_stream.hh"
12#include "nv/interface/data_channel_access.hh"
13
14#include <stdio.h>  // sscanf
15#include <stdlib.h> // atof
16
17using namespace nv;
18
19static void next_line( std::istream& stream )
20{
21        stream.ignore( 1024*1024, '\n' );
22}
23
24static inline void discard( std::istream& stream, const std::string& token )
25{
26        std::string discarded;
27        stream >> discarded;
28        assert( discarded == token );
29}
30
31static void remove_quotes( std::string& str )
32{
33        nv::size_t n;
34        while ( ( n = str.find('\"') ) != std::string::npos ) str.erase(n,1);
35}
36
37static void unit_quat_w( nv::quat& quat )
38{
39        float t = 1.0f - ( quat.x * quat.x ) - ( quat.y * quat.y ) - ( quat.z * quat.z );
40        quat.w = ( t < 0.0f ? 0.0f : -sqrtf(t) );
41}
42
43bool md5_loader::load( stream& source )
44{
45        reset();
46        std_stream sstream( &source );
47        std::string command;
48        size_t num_joints = 0;
49
50        // MESH data
51        dynamic_array< md5_weight > weights;
52        dynamic_array< md5_weight_info > weight_info;
53        size_t num_meshes = 0;
54
55        // MESH data
56        dynamic_array< md5_joint_info > joint_infos;
57        vector< transform >             base_frames;
58        size_t num_animated_components = 0;
59        size_t frame_rate = 0;
60        size_t num_frames = 0;
61
62        sstream >> command;
63        while ( !sstream.eof() )
64        {
65                if ( command == "MD5Version" )
66                {
67                        sstream >> m_md5_version;
68                        assert( m_md5_version == 10 );
69                }
70                else if ( command == "commandline" )
71                {
72                        next_line( sstream );
73                }
74                else if ( command == "numJoints" )
75                {
76                        sstream >> num_joints;
77                        next_line( sstream );
78                }
79                else if ( command == "numMeshes" )
80                {
81                        assert( m_type == UNKNOWN );
82                        m_type = MESH;
83                        sstream >> num_meshes;
84                        m_meshes.resize( num_meshes );
85                        num_meshes = 0;
86                }
87                else if ( command == "numFrames" )
88                {
89                        assert( m_type == UNKNOWN || m_type == ANIMATION );
90                        m_type = ANIMATION;
91                        sstream >> num_frames;
92                        next_line( sstream );
93                }
94                else if ( command == "frameRate" )
95                {
96                        assert( m_type == UNKNOWN || m_type == ANIMATION );
97                        m_type = ANIMATION;
98                        sstream >> frame_rate;
99                        next_line( sstream );
100                }
101                else if ( command == "numAnimatedComponents" )
102                {
103                        assert( m_type == UNKNOWN || m_type == ANIMATION );
104                        m_type = ANIMATION;
105                        sstream >> num_animated_components;
106                        next_line( sstream );
107                }
108                else if ( command == "joints" )
109                {
110                        assert( m_type == MESH );
111                        assert( m_nodes == nullptr );
112                        m_nodes = new mesh_nodes_data( make_name( "md5_bones") );
113                        discard( sstream, "{" );
114                        for ( size_t i = 0; i < m_nodes->size(); ++i )
115                        {
116                                std::string name;
117                                sint16 parent_id;
118                                sstream >> name >> parent_id;
119                                vec3 pos;
120                                quat orient;
121                                discard( sstream, "(" );
122                                sstream >> pos.x >> pos.y >> pos.z;
123                                discard( sstream, ")" );
124                                discard( sstream, "(" );
125                                sstream >> orient.x >> orient.y >> orient.z;
126                                unit_quat_w( orient );
127                                remove_quotes( name );
128                                data_channel_set* set = data_channel_set_creator::create_set( 0 );
129                                data_channel_set_creator access( set );
130                                access.set_parent_id( parent_id );
131                                access.set_transform( transform( pos, orient ).inverse().extract() );
132                                access.set_name( make_name( name.c_str() ) );
133                                next_line( sstream );
134                                m_nodes->push_back( set );
135                        }
136                        discard( sstream, "}" );
137                }
138                else if ( command == "mesh" )
139                {
140                        assert( m_type == MESH );
141                        data_channel_set* mesh = data_channel_set_creator::create_set( 4 );
142                        data_channel_set_creator maccess( mesh );
143
144                        uint32 num_verts   = 0;
145                        uint32 num_tris    = 0;
146                        uint32 num_weights = 0;
147
148                        discard( sstream, "{" );
149                        sstream >> command;
150                        while ( command != "}" )
151                        {
152                                if ( command == "shader" )
153                                {
154                                        std::string shader;
155                                        sstream >> shader;
156                                        remove_quotes( shader );
157                                        maccess.set_name( make_name( shader ) );
158                                        next_line( sstream );
159                                }
160                                else if ( command == "numverts")
161                                {
162                                        sstream >> num_verts;
163
164                                        md5_vtx_t* tdata = nullptr;
165                                        {
166                                                maccess.add_channel<md5_vtx_pnt>( num_verts );
167                                                tdata = maccess.add_channel<md5_vtx_t>( num_verts ).data();
168                                                maccess.add_channel<md5_vtx_pntiw>( num_verts );
169                                        }
170                                        weight_info.resize( num_verts );
171
172                                        next_line( sstream );
173                                        std::string line;
174                                        for ( uint32 i = 0; i < num_verts; ++i )
175                                        {
176                                                size_t weight_count;
177                                                size_t start_weight;
178                                                vec2 texcoord;
179
180                                                std::getline( sstream, line );
181                                                sscanf( line.c_str(), "%*s %*u ( %f %f ) %u %u", &(texcoord.x), &(texcoord.y), &(start_weight), &(weight_count) );
182                                                weight_info[i].start_weight = start_weight;
183                                                weight_info[i].weight_count = weight_count;
184                                                tdata[i].texcoord = texcoord;
185                                        } 
186                                }
187                                else if ( command == "numtris" )
188                                {
189                                        sstream >> num_tris;
190
191                                        uint32* vtx_i = reinterpret_cast< uint32* >( maccess.add_channel< index_u32 >( num_tris * 3 ).raw_data() );
192                                        uint32 idx = 0;
193
194                                        next_line( sstream );
195                                        std::string line;
196                                        for ( uint32 i = 0; i < num_tris; ++i )
197                                        {
198                                                unsigned ti0;
199                                                unsigned ti1;
200                                                unsigned ti2;
201
202                                                std::getline( sstream, line );
203                                                sscanf( line.c_str(), "%*s %*u %u %u %u )", &(ti0), &(ti1), &(ti2));
204
205                                                vtx_i[idx++] = ti0;
206                                                vtx_i[idx++] = ti1;
207                                                vtx_i[idx++] = ti2;
208                                        }             
209
210                                }
211                                else if ( command == "numweights" )
212                                {
213                                        sstream >> num_weights;
214                                        weights.resize( num_weights );
215                                        next_line( sstream );
216                                        std::string line;
217                                        for ( uint32 i = 0; i < num_weights; ++i )
218                                        {
219                                                md5_weight weight;
220
221                                                std::getline( sstream, line );
222                                                sscanf( line.c_str(), "%*s %*u %u %f ( %f %f %f )", &(weight.joint_id), &(weight.bias), &(weight.pos.x), &(weight.pos.y), &(weight.pos.z));
223                                                weights[i] = weight;
224                                        }
225                                }
226                                else
227                                {
228                                        next_line( sstream );
229                                }
230
231                                sstream >> command;
232                        }
233
234                        prepare_mesh( m_nodes, weight_info.size(), mesh, weights.data(), weight_info.data() );
235
236                        m_meshes[ num_meshes ] = mesh;
237                        num_meshes++;
238                } // mesh
239                else if ( command == "hierarchy" )
240                {
241                        assert( m_type == ANIMATION );
242                        assert( m_nodes == nullptr );
243                        m_nodes = new mesh_nodes_data( make_name( "md5_animation" ), static_cast< nv::uint16 >( frame_rate ), static_cast< float >( num_frames ), true );
244                        joint_infos.resize( num_joints );
245
246                        discard( sstream, "{" );
247                        for ( size_t i = 0; i < m_nodes->size(); ++i )
248                        {
249                                std::string    name;
250                                sint16 parent_id;
251                                sstream >> name >> parent_id >> joint_infos[i].flags >> joint_infos[i].start_index;
252                                remove_quotes( name );
253                                data_channel_set* set = data_channel_set_creator::create_set( 1 );
254                                data_channel_set_creator access( set );
255                                access.add_channel< md5_key_t >( num_frames );
256                                access.set_name( make_name( name.c_str() ) );
257                                access.set_parent_id( parent_id );
258                                m_nodes->push_back( set );
259                                next_line( sstream );
260                        }
261                        discard( sstream, "}" );
262                }
263                else if ( command == "bounds" )
264                {
265                        assert( m_type == ANIMATION );
266                        discard( sstream, "{" );
267                        next_line( sstream );
268                        for ( size_t i = 0; i < num_frames; ++i )
269                        {
270                                //                              vec3 min;
271                                //                              vec3 max;
272                                //                              discard( sstream, "(" );
273                                //                              sstream >> min.x >> min.y >> min.z;
274                                //                              discard( sstream, ")" );
275                                //                              discard( sstream, "(" );
276                                //                              sstream >> max.x >> max.y >> max.z;
277                                //                              m_bounds.push_back( bound );
278                                next_line( sstream );
279                        }
280
281                        discard( sstream, "}" );
282                        next_line( sstream );
283                }
284                else if ( command == "baseframe" )
285                {
286                        assert( m_type == ANIMATION );
287                        discard( sstream, "{" );
288                        next_line( sstream );
289
290                        for ( size_t i = 0; i < m_nodes->size(); ++i )
291                        {
292                                transform base_frame;
293                                vec3 pos;
294                                quat orient;
295                                discard( sstream, "(" );
296                                sstream >> pos.x >> pos.y >> pos.z;
297                                discard( sstream, ")" );
298                                discard( sstream, "(" );
299                                sstream >> orient.x >> orient.y >> orient.z;
300                                next_line( sstream );
301
302                                base_frames.emplace_back( pos, orient );
303                        }
304                        discard( sstream, "}" );
305                        next_line( sstream );
306                }
307                else if ( command == "frame" )
308                {
309                        vector<float> frame;
310                        uint32 frame_id;
311                        sstream >> frame_id;
312                        discard( sstream, "{" );
313                        next_line( sstream );
314
315                        frame.reserve( num_animated_components );
316                        char buf[50];
317                        for ( size_t i = 0; i < num_animated_components; ++i )
318                        {
319                                sstream >> buf;
320                                frame.push_back( static_cast< float >( atof(buf) ) );
321                        }
322
323                        build_frame_skeleton( m_nodes, frame_id, joint_infos, base_frames, frame );
324
325                        discard( sstream, "}" );
326                        next_line( sstream );
327                }
328
329                sstream >> command;
330        }
331
332        return true;
333}
334
335bool md5_loader::prepare_mesh( mesh_nodes_data* nodes, uint32 vtx_count, data_channel_set* mdata, md5_weight* weights, md5_weight_info* weight_info )
336{
337        assert( m_type == MESH );
338        data_channel_access< md5_vtx_pnt >   pnt  ( const_cast< raw_data_channel* >( mdata->get_channel< md5_vtx_pnt >() ) );
339        data_channel_access< md5_vtx_pntiw > pntiw( const_cast< raw_data_channel* >( mdata->get_channel< md5_vtx_pntiw >() ) );
340        md5_vtx_pntiw* vtx_data = pntiw.data();
341        md5_vtx_pnt* vtcs = pnt.data();
342
343        for ( uint32 i = 0; i < vtx_count; ++i )
344        {
345                size_t start_weight = weight_info[i].start_weight;
346                size_t weight_count = weight_info[i].weight_count;
347                md5_vtx_pntiw& vdata = vtx_data[i];
348                md5_vtx_pnt& vtc = vtcs[i];
349
350                vtc.position = vec3(0);
351                vtc.normal   = vec3(0);
352                vtc.tangent  = vec3(0);
353
354                stable_sort( weights + start_weight, weights + start_weight + weight_count, [] ( const md5_weight& a, const md5_weight& b ) -> bool { return a.bias > b.bias; } );
355                //std::sort( weights + start_weight, weights + start_weight + weight_count, [](const md5_weight& a, const md5_weight& b) -> bool { return a.bias > b.bias; } );
356
357                if ( weight_count > 4 )
358                {
359                        float sum = 0.0f;
360                        for ( size_t j = 0; j < 4; ++j )
361                        {
362                                sum += weights[start_weight + j].bias;
363                        }
364                        float ratio = 1.0f / sum;
365                        for ( size_t j = 0; j < 4; ++j )
366                        {
367                                weights[start_weight + j].bias = ratio * weights[start_weight + j].bias;
368                        }
369                        weight_count = 4;
370                }
371
372                for ( int j = 0; j < 4; ++j )
373                {
374                        if ( j < int(weight_count) )
375                        {
376                                vdata.boneindex[j]  = int( weights[int(start_weight) + j].joint_id );
377                                vdata.boneweight[j] = weights[int(start_weight) + j].bias;
378                        }
379                        else
380                        {
381                                vdata.boneindex[j]  = 0;
382                                vdata.boneweight[j] = 0.0f;
383                        }
384                }
385
386                for ( size_t j = 0; j < 4; ++j )
387                {
388                        if ( j < weight_count )
389                        {
390                                md5_weight& weight             = weights[start_weight + j];
391                                const data_channel_set*  joint = (*nodes)[weight.joint_id];
392                                const transform tr = transform( joint->get_transform() ).inverse();
393                                vec3 rot_pos = tr.get_orientation() * weight.pos;
394
395                                vtc.position += ( tr.get_position() + rot_pos ) * weight.bias;
396                        }
397                }
398        }
399
400        const uint32*    idata = reinterpret_cast< uint32* >( const_cast< uint8* >( mdata->get_channel( slot::INDEX )->raw_data() ) );
401        const md5_vtx_t* tdata = mdata->get_channel_data<md5_vtx_t>();
402
403        // Prepare normals
404        uint32 tri_count = mdata->get_channel_size( slot::INDEX ) / 3;
405        for ( unsigned int i = 0; i < tri_count; ++i )
406        {
407                uint32 ti0 = idata[ i * 3 ];
408                uint32 ti1 = idata[ i * 3 + 1 ];
409                uint32 ti2 = idata[ i * 3 + 2 ];
410 
411                vec3 v1 = vtcs[ ti0 ].position;
412                vec3 v2 = vtcs[ ti1 ].position;
413                vec3 v3 = vtcs[ ti2 ].position;
414                vec3 xyz1 = v3 - v1;
415                vec3 xyz2 = v2 - v1;
416
417                vec3 normal = glm::cross( xyz1, xyz2 );
418
419                vtcs[ ti0 ].normal += normal;
420                vtcs[ ti1 ].normal += normal;
421                vtcs[ ti2 ].normal += normal;
422
423                const vec2& w1 = tdata[ ti0 ].texcoord;
424                const vec2& w2 = tdata[ ti1 ].texcoord;
425                const vec2& w3 = tdata[ ti2 ].texcoord;
426
427                vec2 st1 = w3 - w1;
428                vec2 st2 = w2 - w1;
429
430                float coef = 1.0f / (st1.x * st2.y - st2.x * st1.y);
431
432                vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
433
434                vtcs[ ti0 ].tangent += tangent;
435                vtcs[ ti1 ].tangent += tangent;
436                vtcs[ ti2 ].tangent += tangent;
437        }
438
439        for ( size_t i = 0; i < vtx_count; ++i )
440        {
441                md5_vtx_pntiw& vdata = vtx_data[i];
442
443                vec3 normal  = glm::normalize( vtcs[i].normal );
444                vec3 tangent = glm::normalize( vtcs[i].tangent );
445                vtcs[i].normal   = normal;
446                vtcs[i].tangent  = tangent;
447
448                vdata.position = vtcs[i].position;
449                vdata.normal   = vec3(0);
450                vdata.tangent  = vec3(0);
451 
452                for ( int j = 0; j < 4; ++j )
453                {
454                        const data_channel_set*  joint = ( *nodes )[vdata.boneindex[j]];
455                        const transform tr = transform( joint->get_transform() ).inverse();
456                        vdata.normal  += ( normal  * tr.get_orientation() ) * vdata.boneweight[j];
457                        vdata.tangent += ( tangent * tr.get_orientation() ) * vdata.boneweight[j];
458                }
459        }
460
461        return true;
462}
463
464void md5_loader::build_frame_skeleton( mesh_nodes_data* nodes, uint32 index, const array_view<md5_joint_info>& joint_infos, const array_view<transform>& base_frames, const array_view<float>& frame_data )
465{
466        assert( m_type == ANIMATION );
467        for ( unsigned int i = 0; i < joint_infos.size(); ++i )
468        {
469                unsigned int j = 0;
470
471                const md5_joint_info& jinfo = joint_infos[i];
472                const data_channel_set* joint = (*nodes)[i];
473                int parent_id         = joint->get_parent_id();
474
475                vec3 pos    = base_frames[i].get_position();
476                quat orient = base_frames[i].get_orientation();
477                if ( jinfo.flags & 1 )  pos.x    = frame_data[ jinfo.start_index + j++ ];
478                if ( jinfo.flags & 2 )  pos.y    = frame_data[ jinfo.start_index + j++ ];
479                if ( jinfo.flags & 4 )  pos.z    = frame_data[ jinfo.start_index + j++ ];
480                if ( jinfo.flags & 8 )  orient.x = frame_data[ jinfo.start_index + j++ ];
481                if ( jinfo.flags & 16 ) orient.y = frame_data[ jinfo.start_index + j++ ];
482                if ( jinfo.flags & 32 ) orient.z = frame_data[ jinfo.start_index + j++ ];
483                unit_quat_w( orient );
484
485                if ( parent_id >= 0 ) // Has a parent joint
486                {
487                        const data_channel_set* pjoint = ( *nodes )[i];
488                        const transform* ptv = reinterpret_cast< const transform* >( pjoint->get_channel(0)->raw_data() );
489                        transform ptr;
490                        if ( pjoint->get_channel(0)->size() > index ) ptr = ptv[ index ];
491                        vec3 rot_pos = ptr.get_orientation() * pos;
492
493                        pos    = ptr.get_position() + rot_pos;
494                        orient = ptr.get_orientation() * orient;
495
496                        orient = glm::normalize( orient );
497                }
498
499                reinterpret_cast< transform* >( const_cast< uint8* >( joint->get_channel(0)->raw_data() ) )[index] = transform( pos, orient );
500        }
501}
502
503data_channel_set* nv::md5_loader::release_mesh_data( size_t index )
504{
505        data_channel_set* result = m_meshes[ index ];
506        m_meshes[ index ] = nullptr;
507        return result;
508}
509
510mesh_nodes_data* nv::md5_loader::release_mesh_nodes_data( size_t )
511{
512        mesh_nodes_data* nodes = m_nodes;
513        m_nodes = nullptr;
514        return nodes;
515}
516
517mesh_data_pack* nv::md5_loader::release_mesh_data_pack()
518{
519        uint32 size = m_meshes.size();
520        data_channel_set* meshes = data_channel_set_creator::create_set_array( size, 4 );
521        for ( uint32 i = 0; i < size; ++i )
522        {
523                meshes[i] = move( *m_meshes[i] );
524                delete m_meshes[i];
525                m_meshes[i] = nullptr;
526        }
527        return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
528}
529
530
531nv::md5_loader::~md5_loader()
532{
533        reset();
534}
535
536void nv::md5_loader::reset()
537{
538        if ( m_nodes ) delete m_nodes;
539        for ( auto m : m_meshes ) { if (m) delete m; }
540        m_meshes.resize(0);
541        m_nodes = nullptr;
542}
543
Note: See TracBrowser for help on using the repository browser.