diff --git a/CMakeLists.txt b/CMakeLists.txt index ade45f719..645d92689 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,7 +170,7 @@ IF(NOT IGNORE_GIT_HASH) # Get the latest abbreviated commit hash of the working branch EXECUTE_PROCESS( - COMMAND git log -1 --format=%h --no-show-signature + COMMAND git rev-parse --short=8 HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE GIT_COMMIT_HASH OUTPUT_STRIP_TRAILING_WHITESPACE diff --git a/code/3DSLoader.cpp b/code/3DSLoader.cpp index cd79b0a6d..34066e44b 100644 --- a/code/3DSLoader.cpp +++ b/code/3DSLoader.cpp @@ -161,19 +161,21 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) { StreamReaderLE stream(pIOHandler->Open(pFile,"rb")); - this->stream = &stream; // We should have at least one chunk if (stream.GetRemainingSize() < 16) { throw DeadlyImportError("3DS file is either empty or corrupt: " + pFile); } + this->stream = &stream; // Allocate our temporary 3DS representation - mScene = new D3DS::Scene(); + D3DS::Scene _scene; + mScene = &_scene; // Initialize members + D3DS::Node _rootNode("UNNAMED"); mLastNodeIndex = -1; - mCurrentNode = new D3DS::Node("UNNAMED"); + mCurrentNode = &_rootNode; mRootNode = mCurrentNode; mRootNode->mHierarchyPos = -1; mRootNode->mHierarchyIndex = -1; @@ -193,7 +195,6 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile, // file. for (auto &mesh : mScene->mMeshes) { if (mesh.mFaces.size() > 0 && mesh.mPositions.size() == 0) { - delete mScene; throw DeadlyImportError("3DS file contains faces but no vertices: " + pFile); } CheckIndices(mesh); @@ -201,7 +202,7 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile, ComputeNormalsWithSmoothingsGroups(mesh); } - // Replace all occurrences of the default material with a + // Replace all occurences of the default material with a // valid material. Generate it if no material containing // DEFAULT in its name has been found in the file ReplaceDefaultMaterial(); @@ -218,10 +219,8 @@ void Discreet3DSImporter::InternReadFile( const std::string& pFile, // Now apply the master scaling factor to the scene ApplyMasterScale(pScene); - // Delete our internal scene representation and the root - // node, so the whole hierarchy will follow - delete mRootNode; - delete mScene; + // Our internal scene representation and the root + // node will be automatically deleted, so the whole hierarchy will follow AI_DEBUG_INVALIDATE_PTR(mRootNode); AI_DEBUG_INVALIDATE_PTR(mScene); diff --git a/code/AssbinExporter.cpp b/code/AssbinExporter.cpp index d64b5c1de..f49389f78 100644 --- a/code/AssbinExporter.cpp +++ b/code/AssbinExporter.cpp @@ -89,7 +89,7 @@ size_t Write(IOStream * stream, const unsigned int& w) { const uint32_t t = (uint32_t)w; if (w > t) { // this shouldn't happen, integers in Assimp data structures never exceed 2^32 - throw new DeadlyExportError("loss of data due to 64 -> 32 bit integer conversion"); + throw DeadlyExportError("loss of data due to 64 -> 32 bit integer conversion"); } stream->Write(&t,4,1); @@ -805,10 +805,16 @@ public: WriteBinaryScene( &uncompressedStream, pScene ); uLongf uncompressedSize = static_cast(uncompressedStream.Tell()); - uLongf compressedSize = (uLongf)(uncompressedStream.Tell() * 1.001 + 12.); + uLongf compressedSize = (uLongf)compressBound(uncompressedSize); uint8_t* compressedBuffer = new uint8_t[ compressedSize ]; - compress2( compressedBuffer, &compressedSize, (const Bytef*)uncompressedStream.GetBufferPointer(), uncompressedSize, 9 ); + int res = compress2( compressedBuffer, &compressedSize, (const Bytef*)uncompressedStream.GetBufferPointer(), uncompressedSize, 9 ); + if(res != Z_OK) + { + delete [] compressedBuffer; + pIOSystem->Close(out); + throw DeadlyExportError("Compression failed."); + } out->Write( &uncompressedSize, sizeof(uint32_t), 1 ); out->Write( compressedBuffer, sizeof(char), compressedSize ); diff --git a/code/AssbinLoader.cpp b/code/AssbinLoader.cpp index 81c77f3fa..321a0cfec 100644 --- a/code/AssbinLoader.cpp +++ b/code/AssbinLoader.cpp @@ -57,6 +57,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #ifdef ASSIMP_BUILD_NO_OWN_ZLIB # include @@ -103,7 +104,9 @@ bool AssbinImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bo template T Read(IOStream * stream) { T t; - stream->Read( &t, sizeof(T), 1 ); + size_t res = stream->Read( &t, sizeof(T), 1 ); + if(res != 1) + throw DeadlyImportError("Unexpected EOF"); return t; } @@ -144,7 +147,8 @@ template <> aiString Read(IOStream * stream) { aiString s; stream->Read(&s.length,4,1); - stream->Read(s.data,s.length,1); + if(s.length) + stream->Read(s.data,s.length,1); s.data[s.length] = 0; return s; } @@ -207,46 +211,48 @@ void ReadBounds( IOStream * stream, T* /*p*/, unsigned int n ) { } // ----------------------------------------------------------------------------------- -void AssbinImporter::ReadBinaryNode( IOStream * stream, aiNode** node, aiNode* parent ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AINODE); +void AssbinImporter::ReadBinaryNode( IOStream * stream, aiNode** onode, aiNode* parent ) { + if(Read(stream) != ASSBIN_CHUNK_AINODE) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); - *node = new aiNode(); + std::unique_ptr node(new aiNode()); - (*node)->mName = Read(stream); - (*node)->mTransformation = Read(stream); - (*node)->mNumChildren = Read(stream); - (*node)->mNumMeshes = Read(stream); + node->mName = Read(stream); + node->mTransformation = Read(stream); + unsigned numChildren = Read(stream); + unsigned numMeshes = Read(stream); unsigned int nb_metadata = Read(stream); if(parent) { - (*node)->mParent = parent; + node->mParent = parent; } - if ((*node)->mNumMeshes) { - (*node)->mMeshes = new unsigned int[(*node)->mNumMeshes]; - for (unsigned int i = 0; i < (*node)->mNumMeshes; ++i) { - (*node)->mMeshes[i] = Read(stream); + if (numMeshes) + { + node->mMeshes = new unsigned int[numMeshes]; + for (unsigned int i = 0; i < numMeshes; ++i) { + node->mMeshes[i] = Read(stream); + node->mNumMeshes++; } } - if ((*node)->mNumChildren) { - (*node)->mChildren = new aiNode*[(*node)->mNumChildren]; - for (unsigned int i = 0; i < (*node)->mNumChildren; ++i) { - ReadBinaryNode( stream, &(*node)->mChildren[i], *node ); + if (numChildren) { + node->mChildren = new aiNode*[numChildren]; + for (unsigned int i = 0; i < numChildren; ++i) { + ReadBinaryNode( stream, &node->mChildren[i], node.get() ); + node->mNumChildren++; } } if ( nb_metadata > 0 ) { - (*node)->mMetaData = aiMetadata::Alloc(nb_metadata); + node->mMetaData = aiMetadata::Alloc(nb_metadata); for (unsigned int i = 0; i < nb_metadata; ++i) { - (*node)->mMetaData->mKeys[i] = Read(stream); - (*node)->mMetaData->mValues[i].mType = (aiMetadataType) Read(stream); - void* data( nullptr ); + node->mMetaData->mKeys[i] = Read(stream); + node->mMetaData->mValues[i].mType = (aiMetadataType) Read(stream); + void* data = nullptr; - switch ((*node)->mMetaData->mValues[i].mType) { + switch (node->mMetaData->mValues[i].mType) { case AI_BOOL: data = new bool(Read(stream)); break; @@ -275,16 +281,16 @@ void AssbinImporter::ReadBinaryNode( IOStream * stream, aiNode** node, aiNode* p break; } - (*node)->mMetaData->mValues[i].mData = data; + node->mMetaData->mValues[i].mData = data; } } + *onode = node.release(); } // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryBone( IOStream * stream, aiBone* b ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AIBONE); + if(Read(stream) != ASSBIN_CHUNK_AIBONE) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); b->mName = Read(stream); @@ -306,12 +312,10 @@ void AssbinImporter::ReadBinaryBone( IOStream * stream, aiBone* b ) { static bool fitsIntoUI16(unsigned int mNumVertices) { return ( mNumVertices < (1u<<16) ); } - // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AIMESH); + if(Read(stream) != ASSBIN_CHUNK_AIMESH) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); mesh->mPrimitiveTypes = Read(stream); @@ -423,9 +427,8 @@ void AssbinImporter::ReadBinaryMesh( IOStream * stream, aiMesh* mesh ) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryMaterialProperty(IOStream * stream, aiMaterialProperty* prop) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AIMATERIALPROPERTY); + if(Read(stream) != ASSBIN_CHUNK_AIMATERIALPROPERTY) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); prop->mKey = Read(stream); @@ -440,9 +443,8 @@ void AssbinImporter::ReadBinaryMaterialProperty(IOStream * stream, aiMaterialPro // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryMaterial(IOStream * stream, aiMaterial* mat) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AIMATERIAL); + if(Read(stream) != ASSBIN_CHUNK_AIMATERIAL) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); mat->mNumAllocated = mat->mNumProperties = Read(stream); @@ -462,9 +464,8 @@ void AssbinImporter::ReadBinaryMaterial(IOStream * stream, aiMaterial* mat) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AINODEANIM); + if(Read(stream) != ASSBIN_CHUNK_AINODEANIM) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); nd->mNodeName = Read(stream); @@ -508,9 +509,8 @@ void AssbinImporter::ReadBinaryNodeAnim(IOStream * stream, aiNodeAnim* nd) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryAnim( IOStream * stream, aiAnimation* anim ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AIANIMATION); + if(Read(stream) != ASSBIN_CHUNK_AIANIMATION) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); anim->mName = Read (stream); @@ -529,9 +529,8 @@ void AssbinImporter::ReadBinaryAnim( IOStream * stream, aiAnimation* anim ) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryTexture(IOStream * stream, aiTexture* tex) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AITEXTURE); + if(Read(stream) != ASSBIN_CHUNK_AITEXTURE) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); tex->mWidth = Read(stream); @@ -551,9 +550,8 @@ void AssbinImporter::ReadBinaryTexture(IOStream * stream, aiTexture* tex) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryLight( IOStream * stream, aiLight* l ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AILIGHT); + if(Read(stream) != ASSBIN_CHUNK_AILIGHT) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); l->mName = Read(stream); @@ -577,9 +575,8 @@ void AssbinImporter::ReadBinaryLight( IOStream * stream, aiLight* l ) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryCamera( IOStream * stream, aiCamera* cam ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AICAMERA); + if(Read(stream) != ASSBIN_CHUNK_AICAMERA) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); cam->mName = Read(stream); @@ -594,9 +591,8 @@ void AssbinImporter::ReadBinaryCamera( IOStream * stream, aiCamera* cam ) { // ----------------------------------------------------------------------------------- void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { - uint32_t chunkID = Read(stream); - (void)(chunkID); - ai_assert(chunkID == ASSBIN_CHUNK_AISCENE); + if(Read(stream) != ASSBIN_CHUNK_AISCENE) + throw DeadlyImportError("Magic chunk identifiers are wrong!"); /*uint32_t size =*/ Read(stream); scene->mFlags = Read(stream); @@ -614,6 +610,7 @@ void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { // Read all meshes if (scene->mNumMeshes) { scene->mMeshes = new aiMesh*[scene->mNumMeshes]; + memset(scene->mMeshes, 0, scene->mNumMeshes*sizeof(aiMesh*)); for (unsigned int i = 0; i < scene->mNumMeshes;++i) { scene->mMeshes[i] = new aiMesh(); ReadBinaryMesh( stream,scene->mMeshes[i]); @@ -623,6 +620,7 @@ void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { // Read materials if (scene->mNumMaterials) { scene->mMaterials = new aiMaterial*[scene->mNumMaterials]; + memset(scene->mMaterials, 0, scene->mNumMaterials*sizeof(aiMaterial*)); for (unsigned int i = 0; i< scene->mNumMaterials; ++i) { scene->mMaterials[i] = new aiMaterial(); ReadBinaryMaterial(stream,scene->mMaterials[i]); @@ -632,6 +630,7 @@ void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { // Read all animations if (scene->mNumAnimations) { scene->mAnimations = new aiAnimation*[scene->mNumAnimations]; + memset(scene->mAnimations, 0, scene->mNumAnimations*sizeof(aiAnimation*)); for (unsigned int i = 0; i < scene->mNumAnimations;++i) { scene->mAnimations[i] = new aiAnimation(); ReadBinaryAnim(stream,scene->mAnimations[i]); @@ -641,6 +640,7 @@ void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { // Read all textures if (scene->mNumTextures) { scene->mTextures = new aiTexture*[scene->mNumTextures]; + memset(scene->mTextures, 0, scene->mNumTextures*sizeof(aiTexture*)); for (unsigned int i = 0; i < scene->mNumTextures;++i) { scene->mTextures[i] = new aiTexture(); ReadBinaryTexture(stream,scene->mTextures[i]); @@ -650,6 +650,7 @@ void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { // Read lights if (scene->mNumLights) { scene->mLights = new aiLight*[scene->mNumLights]; + memset(scene->mLights, 0, scene->mNumLights*sizeof(aiLight*)); for (unsigned int i = 0; i < scene->mNumLights;++i) { scene->mLights[i] = new aiLight(); ReadBinaryLight(stream,scene->mLights[i]); @@ -659,6 +660,7 @@ void AssbinImporter::ReadBinaryScene( IOStream * stream, aiScene* scene ) { // Read cameras if (scene->mNumCameras) { scene->mCameras = new aiCamera*[scene->mNumCameras]; + memset(scene->mCameras, 0, scene->mNumCameras*sizeof(aiCamera*)); for (unsigned int i = 0; i < scene->mNumCameras;++i) { scene->mCameras[i] = new aiCamera(); ReadBinaryCamera(stream,scene->mCameras[i]); @@ -675,7 +677,7 @@ void AssbinImporter::InternReadFile( const std::string& pFile, aiScene* pScene, } // signature - stream->Seek( 44, aiOrigin_CUR ); + stream->Seek( 44, aiOrigin_CUR ); unsigned int versionMajor = Read(stream); unsigned int versionMinor = Read(stream); @@ -701,11 +703,19 @@ void AssbinImporter::InternReadFile( const std::string& pFile, aiScene* pScene, uLongf compressedSize = static_cast(stream->FileSize() - stream->Tell()); unsigned char * compressedData = new unsigned char[ compressedSize ]; - stream->Read( compressedData, 1, compressedSize ); + size_t len = stream->Read( compressedData, 1, compressedSize ); + ai_assert(len == compressedSize); unsigned char * uncompressedData = new unsigned char[ uncompressedSize ]; - uncompress( uncompressedData, &uncompressedSize, compressedData, compressedSize ); + int res = uncompress( uncompressedData, &uncompressedSize, compressedData, len ); + if(res != Z_OK) + { + delete [] uncompressedData; + delete [] compressedData; + pIOHandler->Close(stream); + throw DeadlyImportError("Zlib decompression failed."); + } MemoryIOStream io( uncompressedData, uncompressedSize ); diff --git a/code/MS3DLoader.cpp b/code/MS3DLoader.cpp index b06bea31c..c659d2ec7 100644 --- a/code/MS3DLoader.cpp +++ b/code/MS3DLoader.cpp @@ -181,8 +181,7 @@ void MS3DImporter :: CollectChildJoints(const std::vector& joints, ch->mParent = nd; ch->mTransformation = aiMatrix4x4::Translation(joints[i].position,aiMatrix4x4()=aiMatrix4x4())* - // XXX actually, I don't *know* why we need the inverse here. Probably column vs. row order? - aiMatrix4x4().FromEulerAnglesXYZ(joints[i].rotation).Transpose(); + aiMatrix4x4().FromEulerAnglesXYZ(joints[i].rotation); const aiMatrix4x4 abs = absTrafo*ch->mTransformation; for(unsigned int a = 0; a < mScene->mNumMeshes; ++a) { @@ -639,11 +638,8 @@ void MS3DImporter::InternReadFile( const std::string& pFile, aiQuatKey& q = nd->mRotationKeys[nd->mNumRotationKeys++]; q.mTime = (*rot).time*animfps; - - // XXX it seems our matrix&quaternion code has faults in its conversion routines -- - // aiQuaternion(x,y,z) seems to besomething different as quat(matrix.fromeuler(x,y,z)). - q.mValue = aiQuaternion(aiMatrix3x3(aiMatrix4x4().FromEulerAnglesXYZ((*rot).value)* - aiMatrix4x4().FromEulerAnglesXYZ((*it).rotation)).Transpose()); + q.mValue = aiQuaternion(aiMatrix3x3(aiMatrix4x4().FromEulerAnglesXYZ((*it).rotation)* + aiMatrix4x4().FromEulerAnglesXYZ((*rot).value))); } } diff --git a/code/SMDLoader.cpp b/code/SMDLoader.cpp index dc6706934..c6ec61624 100644 --- a/code/SMDLoader.cpp +++ b/code/SMDLoader.cpp @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2018, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -48,8 +46,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef ASSIMP_BUILD_NO_SMD_IMPORTER -// internal headers -#include "SMDLoader.h" #include #include #include @@ -58,6 +54,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include + +// internal headers +#include "SMDLoader.h" + +#ifndef _WIN32 +#define strtok_s strtok_r +#endif using namespace Assimp; @@ -77,14 +82,14 @@ static const aiImporterDesc desc = { // ------------------------------------------------------------------------------------------------ // Constructor to be privately used by Importer SMDImporter::SMDImporter() -: configFrameID(), -mBuffer(), -pScene( nullptr ), -iFileSize( 0 ), -iSmallestFrame( -1 ), -dLengthOfAnim( 0.0 ), -bHasUVs(false ), -iLineNumber(-1) { +: configFrameID() +, mBuffer() +, pScene( nullptr ) +, iFileSize( 0 ) +, iSmallestFrame( INT_MAX ) +, dLengthOfAnim( 0.0 ) +, bHasUVs(false ) +, iLineNumber(-1) { // empty } @@ -96,23 +101,20 @@ SMDImporter::~SMDImporter() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool SMDImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool) const -{ +bool SMDImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool) const { // fixme: auto format detection return SimpleExtensionCheck(pFile,"smd","vta"); } // ------------------------------------------------------------------------------------------------ // Get a list of all supported file extensions -const aiImporterDesc* SMDImporter::GetInfo () const -{ +const aiImporterDesc* SMDImporter::GetInfo () const { return &desc; } // ------------------------------------------------------------------------------------------------ // Setup configuration properties -void SMDImporter::SetupProperties(const Importer* pImp) -{ +void SMDImporter::SetupProperties(const Importer* pImp) { // The // AI_CONFIG_IMPORT_SMD_KEYFRAME option overrides the // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option. @@ -120,50 +122,21 @@ void SMDImporter::SetupProperties(const Importer* pImp) if(static_cast(-1) == configFrameID) { configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0); } + + bLoadAnimationList = pImp->GetPropertyBool(AI_CONFIG_IMPORT_SMD_LOAD_ANIMATION_LIST, true); + noSkeletonMesh = pImp->GetPropertyBool(AI_CONFIG_IMPORT_NO_SKELETON_MESHES, false); } // ------------------------------------------------------------------------------------------------ // Imports the given file into the given scene structure. -void SMDImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) -{ - std::unique_ptr file( pIOHandler->Open( pFile, "rb")); - - // Check whether we can read from the file - if( file.get() == NULL) { - throw DeadlyImportError( "Failed to open SMD/VTA file " + pFile + "."); - } - - iFileSize = (unsigned int)file->FileSize(); - - // Allocate storage and copy the contents of the file to a memory buffer +void SMDImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) { this->pScene = pScene; - - mBuffer.resize( iFileSize + 1 ); - TextFileToBuffer(file.get(), mBuffer ); - - iSmallestFrame = (1 << 31); - bHasUVs = true; - iLineNumber = 1; - - // Reserve enough space for ... hm ... 10 textures - aszTextures.reserve(10); - - // Reserve enough space for ... hm ... 1000 triangles - asTriangles.reserve(1000); - - // Reserve enough space for ... hm ... 20 bones - asBones.reserve(20); - - - // parse the file ... - ParseFile(); + ReadSmd(pFile, pIOHandler); // If there are no triangles it seems to be an animation SMD, // containing only the animation skeleton. - if (asTriangles.empty()) - { - if (asBones.empty()) - { + if (asTriangles.empty()) { + if (asBones.empty()) { throw DeadlyImportError("SMD: No triangles and no bones have " "been found in the file. This file seems to be invalid."); } @@ -173,15 +146,12 @@ void SMDImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE; } - if (!asBones.empty()) - { + if (!asBones.empty()) { // Check whether all bones have been initialized for (std::vector::const_iterator - i = asBones.begin(); - i != asBones.end();++i) - { - if (!(*i).mName.length()) - { + i = asBones.begin(); + i != asBones.end();++i) { + if (!(*i).mName.length()) { ASSIMP_LOG_WARN("SMD: Not all bones have been initialized"); break; } @@ -189,64 +159,64 @@ void SMDImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS // now fix invalid time values and make sure the animation starts at frame 0 FixTimeValues(); - - // compute absolute bone transformation matrices - // ComputeAbsoluteBoneTransformations(); } - if (!(pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)) - { + // build output nodes (bones are added as empty dummy nodes) + CreateOutputNodes(); + + if (!(pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)) { // create output meshes CreateOutputMeshes(); // build an output material list CreateOutputMaterials(); + + // use root node that renders all meshes + pScene->mRootNode->mNumMeshes = pScene->mNumMeshes; + pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes]; + for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { + pScene->mRootNode->mMeshes[i] = i; + } } // build the output animation - CreateOutputAnimations(); + CreateOutputAnimations(pFile, pIOHandler); - // build output nodes (bones are added as empty dummy nodes) - CreateOutputNodes(); - - if (pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) - { + if ((pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) && !noSkeletonMesh) { SkeletonMeshBuilder skeleton(pScene); } } + // ------------------------------------------------------------------------------------------------ // Write an error message with line number to the log file -void SMDImporter::LogErrorNoThrow(const char* msg) -{ - char szTemp[1024]; - ai_snprintf(szTemp,1024,"Line %u: %s",iLineNumber,msg); +void SMDImporter::LogErrorNoThrow(const char* msg) { + const size_t BufferSize = 1024; + char szTemp[BufferSize]; + ai_snprintf(szTemp,BufferSize,"Line %u: %s",iLineNumber,msg); DefaultLogger::get()->error(szTemp); } // ------------------------------------------------------------------------------------------------ // Write a warning with line number to the log file -void SMDImporter::LogWarning(const char* msg) -{ - char szTemp[1024]; +void SMDImporter::LogWarning(const char* msg) { + const size_t BufferSize = 1024; + char szTemp[BufferSize]; ai_assert(strlen(msg) < 1000); - ai_snprintf(szTemp,1024,"Line %u: %s",iLineNumber,msg); + ai_snprintf(szTemp,BufferSize,"Line %u: %s",iLineNumber,msg); ASSIMP_LOG_WARN(szTemp); } // ------------------------------------------------------------------------------------------------ // Fix invalid time values in the file -void SMDImporter::FixTimeValues() -{ +void SMDImporter::FixTimeValues() { double dDelta = (double)iSmallestFrame; double dMax = 0.0f; for (std::vector::iterator - iBone = asBones.begin(); - iBone != asBones.end();++iBone) - { + iBone = asBones.begin(); + iBone != asBones.end();++iBone) { for (std::vector::iterator - iKey = (*iBone).sAnim.asKeys.begin(); - iKey != (*iBone).sAnim.asKeys.end();++iKey) - { + iKey = (*iBone).sAnim.asKeys.begin(); + iKey != (*iBone).sAnim.asKeys.end();++iKey) { (*iKey).dTime -= dDelta; dMax = std::max(dMax, (*iKey).dTime); } @@ -256,10 +226,10 @@ void SMDImporter::FixTimeValues() // ------------------------------------------------------------------------------------------------ // create output meshes -void SMDImporter::CreateOutputMeshes() -{ - if (aszTextures.empty()) +void SMDImporter::CreateOutputMeshes() { + if (aszTextures.empty()) { aszTextures.push_back(std::string()); + } // we need to sort all faces by their material index // in opposition to other loaders we can be sure that each @@ -273,28 +243,27 @@ void SMDImporter::CreateOutputMeshes() // approximate the space that will be required unsigned int iNum = (unsigned int)asTriangles.size() / pScene->mNumMeshes; iNum += iNum >> 1; - for (unsigned int i = 0; i < pScene->mNumMeshes;++i) + for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { aaiFaces[i].reserve(iNum); - + } // collect all faces iNum = 0; for (std::vector::const_iterator - iFace = asTriangles.begin(); - iFace != asTriangles.end();++iFace,++iNum) - { - if (UINT_MAX == (*iFace).iTexture)aaiFaces[(*iFace).iTexture].push_back( 0 ); - else if ((*iFace).iTexture >= aszTextures.size()) - { + iFace = asTriangles.begin(); + iFace != asTriangles.end();++iFace,++iNum) { + if (UINT_MAX == (*iFace).iTexture) { + aaiFaces[(*iFace).iTexture].push_back( 0 ); + } else if ((*iFace).iTexture >= aszTextures.size()) { ASSIMP_LOG_INFO("[SMD/VTA] Material index overflow in face"); aaiFaces[(*iFace).iTexture].push_back((unsigned int)aszTextures.size()-1); + } else { + aaiFaces[(*iFace).iTexture].push_back(iNum); } - else aaiFaces[(*iFace).iTexture].push_back(iNum); } // now create the output meshes - for (unsigned int i = 0; i < pScene->mNumMeshes;++i) - { + for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { aiMesh*& pcMesh = pScene->mMeshes[i] = new aiMesh(); ai_assert(!aaiFaces[i].empty()); // should not be empty ... @@ -310,8 +279,7 @@ void SMDImporter::CreateOutputMeshes() TempBoneWeightList* aaiBones = new TempBoneWeightList[asBones.size()](); // try to reserve enough memory without wasting too much - for (unsigned int iBone = 0; iBone < asBones.size();++iBone) - { + for (unsigned int iBone = 0; iBone < asBones.size();++iBone) { aaiBones[iBone].reserve(pcMesh->mNumVertices/asBones.size()); } @@ -320,16 +288,14 @@ void SMDImporter::CreateOutputMeshes() aiVector3D* pcNormals = pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices]; aiVector3D* pcVerts = pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices]; - aiVector3D* pcUVs = NULL; - if (bHasUVs) - { + aiVector3D* pcUVs = nullptr; + if (bHasUVs) { pcUVs = pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices]; pcMesh->mNumUVComponents[0] = 2; } iNum = 0; - for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace) - { + for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace) { pcMesh->mFaces[iFace].mIndices = new unsigned int[3]; pcMesh->mFaces[iFace].mNumIndices = 3; @@ -347,25 +313,20 @@ void SMDImporter::CreateOutputMeshes() *pcNormals++ = face.avVertices[2].nor; // fill the texture coordinates - if (pcUVs) - { + if (pcUVs) { *pcUVs++ = face.avVertices[0].uv; *pcUVs++ = face.avVertices[1].uv; *pcUVs++ = face.avVertices[2].uv; } - for (unsigned int iVert = 0; iVert < 3;++iVert) - { + for (unsigned int iVert = 0; iVert < 3;++iVert) { float fSum = 0.0f; - for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone) - { + for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone) { TempWeightListEntry& pairval = face.avVertices[iVert].aiBoneLinks[iBone]; // FIX: The second check is here just to make sure we won't // assign more than one weight to a single vertex index - if (pairval.first >= asBones.size() || - pairval.first == face.avVertices[iVert].iParentNode) - { + if (pairval.first >= asBones.size() || pairval.first == face.avVertices[iVert].iParentNode) { ASSIMP_LOG_ERROR("[SMD/VTA] Bone index overflow. " "The bone index will be ignored, the weight will be assigned " "to the vertex' parent node"); @@ -383,27 +344,23 @@ void SMDImporter::CreateOutputMeshes() // that the parent of a vertex is 0xffffffff (if the corresponding // entry in the file was unreadable) // ****************************************************************** - if (fSum < 0.975f && face.avVertices[iVert].iParentNode != UINT_MAX) - { - if (face.avVertices[iVert].iParentNode >= asBones.size()) - { + if (fSum < 0.975f && face.avVertices[iVert].iParentNode != UINT_MAX) { + if (face.avVertices[iVert].iParentNode >= asBones.size()) { ASSIMP_LOG_ERROR("[SMD/VTA] Bone index overflow. " "The index of the vertex parent bone is invalid. " "The remaining weights will be normalized to 1.0"); - if (fSum) - { + if (fSum) { fSum = 1 / fSum; - for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone) - { + for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone) { TempWeightListEntry& pairval = face.avVertices[iVert].aiBoneLinks[iBone]; - if (pairval.first >= asBones.size())continue; + if (pairval.first >= asBones.size()) { + continue; + } aaiBones[pairval.first].back().second *= fSum; } } - } - else - { + } else { aaiBones[face.avVertices[iVert].iParentNode].push_back( TempWeightListEntry(iNum,1.0f-fSum)); } @@ -414,17 +371,18 @@ void SMDImporter::CreateOutputMeshes() // now build all bones of the mesh iNum = 0; - for (unsigned int iBone = 0; iBone < asBones.size();++iBone) + for (unsigned int iBone = 0; iBone < asBones.size();++iBone) { if (!aaiBones[iBone].empty())++iNum; + } - if (false && iNum) - { + if (iNum) { pcMesh->mNumBones = iNum; pcMesh->mBones = new aiBone*[pcMesh->mNumBones]; iNum = 0; - for (unsigned int iBone = 0; iBone < asBones.size();++iBone) - { - if (aaiBones[iBone].empty())continue; + for (unsigned int iBone = 0; iBone < asBones.size();++iBone) { + if (aaiBones[iBone].empty()) { + continue; + } aiBone*& bone = pcMesh->mBones[iNum] = new aiBone(); bone->mNumWeights = (unsigned int)aaiBones[iBone].size(); @@ -434,8 +392,7 @@ void SMDImporter::CreateOutputMeshes() asBones[iBone].bIsUsed = true; - for (unsigned int iWeight = 0; iWeight < bone->mNumWeights;++iWeight) - { + for (unsigned int iWeight = 0; iWeight < bone->mNumWeights;++iWeight) { bone->mWeights[iWeight].mVertexId = aaiBones[iBone][iWeight].first; bone->mWeights[iWeight].mWeight = aaiBones[iBone][iWeight].second; } @@ -449,34 +406,44 @@ void SMDImporter::CreateOutputMeshes() // ------------------------------------------------------------------------------------------------ // add bone child nodes -void SMDImporter::AddBoneChildren(aiNode* pcNode, uint32_t iParent) -{ - ai_assert( NULL != pcNode ); +void SMDImporter::AddBoneChildren(aiNode* pcNode, uint32_t iParent) { + ai_assert( nullptr != pcNode ); ai_assert( 0 == pcNode->mNumChildren ); - ai_assert( NULL == pcNode->mChildren); + ai_assert( nullptr == pcNode->mChildren); // first count ... - for (unsigned int i = 0; i < asBones.size();++i) - { + for (unsigned int i = 0; i < asBones.size();++i) { SMD::Bone& bone = asBones[i]; - if (bone.iParent == iParent)++pcNode->mNumChildren; + if (bone.iParent == iParent) { + ++pcNode->mNumChildren; + } } // now allocate the output array pcNode->mChildren = new aiNode*[pcNode->mNumChildren]; // and fill all subnodes - unsigned int qq = 0; - for (unsigned int i = 0; i < asBones.size();++i) - { + unsigned int qq( 0 ); + for (unsigned int i = 0; i < asBones.size();++i) { SMD::Bone& bone = asBones[i]; - if (bone.iParent != iParent)continue; + if (bone.iParent != iParent) { + continue; + } aiNode* pc = pcNode->mChildren[qq++] = new aiNode(); pc->mName.Set(bone.mName); // store the local transformation matrix of the bind pose - pc->mTransformation = bone.sAnim.asKeys[bone.sAnim.iFirstTimeKey].matrix; + if (bone.sAnim.asKeys.size()) { + pc->mTransformation = bone.sAnim.asKeys[0].matrix; + } + + if (bone.iParent == static_cast(-1)) { + bone.mOffsetMatrix = pc->mTransformation; + } else { + bone.mOffsetMatrix = asBones[bone.iParent].mOffsetMatrix * pc->mTransformation; + } + pc->mParent = pcNode; // add children to this node, too @@ -486,31 +453,23 @@ void SMDImporter::AddBoneChildren(aiNode* pcNode, uint32_t iParent) // ------------------------------------------------------------------------------------------------ // create output nodes -void SMDImporter::CreateOutputNodes() -{ +void SMDImporter::CreateOutputNodes() { pScene->mRootNode = new aiNode(); - if (!(pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)) - { - // create one root node that renders all meshes - pScene->mRootNode->mNumMeshes = pScene->mNumMeshes; - pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes]; - for (unsigned int i = 0; i < pScene->mNumMeshes;++i) - pScene->mRootNode->mMeshes[i] = i; - } // now add all bones as dummy sub nodes to the graph - // AddBoneChildren(pScene->mRootNode,(uint32_t)-1); + AddBoneChildren(pScene->mRootNode,(uint32_t)-1); + for (auto &bone : asBones) { + bone.mOffsetMatrix.Inverse(); + } // if we have only one bone we can even remove the root node - if (pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE && - 1 == pScene->mRootNode->mNumChildren) - { + if (pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE && 1 == pScene->mRootNode->mNumChildren) { aiNode* pcOldRoot = pScene->mRootNode; pScene->mRootNode = pcOldRoot->mChildren[0]; - pcOldRoot->mChildren[0] = NULL; + pcOldRoot->mChildren[0] = nullptr; delete pcOldRoot; - pScene->mRootNode->mParent = NULL; + pScene->mRootNode->mParent = nullptr; } else { @@ -521,60 +480,63 @@ void SMDImporter::CreateOutputNodes() // ------------------------------------------------------------------------------------------------ // create output animations -void SMDImporter::CreateOutputAnimations() -{ - unsigned int iNumBones = 0; - for (std::vector::const_iterator - i = asBones.begin(); - i != asBones.end();++i) - { - if ((*i).bIsUsed)++iNumBones; - } - if (!iNumBones) - { - // just make sure this case doesn't occur ... (it could occur - // if the file was invalid) - return; - } +void SMDImporter::CreateOutputAnimations(const std::string &pFile, IOSystem* pIOHandler) { + std::vector> animFileList; + if (bLoadAnimationList) { + GetAnimationFileList(pFile, pIOHandler, animFileList); + } + int animCount = animFileList.size() + 1; pScene->mNumAnimations = 1; - pScene->mAnimations = new aiAnimation*[1]; - aiAnimation*& anim = pScene->mAnimations[0] = new aiAnimation(); + pScene->mAnimations = new aiAnimation*[animCount]; + memset(pScene->mAnimations, 0, sizeof(aiAnimation*)*animCount); + CreateOutputAnimation(0, ""); + for (auto &animFile : animFileList) { + ReadSmd(std::get<1>(animFile), pIOHandler); + if (asBones.empty()) { + continue; + } + + FixTimeValues(); + CreateOutputAnimation(pScene->mNumAnimations++, std::get<0>(animFile)); + } +} + +void SMDImporter::CreateOutputAnimation(int index, const std::string &name) { + aiAnimation*& anim = pScene->mAnimations[index] = new aiAnimation(); + + if (name.length()) { + anim->mName.Set(name.c_str()); + } anim->mDuration = dLengthOfAnim; - anim->mNumChannels = iNumBones; + anim->mNumChannels = asBones.size(); anim->mTicksPerSecond = 25.0; // FIXME: is this correct? aiNodeAnim** pp = anim->mChannels = new aiNodeAnim*[anim->mNumChannels]; // now build valid keys unsigned int a = 0; - for (std::vector::const_iterator - i = asBones.begin(); - i != asBones.end();++i) - { - if (!(*i).bIsUsed)continue; - + for (std::vector::const_iterator i = asBones.begin(); i != asBones.end(); ++i) { aiNodeAnim* p = pp[a] = new aiNodeAnim(); // copy the name of the bone - p->mNodeName.Set( i->mName); + p->mNodeName.Set(i->mName); - p->mNumRotationKeys = (unsigned int) (*i).sAnim.asKeys.size(); - if (p->mNumRotationKeys) - { + p->mNumRotationKeys = (unsigned int)(*i).sAnim.asKeys.size(); + if (p->mNumRotationKeys){ p->mNumPositionKeys = p->mNumRotationKeys; aiVectorKey* pVecKeys = p->mPositionKeys = new aiVectorKey[p->mNumRotationKeys]; aiQuatKey* pRotKeys = p->mRotationKeys = new aiQuatKey[p->mNumRotationKeys]; for (std::vector::const_iterator - qq = (*i).sAnim.asKeys.begin(); - qq != (*i).sAnim.asKeys.end(); ++qq) - { + qq = (*i).sAnim.asKeys.begin(); + qq != (*i).sAnim.asKeys.end(); ++qq) { pRotKeys->mTime = pVecKeys->mTime = (*qq).dTime; // compute the rotation quaternion from the euler angles - pRotKeys->mValue = aiQuaternion( (*qq).vRot.x, (*qq).vRot.y, (*qq).vRot.z ); + // aiQuaternion: The order of the parameters is yzx? + pRotKeys->mValue = aiQuaternion((*qq).vRot.y, (*qq).vRot.z, (*qq).vRot.x); pVecKeys->mValue = (*qq).vPos; ++pVecKeys; ++pRotKeys; @@ -586,71 +548,57 @@ void SMDImporter::CreateOutputAnimations() } } -// ------------------------------------------------------------------------------------------------ -void SMDImporter::ComputeAbsoluteBoneTransformations() -{ - // For each bone: determine the key with the lowest time value - // theoretically the SMD format should have all keyframes - // in order. However, I've seen a file where this wasn't true. - for (unsigned int i = 0; i < asBones.size();++i) - { - SMD::Bone& bone = asBones[i]; +void SMDImporter::GetAnimationFileList(const std::string &pFile, IOSystem* pIOHandler, std::vector>& outList) { + auto base = DefaultIOSystem::absolutePath(pFile); + auto name = DefaultIOSystem::completeBaseName(pFile); + auto path = base + "/" + name + "_animation.txt"; - uint32_t iIndex = 0; - double dMin = 10e10; - for (unsigned int i = 0; i < bone.sAnim.asKeys.size();++i) - { - double d = std::min(bone.sAnim.asKeys[i].dTime,dMin); - if (d < dMin) - { - dMin = d; - iIndex = i; - } - } - bone.sAnim.iFirstTimeKey = iIndex; + std::unique_ptr file(pIOHandler->Open(path.c_str(), "rb")); + if (file.get() == nullptr) { + return; } - unsigned int iParent = 0; - while (iParent < asBones.size()) - { - for (unsigned int iBone = 0; iBone < asBones.size();++iBone) - { - SMD::Bone& bone = asBones[iBone]; + // Allocate storage and copy the contents of the file to a memory buffer + std::vector buf; + size_t fileSize = file->FileSize(); + buf.resize(fileSize + 1); + TextFileToBuffer(file.get(), buf); - if (iParent == bone.iParent) - { - SMD::Bone& parentBone = asBones[iParent]; + /* + *_animation.txt format: + name path + idle idle.smd + jump anim/jump.smd + walk.smd + ... + */ + std::string animName, animPath; + char *tok1, *tok2; + char *context1, *context2; - - uint32_t iIndex = bone.sAnim.iFirstTimeKey; - const aiMatrix4x4& mat = bone.sAnim.asKeys[iIndex].matrix; - aiMatrix4x4& matOut = bone.sAnim.asKeys[iIndex].matrixAbsolute; - - // The same for the parent bone ... - iIndex = parentBone.sAnim.iFirstTimeKey; - const aiMatrix4x4& mat2 = parentBone.sAnim.asKeys[iIndex].matrixAbsolute; - - // Compute the absolute transformation matrix - matOut = mat * mat2; + tok1 = strtok_s(&buf[0], "\r\n", &context1); + while (tok1 != NULL) { + tok2 = strtok_s(tok1, " \t", &context2); + if (tok2) { + char *p = tok2; + tok2 = strtok_s(nullptr, " \t", &context2); + if (tok2) { + animPath = tok2; + animName = p; + } else { + // No name + animPath = p; + animName = DefaultIOSystem::completeBaseName(animPath); } + outList.push_back(std::make_tuple(animName, base + "/" + animPath)); } - ++iParent; - } - - // Store the inverse of the absolute transformation matrix - // of the first key as bone offset matrix - for (iParent = 0; iParent < asBones.size();++iParent) - { - SMD::Bone& bone = asBones[iParent]; - bone.mOffsetMatrix = bone.sAnim.asKeys[bone.sAnim.iFirstTimeKey].matrixAbsolute; - bone.mOffsetMatrix.Inverse(); + tok1 = strtok_s(nullptr, "\r\n", &context1); } } -\ + // ------------------------------------------------------------------------------------------------ // create output materials -void SMDImporter::CreateOutputMaterials() -{ +void SMDImporter::CreateOutputMaterials() { ai_assert( nullptr != pScene ); pScene->mNumMaterials = (unsigned int)aszTextures.size(); @@ -674,14 +622,13 @@ void SMDImporter::CreateOutputMaterials() } // create a default material if necessary - if (0 == pScene->mNumMaterials) - { + if (0 == pScene->mNumMaterials) { pScene->mNumMaterials = 1; aiMaterial* pcHelper = new aiMaterial(); pScene->mMaterials[0] = pcHelper; - int iMode = (int)aiShadingMode_Gouraud; + int iMode = static_cast(aiShadingMode_Gouraud); pcHelper->AddProperty(&iMode, 1, AI_MATKEY_SHADING_MODEL); aiColor3D clr; @@ -700,66 +647,94 @@ void SMDImporter::CreateOutputMaterials() // ------------------------------------------------------------------------------------------------ // Parse the file -void SMDImporter::ParseFile() -{ +void SMDImporter::ParseFile() { const char* szCurrent = &mBuffer[0]; // read line per line ... - for ( ;; ) - { - if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break; + for ( ;; ) { + if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) { + break; + } // "version \n", should be 1 for hl and hl2 SMD files - if (TokenMatch(szCurrent,"version",7)) - { + if (TokenMatch(szCurrent,"version",7)) { if(!SkipSpaces(szCurrent,&szCurrent)) break; - if (1 != strtoul10(szCurrent,&szCurrent)) - { + if (1 != strtoul10(szCurrent,&szCurrent)) { ASSIMP_LOG_WARN("SMD.version is not 1. This " "file format is not known. Continuing happily ..."); } continue; } // "nodes\n" - Starts the node section - if (TokenMatch(szCurrent,"nodes",5)) - { + if (TokenMatch(szCurrent,"nodes",5)) { ParseNodesSection(szCurrent,&szCurrent); continue; } // "triangles\n" - Starts the triangle section - if (TokenMatch(szCurrent,"triangles",9)) - { + if (TokenMatch(szCurrent,"triangles",9)) { ParseTrianglesSection(szCurrent,&szCurrent); continue; } // "vertexanimation\n" - Starts the vertex animation section - if (TokenMatch(szCurrent,"vertexanimation",15)) - { + if (TokenMatch(szCurrent,"vertexanimation",15)) { bHasUVs = false; ParseVASection(szCurrent,&szCurrent); continue; } // "skeleton\n" - Starts the skeleton section - if (TokenMatch(szCurrent,"skeleton",8)) - { + if (TokenMatch(szCurrent,"skeleton",8)) { ParseSkeletonSection(szCurrent,&szCurrent); continue; } SkipLine(szCurrent,&szCurrent); } - return; +} + +void SMDImporter::ReadSmd(const std::string &pFile, IOSystem* pIOHandler) { + std::unique_ptr file(pIOHandler->Open(pFile, "rb")); + + // Check whether we can read from the file + if (file.get() == nullptr) { + throw DeadlyImportError("Failed to open SMD/VTA file " + pFile + "."); + } + + iFileSize = (unsigned int)file->FileSize(); + + // Allocate storage and copy the contents of the file to a memory buffer + mBuffer.resize(iFileSize + 1); + TextFileToBuffer(file.get(), mBuffer); + + iSmallestFrame = INT_MAX; + bHasUVs = true; + iLineNumber = 1; + + // Reserve enough space for ... hm ... 10 textures + aszTextures.reserve(10); + + // Reserve enough space for ... hm ... 1000 triangles + asTriangles.reserve(1000); + + // Reserve enough space for ... hm ... 20 bones + asBones.reserve(20); + + aszTextures.clear(); + asTriangles.clear(); + asBones.clear(); + + // parse the file ... + ParseFile(); } // ------------------------------------------------------------------------------------------------ -unsigned int SMDImporter::GetTextureIndex(const std::string& filename) -{ +unsigned int SMDImporter::GetTextureIndex(const std::string& filename) { unsigned int iIndex = 0; for (std::vector::const_iterator - i = aszTextures.begin(); - i != aszTextures.end();++i,++iIndex) - { + i = aszTextures.begin(); + i != aszTextures.end();++i,++iIndex) { // case-insensitive ... it's a path - if (0 == ASSIMP_stricmp ( filename.c_str(),(*i).c_str()))return iIndex; + if (0 == ASSIMP_stricmp ( filename.c_str(),(*i).c_str())) { + return iIndex; + } } iIndex = (unsigned int)aszTextures.size(); aszTextures.push_back(filename); @@ -768,15 +743,10 @@ unsigned int SMDImporter::GetTextureIndex(const std::string& filename) // ------------------------------------------------------------------------------------------------ // Parse the nodes section of the file -void SMDImporter::ParseNodesSection(const char* szCurrent, - const char** szCurrentOut) -{ - for ( ;; ) - { +void SMDImporter::ParseNodesSection(const char* szCurrent, const char** szCurrentOut) { + for ( ;; ) { // "end\n" - Ends the nodes section - if (0 == ASSIMP_strincmp(szCurrent,"end",3) && - IsSpaceOrNewLine(*(szCurrent+3))) - { + if (0 == ASSIMP_strincmp(szCurrent,"end",3) && IsSpaceOrNewLine(*(szCurrent+3))) { szCurrent += 4; break; } @@ -788,18 +758,18 @@ void SMDImporter::ParseNodesSection(const char* szCurrent, // ------------------------------------------------------------------------------------------------ // Parse the triangles section of the file -void SMDImporter::ParseTrianglesSection(const char* szCurrent, - const char** szCurrentOut) -{ +void SMDImporter::ParseTrianglesSection(const char* szCurrent, const char** szCurrentOut) { // Parse a triangle, parse another triangle, parse the next triangle ... // and so on until we reach a token that looks quite similar to "end" - for ( ;; ) - { - if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break; + for ( ;; ) { + if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) { + break; + } // "end\n" - Ends the triangles section - if (TokenMatch(szCurrent,"end",3)) + if (TokenMatch(szCurrent,"end",3)) { break; + } ParseTriangle(szCurrent,&szCurrent); } SkipSpacesAndLineEnd(szCurrent,&szCurrent); @@ -807,40 +777,39 @@ void SMDImporter::ParseTrianglesSection(const char* szCurrent, } // ------------------------------------------------------------------------------------------------ // Parse the vertex animation section of the file -void SMDImporter::ParseVASection(const char* szCurrent, - const char** szCurrentOut) -{ +void SMDImporter::ParseVASection(const char* szCurrent, const char** szCurrentOut) { unsigned int iCurIndex = 0; - for ( ;; ) - { - if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break; + for ( ;; ) { + if (!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) { + break; + } // "end\n" - Ends the "vertexanimation" section - if (TokenMatch(szCurrent,"end",3)) + if (TokenMatch(szCurrent,"end",3)) { break; + } // "time \n" - if (TokenMatch(szCurrent,"time",4)) - { + if (TokenMatch(szCurrent,"time",4)) { // NOTE: The doc says that time values COULD be negative ... // NOTE2: this is the shape key -> valve docs int iTime = 0; - if(!ParseSignedInt(szCurrent,&szCurrent,iTime) || configFrameID != (unsigned int)iTime)break; + if(!ParseSignedInt(szCurrent,&szCurrent,iTime) || configFrameID != (unsigned int)iTime) { + break; + } SkipLine(szCurrent,&szCurrent); - } - else - { - if(0 == iCurIndex) - { + } else { + if(0 == iCurIndex) { asTriangles.push_back(SMD::Face()); } - if (++iCurIndex == 3)iCurIndex = 0; + if (++iCurIndex == 3) { + iCurIndex = 0; + } ParseVertex(szCurrent,&szCurrent,asTriangles.back().avVertices[iCurIndex],true); } } - if (iCurIndex != 2 && !asTriangles.empty()) - { + if (iCurIndex != 2 && !asTriangles.empty()) { // we want to no degenerates, so throw this triangle away asTriangles.pop_back(); } @@ -848,30 +817,30 @@ void SMDImporter::ParseVASection(const char* szCurrent, SkipSpacesAndLineEnd(szCurrent,&szCurrent); *szCurrentOut = szCurrent; } + // ------------------------------------------------------------------------------------------------ // Parse the skeleton section of the file -void SMDImporter::ParseSkeletonSection(const char* szCurrent, - const char** szCurrentOut) -{ +void SMDImporter::ParseSkeletonSection(const char* szCurrent, const char** szCurrentOut) { int iTime = 0; - for ( ;; ) - { - if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break; + for ( ;; ) { + if (!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) { + break; + } // "end\n" - Ends the skeleton section - if (TokenMatch(szCurrent,"end",3)) + if (TokenMatch(szCurrent,"end",3)) { break; - + } else if (TokenMatch(szCurrent,"time",4)) { // "time \n" - Specifies the current animation frame - else if (TokenMatch(szCurrent,"time",4)) - { - // NOTE: The doc says that time values COULD be negative ... - if(!ParseSignedInt(szCurrent,&szCurrent,iTime))break; + if(!ParseSignedInt(szCurrent,&szCurrent,iTime)) { + break; + } iSmallestFrame = std::min(iSmallestFrame,iTime); SkipLine(szCurrent,&szCurrent); + } else { + ParseSkeletonElement(szCurrent,&szCurrent,iTime); } - else ParseSkeletonElement(szCurrent,&szCurrent,iTime); } *szCurrentOut = szCurrent; } @@ -884,45 +853,38 @@ void SMDImporter::ParseSkeletonSection(const char* szCurrent, } // ------------------------------------------------------------------------------------------------ // Parse a node line -void SMDImporter::ParseNodeInfo(const char* szCurrent, - const char** szCurrentOut) -{ +void SMDImporter::ParseNodeInfo(const char* szCurrent, const char** szCurrentOut) { unsigned int iBone = 0; SkipSpacesAndLineEnd(szCurrent,&szCurrent); - if(!ParseUnsignedInt(szCurrent,&szCurrent,iBone) || !SkipSpaces(szCurrent,&szCurrent)) - { + if ( !ParseUnsignedInt(szCurrent,&szCurrent,iBone) || !SkipSpaces(szCurrent,&szCurrent)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone index"); SMDI_PARSE_RETURN; } // add our bone to the list - if (iBone >= asBones.size())asBones.resize(iBone+1); + if (iBone >= asBones.size()) { + asBones.resize(iBone+1); + } SMD::Bone& bone = asBones[iBone]; bool bQuota = true; - if ('\"' != *szCurrent) - { + if ('\"' != *szCurrent) { LogWarning("Bone name is expcted to be enclosed in " "double quotation marks. "); bQuota = false; + } else { + ++szCurrent; } - else ++szCurrent; const char* szEnd = szCurrent; - for ( ;; ) - { - if (bQuota && '\"' == *szEnd) - { + for ( ;; ) { + if (bQuota && '\"' == *szEnd) { iBone = (unsigned int)(szEnd - szCurrent); ++szEnd; break; - } - else if (IsSpaceOrNewLine(*szEnd)) - { + } else if (!bQuota && IsSpaceOrNewLine(*szEnd)) { iBone = (unsigned int)(szEnd - szCurrent); break; - } - else if (!(*szEnd)) - { + } else if (!(*szEnd)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone name"); SMDI_PARSE_RETURN; } @@ -932,8 +894,7 @@ void SMDImporter::ParseNodeInfo(const char* szCurrent, szCurrent = szEnd; // the only negative bone parent index that could occur is -1 AFAIK - if(!ParseSignedInt(szCurrent,&szCurrent,(int&)bone.iParent)) - { + if(!ParseSignedInt(szCurrent,&szCurrent,(int&)bone.iParent)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone parent index. Assuming -1"); SMDI_PARSE_RETURN; } @@ -944,20 +905,16 @@ void SMDImporter::ParseNodeInfo(const char* szCurrent, // ------------------------------------------------------------------------------------------------ // Parse a skeleton element -void SMDImporter::ParseSkeletonElement(const char* szCurrent, - const char** szCurrentOut,int iTime) -{ +void SMDImporter::ParseSkeletonElement(const char* szCurrent, const char** szCurrentOut,int iTime) { aiVector3D vPos; aiVector3D vRot; unsigned int iBone = 0; - if(!ParseUnsignedInt(szCurrent,&szCurrent,iBone)) - { + if(!ParseUnsignedInt(szCurrent,&szCurrent,iBone)) { ASSIMP_LOG_ERROR("Unexpected EOF/EOL while parsing bone index"); SMDI_PARSE_RETURN; } - if (iBone >= asBones.size()) - { + if (iBone >= asBones.size()) { LogErrorNoThrow("Bone index in skeleton section is out of range"); SMDI_PARSE_RETURN; } @@ -967,60 +924,51 @@ void SMDImporter::ParseSkeletonElement(const char* szCurrent, SMD::Bone::Animation::MatrixKey& key = bone.sAnim.asKeys.back(); key.dTime = (double)iTime; - if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.x)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.x)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.x"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.y)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.y)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.y"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.z)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.z)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.z"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.x)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.x)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.x"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.y)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.y)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.y"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.z)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.z)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.z"); SMDI_PARSE_RETURN; } // build the transformation matrix of the key - key.matrix.FromEulerAnglesXYZ(vRot.x,vRot.y,vRot.z); - { + key.matrix.FromEulerAnglesXYZ(vRot.x,vRot.y,vRot.z); { aiMatrix4x4 mTemp; mTemp.a4 = vPos.x; mTemp.b4 = vPos.y; mTemp.c4 = vPos.z; - key.matrix = key.matrix * mTemp; + key.matrix = mTemp * key.matrix; } - + key.vPos = vPos; + key.vRot = vRot; // go to the beginning of the next line SMDI_PARSE_RETURN; } // ------------------------------------------------------------------------------------------------ // Parse a triangle -void SMDImporter::ParseTriangle(const char* szCurrent, - const char** szCurrentOut) -{ +void SMDImporter::ParseTriangle(const char* szCurrent, const char** szCurrentOut) { asTriangles.push_back(SMD::Face()); SMD::Face& face = asTriangles.back(); - if(!SkipSpaces(szCurrent,&szCurrent)) - { + if(!SkipSpaces(szCurrent,&szCurrent)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing a triangle"); return; } @@ -1035,21 +983,18 @@ void SMDImporter::ParseTriangle(const char* szCurrent, SkipSpacesAndLineEnd(szCurrent,&szCurrent); // load three vertices - for (unsigned int iVert = 0; iVert < 3;++iVert) - { - ParseVertex(szCurrent,&szCurrent, - face.avVertices[iVert]); + for (unsigned int iVert = 0; iVert < 3;++iVert) { + ParseVertex(szCurrent,&szCurrent, face.avVertices[iVert]); } *szCurrentOut = szCurrent; } // ------------------------------------------------------------------------------------------------ // Parse a float -bool SMDImporter::ParseFloat(const char* szCurrent, - const char** szCurrentOut, float& out) -{ - if(!SkipSpaces(&szCurrent)) +bool SMDImporter::ParseFloat(const char* szCurrent, const char** szCurrentOut, float& out) { + if(!SkipSpaces(&szCurrent)) { return false; + } *szCurrentOut = fast_atoreal_move(szCurrent,out); return true; @@ -1057,11 +1002,10 @@ bool SMDImporter::ParseFloat(const char* szCurrent, // ------------------------------------------------------------------------------------------------ // Parse an unsigned int -bool SMDImporter::ParseUnsignedInt(const char* szCurrent, - const char** szCurrentOut, unsigned int& out) -{ - if(!SkipSpaces(&szCurrent)) +bool SMDImporter::ParseUnsignedInt(const char* szCurrent, const char** szCurrentOut, unsigned int& out) { + if(!SkipSpaces(&szCurrent)) { return false; + } out = strtoul10(szCurrent,szCurrentOut); return true; @@ -1069,11 +1013,10 @@ bool SMDImporter::ParseUnsignedInt(const char* szCurrent, // ------------------------------------------------------------------------------------------------ // Parse a signed int -bool SMDImporter::ParseSignedInt(const char* szCurrent, - const char** szCurrentOut, int& out) -{ - if(!SkipSpaces(&szCurrent)) +bool SMDImporter::ParseSignedInt(const char* szCurrent, const char** szCurrentOut, int& out) { + if(!SkipSpaces(&szCurrent)) { return false; + } out = strtol10(szCurrent,szCurrentOut); return true; @@ -1082,59 +1025,50 @@ bool SMDImporter::ParseSignedInt(const char* szCurrent, // ------------------------------------------------------------------------------------------------ // Parse a vertex void SMDImporter::ParseVertex(const char* szCurrent, - const char** szCurrentOut, SMD::Vertex& vertex, - bool bVASection /*= false*/) -{ - if (SkipSpaces(&szCurrent) && IsLineEnd(*szCurrent)) - { + const char** szCurrentOut, SMD::Vertex& vertex, + bool bVASection /*= false*/) { + if (SkipSpaces(&szCurrent) && IsLineEnd(*szCurrent)) { SkipSpacesAndLineEnd(szCurrent,&szCurrent); return ParseVertex(szCurrent,szCurrentOut,vertex,bVASection); } - if(!ParseSignedInt(szCurrent,&szCurrent,(int&)vertex.iParentNode)) - { + if(!ParseSignedInt(szCurrent,&szCurrent,(int&)vertex.iParentNode)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.parent"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.x)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.x)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.x"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.y)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.y)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.y"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.z)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.z)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.z"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.x)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.x)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.x"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.y)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.y)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.y"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.z)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.z)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.z"); SMDI_PARSE_RETURN; } - if (bVASection)SMDI_PARSE_RETURN; + if (bVASection) { + SMDI_PARSE_RETURN; + } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.uv.x)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.uv.x)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.uv.x"); SMDI_PARSE_RETURN; } - if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.uv.y)) - { + if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.uv.y)) { LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.uv.y"); SMDI_PARSE_RETURN; } @@ -1142,17 +1076,20 @@ void SMDImporter::ParseVertex(const char* szCurrent, // now read the number of bones affecting this vertex // all elements from now are fully optional, we don't need them unsigned int iSize = 0; - if(!ParseUnsignedInt(szCurrent,&szCurrent,iSize))SMDI_PARSE_RETURN; + if(!ParseUnsignedInt(szCurrent,&szCurrent,iSize)) { + SMDI_PARSE_RETURN; + } vertex.aiBoneLinks.resize(iSize,std::pair(0,0.0f)); for (std::vector >::iterator - i = vertex.aiBoneLinks.begin(); - i != vertex.aiBoneLinks.end();++i) - { - if(!ParseUnsignedInt(szCurrent,&szCurrent,(*i).first)) + i = vertex.aiBoneLinks.begin(); + i != vertex.aiBoneLinks.end();++i) { + if(!ParseUnsignedInt(szCurrent,&szCurrent,(*i).first)) { SMDI_PARSE_RETURN; - if(!ParseFloat(szCurrent,&szCurrent,(*i).second)) + } + if(!ParseFloat(szCurrent,&szCurrent,(*i).second)) { SMDI_PARSE_RETURN; + } } // go to the beginning of the next line diff --git a/code/SMDLoader.h b/code/SMDLoader.h index 40c08385f..a791e7dde 100644 --- a/code/SMDLoader.h +++ b/code/SMDLoader.h @@ -219,6 +219,7 @@ protected: /** Parse the SMD file and create the output scene */ void ParseFile(); + void ReadSmd(const std::string &pFile, IOSystem* pIOHandler); // ------------------------------------------------------------------- /** Parse the triangles section of the SMD file @@ -289,13 +290,6 @@ protected: */ unsigned int GetTextureIndex(const std::string& filename); - // ------------------------------------------------------------------- - /** Computes absolute bone transformations - * All output transformations are in worldspace. - */ - void ComputeAbsoluteBoneTransformations(); - - // ------------------------------------------------------------------- /** Parse a line in the skeleton section */ @@ -344,7 +338,9 @@ protected: */ void CreateOutputMeshes(); void CreateOutputNodes(); - void CreateOutputAnimations(); + void CreateOutputAnimations(const std::string &pFile, IOSystem* pIOHandler); + void CreateOutputAnimation(int index, const std::string &name); + void GetAnimationFileList(const std::string &pFile, IOSystem* pIOHandler, std::vector>& outList); void CreateOutputMaterials(); @@ -413,6 +409,8 @@ private: */ unsigned int iLineNumber; + bool bLoadAnimationList = true; + bool noSkeletonMesh = false; }; } // end of namespace Assimp diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index 687e16ce1..ed37937f4 100755 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -1357,6 +1357,13 @@ inline void Asset::Load(const std::string& pFile, bool isBinary) } } + // Force reading of skins since they're not always directly referenced + if (Value* skinsArray = FindArray(doc, "skins")) { + for (unsigned int i = 0; i < skinsArray->Size(); ++i) { + skins.Retrieve(i); + } + } + if (Value* animsArray = FindArray(doc, "animations")) { for (unsigned int i = 0; i < animsArray->Size(); ++i) { animations.Retrieve(i); diff --git a/contrib/openddlparser/include/openddlparser/OpenDDLParserUtils.h b/contrib/openddlparser/include/openddlparser/OpenDDLParserUtils.h index 64897436e..f0762ac67 100644 --- a/contrib/openddlparser/include/openddlparser/OpenDDLParserUtils.h +++ b/contrib/openddlparser/include/openddlparser/OpenDDLParserUtils.h @@ -59,32 +59,10 @@ bool isSeparator( T in ) { return false; } -static const unsigned char chartype_table[ 256 ] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32-47 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 48-63 - - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 64-79 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 80-95 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 96-111 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 112-127 - - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // > 127 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -}; - template inline bool isNumeric( const T in ) { - return ( chartype_table[ static_cast( in ) ] == 1 ); + return ( in >= '0' && in <= '9' ); } template diff --git a/include/assimp/MemoryIOWrapper.h b/include/assimp/MemoryIOWrapper.h index bfcfff9c2..5260cb2b2 100644 --- a/include/assimp/MemoryIOWrapper.h +++ b/include/assimp/MemoryIOWrapper.h @@ -80,7 +80,9 @@ public: // ------------------------------------------------------------------- // Read from stream size_t Read(void* pvBuffer, size_t pSize, size_t pCount) { - const size_t cnt = std::min(pCount,(length-pos)/pSize),ofs = pSize*cnt; + ai_assert(pvBuffer); + ai_assert(pSize); + const size_t cnt = std::min(pCount,(length-pos)/pSize), ofs = pSize*cnt; memcpy(pvBuffer,buffer+pos,ofs); pos += ofs; diff --git a/include/assimp/color4.h b/include/assimp/color4.h index 570b8f44c..fddd1b638 100644 --- a/include/assimp/color4.h +++ b/include/assimp/color4.h @@ -63,8 +63,7 @@ public: aiColor4t (TReal _r, TReal _g, TReal _b, TReal _a) : r(_r), g(_g), b(_b), a(_a) {} explicit aiColor4t (TReal _r) : r(_r), g(_r), b(_r), a(_r) {} - aiColor4t (const aiColor4t& o) - : r(o.r), g(o.g), b(o.b), a(o.a) {} + aiColor4t (const aiColor4t& o) = default; public: // combined operators diff --git a/include/assimp/config.h.in b/include/assimp/config.h.in index 8de2ea43e..fd828bc6e 100644 --- a/include/assimp/config.h.in +++ b/include/assimp/config.h.in @@ -673,6 +673,12 @@ enum aiComponent #define AI_CONFIG_IMPORT_SMD_KEYFRAME "IMPORT_SMD_KEYFRAME" #define AI_CONFIG_IMPORT_UNREAL_KEYFRAME "IMPORT_UNREAL_KEYFRAME" +// --------------------------------------------------------------------------- +/** Smd load multiple animations + * + * Property type: bool. Default value: true. + */ +#define AI_CONFIG_IMPORT_SMD_LOAD_ANIMATION_LIST "IMPORT_SMD_LOAD_ANIMATION_LIST" // --------------------------------------------------------------------------- /** @brief Configures the AC loader to collect all surfaces which have the diff --git a/include/assimp/matrix4x4.inl b/include/assimp/matrix4x4.inl index 9920f0059..680d7666d 100644 --- a/include/assimp/matrix4x4.inl +++ b/include/assimp/matrix4x4.inl @@ -527,27 +527,25 @@ inline aiMatrix4x4t& aiMatrix4x4t::FromEulerAnglesXYZ(TReal x, TRe { aiMatrix4x4t& _this = *this; - TReal cr = std::cos( x ); - TReal sr = std::sin( x ); - TReal cp = std::cos( y ); - TReal sp = std::sin( y ); - TReal cy = std::cos( z ); - TReal sy = std::sin( z ); + TReal cx = std::cos(x); + TReal sx = std::sin(x); + TReal cy = std::cos(y); + TReal sy = std::sin(y); + TReal cz = std::cos(z); + TReal sz = std::sin(z); - _this.a1 = cp*cy ; - _this.a2 = cp*sy; - _this.a3 = -sp ; + // mz*my*mx + _this.a1 = cz * cy; + _this.a2 = cz * sy * sx - sz * cx; + _this.a3 = sz * sx + cz * sy * cx; - TReal srsp = sr*sp; - TReal crsp = cr*sp; + _this.b1 = sz * cy; + _this.b2 = cz * cx + sz * sy * sx; + _this.b3 = sz * sy * cx - cz * sx; - _this.b1 = srsp*cy-cr*sy ; - _this.b2 = srsp*sy+cr*cy ; - _this.b3 = sr*cp ; - - _this.c1 = crsp*cy+sr*sy ; - _this.c2 = crsp*sy-sr*cy ; - _this.c3 = cr*cp ; + _this.c1 = -sy; + _this.c2 = cy * sx; + _this.c3 = cy * cx; return *this; } diff --git a/include/assimp/vector2.h b/include/assimp/vector2.h index d290945c9..85ca6d0a4 100644 --- a/include/assimp/vector2.h +++ b/include/assimp/vector2.h @@ -53,7 +53,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # include #endif -#include "./Compiler/pushpack1.h" #include "defs.h" // ---------------------------------------------------------------------------------- @@ -62,24 +61,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef __cplusplus template -class aiVector2t -{ +class aiVector2t { public: - aiVector2t () : x(), y() {} aiVector2t (TReal _x, TReal _y) : x(_x), y(_y) {} explicit aiVector2t (TReal _xyz) : x(_xyz), y(_xyz) {} - aiVector2t (const aiVector2t& o) : x(o.x), y(o.y) {} - -public: + aiVector2t (const aiVector2t& o) = default; void Set( TReal pX, TReal pY); TReal SquareLength() const ; TReal Length() const ; aiVector2t& Normalize(); -public: - const aiVector2t& operator += (const aiVector2t& o); const aiVector2t& operator -= (const aiVector2t& o); const aiVector2t& operator *= (TReal f); @@ -111,6 +104,4 @@ struct aiVector2D { #endif // __cplusplus -#include "./Compiler/poppack1.h" - #endif // AI_VECTOR2D_H_INC diff --git a/include/assimp/vector3.h b/include/assimp/vector3.h index 2c610b3a9..33ae7d22f 100644 --- a/include/assimp/vector3.h +++ b/include/assimp/vector3.h @@ -69,7 +69,7 @@ public: aiVector3t() AI_NO_EXCEPT : x(), y(), z() {} aiVector3t(TReal _x, TReal _y, TReal _z) : x(_x), y(_y), z(_z) {} explicit aiVector3t (TReal _xyz ) : x(_xyz), y(_xyz), z(_xyz) {} - aiVector3t( const aiVector3t& o ) : x(o.x), y(o.y), z(o.z) {} + aiVector3t( const aiVector3t& o ) = default; public: diff --git a/tools/assimp_cmd/WriteDumb.cpp b/tools/assimp_cmd/WriteDumb.cpp index 3fbfab824..b05adf249 100644 --- a/tools/assimp_cmd/WriteDumb.cpp +++ b/tools/assimp_cmd/WriteDumb.cpp @@ -82,15 +82,18 @@ void CompressBinaryDump(const char* file, unsigned int head_size) uint8_t* data = new uint8_t[size]; fread(data,1,size,p); - uLongf out_size = (uLongf)((size-head_size) * 1.001 + 12.); + uint32_t uncompressed_size = size-head_size; + uLongf out_size = (uLongf)compressBound(uncompressed_size); uint8_t* out = new uint8_t[out_size]; - compress2(out,&out_size,data+head_size,size-head_size,9); + int res = compress2(out,&out_size,data+head_size,uncompressed_size,9); + if(res != Z_OK) + fprintf(stderr, "compress2: error\n"); fclose(p); p = fopen(file,"w"); fwrite(data,head_size,1,p); - fwrite(&out_size,4,1,p); // write size of uncompressed data + fwrite(&uncompressed_size,4,1,p); // write size of uncompressed data fwrite(out,out_size,1,p); fclose(p);