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