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