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