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