[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"
|
---|
[432] | 10 | #include "nv/stl/hash_store.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 |
|
---|
[425] | 59 | nv::assimp_loader::assimp_loader( string_table* strings, const string_view& a_ext, uint32 a_assimp_flags /*= 0 */ )
|
---|
| 60 | : mesh_loader( strings ), 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 );
|
---|
[425] | 94 | const aiScene* scene = aiImportFileFromMemory( data, size, m_assimp_flags, m_ext.data() );
|
---|
[248] | 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 |
|
---|
[482] | 107 | data_channel_set* nv::assimp_loader::release_mesh_data( size_t index, data_node_info& info )
|
---|
[248] | 108 | {
|
---|
| 109 | if ( index >= m_mesh_count ) return nullptr;
|
---|
[424] | 110 | data_channel_set* result = data_channel_set_creator::create_set( 2 );
|
---|
[482] | 111 | load_mesh_data( result, index, info );
|
---|
[287] | 112 | return result;
|
---|
| 113 | }
|
---|
[482] | 114 | void nv::assimp_loader::load_mesh_data( data_channel_set* data, size_t index, data_node_info& info )
|
---|
[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 >();
|
---|
[417] | 126 | data_channel_set_creator maccess( data );
|
---|
[425] | 127 | const char* name = mesh->mName.data;
|
---|
[482] | 128 | info.name = make_name( name );
|
---|
| 129 | info.parent_id = -1;
|
---|
[417] | 130 | uint8* cdata = maccess.add_channel( desc, mesh->mNumVertices ).raw_data();
|
---|
| 131 | uint16* indices = reinterpret_cast<uint16*>( maccess.add_channel< index_u16 >( mesh->mNumFaces * 3 ).raw_data() );
|
---|
[248] | 132 |
|
---|
[332] | 133 | if ( mesh->mTangents && mesh->mBitangents )
|
---|
[415] | 134 | for ( unsigned int i = 0; i < mesh->mNumVertices; i++ )
|
---|
| 135 | {
|
---|
| 136 | vec3 v = assimp_vec3_cast( mesh->mVertices[i] );
|
---|
[454] | 137 | vec3 n = math::normalize( assimp_vec3_cast( mesh->mNormals[i] ) );
|
---|
| 138 | vec3 t = math::normalize( assimp_vec3_cast( mesh->mTangents[i] ) );
|
---|
| 139 | vec3 b = math::normalize( assimp_vec3_cast( mesh->mBitangents[i] ) );
|
---|
[415] | 140 | vec2 s = assimp_st_cast( mesh->mTextureCoords[0][i] );
|
---|
[248] | 141 |
|
---|
[454] | 142 | vec3 t_i = math::normalize( t - n * math::dot( n, t ) );
|
---|
| 143 | float det = ( math::dot( math::cross( n, t ), b ) );
|
---|
[415] | 144 | det = ( det < 0.0f ? -1.0f : 1.0f );
|
---|
| 145 | nv::vec4 vt( t_i[0], t_i[1], t_i[2], det );
|
---|
| 146 | if ( skinned )
|
---|
| 147 | reinterpret_cast<assimp_skinned_vtx*>( cdata )[i] = assimp_skinned_vtx( v, s, n, vt );
|
---|
| 148 | else
|
---|
| 149 | reinterpret_cast<assimp_plain_vtx*>( cdata )[i] = assimp_plain_vtx( v, s, n, vt );
|
---|
| 150 | }
|
---|
[248] | 151 |
|
---|
| 152 | if ( skinned )
|
---|
| 153 | {
|
---|
[413] | 154 | assimp_skinned_vtx* vtx = reinterpret_cast< assimp_skinned_vtx* >( cdata );
|
---|
[248] | 155 | for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 156 | {
|
---|
| 157 | aiBone* bone = mesh->mBones[m];
|
---|
| 158 | for (unsigned int w=0; w<bone->mNumWeights; w++)
|
---|
| 159 | {
|
---|
| 160 | assimp_skinned_vtx& v = vtx[ bone->mWeights[w].mVertexId ];
|
---|
| 161 | bool found = false;
|
---|
[406] | 162 | for ( int i = 0 ; i < 4; ++i )
|
---|
[248] | 163 | {
|
---|
| 164 | if ( v.boneweight[i] <= 0.0f )
|
---|
| 165 | {
|
---|
[406] | 166 | v.boneindex[i] = int( m );
|
---|
[248] | 167 | v.boneweight[i] = bone->mWeights[w].mWeight;
|
---|
| 168 | found = true;
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | NV_ASSERT( found, "Too many weights!" );
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | for (unsigned int i=0; i<mesh->mNumFaces; i++)
|
---|
| 178 | {
|
---|
| 179 | const aiFace* face = &mesh->mFaces[i];
|
---|
| 180 | for (unsigned int j=0; j<face->mNumIndices; j++)
|
---|
| 181 | {
|
---|
[406] | 182 | indices[ i*3 + j ] = uint16( face->mIndices[j] );
|
---|
[248] | 183 | }
|
---|
| 184 | }
|
---|
[416] | 185 |
|
---|
[248] | 186 | }
|
---|
| 187 |
|
---|
| 188 | nv::assimp_loader::~assimp_loader()
|
---|
| 189 | {
|
---|
[406] | 190 | if ( m_scene != nullptr ) aiReleaseImport( reinterpret_cast<const aiScene*>( m_scene ) );
|
---|
[248] | 191 | }
|
---|
| 192 |
|
---|
[482] | 193 | bool nv::assimp_loader::load_bones( size_t index, array_ref< data_node_info > bones )
|
---|
[248] | 194 | {
|
---|
| 195 | if ( m_scene == nullptr ) return false;
|
---|
[406] | 196 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[248] | 197 | const aiMesh* mesh = scene->mMeshes[ index ];
|
---|
| 198 |
|
---|
| 199 | for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 200 | {
|
---|
| 201 | aiBone* bone = mesh->mBones[m];
|
---|
[293] | 202 | mat4 offset = assimp_mat4_cast( bone->mOffsetMatrix );
|
---|
[425] | 203 | const char* name = bone->mName.data;
|
---|
[482] | 204 | bones[m].name = make_name( name );
|
---|
| 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 |
|
---|
[482] | 285 | data_node_list* nv::assimp_loader::release_merged_bones( data_channel_set* meshes )
|
---|
[248] | 286 | {
|
---|
[406] | 287 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[482] | 288 | data_node_list* result = new data_node_list( make_name( "bones" ) );
|
---|
[432] | 289 | hash_store< shash64, uint16 > names;
|
---|
[291] | 290 | for ( unsigned int m = 0; m < m_mesh_count; ++m )
|
---|
[248] | 291 | {
|
---|
[323] | 292 | uint16 translate[MAX_BONES];
|
---|
[482] | 293 | vector< data_node_info > bones;
|
---|
[291] | 294 | const aiMesh* mesh = scene->mMeshes[ m ];
|
---|
| 295 | if ( mesh->mNumBones != 0 )
|
---|
[248] | 296 | {
|
---|
[291] | 297 | bones.resize( mesh->mNumBones );
|
---|
[482] | 298 | NV_ASSERT( false, "parent ids for bones are not loaded!" );
|
---|
[291] | 299 | load_bones( m, bones );
|
---|
| 300 | for ( unsigned int b = 0; b < mesh->mNumBones; ++b )
|
---|
| 301 | {
|
---|
[248] | 302 |
|
---|
[482] | 303 | data_node_info bone = bones[b];
|
---|
| 304 | auto iname = names.find( bone.name );
|
---|
[291] | 305 | if ( iname == names.end() )
|
---|
| 306 | {
|
---|
[428] | 307 | NV_ASSERT( result->size() < MAX_BONES, "Too many bones to merge!" );
|
---|
| 308 | uint16 index = uint16( result->size() );
|
---|
[475] | 309 | result->append( bone );
|
---|
[482] | 310 | names[ bone.name ] = index;
|
---|
[291] | 311 | translate[b] = index;
|
---|
| 312 | }
|
---|
| 313 | else
|
---|
| 314 | {
|
---|
[323] | 315 | translate[b] = iname->second;
|
---|
[291] | 316 | }
|
---|
[248] | 317 | }
|
---|
[291] | 318 | if ( m > 0 && bones.size() > 0 )
|
---|
[248] | 319 | {
|
---|
[417] | 320 | data_channel_access< assimp_skinned_vtx > channel( const_cast< raw_data_channel* >( meshes[m].get_channel(0) ) );
|
---|
[413] | 321 | for ( unsigned v = 0; v < channel.size(); ++v )
|
---|
[291] | 322 | {
|
---|
[413] | 323 | assimp_skinned_vtx& vertex = channel.data()[v];
|
---|
[248] | 324 |
|
---|
[406] | 325 | for ( int i = 0 ; i < 4; ++i)
|
---|
[248] | 326 | {
|
---|
[291] | 327 | if ( vertex.boneweight[i] > 0.0f )
|
---|
| 328 | {
|
---|
[406] | 329 | vertex.boneindex[i] = int( translate[vertex.boneindex[i]] );
|
---|
[291] | 330 | }
|
---|
[248] | 331 | }
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
[291] | 334 | }
|
---|
[248] | 335 | }
|
---|
[482] | 336 | //result->initialize();
|
---|
[427] | 337 |
|
---|
| 338 | return result;
|
---|
[248] | 339 | }
|
---|
| 340 |
|
---|
[294] | 341 | mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data( size_t index /*= 0*/ )
|
---|
[249] | 342 | {
|
---|
| 343 | if ( m_scene == nullptr ) return nullptr;
|
---|
[406] | 344 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[291] | 345 | if ( scene->mRootNode == nullptr || scene->mAnimations == nullptr || scene->mAnimations[index] == nullptr) return nullptr;
|
---|
[249] | 346 |
|
---|
| 347 | const aiNode* root = scene->mRootNode;
|
---|
[291] | 348 | const aiAnimation* anim = scene->mAnimations[index];
|
---|
[249] | 349 |
|
---|
[291] | 350 | uint32 count = count_nodes( scene->mRootNode );
|
---|
[249] | 351 |
|
---|
[406] | 352 | uint16 frame_rate = static_cast<uint16>( anim->mTicksPerSecond );
|
---|
[470] | 353 | uint16 duration = static_cast<uint16>( anim->mDuration );
|
---|
[287] | 354 |
|
---|
[482] | 355 | data_channel_set** temp = new data_channel_set*[ count ];
|
---|
| 356 | data_node_info* temp2 = new data_node_info[count];
|
---|
[427] | 357 | array_ref< data_channel_set* > temp_ref( temp, count );
|
---|
[482] | 358 | array_ref< data_node_info > temp2_ref( temp2, count );
|
---|
| 359 | load_node( index, temp_ref, temp2_ref, root, 0, -1 );
|
---|
[291] | 360 |
|
---|
[482] | 361 | mesh_nodes_data* result = new mesh_nodes_data( make_name( static_cast<const char*>( anim->mName.data ) ), frame_rate, duration );
|
---|
| 362 | for ( nv::uint32 i = 0; i < count; ++i )
|
---|
[427] | 363 | {
|
---|
[482] | 364 | result->append( temp_ref[i], temp2_ref[i] );
|
---|
[427] | 365 | }
|
---|
[475] | 366 | result->initialize();
|
---|
[427] | 367 | delete temp;
|
---|
[482] | 368 | delete temp2;
|
---|
[427] | 369 | return result;
|
---|
[249] | 370 | }
|
---|
| 371 |
|
---|
[482] | 372 | data_node_list* nv::assimp_loader::release_data_node_list( size_t index /*= 0 */ )
|
---|
| 373 | {
|
---|
| 374 | int this_is_incorrect;
|
---|
| 375 | NV_ASSERT( false, "unimplemented!" );
|
---|
| 376 | // mesh_nodes_data* half_result = release_mesh_nodes_data( index );
|
---|
| 377 | // data_node_list* result = new data_node_list( half_result->get_name() );
|
---|
| 378 | // for ( auto node : *half_result )
|
---|
| 379 | // result->append( node->get_info() );
|
---|
| 380 | // delete half_result;
|
---|
| 381 | // return result;
|
---|
| 382 | return nullptr;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | bool nv::assimp_loader::is_animated( size_t /*= 0 */ )
|
---|
| 386 | {
|
---|
| 387 | int this_is_incorrect;
|
---|
| 388 | return false;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[249] | 391 | nv::uint32 nv::assimp_loader::count_nodes( const void* node ) const
|
---|
| 392 | {
|
---|
[406] | 393 | const aiNode* ainode = reinterpret_cast< const aiNode* >( node );
|
---|
[249] | 394 | nv::uint32 count = 1;
|
---|
| 395 | for ( unsigned i = 0; i < ainode->mNumChildren; ++i )
|
---|
| 396 | {
|
---|
| 397 | count += count_nodes( ainode->mChildren[i] );
|
---|
| 398 | }
|
---|
| 399 | return count;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[482] | 402 | nv::sint16 nv::assimp_loader::load_node( uint32 anim_id, array_ref< data_channel_set* > nodes, array_ref< data_node_info > infos, const void* vnode, sint16 this_id, sint16 parent_id )
|
---|
[249] | 403 | {
|
---|
[406] | 404 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
| 405 | const aiNode* node = reinterpret_cast<const aiNode*>( vnode );
|
---|
[425] | 406 | string_view name( static_cast< const char* >( node->mName.data ) );
|
---|
[291] | 407 | const aiAnimation* anim = scene->mAnimations[anim_id];
|
---|
[249] | 408 | const aiNodeAnim* anode = nullptr;
|
---|
| 409 |
|
---|
| 410 | for ( unsigned i = 0 ; i < anim->mNumChannels ; i++ )
|
---|
| 411 | {
|
---|
| 412 | anode = anim->mChannels[i];
|
---|
[425] | 413 | if ( string_view( static_cast< const char* >( anode->mNodeName.data ) ) == name ) break;
|
---|
[249] | 414 | anode = nullptr;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[427] | 417 | nodes[ this_id ] = anode ? create_keys( anode ) : data_channel_set_creator::create_set( 0 );
|
---|
[249] | 418 |
|
---|
[482] | 419 | infos[this_id].name = make_name( name );
|
---|
| 420 | infos[this_id].parent_id = parent_id;
|
---|
[279] | 421 | // This value is ignored by the create_transformed_keys, but needed by create_direct_keys!
|
---|
| 422 | // TODO: find a common solution!
|
---|
| 423 | // This is bad because create_transformed_keys never uses node-transformations for
|
---|
| 424 | // node's without keys
|
---|
[482] | 425 | // TODO: this can probably be deleted
|
---|
| 426 | infos[this_id].transform = nv::assimp_mat4_cast( node->mTransformation );
|
---|
| 427 | if ( this_id == 0 ) infos[this_id].transform = mat4();
|
---|
[249] | 428 |
|
---|
[291] | 429 | nv::sint16 next = this_id + 1;
|
---|
[249] | 430 | for ( unsigned i = 0; i < node->mNumChildren; ++i )
|
---|
| 431 | {
|
---|
[482] | 432 | next = load_node( anim_id, nodes, infos, node->mChildren[i], next, this_id );
|
---|
[249] | 433 | }
|
---|
[287] | 434 |
|
---|
[249] | 435 | return next;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[427] | 438 | data_channel_set* nv::assimp_loader::create_keys( const void* vnode )
|
---|
[249] | 439 | {
|
---|
[406] | 440 | const aiNodeAnim* node = reinterpret_cast< const aiNodeAnim* >( vnode );
|
---|
[282] | 441 | if ( node->mNumPositionKeys == 0 && node->mNumRotationKeys == 0 && node->mNumScalingKeys == 0 )
|
---|
| 442 | {
|
---|
[427] | 443 | return data_channel_set_creator::create_set( 0 );
|
---|
[282] | 444 | }
|
---|
[427] | 445 |
|
---|
| 446 | data_channel_set* set = data_channel_set_creator::create_set( 2 );
|
---|
| 447 | data_channel_set_creator key_set( set );
|
---|
[249] | 448 |
|
---|
[417] | 449 | assimp_key_p* pchannel = key_set.add_channel< assimp_key_p >( node->mNumPositionKeys ).data();
|
---|
| 450 | assimp_key_r* rchannel = key_set.add_channel< assimp_key_r >( node->mNumRotationKeys ).data();
|
---|
| 451 | // assimp_key_s* schannel = key_set.add_channel< assimp_key_s >( node->mNumScalingKeys ).data();
|
---|
[282] | 452 |
|
---|
[249] | 453 | for ( unsigned np = 0; np < node->mNumPositionKeys; ++np )
|
---|
| 454 | {
|
---|
[410] | 455 | pchannel[np].time = static_cast<float>( node->mPositionKeys[np].mTime );
|
---|
| 456 | pchannel[np].translation = assimp_vec3_cast(node->mPositionKeys[np].mValue);
|
---|
[249] | 457 | }
|
---|
| 458 | for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
|
---|
| 459 | {
|
---|
[406] | 460 | rchannel[np].time = static_cast<float>( node->mRotationKeys[np].mTime );
|
---|
[293] | 461 | rchannel[np].rotation = assimp_quat_cast(node->mRotationKeys[np].mValue );
|
---|
[249] | 462 | }
|
---|
[287] | 463 | // if ( node->mNumScalingKeys > 0 )
|
---|
| 464 | // {
|
---|
[451] | 465 | // vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue );
|
---|
[454] | 466 | // float scale_value = math::length( math::abs( scale_vec0 - vec3(1,1,1) ) );
|
---|
[287] | 467 | // if ( node->mNumScalingKeys > 1 || scale_value > 0.001 )
|
---|
| 468 | // {
|
---|
| 469 | // NV_LOG( nv::LOG_WARNING, "scale key significant!" );
|
---|
| 470 | // for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
|
---|
| 471 | // {
|
---|
| 472 | // schannel[np].time = (float)node->mScalingKeys[np].mTime;
|
---|
| 473 | // schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue);
|
---|
| 474 | // }
|
---|
| 475 | // }
|
---|
| 476 | // else
|
---|
| 477 | // {
|
---|
| 478 | // schannel[0].time = (float)node->mScalingKeys[0].mTime;
|
---|
| 479 | // schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue);
|
---|
| 480 | // }
|
---|
| 481 | // }
|
---|
[427] | 482 | return set;
|
---|
[287] | 483 | }
|
---|
| 484 |
|
---|
[480] | 485 | // mesh_data_pack* nv::assimp_loader::release_mesh_data_pack()
|
---|
| 486 | // {
|
---|
| 487 | // if ( m_scene == nullptr || m_mesh_count == 0 ) return nullptr;
|
---|
| 488 | // const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
| 489 | // bool has_bones = false;
|
---|
| 490 | // data_channel_set* meshes = data_channel_set_creator::create_set_array( m_mesh_count, 2 );
|
---|
| 491 | // for ( size_t m = 0; m < m_mesh_count; ++m )
|
---|
| 492 | // {
|
---|
| 493 | // const aiMesh* mesh = scene->mMeshes[ m ];
|
---|
| 494 | // data_channel_set_creator( &meshes[m] ).set_name( make_name( static_cast<const char*>( mesh->mName.data ) ) );
|
---|
| 495 | // if ( mesh->mNumBones > 0 ) has_bones = true;
|
---|
| 496 | // load_mesh_data(&meshes[m],m);
|
---|
| 497 | // }
|
---|
| 498 | //
|
---|
| 499 | // mesh_nodes_data* nodes = ( has_bones ? release_merged_bones( meshes ) : release_mesh_nodes_data(0) );
|
---|
| 500 | // return new mesh_data_pack( m_mesh_count, meshes, nodes );
|
---|
| 501 | // }
|
---|
[291] | 502 |
|
---|
[376] | 503 | nv::size_t nv::assimp_loader::get_nodes_data_count() const
|
---|
[287] | 504 | {
|
---|
[291] | 505 | if ( m_scene == nullptr ) return 0;
|
---|
[406] | 506 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
[291] | 507 | return scene->mNumAnimations;
|
---|
[249] | 508 | }
|
---|
| 509 |
|
---|
[291] | 510 |
|
---|