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