[190] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/formats/md5_loader.hh"
|
---|
| 8 |
|
---|
| 9 | #include <glm/gtc/constants.hpp>
|
---|
| 10 | #include "nv/logging.hh"
|
---|
| 11 | #include "nv/io/std_stream.hh"
|
---|
| 12 | #include <cstring>
|
---|
| 13 |
|
---|
| 14 | using namespace nv;
|
---|
| 15 |
|
---|
[191] | 16 | // based on http://tfc.duke.free.fr/coding/md5-specs-en.html
|
---|
| 17 |
|
---|
[190] | 18 | static void next_line( std::istream& stream )
|
---|
| 19 | {
|
---|
| 20 | stream.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | static inline void discard( std::istream& stream, const std::string& token )
|
---|
| 24 | {
|
---|
[226] | 25 | // stream.ignore( std::numeric_limits<std::streamsize>::max(), ' ' );
|
---|
[190] | 26 | std::string discarded;
|
---|
| 27 | stream >> discarded;
|
---|
| 28 | assert( discarded == token );
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | static void remove_quotes( std::string& str )
|
---|
| 33 | {
|
---|
| 34 | size_t n;
|
---|
| 35 | while ( ( n = str.find('\"') ) != std::string::npos ) str.erase(n,1);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[198] | 38 | static void unit_quat_w( glm::quat& quat )
|
---|
[190] | 39 | {
|
---|
| 40 | float t = 1.0f - ( quat.x * quat.x ) - ( quat.y * quat.y ) - ( quat.z * quat.z );
|
---|
| 41 | quat.w = ( t < 0.0f ? 0.0f : -sqrtf(t) );
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | bool md5_loader::load( stream& source )
|
---|
| 45 | {
|
---|
| 46 | std_stream sstream( &source );
|
---|
| 47 | std::string command;
|
---|
| 48 |
|
---|
| 49 | sstream >> command;
|
---|
| 50 | while ( !sstream.eof() )
|
---|
| 51 | {
|
---|
| 52 | if ( command == "MD5Version" )
|
---|
| 53 | {
|
---|
| 54 | sstream >> m_md5_version;
|
---|
| 55 | assert( m_md5_version == 10 );
|
---|
| 56 | }
|
---|
| 57 | else if ( command == "commandline" )
|
---|
| 58 | {
|
---|
| 59 | next_line( sstream );
|
---|
| 60 | }
|
---|
| 61 | else if ( command == "numJoints" )
|
---|
| 62 | {
|
---|
| 63 | sstream >> m_num_joints;
|
---|
| 64 | m_joints.reserve( m_num_joints );
|
---|
| 65 | }
|
---|
| 66 | else if ( command == "numMeshes" )
|
---|
| 67 | {
|
---|
| 68 | sstream >> m_num_meshes;
|
---|
| 69 | m_meshes.reserve( m_num_meshes );
|
---|
| 70 | }
|
---|
| 71 | else if ( command == "joints" )
|
---|
| 72 | {
|
---|
| 73 | discard( sstream, "{" );
|
---|
| 74 | md5_joint joint;
|
---|
[191] | 75 | for ( size_t i = 0; i < m_num_joints; ++i )
|
---|
[190] | 76 | {
|
---|
| 77 | sstream >> joint.name >> joint.parent_id;
|
---|
| 78 | discard( sstream, "(" );
|
---|
| 79 | sstream >> joint.pos.x >> joint.pos.y >> joint.pos.z;
|
---|
| 80 | discard( sstream, ")" );
|
---|
| 81 | discard( sstream, "(" );
|
---|
| 82 | sstream >> joint.orient.x >> joint.orient.y >> joint.orient.z;
|
---|
| 83 | remove_quotes( joint.name );
|
---|
| 84 | unit_quat_w( joint.orient );
|
---|
| 85 | m_joints.push_back( joint );
|
---|
| 86 | next_line( sstream );
|
---|
| 87 | }
|
---|
| 88 | discard( sstream, "}" );
|
---|
| 89 | }
|
---|
| 90 | else if ( command == "mesh" )
|
---|
| 91 | {
|
---|
[239] | 92 | md5_mesh_data* mesh = new md5_mesh_data();
|
---|
| 93 |
|
---|
[190] | 94 | int num_verts, num_tris, num_weights;
|
---|
| 95 |
|
---|
| 96 | discard( sstream, "{" );
|
---|
| 97 | sstream >> command;
|
---|
| 98 | while ( command != "}" )
|
---|
| 99 | {
|
---|
| 100 | if ( command == "shader" )
|
---|
| 101 | {
|
---|
[239] | 102 | sstream >> mesh->m_shader;
|
---|
| 103 | remove_quotes( mesh->m_shader );
|
---|
[190] | 104 | // texturePath.replace_extension( ".tga" );
|
---|
| 105 | next_line( sstream );
|
---|
| 106 | }
|
---|
| 107 | else if ( command == "numverts")
|
---|
| 108 | {
|
---|
| 109 | sstream >> num_verts;
|
---|
[239] | 110 |
|
---|
| 111 | {
|
---|
| 112 | mesh_raw_channel* ch_pnt = mesh_raw_channel::create<md5_vtx_pnt>( num_verts );
|
---|
| 113 | mesh_raw_channel* ch_t = mesh_raw_channel::create<md5_vtx_t>( num_verts );
|
---|
| 114 | mesh->m_pntdata = (md5_vtx_pnt*)ch_pnt->data;
|
---|
| 115 | mesh->m_tdata = (md5_vtx_t*)ch_t->data;
|
---|
| 116 | mesh->add_channel( ch_pnt );
|
---|
| 117 | mesh->add_channel( ch_t );
|
---|
| 118 | }
|
---|
| 119 | mesh->m_vtx_data.resize( num_verts );
|
---|
| 120 |
|
---|
[190] | 121 | next_line( sstream );
|
---|
[226] | 122 | std::string line;
|
---|
[190] | 123 | for ( int i = 0; i < num_verts; ++i )
|
---|
| 124 | {
|
---|
[239] | 125 | md5_vtx_data& vdata = mesh->m_vtx_data[i];
|
---|
| 126 | size_t weight_count;
|
---|
| 127 | size_t start_weight;
|
---|
| 128 | vec2 texcoord;
|
---|
[190] | 129 |
|
---|
[226] | 130 | std::getline( sstream, line );
|
---|
[239] | 131 | sscanf( line.c_str(), "%*s %*u ( %f %f ) %u %u", &(texcoord.x), &(texcoord.y), &(start_weight), &(weight_count) );
|
---|
| 132 | vdata.start_weight = start_weight;
|
---|
| 133 | vdata.weight_count = weight_count;
|
---|
| 134 | mesh->m_tdata[i].texcoord = texcoord;
|
---|
[190] | 135 | }
|
---|
| 136 | }
|
---|
| 137 | else if ( command == "numtris" )
|
---|
| 138 | {
|
---|
| 139 | sstream >> num_tris;
|
---|
[239] | 140 |
|
---|
| 141 | mesh_raw_index_channel* ch_i = mesh_raw_index_channel::create<uint32>( num_tris * 3 );
|
---|
| 142 | uint32* vtx_i = (uint32*)ch_i->data;
|
---|
| 143 | mesh->m_idata = vtx_i;
|
---|
| 144 | uint32 idx = 0;
|
---|
| 145 | mesh->set_index_channel( ch_i );
|
---|
| 146 |
|
---|
[190] | 147 | next_line( sstream );
|
---|
[226] | 148 | std::string line;
|
---|
[190] | 149 | for ( int i = 0; i < num_tris; ++i )
|
---|
| 150 | {
|
---|
[239] | 151 | size_t ti0;
|
---|
| 152 | size_t ti1;
|
---|
| 153 | size_t ti2;
|
---|
[190] | 154 |
|
---|
[226] | 155 | std::getline( sstream, line );
|
---|
[239] | 156 | sscanf( line.c_str(), "%*s %*u %u %u %u )", &(ti0), &(ti1), &(ti2));
|
---|
[226] | 157 |
|
---|
[239] | 158 | vtx_i[idx++] = (uint32)ti0;
|
---|
| 159 | vtx_i[idx++] = (uint32)ti1;
|
---|
| 160 | vtx_i[idx++] = (uint32)ti2;
|
---|
[190] | 161 | }
|
---|
| 162 | }
|
---|
| 163 | else if ( command == "numweights" )
|
---|
| 164 | {
|
---|
| 165 | sstream >> num_weights;
|
---|
[239] | 166 | mesh->m_weights.reserve( num_weights );
|
---|
[190] | 167 | next_line( sstream );
|
---|
[226] | 168 | std::string line;
|
---|
[190] | 169 | for ( int i = 0; i < num_weights; ++i )
|
---|
| 170 | {
|
---|
| 171 | md5_weight weight;
|
---|
[226] | 172 |
|
---|
| 173 | std::getline( sstream, line );
|
---|
| 174 | 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));
|
---|
[239] | 175 | mesh->m_weights.push_back(weight);
|
---|
[190] | 176 | }
|
---|
| 177 | }
|
---|
| 178 | else
|
---|
| 179 | {
|
---|
| 180 | next_line( sstream );
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | sstream >> command;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | prepare_mesh( mesh );
|
---|
| 187 |
|
---|
| 188 | m_meshes.push_back(mesh);
|
---|
| 189 | }
|
---|
| 190 | sstream >> command;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | assert( m_joints.size() == m_num_joints );
|
---|
| 194 | assert( m_meshes.size() == m_num_meshes );
|
---|
| 195 | return true;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[239] | 198 | bool md5_loader::prepare_mesh( md5_mesh_data* mdata )
|
---|
[190] | 199 | {
|
---|
[239] | 200 | uint32 vtx_count = mdata->m_vtx_data.size();
|
---|
| 201 | md5_vtx_pnt* vtcs = mdata->m_pntdata;
|
---|
[190] | 202 |
|
---|
[239] | 203 | for ( uint32 i = 0; i < vtx_count; ++i )
|
---|
[190] | 204 | {
|
---|
[239] | 205 | md5_vtx_data& vdata = mdata->m_vtx_data[i];
|
---|
| 206 | md5_vtx_pnt& vtc = vtcs[i];
|
---|
[190] | 207 |
|
---|
[239] | 208 | vtc.position = glm::vec3(0);
|
---|
| 209 | vtc.normal = glm::vec3(0);
|
---|
| 210 | vtc.tangent = glm::vec3(0);
|
---|
[190] | 211 |
|
---|
[239] | 212 | for ( size_t j = 0; j < vdata.weight_count; ++j )
|
---|
[190] | 213 | {
|
---|
[239] | 214 | md5_weight& weight = mdata->m_weights[vdata.start_weight + j];
|
---|
[190] | 215 | md5_joint& joint = m_joints[weight.joint_id];
|
---|
| 216 |
|
---|
| 217 | glm::vec3 rot_pos = joint.orient * weight.pos;
|
---|
| 218 |
|
---|
[239] | 219 | vtc.position += ( joint.pos + rot_pos ) * weight.bias;
|
---|
[190] | 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[239] | 223 | // Prepare normals
|
---|
| 224 | uint32 tri_count = mdata->get_count() / 3;
|
---|
| 225 | for ( unsigned int i = 0; i < tri_count; ++i )
|
---|
[190] | 226 | {
|
---|
[239] | 227 | uint32 ti0 = mdata->m_idata[ i * 3 ];
|
---|
| 228 | uint32 ti1 = mdata->m_idata[ i * 3 + 1 ];
|
---|
| 229 | uint32 ti2 = mdata->m_idata[ i * 3 + 2 ];
|
---|
| 230 |
|
---|
| 231 | glm::vec3 v1 = vtcs[ ti0 ].position;
|
---|
| 232 | glm::vec3 v2 = vtcs[ ti1 ].position;
|
---|
| 233 | glm::vec3 v3 = vtcs[ ti2 ].position;
|
---|
[190] | 234 | glm::vec3 xyz1 = v3 - v1;
|
---|
| 235 | glm::vec3 xyz2 = v2 - v1;
|
---|
| 236 |
|
---|
| 237 | glm::vec3 normal = glm::cross( xyz1, xyz2 );
|
---|
| 238 |
|
---|
[239] | 239 | vtcs[ ti0 ].normal += normal;
|
---|
| 240 | vtcs[ ti1 ].normal += normal;
|
---|
| 241 | vtcs[ ti2 ].normal += normal;
|
---|
[190] | 242 |
|
---|
[239] | 243 | const vec2& w1 = mdata->m_tdata[ ti0 ].texcoord;
|
---|
| 244 | const vec2& w2 = mdata->m_tdata[ ti1 ].texcoord;
|
---|
| 245 | const vec2& w3 = mdata->m_tdata[ ti2 ].texcoord;
|
---|
[190] | 246 |
|
---|
| 247 | vec2 st1 = w3 - w1;
|
---|
| 248 | vec2 st2 = w2 - w1;
|
---|
| 249 |
|
---|
| 250 | float coef = 1.0f / (st1.x * st2.y - st2.x * st1.y);
|
---|
| 251 |
|
---|
| 252 | vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
|
---|
| 253 |
|
---|
[239] | 254 | vtcs[ ti0 ].tangent += tangent;
|
---|
| 255 | vtcs[ ti1 ].tangent += tangent;
|
---|
| 256 | vtcs[ ti2 ].tangent += tangent;
|
---|
[190] | 257 | }
|
---|
| 258 |
|
---|
[239] | 259 | for ( size_t i = 0; i < vtx_count; ++i )
|
---|
[190] | 260 | {
|
---|
[239] | 261 | md5_vtx_data& vdata = mdata->m_vtx_data[i];
|
---|
[190] | 262 |
|
---|
[239] | 263 | glm::vec3 normal = glm::normalize( vtcs[i].normal );
|
---|
| 264 | glm::vec3 tangent = glm::normalize( vtcs[i].tangent );
|
---|
| 265 | vtcs[i].normal = normal;
|
---|
| 266 | vtcs[i].tangent = tangent;
|
---|
[190] | 267 |
|
---|
[239] | 268 | vdata.normal = glm::vec3(0);
|
---|
| 269 | vdata.tangent = glm::vec3(0);
|
---|
| 270 |
|
---|
| 271 | for ( size_t j = 0; j < vdata.weight_count; ++j )
|
---|
| 272 | {
|
---|
| 273 | const md5_weight& weight = mdata->m_weights[vdata.start_weight + j];
|
---|
| 274 | const md5_joint& joint = m_joints[weight.joint_id];
|
---|
| 275 | vdata.normal += ( normal * joint.orient ) * weight.bias;
|
---|
| 276 | vdata.tangent += ( tangent * joint.orient ) * weight.bias;
|
---|
| 277 | }
|
---|
[190] | 278 | }
|
---|
| 279 |
|
---|
| 280 | return true;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[224] | 283 |
|
---|
[191] | 284 | md5_animation::md5_animation()
|
---|
| 285 | : m_md5_version( 0 )
|
---|
| 286 | , m_num_frames( 0 )
|
---|
| 287 | , m_num_joints( 0 )
|
---|
| 288 | , m_frame_rate( 0 )
|
---|
| 289 | , m_num_animated_components( 0 )
|
---|
| 290 | , m_anim_duration( 0 )
|
---|
| 291 | , m_frame_duration( 0 )
|
---|
| 292 | , m_anim_time( 0 )
|
---|
| 293 | {
|
---|
| 294 |
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | md5_animation::~md5_animation()
|
---|
| 298 | {
|
---|
| 299 |
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | bool md5_animation::load_animation( stream& source )
|
---|
| 303 | {
|
---|
| 304 | m_joint_infos.clear();
|
---|
| 305 | m_bounds.clear();
|
---|
| 306 | m_base_frames.clear();
|
---|
| 307 | m_frames.clear();
|
---|
| 308 | m_animated_skeleton.joints.clear();
|
---|
| 309 | m_num_frames = 0;
|
---|
| 310 |
|
---|
| 311 | std_stream sstream( &source );
|
---|
| 312 | std::string command;
|
---|
| 313 |
|
---|
| 314 | sstream >> command;
|
---|
| 315 | while ( !sstream.eof() )
|
---|
| 316 | {
|
---|
| 317 | if ( command == "MD5Version" )
|
---|
| 318 | {
|
---|
| 319 | sstream >> m_md5_version;
|
---|
| 320 | assert( m_md5_version == 10 );
|
---|
| 321 | }
|
---|
| 322 | else if ( command == "commandline" )
|
---|
| 323 | {
|
---|
| 324 | next_line( sstream );
|
---|
| 325 | }
|
---|
| 326 | else if ( command == "numFrames" )
|
---|
| 327 | {
|
---|
| 328 | sstream >> m_num_frames;
|
---|
| 329 | next_line( sstream );
|
---|
| 330 | }
|
---|
| 331 | else if ( command == "numJoints" )
|
---|
| 332 | {
|
---|
| 333 | sstream >> m_num_joints;
|
---|
| 334 | next_line( sstream );
|
---|
| 335 | }
|
---|
| 336 | else if ( command == "frameRate" )
|
---|
| 337 | {
|
---|
| 338 | sstream >> m_frame_rate;
|
---|
| 339 | next_line( sstream );
|
---|
| 340 | }
|
---|
| 341 | else if ( command == "numAnimatedComponents" )
|
---|
| 342 | {
|
---|
| 343 | sstream >> m_num_animated_components;
|
---|
| 344 | next_line( sstream );
|
---|
| 345 | }
|
---|
| 346 | else if ( command == "hierarchy" )
|
---|
| 347 | {
|
---|
| 348 | discard( sstream, "{" );
|
---|
[198] | 349 | for ( size_t i = 0; i < m_num_joints; ++i )
|
---|
[191] | 350 | {
|
---|
| 351 | md5_joint_info joint;
|
---|
| 352 | sstream >> joint.name >> joint.parent_id >> joint.flags >> joint.start_index;
|
---|
| 353 | remove_quotes( joint.name );
|
---|
| 354 | m_joint_infos.push_back( joint );
|
---|
| 355 | next_line( sstream );
|
---|
| 356 | }
|
---|
| 357 | discard( sstream, "}" );
|
---|
| 358 | }
|
---|
| 359 | else if ( command == "bounds" )
|
---|
| 360 | {
|
---|
| 361 | discard( sstream, "{" );
|
---|
| 362 | next_line( sstream );
|
---|
[198] | 363 | for ( size_t i = 0; i < m_num_frames; ++i )
|
---|
[191] | 364 | {
|
---|
| 365 | md5_bound bound;
|
---|
| 366 | discard( sstream, "(" );
|
---|
| 367 | sstream >> bound.min.x >> bound.min.y >> bound.min.z;
|
---|
| 368 | discard( sstream, ")" );
|
---|
| 369 | discard( sstream, "(" );
|
---|
| 370 | sstream >> bound.max.x >> bound.max.y >> bound.max.z;
|
---|
| 371 |
|
---|
| 372 | m_bounds.push_back( bound );
|
---|
| 373 |
|
---|
| 374 | next_line( sstream );
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | discard( sstream, "}" );
|
---|
| 378 | next_line( sstream );
|
---|
| 379 | }
|
---|
| 380 | else if ( command == "baseframe" )
|
---|
| 381 | {
|
---|
| 382 | discard( sstream, "{" );
|
---|
| 383 | next_line( sstream );
|
---|
| 384 |
|
---|
[198] | 385 | for ( size_t i = 0; i < m_num_joints; ++i )
|
---|
[191] | 386 | {
|
---|
| 387 | md5_base_frame base_frame;
|
---|
| 388 | discard( sstream, "(" );
|
---|
| 389 | sstream >> base_frame.pos.x >> base_frame.pos.y >> base_frame.pos.z;
|
---|
| 390 | discard( sstream, ")" );
|
---|
| 391 | discard( sstream, "(" );
|
---|
| 392 | sstream >> base_frame.orient.x >> base_frame.orient.y >> base_frame.orient.z;
|
---|
| 393 | next_line( sstream );
|
---|
| 394 |
|
---|
| 395 | m_base_frames.push_back( base_frame );
|
---|
| 396 | }
|
---|
| 397 | discard( sstream, "}" );
|
---|
| 398 | next_line( sstream );
|
---|
| 399 | }
|
---|
| 400 | else if ( command == "frame" )
|
---|
| 401 | {
|
---|
| 402 | md5_frame_data frame;
|
---|
| 403 | sstream >> frame.frame_id;
|
---|
| 404 | discard( sstream, "{" );
|
---|
| 405 | next_line( sstream );
|
---|
| 406 |
|
---|
[226] | 407 | frame.frame_data.reserve( m_num_animated_components );
|
---|
| 408 | char buf[50];
|
---|
[198] | 409 | for ( size_t i = 0; i < m_num_animated_components; ++i )
|
---|
[191] | 410 | {
|
---|
[226] | 411 | sstream >> buf;
|
---|
| 412 | frame.frame_data.push_back((float)atof(buf));
|
---|
[191] | 413 | }
|
---|
| 414 |
|
---|
| 415 | m_frames.push_back(frame);
|
---|
| 416 |
|
---|
| 417 | build_frame_skeleton( m_skeletons, m_joint_infos, m_base_frames, frame );
|
---|
| 418 |
|
---|
| 419 | discard( sstream, "}" );
|
---|
| 420 | next_line( sstream );
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | sstream >> command;
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | m_animated_skeleton.joints.assign( m_num_joints, md5_skeleton_joint() );
|
---|
| 427 |
|
---|
| 428 | m_frame_duration = 1.0f / (float)m_frame_rate;
|
---|
| 429 | m_anim_duration = ( m_frame_duration * (float)m_num_frames );
|
---|
| 430 | m_anim_time = 0.0f;
|
---|
| 431 |
|
---|
[198] | 432 | assert( m_joint_infos.size() == m_num_joints );
|
---|
| 433 | assert( m_bounds.size() == m_num_frames );
|
---|
| 434 | assert( m_base_frames.size() == m_num_joints );
|
---|
| 435 | assert( m_frames.size() == m_num_frames );
|
---|
| 436 | assert( m_skeletons.size() == m_num_frames );
|
---|
[191] | 437 |
|
---|
| 438 | return true;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | void md5_animation::update( float delta_time )
|
---|
| 442 | {
|
---|
| 443 | if ( m_num_frames < 1 ) return;
|
---|
| 444 |
|
---|
| 445 | m_anim_time += delta_time;
|
---|
| 446 |
|
---|
| 447 | while ( m_anim_time > m_anim_duration ) m_anim_time -= m_anim_duration;
|
---|
| 448 | while ( m_anim_time < 0.0f ) m_anim_time += m_anim_duration;
|
---|
| 449 |
|
---|
| 450 | float frame_num = m_anim_time * (float)m_frame_rate;
|
---|
[200] | 451 | size_t frame0 = (size_t)floorf( frame_num );
|
---|
| 452 | size_t frame1 = (size_t)ceilf( frame_num );
|
---|
[191] | 453 | frame0 = frame0 % m_num_frames;
|
---|
| 454 | frame1 = frame1 % m_num_frames;
|
---|
| 455 |
|
---|
| 456 | float interpolate = fmodf( m_anim_time, m_frame_duration ) / m_frame_duration;
|
---|
| 457 |
|
---|
| 458 | interpolate_skeletons( m_animated_skeleton, m_skeletons[frame0], m_skeletons[frame1], interpolate );
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | void md5_animation::build_frame_skeleton( md5_frame_skeleton_list& skeletons, const md5_joint_info_list& joint_infos, const md5_base_frame_list& base_frames, const md5_frame_data& frame_data )
|
---|
| 462 | {
|
---|
| 463 | md5_frame_skeleton skeleton;
|
---|
| 464 |
|
---|
| 465 | for ( unsigned int i = 0; i < joint_infos.size(); ++i )
|
---|
| 466 | {
|
---|
| 467 | unsigned int j = 0;
|
---|
| 468 |
|
---|
| 469 | const md5_joint_info& jinfo = joint_infos[i];
|
---|
| 470 | md5_skeleton_joint animated_joint = base_frames[i];
|
---|
| 471 |
|
---|
| 472 | animated_joint.parent = jinfo.parent_id;
|
---|
| 473 |
|
---|
| 474 | if ( jinfo.flags & 1 ) animated_joint.pos.x = frame_data.frame_data[ jinfo.start_index + j++ ];
|
---|
| 475 | if ( jinfo.flags & 2 ) animated_joint.pos.y = frame_data.frame_data[ jinfo.start_index + j++ ];
|
---|
| 476 | if ( jinfo.flags & 4 ) animated_joint.pos.z = frame_data.frame_data[ jinfo.start_index + j++ ];
|
---|
| 477 | if ( jinfo.flags & 8 ) animated_joint.orient.x = frame_data.frame_data[ jinfo.start_index + j++ ];
|
---|
| 478 | if ( jinfo.flags & 16 ) animated_joint.orient.y = frame_data.frame_data[ jinfo.start_index + j++ ];
|
---|
| 479 | if ( jinfo.flags & 32 ) animated_joint.orient.z = frame_data.frame_data[ jinfo.start_index + j++ ];
|
---|
| 480 |
|
---|
| 481 | unit_quat_w( animated_joint.orient );
|
---|
| 482 |
|
---|
| 483 | if ( animated_joint.parent >= 0 ) // Has a parent joint
|
---|
| 484 | {
|
---|
[200] | 485 | md5_skeleton_joint& pjoint = skeleton.joints[static_cast< size_t >( animated_joint.parent ) ];
|
---|
[191] | 486 | glm::vec3 rot_pos = pjoint.orient * animated_joint.pos;
|
---|
| 487 |
|
---|
| 488 | animated_joint.pos = pjoint.pos + rot_pos;
|
---|
| 489 | animated_joint.orient = pjoint.orient * animated_joint.orient;
|
---|
| 490 |
|
---|
| 491 | animated_joint.orient = glm::normalize( animated_joint.orient );
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | skeleton.joints.push_back( animated_joint );
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | skeletons.push_back( skeleton );
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | void md5_animation::interpolate_skeletons( md5_frame_skeleton& final_skeleton, const md5_frame_skeleton& skeleton0, const md5_frame_skeleton& skeleton1, float interpolate )
|
---|
| 501 | {
|
---|
[198] | 502 | for ( size_t i = 0; i < m_num_joints; ++i )
|
---|
[191] | 503 | {
|
---|
| 504 | md5_skeleton_joint& final_joint = final_skeleton.joints[i];
|
---|
| 505 | const md5_skeleton_joint& joint0 = skeleton0.joints[i];
|
---|
| 506 | const md5_skeleton_joint& joint1 = skeleton1.joints[i];
|
---|
| 507 |
|
---|
| 508 | final_joint.parent = joint0.parent;
|
---|
| 509 |
|
---|
[192] | 510 | final_joint.orient = glm::slerp( joint0.orient, joint1.orient, interpolate );
|
---|
[191] | 511 | final_joint.pos = glm::mix( joint0.pos, joint1.pos, interpolate );
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | bool md5_loader::check_animation( const md5_animation& animation ) const
|
---|
| 516 | {
|
---|
| 517 | if ( m_num_joints != animation.get_num_joints() )
|
---|
| 518 | {
|
---|
| 519 | return false;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | for ( uint32 i = 0; i < m_joints.size(); ++i )
|
---|
| 523 | {
|
---|
| 524 | const md5_joint& mjoint = m_joints[i];
|
---|
| 525 | const md5_animation::md5_joint_info& ajoint = animation.get_joint_info( i );
|
---|
| 526 |
|
---|
| 527 | if ( mjoint.name != ajoint.name || mjoint.parent_id != ajoint.parent_id )
|
---|
| 528 | {
|
---|
| 529 | return false;
|
---|
| 530 | }
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | return true;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[239] | 536 | mesh_data* nv::md5_loader::release_mesh_data( uint32 mesh )
|
---|
[191] | 537 | {
|
---|
[239] | 538 | mesh_data* result = m_meshes[ mesh ];
|
---|
| 539 | m_meshes[ mesh ] = nullptr;
|
---|
| 540 | return result;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | void nv::md5_mesh_data::apply( const md5_animation& animation )
|
---|
| 544 | {
|
---|
| 545 | const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton();
|
---|
| 546 |
|
---|
| 547 | for ( unsigned int i = 0; i < m_vtx_data.size(); ++i )
|
---|
[191] | 548 | {
|
---|
[239] | 549 | const md5_vtx_data& vert = m_vtx_data[i];
|
---|
| 550 | md5_vtx_pnt& result = m_pntdata[i];
|
---|
[191] | 551 |
|
---|
[239] | 552 | result.position = glm::vec3(0);
|
---|
| 553 | result.normal = glm::vec3(0);
|
---|
| 554 | result.tangent = glm::vec3(0);
|
---|
[191] | 555 |
|
---|
[200] | 556 | for ( size_t j = 0; j < vert.weight_count; ++j )
|
---|
[191] | 557 | {
|
---|
[239] | 558 | const md5_weight& weight = m_weights[vert.start_weight + j];
|
---|
| 559 | const md5_animation::md5_skeleton_joint& joint = skeleton.joints[weight.joint_id];
|
---|
[191] | 560 |
|
---|
| 561 | glm::vec3 rot_pos = joint.orient * weight.pos;
|
---|
[239] | 562 | result.position += ( joint.pos + rot_pos ) * weight.bias;
|
---|
[191] | 563 |
|
---|
[239] | 564 | result.normal += ( joint.orient * vert.normal ) * weight.bias;
|
---|
| 565 | result.tangent += ( joint.orient * vert.tangent ) * weight.bias;
|
---|
[191] | 566 | }
|
---|
| 567 | }
|
---|
| 568 | }
|
---|
| 569 |
|
---|
[239] | 570 | nv::md5_loader::~md5_loader()
|
---|
[191] | 571 | {
|
---|
[239] | 572 | for ( auto m : m_meshes ) { if (m) delete m; }
|
---|
[191] | 573 | }
|
---|