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