[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 |
|
---|
[485] | 15 | namespace nv {
|
---|
| 16 |
|
---|
| 17 | struct assimp_data
|
---|
| 18 | {
|
---|
| 19 | const aiScene* scene;
|
---|
| 20 | vector< const aiBone* > bones;
|
---|
| 21 | vector< const aiNode* > nodes;
|
---|
| 22 | vector< const aiMesh* > meshes;
|
---|
| 23 | vector< const aiNode* > skeletons;
|
---|
| 24 |
|
---|
| 25 | hash_store< shash64, const aiBone* > bone_by_name;
|
---|
| 26 | hash_store< shash64, const aiNode* > node_by_name;
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[323] | 31 | const unsigned MAX_BONES = 64;
|
---|
[250] | 32 |
|
---|
[293] | 33 | struct assimp_plain_vtx
|
---|
| 34 | {
|
---|
| 35 | vec3 position;
|
---|
| 36 | vec3 normal;
|
---|
| 37 | vec2 texcoord;
|
---|
| 38 | vec4 tangent;
|
---|
| 39 |
|
---|
| 40 | assimp_plain_vtx() {}
|
---|
| 41 | assimp_plain_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )
|
---|
| 42 | {
|
---|
| 43 | position = v;
|
---|
| 44 | texcoord = t;
|
---|
| 45 | normal = n;
|
---|
| 46 | tangent = g;
|
---|
| 47 | }
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | struct assimp_skinned_vtx
|
---|
| 51 | {
|
---|
| 52 | vec3 position;
|
---|
| 53 | vec3 normal;
|
---|
| 54 | vec2 texcoord;
|
---|
| 55 | vec4 tangent;
|
---|
| 56 | ivec4 boneindex;
|
---|
| 57 | vec4 boneweight;
|
---|
| 58 |
|
---|
| 59 | assimp_skinned_vtx() {}
|
---|
| 60 | assimp_skinned_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )
|
---|
| 61 | {
|
---|
| 62 | position = v;
|
---|
| 63 | texcoord = t;
|
---|
| 64 | normal = n;
|
---|
| 65 | tangent = g;
|
---|
| 66 | }
|
---|
| 67 | };
|
---|
| 68 |
|
---|
[410] | 69 | struct assimp_key_p { float time; vec3 translation; };
|
---|
[291] | 70 | struct assimp_key_r { float time; quat rotation; };
|
---|
| 71 | struct assimp_key_s { float time; vec3 scale; };
|
---|
| 72 | struct assimp_key_tr { transform tform; };
|
---|
| 73 |
|
---|
| 74 |
|
---|
[425] | 75 | nv::assimp_loader::assimp_loader( string_table* strings, const string_view& a_ext, uint32 a_assimp_flags /*= 0 */ )
|
---|
[485] | 76 | : mesh_loader( strings ), m_mesh_count(0)
|
---|
[250] | 77 | {
|
---|
[284] | 78 | m_ext = a_ext;
|
---|
| 79 | m_assimp_flags = a_assimp_flags;
|
---|
[277] | 80 | if ( m_assimp_flags == 0 )
|
---|
| 81 | {
|
---|
| 82 | m_assimp_flags = (
|
---|
| 83 | aiProcess_CalcTangentSpace |
|
---|
| 84 | aiProcess_GenSmoothNormals |
|
---|
| 85 | aiProcess_JoinIdenticalVertices |
|
---|
| 86 | aiProcess_ImproveCacheLocality |
|
---|
| 87 | aiProcess_LimitBoneWeights |
|
---|
| 88 | aiProcess_RemoveRedundantMaterials |
|
---|
| 89 | aiProcess_SplitLargeMeshes |
|
---|
| 90 | aiProcess_Triangulate |
|
---|
| 91 | aiProcess_GenUVCoords |
|
---|
| 92 | aiProcess_SortByPType |
|
---|
| 93 | aiProcess_FindDegenerates |
|
---|
[485] | 94 | aiProcess_FindDegenerates |
|
---|
[277] | 95 | 0 );
|
---|
| 96 | }
|
---|
[485] | 97 | m_data = new assimp_data;
|
---|
| 98 | m_data->scene = nullptr;
|
---|
[277] | 99 | }
|
---|
[284] | 100 |
|
---|
| 101 |
|
---|
[248] | 102 | bool nv::assimp_loader::load( stream& source )
|
---|
| 103 | {
|
---|
[292] | 104 | load_assimp_library();
|
---|
[485] | 105 | if ( m_data->scene != nullptr ) aiReleaseImport( m_data->scene );
|
---|
| 106 | m_data->scene = nullptr;
|
---|
[248] | 107 | m_mesh_count = 0;
|
---|
[365] | 108 | NV_LOG_NOTICE( "AssImp loading file..." );
|
---|
[323] | 109 | size_t size = source.size();
|
---|
[248] | 110 | char* data = new char[ size ];
|
---|
| 111 | source.read( data, size, 1 );
|
---|
[425] | 112 | const aiScene* scene = aiImportFileFromMemory( data, size, m_assimp_flags, m_ext.data() );
|
---|
[248] | 113 |
|
---|
| 114 | if( !scene)
|
---|
| 115 | {
|
---|
[365] | 116 | NV_LOG_ERROR( aiGetErrorString() );
|
---|
[248] | 117 | return false;
|
---|
| 118 | }
|
---|
[485] | 119 | m_data->scene = scene;
|
---|
| 120 | m_mesh_count = scene->mNumMeshes;
|
---|
[365] | 121 | NV_LOG_NOTICE( "Loading successfull" );
|
---|
[485] | 122 |
|
---|
| 123 | scan_nodes( scene->mRootNode );
|
---|
| 124 |
|
---|
| 125 | for ( unsigned i = 0; i < scene->mRootNode->mNumChildren; ++i )
|
---|
| 126 | {
|
---|
| 127 | const aiNode* node = scene->mRootNode->mChildren[i];
|
---|
| 128 | if ( node->mNumMeshes == 0 )
|
---|
| 129 | m_data->skeletons.push_back( node );
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | for ( nv::uint32 i = 0; i < m_mesh_count; i++ )
|
---|
| 133 | {
|
---|
| 134 | data_node_info info;
|
---|
[486] | 135 | data_channel_set* mdata = data_channel_set_creator::create_set( 2 );
|
---|
| 136 | load_mesh_data( mdata, i, info );
|
---|
| 137 | m_meshes.push_back( mdata );
|
---|
[485] | 138 | m_mesh_info.push_back( info );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | scene_report();
|
---|
[248] | 142 | return true;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[482] | 145 | data_channel_set* nv::assimp_loader::release_mesh_data( size_t index, data_node_info& info )
|
---|
[248] | 146 | {
|
---|
| 147 | if ( index >= m_mesh_count ) return nullptr;
|
---|
[485] | 148 | info = m_mesh_info[index];
|
---|
| 149 | return m_meshes[index];
|
---|
[287] | 150 | }
|
---|
[482] | 151 | void nv::assimp_loader::load_mesh_data( data_channel_set* data, size_t index, data_node_info& info )
|
---|
[287] | 152 | {
|
---|
[485] | 153 | const aiMesh* mesh = m_data->scene->mMeshes[ index ];
|
---|
[248] | 154 |
|
---|
| 155 | bool skinned = mesh->mNumBones > 0;
|
---|
[415] | 156 |
|
---|
| 157 | data_descriptor desc;
|
---|
[248] | 158 | if ( skinned )
|
---|
[415] | 159 | desc.initialize< assimp_skinned_vtx >();
|
---|
[248] | 160 | else
|
---|
[415] | 161 | desc.initialize< assimp_plain_vtx >();
|
---|
[417] | 162 | data_channel_set_creator maccess( data );
|
---|
[485] | 163 | string64 name( mesh->mName.data, mesh->mName.length );
|
---|
| 164 | if ( mesh->mName.length == 0 )
|
---|
| 165 | {
|
---|
| 166 | for ( auto node : m_data->nodes )
|
---|
| 167 | {
|
---|
| 168 | if ( node->mMeshes )
|
---|
| 169 | for ( uint32 i = 0; i < node->mNumMeshes; ++i )
|
---|
| 170 | if ( node->mMeshes[i] == index )
|
---|
| 171 | {
|
---|
| 172 | name.assign( node->mName.data, node->mName.length );
|
---|
| 173 | if ( i != 0 )
|
---|
| 174 | {
|
---|
| 175 | name.append( "#0" );
|
---|
| 176 | name.append( i );
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
[482] | 182 | info.name = make_name( name );
|
---|
| 183 | info.parent_id = -1;
|
---|
[485] | 184 | int hack_for_node_anim;
|
---|
| 185 | if ( is_node_animated() )
|
---|
[486] | 186 | info.parent_id = sint16( index );
|
---|
[485] | 187 |
|
---|
| 188 |
|
---|
[417] | 189 | uint8* cdata = maccess.add_channel( desc, mesh->mNumVertices ).raw_data();
|
---|
[491] | 190 | uint32* indices = reinterpret_cast<uint32*>( maccess.add_channel< index_u32 >( mesh->mNumFaces * 3 ).raw_data() );
|
---|
[248] | 191 |
|
---|
[332] | 192 | if ( mesh->mTangents && mesh->mBitangents )
|
---|
[415] | 193 | for ( unsigned int i = 0; i < mesh->mNumVertices; i++ )
|
---|
| 194 | {
|
---|
| 195 | vec3 v = assimp_vec3_cast( mesh->mVertices[i] );
|
---|
[454] | 196 | vec3 n = math::normalize( assimp_vec3_cast( mesh->mNormals[i] ) );
|
---|
| 197 | vec3 t = math::normalize( assimp_vec3_cast( mesh->mTangents[i] ) );
|
---|
| 198 | vec3 b = math::normalize( assimp_vec3_cast( mesh->mBitangents[i] ) );
|
---|
[415] | 199 | vec2 s = assimp_st_cast( mesh->mTextureCoords[0][i] );
|
---|
[248] | 200 |
|
---|
[454] | 201 | vec3 t_i = math::normalize( t - n * math::dot( n, t ) );
|
---|
| 202 | float det = ( math::dot( math::cross( n, t ), b ) );
|
---|
[415] | 203 | det = ( det < 0.0f ? -1.0f : 1.0f );
|
---|
| 204 | nv::vec4 vt( t_i[0], t_i[1], t_i[2], det );
|
---|
| 205 | if ( skinned )
|
---|
| 206 | reinterpret_cast<assimp_skinned_vtx*>( cdata )[i] = assimp_skinned_vtx( v, s, n, vt );
|
---|
| 207 | else
|
---|
| 208 | reinterpret_cast<assimp_plain_vtx*>( cdata )[i] = assimp_plain_vtx( v, s, n, vt );
|
---|
| 209 | }
|
---|
[248] | 210 |
|
---|
| 211 | if ( skinned )
|
---|
| 212 | {
|
---|
[413] | 213 | assimp_skinned_vtx* vtx = reinterpret_cast< assimp_skinned_vtx* >( cdata );
|
---|
[248] | 214 | for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 215 | {
|
---|
| 216 | aiBone* bone = mesh->mBones[m];
|
---|
[487] | 217 | for ( size_t w=0; w<bone->mNumWeights; w++)
|
---|
[248] | 218 | {
|
---|
| 219 | assimp_skinned_vtx& v = vtx[ bone->mWeights[w].mVertexId ];
|
---|
| 220 | bool found = false;
|
---|
[487] | 221 | for ( size_t i = 0 ; i < 4; ++i )
|
---|
[248] | 222 | {
|
---|
| 223 | if ( v.boneweight[i] <= 0.0f )
|
---|
| 224 | {
|
---|
[406] | 225 | v.boneindex[i] = int( m );
|
---|
[248] | 226 | v.boneweight[i] = bone->mWeights[w].mWeight;
|
---|
| 227 | found = true;
|
---|
| 228 | break;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | NV_ASSERT( found, "Too many weights!" );
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | for (unsigned int i=0; i<mesh->mNumFaces; i++)
|
---|
| 237 | {
|
---|
| 238 | const aiFace* face = &mesh->mFaces[i];
|
---|
| 239 | for (unsigned int j=0; j<face->mNumIndices; j++)
|
---|
| 240 | {
|
---|
[491] | 241 | indices[ i*3 + j ] = uint32( face->mIndices[j] );
|
---|
[248] | 242 | }
|
---|
| 243 | }
|
---|
[416] | 244 |
|
---|
[248] | 245 | }
|
---|
| 246 |
|
---|
| 247 | nv::assimp_loader::~assimp_loader()
|
---|
| 248 | {
|
---|
[485] | 249 | if ( m_data->scene != nullptr ) aiReleaseImport( m_data->scene );
|
---|
| 250 | delete m_data;
|
---|
[248] | 251 | }
|
---|
| 252 |
|
---|
| 253 | void nv::assimp_loader::scene_report() const
|
---|
| 254 | {
|
---|
[485] | 255 | if ( m_data->scene == nullptr ) return;
|
---|
[248] | 256 |
|
---|
[365] | 257 | NV_LOG_NOTICE( "------------------------" );
|
---|
[485] | 258 | NV_LOG_NOTICE( "Texture count - ", m_data->scene->mNumTextures );
|
---|
| 259 | NV_LOG_NOTICE( "Animation count - ", m_data->scene->mNumAnimations );
|
---|
| 260 | NV_LOG_NOTICE( "Material count - ", m_data->scene->mNumMaterials );
|
---|
| 261 | NV_LOG_NOTICE( "Meshes count - ", m_data->scene->mNumMeshes );
|
---|
[365] | 262 | NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 263 |
|
---|
[485] | 264 | aiNode* root = m_data->scene->mRootNode;
|
---|
[248] | 265 | if (root)
|
---|
| 266 | {
|
---|
[365] | 267 | NV_LOG_NOTICE( "Root node - ", root->mName.data );
|
---|
| 268 | NV_LOG_NOTICE( " meshes - ", root->mNumMeshes );
|
---|
| 269 | NV_LOG_NOTICE( " children - ", root->mNumChildren );
|
---|
[248] | 270 | }
|
---|
| 271 | else
|
---|
| 272 | {
|
---|
[365] | 273 | NV_LOG_NOTICE( "No root node!" );
|
---|
[248] | 274 | }
|
---|
[365] | 275 | NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 276 |
|
---|
[485] | 277 | if ( m_data->scene->mNumMeshes > 0 )
|
---|
[248] | 278 | {
|
---|
[485] | 279 | for ( nv::uint32 mc = 0; mc < m_data->scene->mNumMeshes; mc++ )
|
---|
[248] | 280 | {
|
---|
[485] | 281 | aiMesh* mesh = m_data->scene->mMeshes[mc];
|
---|
[248] | 282 |
|
---|
[485] | 283 | NV_LOG_NOTICE( "Mesh #", mc, " - ", string_view( static_cast<char*>( mesh->mName.data ), mesh->mName.length ) );
|
---|
[365] | 284 | NV_LOG_NOTICE( " bones - ", mesh->mNumBones );
|
---|
| 285 | NV_LOG_NOTICE( " uvs - ", mesh->mNumUVComponents[0] );
|
---|
| 286 | NV_LOG_NOTICE( " verts - ", mesh->mNumVertices );
|
---|
| 287 | NV_LOG_NOTICE( " faces - ", mesh->mNumFaces );
|
---|
[248] | 288 |
|
---|
[365] | 289 | // NV_LOG_NOTICE( "Bones:" );
|
---|
[248] | 290 | // for (unsigned int m=0; m<mesh->mNumBones; m++)
|
---|
| 291 | // {
|
---|
| 292 | // aiBone* bone = mesh->mBones[m];
|
---|
[365] | 293 | // NV_LOG_NOTICE( bone->mName.C_Str() );
|
---|
[248] | 294 | // }
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | else
|
---|
| 298 | {
|
---|
[365] | 299 | NV_LOG_NOTICE( "No meshes!" );
|
---|
[248] | 300 | }
|
---|
[365] | 301 | NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 302 |
|
---|
[485] | 303 | for ( auto node : m_data->nodes )
|
---|
| 304 | {
|
---|
| 305 | NV_LOG_NOTICE( "Node : ", string_view( node->mName.data, node->mName.length ) );
|
---|
| 306 | }
|
---|
[248] | 307 |
|
---|
[485] | 308 | for ( auto skeleton : m_data->skeletons )
|
---|
| 309 | {
|
---|
| 310 | NV_LOG_NOTICE( "Skeleton : ", string_view( skeleton->mName.data, skeleton->mName.length ) );
|
---|
| 311 | }
|
---|
| 312 |
|
---|
[248] | 313 | // if ( scene->mNumMaterials > 0 )
|
---|
| 314 | // {
|
---|
| 315 | // for (unsigned int m=0; m < scene->mNumMaterials; m++)
|
---|
| 316 | // {
|
---|
| 317 | // int texIndex = 0;
|
---|
| 318 | // aiReturn texFound = aiReturn_SUCCESS;
|
---|
| 319 | // aiString path; // filename
|
---|
| 320 | // while (texFound == aiReturn_SUCCESS)
|
---|
| 321 | // {
|
---|
| 322 | // texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path);
|
---|
[365] | 323 | // NV_LOG_NOTICE( " material - ", path.data );
|
---|
[248] | 324 | // texIndex++;
|
---|
| 325 | // }
|
---|
| 326 | // }
|
---|
| 327 | // }
|
---|
| 328 | // else
|
---|
| 329 | // {
|
---|
[365] | 330 | // NV_LOG_NOTICE( "No materials" );
|
---|
[248] | 331 | // }
|
---|
[365] | 332 | // NV_LOG_NOTICE( "------------------------" );
|
---|
[248] | 333 |
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[485] | 336 | bool nv::assimp_loader::is_node_animated()
|
---|
[248] | 337 | {
|
---|
[485] | 338 | return is_animated() && m_data->skeletons.empty();
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[486] | 341 | void nv::assimp_loader::build_skeleton( vector< data_node_info >& skeleton, const void* node, sint16 parent_id )
|
---|
[485] | 342 | {
|
---|
| 343 | const aiNode* ainode = reinterpret_cast<const aiNode*>( node );
|
---|
| 344 |
|
---|
| 345 | if ( ainode->mNumMeshes > 0 )
|
---|
| 346 | {
|
---|
| 347 | int error;
|
---|
| 348 | int bug_this_works_only_if_before_releasing_meshes;
|
---|
| 349 |
|
---|
| 350 | nv::uint32 mid = ainode->mMeshes[0];
|
---|
| 351 | m_mesh_info[mid].parent_id = parent_id;
|
---|
| 352 | return;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | string_view name( ainode->mName.data, ainode->mName.length );
|
---|
| 356 | if ( name.starts_with( '_' ) ) return;
|
---|
| 357 |
|
---|
| 358 | data_node_info info;
|
---|
| 359 | info.name = make_name( name );
|
---|
| 360 | info.parent_id = parent_id;
|
---|
| 361 |
|
---|
[486] | 362 | sint16 this_id = sint16( skeleton.size() );
|
---|
[485] | 363 | skeleton.push_back( info );
|
---|
| 364 | for ( unsigned i = 0; i < ainode->mNumChildren; ++i )
|
---|
| 365 | {
|
---|
| 366 | build_skeleton( skeleton, ainode->mChildren[i], this_id );
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | data_node_list* nv::assimp_loader::release_merged_bones()
|
---|
| 371 | {
|
---|
| 372 | if ( m_data->skeletons.empty() ) return nullptr;
|
---|
| 373 | vector< data_node_info > bone_data;
|
---|
| 374 | hash_store< shash64, uint16 > bone_map;
|
---|
| 375 |
|
---|
| 376 | {
|
---|
| 377 | const aiNode* skeleton = m_data->skeletons[0];
|
---|
| 378 | build_skeleton( bone_data, skeleton, -1 );
|
---|
| 379 |
|
---|
| 380 | for ( uint32 i = 0; i < bone_data.size(); ++i )
|
---|
| 381 | {
|
---|
| 382 | bone_map[bone_data[i].name] = uint16(i);
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
[291] | 386 | for ( unsigned int m = 0; m < m_mesh_count; ++m )
|
---|
[248] | 387 | {
|
---|
[323] | 388 | uint16 translate[MAX_BONES];
|
---|
[482] | 389 | vector< data_node_info > bones;
|
---|
[485] | 390 | const aiMesh* mesh = m_data->scene->mMeshes[m];
|
---|
[291] | 391 | if ( mesh->mNumBones != 0 )
|
---|
[248] | 392 | {
|
---|
[485] | 393 | for ( unsigned int b = 0; b < mesh->mNumBones; b++ )
|
---|
[291] | 394 | {
|
---|
[485] | 395 | aiBone* bone = mesh->mBones[b];
|
---|
| 396 | mat4 offset = assimp_mat4_cast( bone->mOffsetMatrix );
|
---|
| 397 | shash64 bone_name( bone->mName.data );
|
---|
[248] | 398 |
|
---|
[485] | 399 | int remove_this;
|
---|
| 400 |
|
---|
| 401 | NV_ASSERT( bone_map.find( shash64( bone->mName.data ) ) != bone_map.end(), "BONE NOT FOUND!" );
|
---|
| 402 | uint16 index = bone_map[bone_name];
|
---|
| 403 | bone_data[index].transform = offset;
|
---|
| 404 | translate[b] = index;
|
---|
[248] | 405 | }
|
---|
[485] | 406 |
|
---|
| 407 | data_channel_access< assimp_skinned_vtx > channel( const_cast<raw_data_channel*>( m_meshes[m]->get_channel( 0 ) ) );
|
---|
| 408 | for ( unsigned v = 0; v < channel.size(); ++v )
|
---|
[248] | 409 | {
|
---|
[485] | 410 | assimp_skinned_vtx& vertex = channel.data()[v];
|
---|
[487] | 411 | for ( size_t i = 0; i < 4; ++i )
|
---|
[291] | 412 | {
|
---|
[485] | 413 | if ( vertex.boneweight[i] > 0.0f )
|
---|
[248] | 414 | {
|
---|
[485] | 415 | vertex.boneindex[i] = int( translate[vertex.boneindex[i]] );
|
---|
[248] | 416 | }
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
[485] | 419 |
|
---|
| 420 | }
|
---|
[248] | 421 | }
|
---|
[427] | 422 |
|
---|
[485] | 423 | for ( uint32 i = 0; i < bone_data.size(); ++i )
|
---|
| 424 | {
|
---|
| 425 | int error; // not really, just
|
---|
| 426 | int check_this_shit;
|
---|
| 427 | if ( bone_data[i].transform == mat4() )
|
---|
| 428 | {
|
---|
| 429 | mat4 tr = nv::math::inverse( assimp_mat4_cast( m_data->node_by_name[bone_data[i].name]->mTransformation ) );
|
---|
[486] | 430 | int pid = bone_data[i].parent_id;
|
---|
[487] | 431 | if ( pid >= 0 )
|
---|
| 432 | bone_data[i].transform = tr * bone_data[ size_t( pid ) ].transform;
|
---|
[486] | 433 | else
|
---|
| 434 | bone_data[i].transform = tr;
|
---|
[485] | 435 | }
|
---|
| 436 | // list->append( bone_data[i] );
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 |
|
---|
| 440 | data_node_list* list = new data_node_list( make_name( "bones" ) );
|
---|
| 441 | for ( uint32 i = 0; i < bone_data.size(); ++i )
|
---|
| 442 | {
|
---|
| 443 | list->append( bone_data[i] );
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | return list;
|
---|
[248] | 447 | }
|
---|
| 448 |
|
---|
[294] | 449 | mesh_nodes_data* nv::assimp_loader::release_mesh_nodes_data( size_t index /*= 0*/ )
|
---|
[249] | 450 | {
|
---|
[485] | 451 | if ( m_data->scene == nullptr ) return nullptr;
|
---|
| 452 | const aiScene* scene = reinterpret_cast<const aiScene*>( m_data->scene );
|
---|
[291] | 453 | if ( scene->mRootNode == nullptr || scene->mAnimations == nullptr || scene->mAnimations[index] == nullptr) return nullptr;
|
---|
[249] | 454 |
|
---|
[291] | 455 | const aiAnimation* anim = scene->mAnimations[index];
|
---|
[249] | 456 |
|
---|
[291] | 457 | uint32 count = count_nodes( scene->mRootNode );
|
---|
[485] | 458 | count = count - 1;
|
---|
[249] | 459 |
|
---|
[406] | 460 | uint16 frame_rate = static_cast<uint16>( anim->mTicksPerSecond );
|
---|
[485] | 461 | uint16 duration = static_cast<uint16>( anim->mDuration )+1;
|
---|
[287] | 462 |
|
---|
[482] | 463 | data_channel_set** temp = new data_channel_set*[ count ];
|
---|
[485] | 464 | data_node_info* temp2 = new data_node_info[count ];
|
---|
[427] | 465 | array_ref< data_channel_set* > temp_ref( temp, count );
|
---|
[482] | 466 | array_ref< data_node_info > temp2_ref( temp2, count );
|
---|
[291] | 467 |
|
---|
[485] | 468 | nv::sint16 next = 0;
|
---|
| 469 | for ( unsigned i = 0; i < scene->mRootNode->mNumChildren; ++i )
|
---|
| 470 | {
|
---|
| 471 | next = load_node( index, temp_ref, temp2_ref, scene->mRootNode->mChildren[i], next, -1 );
|
---|
| 472 | }
|
---|
| 473 | // load_node( index, temp_ref, temp2_ref, scene->mRootNode, 0, -1 );
|
---|
| 474 |
|
---|
[482] | 475 | mesh_nodes_data* result = new mesh_nodes_data( make_name( static_cast<const char*>( anim->mName.data ) ), frame_rate, duration );
|
---|
| 476 | for ( nv::uint32 i = 0; i < count; ++i )
|
---|
[427] | 477 | {
|
---|
[482] | 478 | result->append( temp_ref[i], temp2_ref[i] );
|
---|
[427] | 479 | }
|
---|
[475] | 480 | result->initialize();
|
---|
[427] | 481 | delete temp;
|
---|
[482] | 482 | delete temp2;
|
---|
[427] | 483 | return result;
|
---|
[249] | 484 | }
|
---|
| 485 |
|
---|
[486] | 486 | data_node_list* nv::assimp_loader::release_data_node_list( size_t /*= 0 */ )
|
---|
[482] | 487 | {
|
---|
[485] | 488 | return release_merged_bones();
|
---|
[482] | 489 | }
|
---|
| 490 |
|
---|
| 491 | bool nv::assimp_loader::is_animated( size_t /*= 0 */ )
|
---|
| 492 | {
|
---|
| 493 | int this_is_incorrect;
|
---|
[487] | 494 | return m_mesh_count == 0 || ( m_data->scene->mNumAnimations > 0 && m_data->skeletons.size() == 0 );
|
---|
[482] | 495 | }
|
---|
| 496 |
|
---|
[485] | 497 | void nv::assimp_loader::scan_nodes( const void* node ) const
|
---|
| 498 | {
|
---|
| 499 | const aiNode* ainode = reinterpret_cast<const aiNode*>( node );
|
---|
| 500 | m_data->nodes.push_back( ainode );
|
---|
| 501 | m_data->node_by_name[shash64(ainode->mName.data)] = ainode;
|
---|
| 502 |
|
---|
| 503 | for ( unsigned i = 0; i < ainode->mNumChildren; ++i )
|
---|
| 504 | {
|
---|
| 505 | scan_nodes( ainode->mChildren[i] );
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[249] | 509 | nv::uint32 nv::assimp_loader::count_nodes( const void* node ) const
|
---|
| 510 | {
|
---|
[406] | 511 | const aiNode* ainode = reinterpret_cast< const aiNode* >( node );
|
---|
[249] | 512 | nv::uint32 count = 1;
|
---|
| 513 | for ( unsigned i = 0; i < ainode->mNumChildren; ++i )
|
---|
| 514 | {
|
---|
| 515 | count += count_nodes( ainode->mChildren[i] );
|
---|
| 516 | }
|
---|
| 517 | return count;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[482] | 520 | 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] | 521 | {
|
---|
[406] | 522 | const aiNode* node = reinterpret_cast<const aiNode*>( vnode );
|
---|
[425] | 523 | string_view name( static_cast< const char* >( node->mName.data ) );
|
---|
[485] | 524 | const aiAnimation* anim = m_data->scene->mAnimations[anim_id];
|
---|
[249] | 525 | const aiNodeAnim* anode = nullptr;
|
---|
| 526 |
|
---|
| 527 | for ( unsigned i = 0 ; i < anim->mNumChannels ; i++ )
|
---|
| 528 | {
|
---|
| 529 | anode = anim->mChannels[i];
|
---|
[425] | 530 | if ( string_view( static_cast< const char* >( anode->mNodeName.data ) ) == name ) break;
|
---|
[249] | 531 | anode = nullptr;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 |
|
---|
[485] | 535 | transform t = nv::transform( nv::assimp_mat4_cast( node->mTransformation ) );
|
---|
| 536 |
|
---|
[487] | 537 | nodes[ uint32( this_id ) ] = anode ? create_keys( anode, t ) : data_channel_set_creator::create_set( 0 );
|
---|
| 538 | infos[ uint32( this_id ) ].name = make_name( name );
|
---|
| 539 | infos[ uint32( this_id ) ].parent_id = parent_id;
|
---|
[279] | 540 | // This value is ignored by the create_transformed_keys, but needed by create_direct_keys!
|
---|
| 541 | // TODO: find a common solution!
|
---|
| 542 | // This is bad because create_transformed_keys never uses node-transformations for
|
---|
| 543 | // node's without keys
|
---|
[482] | 544 | // TODO: this can probably be deleted
|
---|
[485] | 545 | // infos[this_id].transform = nv::assimp_mat4_cast( node->mTransformation );
|
---|
| 546 | // if ( this_id == 0 ) infos[this_id].transform = mat4();
|
---|
[249] | 547 |
|
---|
[485] | 548 |
|
---|
[291] | 549 | nv::sint16 next = this_id + 1;
|
---|
[249] | 550 | for ( unsigned i = 0; i < node->mNumChildren; ++i )
|
---|
| 551 | {
|
---|
[482] | 552 | next = load_node( anim_id, nodes, infos, node->mChildren[i], next, this_id );
|
---|
[249] | 553 | }
|
---|
[287] | 554 |
|
---|
[249] | 555 | return next;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[485] | 558 | data_channel_set* nv::assimp_loader::create_keys( const void* vnode, const transform& tr )
|
---|
[249] | 559 | {
|
---|
[485] | 560 | const aiNodeAnim* node = reinterpret_cast< const aiNodeAnim* >( vnode );
|
---|
[282] | 561 | if ( node->mNumPositionKeys == 0 && node->mNumRotationKeys == 0 && node->mNumScalingKeys == 0 )
|
---|
| 562 | {
|
---|
[427] | 563 | return data_channel_set_creator::create_set( 0 );
|
---|
[282] | 564 | }
|
---|
[485] | 565 |
|
---|
| 566 | /* OLD
|
---|
[427] | 567 | data_channel_set* set = data_channel_set_creator::create_set( 2 );
|
---|
| 568 | data_channel_set_creator key_set( set );
|
---|
[249] | 569 |
|
---|
[417] | 570 | assimp_key_p* pchannel = key_set.add_channel< assimp_key_p >( node->mNumPositionKeys ).data();
|
---|
| 571 | assimp_key_r* rchannel = key_set.add_channel< assimp_key_r >( node->mNumRotationKeys ).data();
|
---|
| 572 | // assimp_key_s* schannel = key_set.add_channel< assimp_key_s >( node->mNumScalingKeys ).data();
|
---|
[282] | 573 |
|
---|
[249] | 574 | for ( unsigned np = 0; np < node->mNumPositionKeys; ++np )
|
---|
| 575 | {
|
---|
[410] | 576 | pchannel[np].time = static_cast<float>( node->mPositionKeys[np].mTime );
|
---|
| 577 | pchannel[np].translation = assimp_vec3_cast(node->mPositionKeys[np].mValue);
|
---|
[249] | 578 | }
|
---|
| 579 | for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
|
---|
| 580 | {
|
---|
[406] | 581 | rchannel[np].time = static_cast<float>( node->mRotationKeys[np].mTime );
|
---|
[293] | 582 | rchannel[np].rotation = assimp_quat_cast(node->mRotationKeys[np].mValue );
|
---|
[249] | 583 | }
|
---|
[485] | 584 | */
|
---|
| 585 | data_channel_set* set = data_channel_set_creator::create_set( 1 );
|
---|
| 586 | data_channel_set_creator key_set( set );
|
---|
| 587 |
|
---|
| 588 | assimp_key_tr* channel = key_set.add_channel< assimp_key_tr >( node->mNumPositionKeys ).data();
|
---|
| 589 | for ( unsigned np = 0; np < node->mNumPositionKeys; ++np )
|
---|
| 590 | {
|
---|
| 591 | channel[np].tform.set_position( assimp_vec3_cast( node->mPositionKeys[np].mValue ) );
|
---|
| 592 | channel[np].tform.set_orientation( assimp_quat_cast( node->mRotationKeys[np].mValue ) );
|
---|
| 593 | if ( is_node_animated() )
|
---|
| 594 | channel[np].tform = tr.inverse() * channel[np].tform ;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[287] | 597 | // if ( node->mNumScalingKeys > 0 )
|
---|
| 598 | // {
|
---|
[451] | 599 | // vec3 scale_vec0 = assimp_vec3_cast( node->mScalingKeys[0].mValue );
|
---|
[454] | 600 | // float scale_value = math::length( math::abs( scale_vec0 - vec3(1,1,1) ) );
|
---|
[287] | 601 | // if ( node->mNumScalingKeys > 1 || scale_value > 0.001 )
|
---|
| 602 | // {
|
---|
| 603 | // NV_LOG( nv::LOG_WARNING, "scale key significant!" );
|
---|
| 604 | // for ( unsigned np = 0; np < node->mNumRotationKeys; ++np )
|
---|
| 605 | // {
|
---|
| 606 | // schannel[np].time = (float)node->mScalingKeys[np].mTime;
|
---|
| 607 | // schannel[np].scale = assimp_vec3_cast(node->mScalingKeys[np].mValue);
|
---|
| 608 | // }
|
---|
| 609 | // }
|
---|
| 610 | // else
|
---|
| 611 | // {
|
---|
| 612 | // schannel[0].time = (float)node->mScalingKeys[0].mTime;
|
---|
| 613 | // schannel[0].scale = assimp_vec3_cast(node->mScalingKeys[0].mValue);
|
---|
| 614 | // }
|
---|
| 615 | // }
|
---|
[427] | 616 | return set;
|
---|
[287] | 617 | }
|
---|
| 618 |
|
---|
[480] | 619 | // mesh_data_pack* nv::assimp_loader::release_mesh_data_pack()
|
---|
| 620 | // {
|
---|
| 621 | // if ( m_scene == nullptr || m_mesh_count == 0 ) return nullptr;
|
---|
| 622 | // const aiScene* scene = reinterpret_cast<const aiScene*>( m_scene );
|
---|
| 623 | // bool has_bones = false;
|
---|
| 624 | // data_channel_set* meshes = data_channel_set_creator::create_set_array( m_mesh_count, 2 );
|
---|
| 625 | // for ( size_t m = 0; m < m_mesh_count; ++m )
|
---|
| 626 | // {
|
---|
| 627 | // const aiMesh* mesh = scene->mMeshes[ m ];
|
---|
| 628 | // data_channel_set_creator( &meshes[m] ).set_name( make_name( static_cast<const char*>( mesh->mName.data ) ) );
|
---|
| 629 | // if ( mesh->mNumBones > 0 ) has_bones = true;
|
---|
| 630 | // load_mesh_data(&meshes[m],m);
|
---|
| 631 | // }
|
---|
| 632 | //
|
---|
| 633 | // mesh_nodes_data* nodes = ( has_bones ? release_merged_bones( meshes ) : release_mesh_nodes_data(0) );
|
---|
| 634 | // return new mesh_data_pack( m_mesh_count, meshes, nodes );
|
---|
| 635 | // }
|
---|
[291] | 636 |
|
---|
[376] | 637 | nv::size_t nv::assimp_loader::get_nodes_data_count() const
|
---|
[287] | 638 | {
|
---|
[485] | 639 | if ( m_data->scene == nullptr ) return 0;
|
---|
| 640 | return m_data->scene->mNumAnimations;
|
---|
[249] | 641 | }
|
---|
| 642 |
|
---|
[291] | 643 |
|
---|