[395] | 1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
[248] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[248] | 6 |
|
---|
| 7 | #include "nv/formats/assimp_loader.hh"
|
---|
[392] | 8 | #include "nv/stl/unordered_map.hh"
|
---|
[248] | 9 | #include "nv/lib/assimp.hh"
|
---|
| 10 |
|
---|
| 11 | using namespace nv;
|
---|
| 12 |
|
---|
[323] | 13 | const unsigned MAX_BONES = 64;
|
---|
[250] | 14 |
|
---|
[293] | 15 | struct assimp_plain_vtx
|
---|
| 16 | {
|
---|
| 17 | vec3 position;
|
---|
| 18 | vec3 normal;
|
---|
| 19 | vec2 texcoord;
|
---|
| 20 | vec4 tangent;
|
---|
| 21 |
|
---|
| 22 | assimp_plain_vtx() {}
|
---|
| 23 | assimp_plain_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )
|
---|
| 24 | {
|
---|
| 25 | position = v;
|
---|
| 26 | texcoord = t;
|
---|
| 27 | normal = n;
|
---|
| 28 | tangent = g;
|
---|
| 29 | }
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | struct assimp_skinned_vtx
|
---|
| 33 | {
|
---|
| 34 | vec3 position;
|
---|
| 35 | vec3 normal;
|
---|
| 36 | vec2 texcoord;
|
---|
| 37 | vec4 tangent;
|
---|
| 38 | ivec4 boneindex;
|
---|
| 39 | vec4 boneweight;
|
---|
| 40 |
|
---|
| 41 | assimp_skinned_vtx() {}
|
---|
| 42 | assimp_skinned_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )
|
---|
| 43 | {
|
---|
| 44 | position = v;
|
---|
| 45 | texcoord = t;
|
---|
| 46 | normal = n;
|
---|
| 47 | tangent = g;
|
---|
| 48 | }
|
---|
| 49 | };
|
---|
| 50 |
|
---|
[410] | 51 | struct assimp_key_p { float time; vec3 translation; };
|
---|
[291] | 52 | struct assimp_key_r { float time; quat rotation; };
|
---|
| 53 | struct assimp_key_s { float time; vec3 scale; };
|
---|
| 54 | struct assimp_key_tr { transform tform; };
|
---|
| 55 |
|
---|
| 56 |
|
---|
[380] | 57 | nv::assimp_loader::assimp_loader( const std::string& a_ext, uint32 a_assimp_flags /*= 0 */ )
|
---|
[293] | 58 | : m_scene( nullptr ), m_mesh_count(0)
|
---|
[250] | 59 | {
|
---|
[284] | 60 | m_ext = a_ext;
|
---|
| 61 | m_assimp_flags = a_assimp_flags;
|
---|
[277] | 62 | if ( m_assimp_flags == 0 )
|
---|
| 63 | {
|
---|
| 64 | m_assimp_flags = (
|
---|
| 65 | aiProcess_CalcTangentSpace |
|
---|
| 66 | aiProcess_GenSmoothNormals |
|
---|
| 67 | aiProcess_JoinIdenticalVertices |
|
---|
| 68 | aiProcess_ImproveCacheLocality |
|
---|
| 69 | aiProcess_LimitBoneWeights |
|
---|
| 70 | aiProcess_RemoveRedundantMaterials |
|
---|
| 71 | aiProcess_SplitLargeMeshes |
|
---|
| 72 | aiProcess_Triangulate |
|
---|
| 73 | aiProcess_GenUVCoords |
|
---|
| 74 | aiProcess_SortByPType |
|
---|
| 75 | aiProcess_FindDegenerates |
|
---|
| 76 | aiProcess_FindInvalidData |
|
---|
| 77 | 0 );
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[284] | 80 |
|
---|
| 81 |
|
---|
[248] | 82 | bool nv::assimp_loader::load( stream& source )
|
---|
| 83 | {
|
---|
[292] | 84 | load_assimp_library();
|
---|
[406] | 85 | if ( m_scene != nullptr ) aiReleaseImport( reinterpret_cast<const aiScene*>( m_scene ) );
|
---|
[248] | 86 | m_scene = nullptr;
|
---|
| 87 | m_mesh_count = 0;
|
---|
[365] | 88 | NV_LOG_NOTICE( "AssImp loading file..." );
|
---|
[323] | 89 | size_t size = source.size();
|
---|
[248] | 90 | char* data = new char[ size ];
|
---|
| 91 | source.read( data, size, 1 );
|
---|
| 92 | const aiScene* scene = aiImportFileFromMemory( data, size, m_assimp_flags, m_ext.c_str() );
|
---|
| 93 |
|
---|
| 94 | if( !scene)
|
---|
| 95 | {
|
---|
[365] | 96 | NV_LOG_ERROR( aiGetErrorString() );
|
---|
[248] | 97 | return false;
|
---|
| 98 | }
|
---|
| 99 | m_scene = scene;
|
---|
| 100 | m_mesh_count = scene->mNumMeshes;
|
---|
[365] | 101 | NV_LOG_NOTICE( "Loading successfull" );
|
---|
[248] | 102 | return true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | mesh_data* nv::assimp_loader::release_mesh_data( size_t index /*= 0 */ )
|
---|
| 106 | {
|
---|
| 107 | if ( index >= m_mesh_count ) return nullptr;
|
---|
[287] | 108 | mesh_data* result = new mesh_data;
|
---|
| 109 | load_mesh_data( result, index );
|
---|
| 110 | return result;
|
---|
| 111 | }
|
---|
| 112 | void nv::assimp_loader::load_mesh_data( mesh_data* data, size_t index )
|
---|
| 113 | {
|
---|
[406] | 114 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[248] | 115 | const aiMesh* mesh = scene->mMeshes[ index ];
|
---|
[287] | 116 | data->set_name( mesh->mName.data );
|
---|
[248] | 117 |
|
---|
| 118 | bool skinned = mesh->mNumBones > 0;
|
---|
[415] | 119 |
|
---|
| 120 | data_descriptor desc;
|
---|
[248] | 121 | if ( skinned )
|
---|
[415] | 122 | desc.initialize< assimp_skinned_vtx >();
|
---|
[248] | 123 | else
|
---|
[415] | 124 | desc.initialize< assimp_plain_vtx >();
|
---|
| 125 | raw_data_channel_creator channel( desc, mesh->mNumVertices );
|
---|
| 126 | uint8* cdata = channel.raw_data();
|
---|
[248] | 127 |
|
---|
[332] | 128 | if ( mesh->mTangents && mesh->mBitangents )
|
---|
[415] | 129 | for ( unsigned int i = 0; i < mesh->mNumVertices; i++ )
|
---|
| 130 | {
|
---|
| 131 | vec3 v = assimp_vec3_cast( mesh->mVertices[i] );
|
---|
| 132 | vec3 n = glm::normalize( assimp_vec3_cast( mesh->mNormals[i] ) );
|
---|
| 133 | vec3 t = glm::normalize( assimp_vec3_cast( mesh->mTangents[i] ) );
|
---|
| 134 | vec3 b = glm::normalize( assimp_vec3_cast( mesh->mBitangents[i] ) );
|
---|
| 135 | vec2 s = assimp_st_cast( mesh->mTextureCoords[0][i] );
|
---|
[248] | 136 |
|
---|
[415] | 137 | glm::vec3 t_i = glm::normalize( t - n * glm::dot( n, t ) );
|
---|
| 138 | float det = ( glm::dot( glm::cross( n, t ), b ) );
|
---|
| 139 | det = ( det < 0.0f ? -1.0f : 1.0f );
|
---|
| 140 | nv::vec4 vt( t_i[0], t_i[1], t_i[2], det );
|
---|
| 141 | if ( skinned )
|
---|
| 142 | reinterpret_cast<assimp_skinned_vtx*>( cdata )[i] = assimp_skinned_vtx( v, s, n, vt );
|
---|
| 143 | else
|
---|
| 144 | reinterpret_cast<assimp_plain_vtx*>( cdata )[i] = assimp_plain_vtx( v, s, n, vt );
|
---|
| 145 | }
|
---|
[248] | 146 |
|
---|
| 147 | if ( skinned )
|
---|
| 148 | {
|
---|
[413] | 149 | assimp_skinned_vtx* vtx = reinterpret_cast< assimp_skinned_vtx* >( cdata );
|
---|
[248] | 150 | for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 151 | {
|
---|
| 152 | aiBone* bone = mesh->mBones[m];
|
---|
| 153 | for (unsigned int w=0; w<bone->mNumWeights; w++)
|
---|
| 154 | {
|
---|
| 155 | assimp_skinned_vtx& v = vtx[ bone->mWeights[w].mVertexId ];
|
---|
| 156 | bool found = false;
|
---|
[406] | 157 | for ( int i = 0 ; i < 4; ++i )
|
---|
[248] | 158 | {
|
---|
| 159 | if ( v.boneweight[i] <= 0.0f )
|
---|
| 160 | {
|
---|
[406] | 161 | v.boneindex[i] = int( m );
|
---|
[248] | 162 | v.boneweight[i] = bone->mWeights[w].mWeight;
|
---|
| 163 | found = true;
|
---|
| 164 | break;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | NV_ASSERT( found, "Too many weights!" );
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[413] | 172 | data_channel_creator< index_u16 > ichannel( mesh->mNumFaces * 3 );
|
---|
| 173 | uint16* indices = reinterpret_cast<uint16*>( ichannel.raw_data() );
|
---|
[248] | 174 | for (unsigned int i=0; i<mesh->mNumFaces; i++)
|
---|
| 175 | {
|
---|
| 176 | const aiFace* face = &mesh->mFaces[i];
|
---|
| 177 | for (unsigned int j=0; j<face->mNumIndices; j++)
|
---|
| 178 | {
|
---|
[406] | 179 | indices[ i*3 + j ] = uint16( face->mIndices[j] );
|
---|
[248] | 180 | }
|
---|
| 181 | }
|
---|
[415] | 182 | data->add_channel( channel.release() );
|
---|
[413] | 183 | data->add_channel( ichannel.release() );
|
---|
[248] | 184 | }
|
---|
| 185 |
|
---|
| 186 | nv::assimp_loader::~assimp_loader()
|
---|
| 187 | {
|
---|
[406] | 188 | if ( m_scene != nullptr ) aiReleaseImport( reinterpret_cast<const aiScene*>( m_scene ) );
|
---|
[248] | 189 | }
|
---|
| 190 |
|
---|
[383] | 191 | bool nv::assimp_loader::load_bones( size_t index, array_ref< mesh_node_data > bones )
|
---|
[248] | 192 | {
|
---|
| 193 | if ( m_scene == nullptr ) return false;
|
---|
[406] | 194 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[248] | 195 | const aiMesh* mesh = scene->mMeshes[ index ];
|
---|
| 196 |
|
---|
| 197 | for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 198 | {
|
---|
| 199 | aiBone* bone = mesh->mBones[m];
|
---|
[293] | 200 | mat4 offset = assimp_mat4_cast( bone->mOffsetMatrix );
|
---|
[291] | 201 | bones[m].name = bone->mName.data;
|
---|
| 202 | bones[m].data = nullptr;
|
---|
| 203 | bones[m].parent_id = -1;
|
---|
| 204 | bones[m].target_id = -1;
|
---|
| 205 | bones[m].transform = offset;
|
---|
[248] | 206 | }
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | void nv::assimp_loader::scene_report() const
|
---|
| 211 | {
|
---|
[406] | 212 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[248] | 213 | if ( scene == nullptr ) return;
|
---|
| 214 |
|
---|
[365] | 215 | NV_LOG_NOTICE( "------------------------" );
|
---|
| 216 | NV_LOG_NOTICE( "Texture count - ", scene->mNumTextures );
|
---|
| 217 | NV_LOG_NOTICE( "Animation count - ", scene->mNumAnimations );
|
---|
| 218 | NV_LOG_NOTICE( "Material count - ", scene->mNumMaterials );
|
---|
| 219 | NV_LOG_NOTICE( "Meshes count - ", scene->mNumMeshes );
|
---|
| 220 | NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 221 |
|
---|
| 222 | aiNode* root = scene->mRootNode;
|
---|
| 223 | if (root)
|
---|
| 224 | {
|
---|
[365] | 225 | NV_LOG_NOTICE( "Root node - ", root->mName.data );
|
---|
| 226 | NV_LOG_NOTICE( " meshes - ", root->mNumMeshes );
|
---|
| 227 | NV_LOG_NOTICE( " children - ", root->mNumChildren );
|
---|
[248] | 228 | }
|
---|
| 229 | else
|
---|
| 230 | {
|
---|
[365] | 231 | NV_LOG_NOTICE( "No root node!" );
|
---|
[248] | 232 | }
|
---|
[365] | 233 | NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 234 |
|
---|
| 235 | if ( scene->mNumMeshes > 0 )
|
---|
| 236 | {
|
---|
| 237 | for ( nv::uint32 mc = 0; mc < scene->mNumMeshes; mc++ )
|
---|
| 238 | {
|
---|
| 239 | aiMesh* mesh = scene->mMeshes[mc];
|
---|
| 240 |
|
---|
[406] | 241 | NV_LOG_NOTICE( "Mesh #", mc, " - ", string_view( static_cast<char*>( mesh->mName.data ) ) );
|
---|
[365] | 242 | NV_LOG_NOTICE( " bones - ", mesh->mNumBones );
|
---|
| 243 | NV_LOG_NOTICE( " uvs - ", mesh->mNumUVComponents[0] );
|
---|
| 244 | NV_LOG_NOTICE( " verts - ", mesh->mNumVertices );
|
---|
| 245 | NV_LOG_NOTICE( " faces - ", mesh->mNumFaces );
|
---|
[248] | 246 |
|
---|
[365] | 247 | // NV_LOG_NOTICE( "Bones:" );
|
---|
[248] | 248 | // for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 249 | // {
|
---|
| 250 | // aiBone* bone = mesh->mBones[m];
|
---|
[365] | 251 | // NV_LOG_NOTICE( bone->mName.C_Str() );
|
---|
[248] | 252 | // }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | else
|
---|
| 256 | {
|
---|
[365] | 257 | NV_LOG_NOTICE( "No meshes!" );
|
---|
[248] | 258 | }
|
---|
[365] | 259 | NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 260 |
|
---|
| 261 |
|
---|
| 262 | // if ( scene->mNumMaterials > 0 )
|
---|
| 263 | // {
|
---|
| 264 | // for (unsigned int m=0; m < scene->mNumMaterials; m++)
|
---|
| 265 | // {
|
---|
| 266 | // int texIndex = 0;
|
---|
| 267 | // aiReturn texFound = aiReturn_SUCCESS;
|
---|
| 268 | // aiString path; // filename
|
---|
| 269 | // while (texFound == aiReturn_SUCCESS)
|
---|
| 270 | // {
|
---|
| 271 | // texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path);
|
---|
[365] | 272 | // NV_LOG_NOTICE( " material - ", path.data );
|
---|
[248] | 273 | // texIndex++;
|
---|
| 274 | // }
|
---|
| 275 | // }
|
---|
| 276 | // }
|
---|
| 277 | // else
|
---|
| 278 | // {
|
---|
[365] | 279 | // NV_LOG_NOTICE( "No materials" );
|
---|
[248] | 280 | // }
|
---|
[365] | 281 | // NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 282 |
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[291] | 285 | mesh_nodes_data* nv::assimp_loader::release_merged_bones( mesh_data* meshes )
|
---|
[248] | 286 | {
|
---|
[406] | 287 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[383] | 288 | vector< mesh_node_data > final_bones;
|
---|
[392] | 289 | unordered_map< std::string, uint16 > names;
|
---|
[291] | 290 | for ( unsigned int m = 0; m < m_mesh_count; ++m )
|
---|
[248] | 291 | {
|
---|
[323] | 292 | uint16 translate[MAX_BONES];
|
---|
[383] | 293 | vector< mesh_node_data > bones;
|
---|
[291] | 294 | const aiMesh* mesh = scene->mMeshes[ m ];
|
---|
| 295 | if ( mesh->mNumBones != 0 )
|
---|
[248] | 296 | {
|
---|
[291] | 297 | bones.resize( mesh->mNumBones );
|
---|
| 298 | load_bones( m, bones );
|
---|
| 299 | for ( unsigned int b = 0; b < mesh->mNumBones; ++b )
|
---|
| 300 | {
|
---|
[248] | 301 |
|
---|
[291] | 302 | mesh_node_data& bone = bones[b];
|
---|
| 303 | auto iname = names.find( bone.name );
|
---|
| 304 | if ( iname == names.end() )
|
---|
| 305 | {
|
---|
| 306 | NV_ASSERT( final_bones.size() < MAX_BONES, "Too many bones to merge!" );
|
---|
[406] | 307 | uint16 index = uint16( final_bones.size() );
|
---|
[291] | 308 | final_bones.push_back( bone );
|
---|
| 309 | names[ bone.name ] = index;
|
---|
| 310 | translate[b] = index;
|
---|
| 311 | }
|
---|
| 312 | else
|
---|
| 313 | {
|
---|
[323] | 314 | translate[b] = iname->second;
|
---|
[291] | 315 | }
|
---|
[248] | 316 | }
|
---|
[291] | 317 | if ( m > 0 && bones.size() > 0 )
|
---|
[248] | 318 | {
|
---|
[413] | 319 | data_channel_creator< assimp_skinned_vtx > channel( meshes[m].get_raw_channels()[0] );
|
---|
| 320 | for ( unsigned v = 0; v < channel.size(); ++v )
|
---|
[291] | 321 | {
|
---|
[413] | 322 | assimp_skinned_vtx& vertex = channel.data()[v];
|
---|
[248] | 323 |
|
---|
[406] | 324 | for ( int i = 0 ; i < 4; ++i)
|
---|
[248] | 325 | {
|
---|
[291] | 326 | if ( vertex.boneweight[i] > 0.0f )
|
---|
| 327 | {
|
---|
[406] | 328 | vertex.boneindex[i] = int( translate[vertex.boneindex[i]] );
|
---|
[291] | 329 | }
|
---|
[248] | 330 | }
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
[291] | 333 | }
|
---|
[248] | 334 | }
|
---|
[291] | 335 | mesh_node_data* bones = new mesh_node_data[ final_bones.size() ];
|
---|
[392] | 336 | raw_copy( final_bones.begin(), final_bones.end(), bones );
|
---|
[291] | 337 | return new mesh_nodes_data( "bones", final_bones.size(), bones );
|
---|
[248] | 338 | }
|
---|
| 339 |
|
---|
[294] | 340 | mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data( size_t index /*= 0*/ )
|
---|
[249] | 341 | {
|
---|
| 342 | if ( m_scene == nullptr ) return nullptr;
|
---|
[406] | 343 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[291] | 344 | if ( scene->mRootNode == nullptr || scene->mAnimations == nullptr || scene->mAnimations[index] == nullptr) return nullptr;
|
---|
[249] | 345 |
|
---|
| 346 | const aiNode* root = scene->mRootNode;
|
---|
[291] | 347 | const aiAnimation* anim = scene->mAnimations[index];
|
---|
[249] | 348 |
|
---|
[291] | 349 | uint32 count = count_nodes( scene->mRootNode );
|
---|
| 350 | mesh_node_data* data = new mesh_node_data[count];
|
---|
[249] | 351 |
|
---|
[406] | 352 | uint16 frame_rate = static_cast<uint16>( anim->mTicksPerSecond );
|
---|
| 353 | float duration = static_cast<float>( anim->mDuration );
|
---|
[293] | 354 | bool flat = false;
|
---|
[287] | 355 |
|
---|
[293] | 356 | load_node( index, data, root, 0, -1 );
|
---|
[291] | 357 |
|
---|
[293] | 358 | return new mesh_nodes_data( anim->mName.data, count, data, frame_rate, duration, flat );
|
---|
[249] | 359 | }
|
---|
| 360 |
|
---|
| 361 | nv::uint32 nv::assimp_loader::count_nodes( const void* node ) const
|
---|
| 362 | {
|
---|
[406] | 363 | const aiNode* ainode = reinterpret_cast< const aiNode* >( node );
|
---|
[249] | 364 | nv::uint32 count = 1;
|
---|
| 365 | for ( unsigned i = 0; i < ainode->mNumChildren; ++i )
|
---|
| 366 | {
|
---|
| 367 | count += count_nodes( ainode->mChildren[i] );
|
---|
| 368 | }
|
---|
| 369 | return count;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[293] | 372 | nv::sint16 nv::assimp_loader::load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id )
|
---|
[249] | 373 | {
|
---|
[406] | 374 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
| 375 | const aiNode* node = reinterpret_cast<const aiNode*>( vnode );
|
---|
[380] | 376 | std::string name( node->mName.data );
|
---|
[291] | 377 | const aiAnimation* anim = scene->mAnimations[anim_id];
|
---|
[249] | 378 | const aiNodeAnim* anode = nullptr;
|
---|
| 379 |
|
---|
| 380 | for ( unsigned i = 0 ; i < anim->mNumChannels ; i++ )
|
---|
| 381 | {
|
---|
| 382 | anode = anim->mChannels[i];
|
---|
| 383 | if ( std::string( anode->mNodeName.data ) == name ) break;
|
---|
| 384 | anode = nullptr;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[291] | 387 | mesh_node_data& a_data = nodes[ this_id ];
|
---|
[249] | 388 |
|
---|
| 389 | a_data.name = name;
|
---|
[291] | 390 | a_data.target_id = -1;
|
---|
[249] | 391 | a_data.parent_id = parent_id;
|
---|
[279] | 392 | // This value is ignored by the create_transformed_keys, but needed by create_direct_keys!
|
---|
| 393 | // TODO: find a common solution!
|
---|
| 394 | // This is bad because create_transformed_keys never uses node-transformations for
|
---|
| 395 | // node's without keys
|
---|
[249] | 396 | a_data.transform = nv::assimp_mat4_cast( node->mTransformation );
|
---|
[284] | 397 | if (this_id == 0)
|
---|
| 398 | a_data.transform = mat4();
|
---|
[285] | 399 | a_data.data = nullptr;
|
---|
[249] | 400 |
|
---|
[293] | 401 | if (anode) create_keys( &a_data, anode );
|
---|
[249] | 402 |
|
---|
[291] | 403 | nv::sint16 next = this_id + 1;
|
---|
[249] | 404 | for ( unsigned i = 0; i < node->mNumChildren; ++i )
|
---|
| 405 | {
|
---|
[293] | 406 | next = load_node( anim_id, nodes, node->mChildren[i], next, this_id );
|
---|
[249] | 407 | }
|
---|
[287] | 408 |
|
---|
[249] | 409 | return next;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[293] | 412 | void nv::assimp_loader::create_keys( mesh_node_data* data, const void* vnode )
|
---|
[249] | 413 | {
|
---|
[406] | 414 | const aiNodeAnim* node = reinterpret_cast< const aiNodeAnim* >( vnode );
|
---|
[282] | 415 | if ( node->mNumPositionKeys == 0 && node->mNumRotationKeys == 0 && node->mNumScalingKeys == 0 )
|
---|
| 416 | {
|
---|
| 417 | return;
|
---|
| 418 | }
|
---|
[249] | 419 |
|
---|
[285] | 420 | data->data = new key_data;
|
---|
[413] | 421 | data_channel_creator< assimp_key_p > pchannel_creator( node->mNumPositionKeys );
|
---|
| 422 | data_channel_creator< assimp_key_r > rchannel_creator( node->mNumRotationKeys );
|
---|
| 423 | // data_channel_creator< assimp_key_s > schannel_creator( node->mNumScalingKeys );
|
---|
| 424 |
|
---|
| 425 | assimp_key_p* pchannel = pchannel_creator.data();
|
---|
| 426 | assimp_key_r* rchannel = rchannel_creator.data();
|
---|
[287] | 427 | //assimp_key_s* schannel = ((assimp_key_s*)(raw_schannel->data));
|
---|
[282] | 428 |
|
---|
[249] | 429 | for ( unsigned np = 0; np < node->mNumPositionKeys; ++np )
|
---|
| 430 | {
|
---|
[410] | 431 | pchannel[np].time = static_cast<float>( node->mPositionKeys[np].mTime );
|
---|
| 432 | pchannel[np].translation = assimp_vec3_cast(node->mPositionKeys[np].mValue);
|
---|
[249] | 433 | }
|
---|
| 434 | for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
|
---|
| 435 | {
|
---|
[406] | 436 | rchannel[np].time = static_cast<float>( node->mRotationKeys[np].mTime );
|
---|
[293] | 437 | rchannel[np].rotation = assimp_quat_cast(node->mRotationKeys[np].mValue );
|
---|
[249] | 438 | }
|
---|
[287] | 439 | // if ( node->mNumScalingKeys > 0 )
|
---|
| 440 | // {
|
---|
| 441 | // nv::vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue );
|
---|
| 442 | // float scale_value = glm::length( glm::abs( scale_vec0 - nv::vec3(1,1,1) ) );
|
---|
| 443 | // if ( node->mNumScalingKeys > 1 || scale_value > 0.001 )
|
---|
| 444 | // {
|
---|
| 445 | // NV_LOG( nv::LOG_WARNING, "scale key significant!" );
|
---|
| 446 | // for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
|
---|
| 447 | // {
|
---|
| 448 | // schannel[np].time = (float)node->mScalingKeys[np].mTime;
|
---|
| 449 | // schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue);
|
---|
| 450 | // }
|
---|
| 451 | // }
|
---|
| 452 | // else
|
---|
| 453 | // {
|
---|
| 454 | // schannel[0].time = (float)node->mScalingKeys[0].mTime;
|
---|
| 455 | // schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue);
|
---|
| 456 | // }
|
---|
| 457 | // }
|
---|
[413] | 458 | data->data->add_channel( pchannel_creator.release() );
|
---|
| 459 | data->data->add_channel( rchannel_creator.release() );
|
---|
| 460 | // data->data->add_channel( schannel_creator.release() );
|
---|
[287] | 461 | }
|
---|
| 462 |
|
---|
| 463 | mesh_data_pack* nv::assimp_loader::release_mesh_data_pack()
|
---|
| 464 | {
|
---|
[291] | 465 | if ( m_scene == nullptr || m_mesh_count == 0 ) return nullptr;
|
---|
[406] | 466 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[291] | 467 | bool has_bones = false;
|
---|
[287] | 468 | mesh_data* meshes = new mesh_data[ m_mesh_count ];
|
---|
[291] | 469 | for ( size_t m = 0; m < m_mesh_count; ++m )
|
---|
[249] | 470 | {
|
---|
[291] | 471 | const aiMesh* mesh = scene->mMeshes[ m ];
|
---|
| 472 | meshes[m].set_name( mesh->mName.data );
|
---|
| 473 | if ( mesh->mNumBones > 0 ) has_bones = true;
|
---|
| 474 | load_mesh_data(&meshes[m],m);
|
---|
[249] | 475 | }
|
---|
[291] | 476 |
|
---|
[294] | 477 | mesh_nodes_data* nodes = ( has_bones ? release_merged_bones( meshes ) : release_mesh_nodes_data(0) );
|
---|
[291] | 478 | return new mesh_data_pack( m_mesh_count, meshes, nodes );
|
---|
[287] | 479 | }
|
---|
[284] | 480 |
|
---|
[376] | 481 | nv::size_t nv::assimp_loader::get_nodes_data_count() const
|
---|
[287] | 482 | {
|
---|
[291] | 483 | if ( m_scene == nullptr ) return 0;
|
---|
[406] | 484 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[291] | 485 | return scene->mNumAnimations;
|
---|
[249] | 486 | }
|
---|
| 487 |
|
---|
[291] | 488 |
|
---|