diff --git a/.gitignore b/.gitignore index e975976bf..fe59f9a70 100644 --- a/.gitignore +++ b/.gitignore @@ -79,6 +79,12 @@ test/gtest/src/gtest-stamp/Debug/ tools/assimp_view/assimp_viewer.vcxproj.user *.pyc +### Rust ### +# Generated by Cargo; will have compiled files and executables +port/assimp_rs/target/ +# Backup files generated by rustfmt +port/assimp_rs/**/*.rs.bk + # Unix editor backups *~ test/gtest/src/gtest-stamp/gtest-gitinfo.txt diff --git a/assimpTargets-debug.cmake.in b/assimpTargets-debug.cmake.in index de6459eaf..b7efe71f9 100644 --- a/assimpTargets-debug.cmake.in +++ b/assimpTargets-debug.cmake.in @@ -73,6 +73,9 @@ else() else() set(sharedLibraryName "libassimp${ASSIMP_LIBRARY_SUFFIX}@CMAKE_DEBUG_POSTFIX@@CMAKE_SHARED_LIBRARY_SUFFIX@.@ASSIMP_VERSION_MAJOR@") endif() + + # Import target "assimp::assimp" for configuration "Debug" + set_property(TARGET assimp::assimp APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG) set_target_properties(assimp::assimp PROPERTIES IMPORTED_SONAME_DEBUG "${sharedLibraryName}" IMPORTED_LOCATION_DEBUG "@CMAKE_INSTALL_FULL_LIBDIR@/${sharedLibraryName}" @@ -81,6 +84,9 @@ else() list(APPEND _IMPORT_CHECK_FILES_FOR_assimp::assimp "@CMAKE_INSTALL_FULL_LIBDIR@/${sharedLibraryName}" ) else() set(staticLibraryName "libassimp${ASSIMP_LIBRARY_SUFFIX}@CMAKE_DEBUG_POSTFIX@@CMAKE_STATIC_LIBRARY_SUFFIX@") + + # Import target "assimp::assimp" for configuration "Debug" + set_property(TARGET assimp::assimp APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG) set_target_properties(assimp::assimp PROPERTIES IMPORTED_LOCATION_DEBUG "@CMAKE_INSTALL_FULL_LIBDIR@/${staticLibraryName}" ) diff --git a/assimpTargets-release.cmake.in b/assimpTargets-release.cmake.in index 6a5bafcf7..c716006dd 100644 --- a/assimpTargets-release.cmake.in +++ b/assimpTargets-release.cmake.in @@ -73,6 +73,9 @@ else() else() set(sharedLibraryName "libassimp${ASSIMP_LIBRARY_SUFFIX}@CMAKE_SHARED_LIBRARY_SUFFIX@.@ASSIMP_VERSION_MAJOR@") endif() + + # Import target "assimp::assimp" for configuration "Release" + set_property(TARGET assimp::assimp APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) set_target_properties(assimp::assimp PROPERTIES IMPORTED_SONAME_RELEASE "${sharedLibraryName}" IMPORTED_LOCATION_RELEASE "@CMAKE_INSTALL_FULL_LIBDIR@/${sharedLibraryName}" @@ -81,6 +84,9 @@ else() list(APPEND _IMPORT_CHECK_FILES_FOR_assimp::assimp "@CMAKE_INSTALL_FULL_LIBDIR@/${sharedLibraryName}" ) else() set(staticLibraryName "libassimp${ASSIMP_LIBRARY_SUFFIX}@CMAKE_STATIC_LIBRARY_SUFFIX@") + + # Import target "assimp::assimp" for configuration "Release" + set_property(TARGET assimp::assimp APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) set_target_properties(assimp::assimp PROPERTIES IMPORTED_LOCATION_RELEASE "@CMAKE_INSTALL_FULL_LIBDIR@/${staticLibraryName}" ) diff --git a/code/AssetLib/Collada/ColladaExporter.cpp b/code/AssetLib/Collada/ColladaExporter.cpp index c2b71d00c..567f7c8e7 100644 --- a/code/AssetLib/Collada/ColladaExporter.cpp +++ b/code/AssetLib/Collada/ColladaExporter.cpp @@ -44,8 +44,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER #include "ColladaExporter.h" + #include +#include #include +#include #include #include #include @@ -56,15 +59,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include - #include -#include #include -#include -#include - -using namespace Assimp; namespace Assimp { @@ -91,8 +87,6 @@ void ExportSceneCollada(const char *pFile, IOSystem *pIOSystem, const aiScene *p outfile->Write(iDoTheExportThing.mOutput.str().c_str(), static_cast(iDoTheExportThing.mOutput.tellp()), 1); } -} // end of namespace Assimp - // ------------------------------------------------------------------------------------------------ // Encodes a string into a valid XML ID using the xsd:ID schema qualifications. static const std::string XMLIDEncode(const std::string &name) { @@ -115,7 +109,7 @@ static const std::string XMLIDEncode(const std::string &name) { if (strchr(XML_ID_CHARS, *it) != nullptr) { idEncoded << *it; } else { - // Select placeholder character based on invalid character to prevent name collisions + // Select placeholder character based on invalid character to reduce ID collisions idEncoded << XML_ID_CHARS[(*it) % XML_ID_CHARS_COUNT]; } } @@ -123,20 +117,37 @@ static const std::string XMLIDEncode(const std::string &name) { return idEncoded.str(); } +// ------------------------------------------------------------------------------------------------ +// Helper functions to create unique ids +inline bool IsUniqueId(const std::unordered_set &idSet, const std::string &idStr) { + return (idSet.find(idStr) == idSet.end()); +} + +inline std::string MakeUniqueId(const std::unordered_set &idSet, const std::string &idPrefix, const std::string &postfix) { + std::string result(idPrefix + postfix); + if (!IsUniqueId(idSet, result)) { + // Select a number to append + size_t idnum = 1; + do { + result = idPrefix + '_' + to_string(idnum) + postfix; + ++idnum; + } while (!IsUniqueId(idSet, result)); + } + return result; +} + // ------------------------------------------------------------------------------------------------ // Constructor for a specific scene to export ColladaExporter::ColladaExporter(const aiScene *pScene, IOSystem *pIOSystem, const std::string &path, const std::string &file) : - mIOSystem(pIOSystem), mPath(path), mFile(file) { + mIOSystem(pIOSystem), + mPath(path), + mFile(file), + mScene(pScene), + endstr("\n") { // make sure that all formatting happens using the standard, C locale and not the user's current locale mOutput.imbue(std::locale("C")); mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION); - mScene = pScene; - mSceneOwned = false; - - // set up strings - endstr = "\n"; - // start writing the file WriteFile(); } @@ -144,9 +155,6 @@ ColladaExporter::ColladaExporter(const aiScene *pScene, IOSystem *pIOSystem, con // ------------------------------------------------------------------------------------------------ // Destructor ColladaExporter::~ColladaExporter() { - if (mSceneOwned) { - delete mScene; - } } // ------------------------------------------------------------------------------------------------ @@ -161,6 +169,9 @@ void ColladaExporter::WriteFile() { WriteTextures(); WriteHeader(); + // Add node names to the unique id database first so they are most likely to use their names as unique ids + CreateNodeIds(mScene->mRootNode); + WriteCamerasLibrary(); WriteLightsLibrary(); WriteMaterials(); @@ -172,10 +183,11 @@ void ColladaExporter::WriteFile() { // customized, Writes the animation library WriteAnimationsLibrary(); - // useless Collada fu at the end, just in case we haven't had enough indirections, yet. + // instantiate the scene(s) + // For Assimp there will only ever be one mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "mRootNode->mName.C_Str()) + "\" />" << endstr; + mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); @@ -201,7 +213,7 @@ void ColladaExporter::WriteHeader() { static const unsigned int date_nb_chars = 20; char date_str[date_nb_chars]; - std::time_t date = std::time(NULL); + std::time_t date = std::time(nullptr); std::strftime(date_str, date_nb_chars, "%Y-%m-%dT%H:%M:%S", std::localtime(&date)); aiVector3D scaling; @@ -210,13 +222,13 @@ void ColladaExporter::WriteHeader() { mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position); rotation.Normalize(); - bool add_root_node = false; + mAdd_root_node = false; ai_real scale = 1.0; if (std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) { scale = (ai_real)((((double)scaling.x) + ((double)scaling.y) + ((double)scaling.z)) / 3.0); } else { - add_root_node = true; + mAdd_root_node = true; } std::string up_axis = "Y_UP"; @@ -227,33 +239,19 @@ void ColladaExporter::WriteHeader() { } else if (rotation.Equal(z_rot, epsilon)) { up_axis = "Z_UP"; } else { - add_root_node = true; + mAdd_root_node = true; } if (!position.Equal(aiVector3D(0, 0, 0))) { - add_root_node = true; + mAdd_root_node = true; } - if (mScene->mRootNode->mNumChildren == 0) { - add_root_node = true; + // Assimp root nodes can have meshes, Collada Scenes cannot + if (mScene->mRootNode->mNumChildren == 0 || mScene->mRootNode->mMeshes != 0) { + mAdd_root_node = true; } - if (add_root_node) { - aiScene *scene; - SceneCombiner::CopyScene(&scene, mScene); - - aiNode *root = new aiNode("Scene"); - - root->mNumChildren = 1; - root->mChildren = new aiNode *[root->mNumChildren]; - - root->mChildren[0] = scene->mRootNode; - scene->mRootNode->mParent = root; - scene->mRootNode = root; - - mScene = scene; - mSceneOwned = true; - + if (mAdd_root_node) { up_axis = "Y_UP"; scale = 1.0; } @@ -352,7 +350,7 @@ void ColladaExporter::WriteTextures() { std::string name = mFile + "_texture_" + (i < 1000 ? "0" : "") + (i < 100 ? "0" : "") + (i < 10 ? "0" : "") + str + "." + ((const char *)texture->achFormatHint); std::unique_ptr outfile(mIOSystem->Open(mPath + mIOSystem->getOsSeparator() + name, "wb")); - if (outfile == NULL) { + if (outfile == nullptr) { throw DeadlyExportError("could not open output texture file: " + mPath + name); } @@ -388,10 +386,10 @@ void ColladaExporter::WriteCamerasLibrary() { void ColladaExporter::WriteCamera(size_t pIndex) { const aiCamera *cam = mScene->mCameras[pIndex]; - const std::string cameraName = XMLEscape(cam->mName.C_Str()); - const std::string cameraId = XMLIDEncode(cam->mName.C_Str()); + const std::string cameraId = GetObjectUniqueId(AiObjectType::Camera, pIndex); + const std::string cameraName = GetObjectName(AiObjectType::Camera, pIndex); - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); @@ -441,10 +439,10 @@ void ColladaExporter::WriteLightsLibrary() { void ColladaExporter::WriteLight(size_t pIndex) { const aiLight *light = mScene->mLights[pIndex]; - const std::string lightName = XMLEscape(light->mName.C_Str()); - const std::string lightId = XMLIDEncode(light->mName.C_Str()); + const std::string lightId = GetObjectUniqueId(AiObjectType::Light, pIndex); + const std::string lightName = GetObjectName(AiObjectType::Light, pIndex); - mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; @@ -561,12 +559,11 @@ void ColladaExporter::WriteAmbienttLight(const aiLight *const light) { // ------------------------------------------------------------------------------------------------ // Reads a single surface entry from the given material keys -void ColladaExporter::ReadMaterialSurface(Surface &poSurface, const aiMaterial *pSrcMat, - aiTextureType pTexture, const char *pKey, size_t pType, size_t pIndex) { - if (pSrcMat->GetTextureCount(pTexture) > 0) { +bool ColladaExporter::ReadMaterialSurface(Surface &poSurface, const aiMaterial &pSrcMat, aiTextureType pTexture, const char *pKey, size_t pType, size_t pIndex) { + if (pSrcMat.GetTextureCount(pTexture) > 0) { aiString texfile; unsigned int uvChannel = 0; - pSrcMat->GetTexture(pTexture, 0, &texfile, NULL, &uvChannel); + pSrcMat.GetTexture(pTexture, 0, &texfile, nullptr, &uvChannel); std::string index_str(texfile.C_Str()); @@ -596,8 +593,9 @@ void ColladaExporter::ReadMaterialSurface(Surface &poSurface, const aiMaterial * poSurface.exist = true; } else { if (pKey) - poSurface.exist = pSrcMat->Get(pKey, static_cast(pType), static_cast(pIndex), poSurface.color) == aiReturn_SUCCESS; + poSurface.exist = pSrcMat.Get(pKey, static_cast(pType), static_cast(pIndex), poSurface.color) == aiReturn_SUCCESS; } + return poSurface.exist; } // ------------------------------------------------------------------------------------------------ @@ -608,9 +606,9 @@ static bool isalnum_C(char c) { // ------------------------------------------------------------------------------------------------ // Writes an image entry for the given surface -void ColladaExporter::WriteImageEntry(const Surface &pSurface, const std::string &pNameAdd) { +void ColladaExporter::WriteImageEntry(const Surface &pSurface, const std::string &imageId) { if (!pSurface.texture.empty()) { - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << ""; @@ -631,14 +629,14 @@ void ColladaExporter::WriteImageEntry(const Surface &pSurface, const std::string // ------------------------------------------------------------------------------------------------ // Writes a color-or-texture entry into an effect definition -void ColladaExporter::WriteTextureColorEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &pImageName) { +void ColladaExporter::WriteTextureColorEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &imageId) { if (pSurface.exist) { mOutput << startstr << "<" << pTypeName << ">" << endstr; PushTag(); if (pSurface.texture.empty()) { mOutput << startstr << "" << pSurface.color.r << " " << pSurface.color.g << " " << pSurface.color.b << " " << pSurface.color.a << "" << endstr; } else { - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; } PopTag(); mOutput << startstr << "" << endstr; @@ -647,24 +645,24 @@ void ColladaExporter::WriteTextureColorEntry(const Surface &pSurface, const std: // ------------------------------------------------------------------------------------------------ // Writes the two parameters necessary for referencing a texture in an effect entry -void ColladaExporter::WriteTextureParamEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &pMatName) { +void ColladaExporter::WriteTextureParamEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &materialId) { // if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture if (!pSurface.texture.empty()) { - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "" << XMLIDEncode(pMatName) << "-" << pTypeName << "-image" << endstr; + mOutput << startstr << "" << materialId << "-" << pTypeName << "-image" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "" << XMLIDEncode(pMatName) << "-" << pTypeName << "-surface" << endstr; + mOutput << startstr << "" << materialId << "-" << pTypeName << "-surface" << endstr; PopTag(); mOutput << startstr << "" << endstr; PopTag(); @@ -687,80 +685,63 @@ void ColladaExporter::WriteFloatEntry(const Property &pProperty, const std::stri // ------------------------------------------------------------------------------------------------ // Writes the material setup void ColladaExporter::WriteMaterials() { + std::vector materials; materials.resize(mScene->mNumMaterials); /// collect all materials from the scene size_t numTextures = 0; for (size_t a = 0; a < mScene->mNumMaterials; ++a) { - const aiMaterial *mat = mScene->mMaterials[a]; - - aiString name; - if (mat->Get(AI_MATKEY_NAME, name) != aiReturn_SUCCESS) { - name = "mat"; - materials[a].name = std::string("m") + to_string(a) + name.C_Str(); - } else { - // try to use the material's name if no other material has already taken it, else append # - std::string testName = name.C_Str(); - size_t materialCountWithThisName = 0; - for (size_t i = 0; i < a; i++) { - if (materials[i].name == testName) { - materialCountWithThisName++; - } - } - if (materialCountWithThisName == 0) { - materials[a].name = name.C_Str(); - } else { - materials[a].name = std::string(name.C_Str()) + to_string(materialCountWithThisName); - } - } + Material &material = materials[a]; + material.id = GetObjectUniqueId(AiObjectType::Material, a); + material.name = GetObjectName(AiObjectType::Material, a); + const aiMaterial &mat = *(mScene->mMaterials[a]); aiShadingMode shading = aiShadingMode_Flat; - materials[a].shading_model = "phong"; - if (mat->Get(AI_MATKEY_SHADING_MODEL, shading) == aiReturn_SUCCESS) { + material.shading_model = "phong"; + if (mat.Get(AI_MATKEY_SHADING_MODEL, shading) == aiReturn_SUCCESS) { if (shading == aiShadingMode_Phong) { - materials[a].shading_model = "phong"; + material.shading_model = "phong"; } else if (shading == aiShadingMode_Blinn) { - materials[a].shading_model = "blinn"; + material.shading_model = "blinn"; } else if (shading == aiShadingMode_NoShading) { - materials[a].shading_model = "constant"; + material.shading_model = "constant"; } else if (shading == aiShadingMode_Gouraud) { - materials[a].shading_model = "lambert"; + material.shading_model = "lambert"; } } - ReadMaterialSurface(materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT); - if (!materials[a].ambient.texture.empty()) numTextures++; - ReadMaterialSurface(materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE); - if (!materials[a].diffuse.texture.empty()) numTextures++; - ReadMaterialSurface(materials[a].specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR); - if (!materials[a].specular.texture.empty()) numTextures++; - ReadMaterialSurface(materials[a].emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE); - if (!materials[a].emissive.texture.empty()) numTextures++; - ReadMaterialSurface(materials[a].reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE); - if (!materials[a].reflective.texture.empty()) numTextures++; - ReadMaterialSurface(materials[a].transparent, mat, aiTextureType_OPACITY, AI_MATKEY_COLOR_TRANSPARENT); - if (!materials[a].transparent.texture.empty()) numTextures++; - ReadMaterialSurface(materials[a].normal, mat, aiTextureType_NORMALS, NULL, 0, 0); - if (!materials[a].normal.texture.empty()) numTextures++; + if (ReadMaterialSurface(material.ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT)) + ++numTextures; + if (ReadMaterialSurface(material.diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE)) + ++numTextures; + if (ReadMaterialSurface(material.specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR)) + ++numTextures; + if (ReadMaterialSurface(material.emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE)) + ++numTextures; + if (ReadMaterialSurface(material.reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE)) + ++numTextures; + if (ReadMaterialSurface(material.transparent, mat, aiTextureType_OPACITY, AI_MATKEY_COLOR_TRANSPARENT)) + ++numTextures; + if (ReadMaterialSurface(material.normal, mat, aiTextureType_NORMALS, nullptr, 0, 0)) + ++numTextures; - materials[a].shininess.exist = mat->Get(AI_MATKEY_SHININESS, materials[a].shininess.value) == aiReturn_SUCCESS; - materials[a].transparency.exist = mat->Get(AI_MATKEY_OPACITY, materials[a].transparency.value) == aiReturn_SUCCESS; - materials[a].index_refraction.exist = mat->Get(AI_MATKEY_REFRACTI, materials[a].index_refraction.value) == aiReturn_SUCCESS; + material.shininess.exist = mat.Get(AI_MATKEY_SHININESS, material.shininess.value) == aiReturn_SUCCESS; + material.transparency.exist = mat.Get(AI_MATKEY_OPACITY, material.transparency.value) == aiReturn_SUCCESS; + material.index_refraction.exist = mat.Get(AI_MATKEY_REFRACTI, material.index_refraction.value) == aiReturn_SUCCESS; } // output textures if present if (numTextures > 0) { mOutput << startstr << "" << endstr; PushTag(); - for (std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it) { - const Material &mat = *it; - WriteImageEntry(mat.ambient, mat.name + "-ambient-image"); - WriteImageEntry(mat.diffuse, mat.name + "-diffuse-image"); - WriteImageEntry(mat.specular, mat.name + "-specular-image"); - WriteImageEntry(mat.emissive, mat.name + "-emission-image"); - WriteImageEntry(mat.reflective, mat.name + "-reflective-image"); - WriteImageEntry(mat.transparent, mat.name + "-transparent-image"); - WriteImageEntry(mat.normal, mat.name + "-normal-image"); + for (const Material &mat : materials) { + WriteImageEntry(mat.ambient, mat.id + "-ambient-image"); + WriteImageEntry(mat.diffuse, mat.id + "-diffuse-image"); + WriteImageEntry(mat.specular, mat.id + "-specular-image"); + WriteImageEntry(mat.emissive, mat.id + "-emission-image"); + WriteImageEntry(mat.reflective, mat.id + "-reflective-image"); + WriteImageEntry(mat.transparent, mat.id + "-transparent-image"); + WriteImageEntry(mat.normal, mat.id + "-normal-image"); } PopTag(); mOutput << startstr << "" << endstr; @@ -770,40 +751,39 @@ void ColladaExporter::WriteMaterials() { if (!materials.empty()) { mOutput << startstr << "" << endstr; PushTag(); - for (std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it) { - const Material &mat = *it; + for (const Material &mat : materials) { // this is so ridiculous it must be right - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "" << endstr; PushTag(); // write sampler- and surface params for the texture entries - WriteTextureParamEntry(mat.emissive, "emission", mat.name); - WriteTextureParamEntry(mat.ambient, "ambient", mat.name); - WriteTextureParamEntry(mat.diffuse, "diffuse", mat.name); - WriteTextureParamEntry(mat.specular, "specular", mat.name); - WriteTextureParamEntry(mat.reflective, "reflective", mat.name); - WriteTextureParamEntry(mat.transparent, "transparent", mat.name); - WriteTextureParamEntry(mat.normal, "normal", mat.name); + WriteTextureParamEntry(mat.emissive, "emission", mat.id); + WriteTextureParamEntry(mat.ambient, "ambient", mat.id); + WriteTextureParamEntry(mat.diffuse, "diffuse", mat.id); + WriteTextureParamEntry(mat.specular, "specular", mat.id); + WriteTextureParamEntry(mat.reflective, "reflective", mat.id); + WriteTextureParamEntry(mat.transparent, "transparent", mat.id); + WriteTextureParamEntry(mat.normal, "normal", mat.id); mOutput << startstr << "" << endstr; PushTag(); mOutput << startstr << "<" << mat.shading_model << ">" << endstr; PushTag(); - WriteTextureColorEntry(mat.emissive, "emission", mat.name + "-emission-sampler"); - WriteTextureColorEntry(mat.ambient, "ambient", mat.name + "-ambient-sampler"); - WriteTextureColorEntry(mat.diffuse, "diffuse", mat.name + "-diffuse-sampler"); - WriteTextureColorEntry(mat.specular, "specular", mat.name + "-specular-sampler"); + WriteTextureColorEntry(mat.emissive, "emission", mat.id + "-emission-sampler"); + WriteTextureColorEntry(mat.ambient, "ambient", mat.id + "-ambient-sampler"); + WriteTextureColorEntry(mat.diffuse, "diffuse", mat.id + "-diffuse-sampler"); + WriteTextureColorEntry(mat.specular, "specular", mat.id + "-specular-sampler"); WriteFloatEntry(mat.shininess, "shininess"); - WriteTextureColorEntry(mat.reflective, "reflective", mat.name + "-reflective-sampler"); - WriteTextureColorEntry(mat.transparent, "transparent", mat.name + "-transparent-sampler"); + WriteTextureColorEntry(mat.reflective, "reflective", mat.id + "-reflective-sampler"); + WriteTextureColorEntry(mat.transparent, "transparent", mat.id + "-transparent-sampler"); WriteFloatEntry(mat.transparency, "transparency"); WriteFloatEntry(mat.index_refraction, "index_of_refraction"); if (!mat.normal.texture.empty()) { - WriteTextureColorEntry(mat.normal, "bump", mat.name + "-normal-sampler"); + WriteTextureColorEntry(mat.normal, "bump", mat.id + "-normal-sampler"); } PopTag(); @@ -823,9 +803,9 @@ void ColladaExporter::WriteMaterials() { PushTag(); for (std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it) { const Material &mat = *it; - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; } @@ -852,20 +832,18 @@ void ColladaExporter::WriteControllerLibrary() { // Writes a skin controller of the given mesh void ColladaExporter::WriteController(size_t pIndex) { const aiMesh *mesh = mScene->mMeshes[pIndex]; - const std::string idstr = mesh->mName.length == 0 ? GetMeshId(pIndex) : mesh->mName.C_Str(); - const std::string idstrEscaped = XMLIDEncode(idstr); - - if (mesh->mNumFaces == 0 || mesh->mNumVertices == 0) + // Is there a skin controller? + if (mesh->mNumBones == 0 || mesh->mNumFaces == 0 || mesh->mNumVertices == 0) return; - if (mesh->mNumBones == 0) - return; + const std::string idstr = GetObjectUniqueId(AiObjectType::Mesh, pIndex); + const std::string namestr = GetObjectName(AiObjectType::Mesh, pIndex); - mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); // bind pose matrix @@ -882,20 +860,20 @@ void ColladaExporter::WriteController(size_t pIndex) { PopTag(); mOutput << startstr << "" << endstr; - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "mNumBones << "\">"; + mOutput << startstr << "mNumBones << "\">"; for (size_t i = 0; i < mesh->mNumBones; ++i) - mOutput << XMLIDEncode(mesh->mBones[i]->mName.C_Str()) << " "; + mOutput << GetBoneUniqueId(mesh->mBones[i]) << ' '; mOutput << "" << endstr; mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "mNumBones << "\" stride=\"" << 1 << "\">" << endstr; + mOutput << startstr << "mNumBones << "\" stride=\"" << 1 << "\">" << endstr; PushTag(); mOutput << startstr << "" << endstr; @@ -932,8 +910,8 @@ void ColladaExporter::WriteController(size_t pIndex) { mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "" << endstr; - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PopTag(); mOutput << startstr << "" << endstr; @@ -941,8 +919,8 @@ void ColladaExporter::WriteController(size_t pIndex) { mOutput << startstr << "mNumVertices << "\">" << endstr; PushTag(); - mOutput << startstr << "" << endstr; - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; mOutput << startstr << ""; @@ -1017,9 +995,8 @@ void ColladaExporter::WriteGeometryLibrary() { // Writes the given mesh void ColladaExporter::WriteGeometry(size_t pIndex) { const aiMesh *mesh = mScene->mMeshes[pIndex]; - const std::string idstr = mesh->mName.length == 0 ? GetMeshId(pIndex) : mesh->mName.C_Str(); - const std::string geometryName = XMLEscape(idstr); - const std::string geometryId = XMLIDEncode(idstr); + const std::string geometryId = GetObjectUniqueId(AiObjectType::Mesh, pIndex); + const std::string geometryName = GetObjectName(AiObjectType::Mesh, pIndex); if (mesh->mNumFaces == 0 || mesh->mNumVertices == 0) return; @@ -1032,15 +1009,15 @@ void ColladaExporter::WriteGeometry(size_t pIndex) { PushTag(); // Positions - WriteFloatArray(idstr + "-positions", FloatType_Vector, (ai_real *)mesh->mVertices, mesh->mNumVertices); + WriteFloatArray(geometryId + "-positions", FloatType_Vector, (ai_real *)mesh->mVertices, mesh->mNumVertices); // Normals, if any if (mesh->HasNormals()) - WriteFloatArray(idstr + "-normals", FloatType_Vector, (ai_real *)mesh->mNormals, mesh->mNumVertices); + WriteFloatArray(geometryId + "-normals", FloatType_Vector, (ai_real *)mesh->mNormals, mesh->mNumVertices); // texture coords for (size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) { if (mesh->HasTextureCoords(static_cast(a))) { - WriteFloatArray(idstr + "-tex" + to_string(a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2, + WriteFloatArray(geometryId + "-tex" + to_string(a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2, (ai_real *)mesh->mTextureCoords[a], mesh->mNumVertices); } } @@ -1048,7 +1025,7 @@ void ColladaExporter::WriteGeometry(size_t pIndex) { // vertex colors for (size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) { if (mesh->HasVertexColors(static_cast(a))) - WriteFloatArray(idstr + "-color" + to_string(a), FloatType_Color, (ai_real *)mesh->mColors[a], mesh->mNumVertices); + WriteFloatArray(geometryId + "-color" + to_string(a), FloatType_Color, (ai_real *)mesh->mColors[a], mesh->mNumVertices); } // assemble vertex structure @@ -1248,17 +1225,29 @@ void ColladaExporter::WriteFloatArray(const std::string &pIdString, FloatDataTyp // ------------------------------------------------------------------------------------------------ // Writes the scene library void ColladaExporter::WriteSceneLibrary() { - const std::string sceneName = XMLEscape(mScene->mRootNode->mName.C_Str()); - const std::string sceneId = XMLIDEncode(mScene->mRootNode->mName.C_Str()); + // Determine if we are using the aiScene root or our own + std::string sceneName("Scene"); + if (mAdd_root_node) { + mSceneId = MakeUniqueId(mUniqueIds, sceneName, std::string()); + mUniqueIds.insert(mSceneId); + } else { + mSceneId = GetNodeUniqueId(mScene->mRootNode); + sceneName = GetNodeName(mScene->mRootNode); + } mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); - // start recursive write at the root node - for (size_t a = 0; a < mScene->mRootNode->mNumChildren; ++a) - WriteNode(mScene, mScene->mRootNode->mChildren[a]); + if (mAdd_root_node) { + // Export the root node + WriteNode(mScene->mRootNode); + } else { + // Have already exported the root node + for (size_t a = 0; a < mScene->mRootNode->mNumChildren; ++a) + WriteNode(mScene->mRootNode->mChildren[a]); + } PopTag(); mOutput << startstr << "" << endstr; @@ -1272,20 +1261,10 @@ void ColladaExporter::WriteAnimationLibrary(size_t pIndex) { if (anim->mNumChannels == 0 && anim->mNumMeshChannels == 0 && anim->mNumMorphMeshChannels == 0) return; - const std::string animation_name_escaped = XMLEscape(anim->mName.C_Str()); - std::string idstr = anim->mName.C_Str(); - std::string ending = std::string("AnimId") + to_string(pIndex); - if (idstr.length() >= ending.length()) { - if (0 != idstr.compare(idstr.length() - ending.length(), ending.length(), ending)) { - idstr = idstr + ending; - } - } else { - idstr = idstr + ending; - } + const std::string animationNameEscaped = GetObjectName(AiObjectType::Animation, pIndex); + const std::string idstrEscaped = GetObjectUniqueId(AiObjectType::Animation, pIndex); - const std::string idstrEscaped = XMLIDEncode(idstr); - - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); std::string cur_node_idstr; @@ -1435,20 +1414,21 @@ void ColladaExporter::WriteAnimationsLibrary() { } // ------------------------------------------------------------------------------------------------ // Helper to find a bone by name in the scene -aiBone *findBone(const aiScene *scene, const char *name) { +aiBone *findBone(const aiScene *scene, const aiString &name) { for (size_t m = 0; m < scene->mNumMeshes; m++) { aiMesh *mesh = scene->mMeshes[m]; for (size_t b = 0; b < mesh->mNumBones; b++) { aiBone *bone = mesh->mBones[b]; - if (0 == strcmp(name, bone->mName.C_Str())) { + if (name == bone->mName) { return bone; } } } - return NULL; + return nullptr; } // ------------------------------------------------------------------------------------------------ +// Helper to find the node associated with a bone in the scene const aiNode *findBoneNode(const aiNode *aNode, const aiBone *bone) { if (aNode && bone && aNode->mName == bone->mName) { return aNode; @@ -1457,15 +1437,17 @@ const aiNode *findBoneNode(const aiNode *aNode, const aiBone *bone) { if (aNode && bone) { for (unsigned int i = 0; i < aNode->mNumChildren; ++i) { aiNode *aChild = aNode->mChildren[i]; - const aiNode *foundFromChild = 0; + const aiNode *foundFromChild = nullptr; if (aChild) { foundFromChild = findBoneNode(aChild, bone); - if (foundFromChild) return foundFromChild; + if (foundFromChild) { + return foundFromChild; + } } } } - return NULL; + return nullptr; } const aiNode *findSkeletonRootNode(const aiScene *scene, const aiMesh *mesh) { @@ -1476,7 +1458,7 @@ const aiNode *findSkeletonRootNode(const aiScene *scene, const aiMesh *mesh) { const aiNode *node = findBoneNode(scene->mRootNode, bone); if (node) { - while (node->mParent && findBone(scene, node->mParent->mName.C_Str()) != 0) { + while (node->mParent && findBone(scene, node->mParent->mName) != nullptr) { node = node->mParent; } topParentBoneNodes.insert(node); @@ -1496,45 +1478,36 @@ const aiNode *findSkeletonRootNode(const aiScene *scene, const aiMesh *mesh) { } } - return NULL; + return nullptr; } // ------------------------------------------------------------------------------------------------ // Recursively writes the given node -void ColladaExporter::WriteNode(const aiScene *pScene, aiNode *pNode) { - // the node must have a name - if (pNode->mName.length == 0) { - std::stringstream ss; - ss << "Node_" << pNode; - pNode->mName.Set(ss.str()); - } - +void ColladaExporter::WriteNode(const aiNode *pNode) { // If the node is associated with a bone, it is a joint node (JOINT) // otherwise it is a normal node (NODE) + // Assimp-specific: nodes with no name cannot be associated with bones const char *node_type; bool is_joint, is_skeleton_root = false; - if (nullptr == findBone(pScene, pNode->mName.C_Str())) { + if (pNode->mName.length == 0 || nullptr == findBone(mScene, pNode->mName)) { node_type = "NODE"; is_joint = false; } else { node_type = "JOINT"; is_joint = true; - if (!pNode->mParent || nullptr == findBone(pScene, pNode->mParent->mName.C_Str())) { + if (!pNode->mParent || nullptr == findBone(mScene, pNode->mParent->mName)) { is_skeleton_root = true; } } - const std::string node_id = XMLIDEncode(pNode->mName.data); - const std::string node_name = XMLEscape(pNode->mName.data); + const std::string node_id = GetNodeUniqueId(pNode); + const std::string node_name = GetNodeName(pNode); mOutput << startstr << "" << endstr; PushTag(); @@ -1573,14 +1546,14 @@ void ColladaExporter::WriteNode(const aiScene *pScene, aiNode *pNode) { //check if it is a camera node for (size_t i = 0; i < mScene->mNumCameras; i++) { if (mScene->mCameras[i]->mName == pNode->mName) { - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; break; } } //check if it is a light node for (size_t i = 0; i < mScene->mNumLights; i++) { if (mScene->mLights[i]->mName == pNode->mName) { - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; break; } } @@ -1593,22 +1566,22 @@ void ColladaExporter::WriteNode(const aiScene *pScene, aiNode *pNode) { if (mesh->mNumFaces == 0 || mesh->mNumVertices == 0) continue; - const std::string meshName = mesh->mName.length == 0 ? GetMeshId(pNode->mMeshes[a]) : mesh->mName.C_Str(); + const std::string meshId = GetObjectUniqueId(AiObjectType::Mesh, pNode->mMeshes[a]); if (mesh->mNumBones == 0) { - mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; PushTag(); } else { mOutput << startstr - << "" + << "" << endstr; PushTag(); // note! this mFoundSkeletonRootNodeID some how affects animation, it makes the mesh attaches to armature skeleton root node. // use the first bone to find skeleton root - const aiNode *skeletonRootBoneNode = findSkeletonRootNode(pScene, mesh); + const aiNode *skeletonRootBoneNode = findSkeletonRootNode(mScene, mesh); if (skeletonRootBoneNode) { - mFoundSkeletonRootNodeID = XMLIDEncode(skeletonRootBoneNode->mName.C_Str()); + mFoundSkeletonRootNodeID = GetNodeUniqueId(skeletonRootBoneNode); } mOutput << startstr << "#" << mFoundSkeletonRootNodeID << "" << endstr; } @@ -1616,7 +1589,7 @@ void ColladaExporter::WriteNode(const aiScene *pScene, aiNode *pNode) { PushTag(); mOutput << startstr << "" << endstr; PushTag(); - mOutput << startstr << "mMaterialIndex].name) << "\">" << endstr; + mOutput << startstr << "mMaterialIndex) << "\">" << endstr; PushTag(); for (size_t aa = 0; aa < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++aa) { if (mesh->HasTextureCoords(static_cast(aa))) @@ -1641,11 +1614,135 @@ void ColladaExporter::WriteNode(const aiScene *pScene, aiNode *pNode) { // recurse into subnodes for (size_t a = 0; a < pNode->mNumChildren; ++a) - WriteNode(pScene, pNode->mChildren[a]); + WriteNode(pNode->mChildren[a]); PopTag(); mOutput << startstr << "" << endstr; } +void ColladaExporter::CreateNodeIds(const aiNode *node) { + GetNodeUniqueId(node); + for (size_t a = 0; a < node->mNumChildren; ++a) + CreateNodeIds(node->mChildren[a]); +} + +std::string ColladaExporter::GetNodeUniqueId(const aiNode *node) { + // Use the pointer as the key. This is safe because the scene is immutable. + auto idIt = mNodeIdMap.find(node); + if (idIt != mNodeIdMap.cend()) + return idIt->second; + + // Prefer the requested Collada Id if extant + std::string idStr; + aiString origId; + if (node->mMetaData && node->mMetaData->Get(AI_METADATA_COLLADA_ID, origId)) { + idStr = origId.C_Str(); + } else { + idStr = node->mName.C_Str(); + } + // Make sure the requested id is valid + if (idStr.empty()) + idStr = "node"; + else + idStr = XMLIDEncode(idStr); + + // Ensure it's unique + idStr = MakeUniqueId(mUniqueIds, idStr, std::string()); + mUniqueIds.insert(idStr); + mNodeIdMap.insert(std::make_pair(node, idStr)); + return idStr; +} + +std::string ColladaExporter::GetNodeName(const aiNode *node) { + + return XMLEscape(node->mName.C_Str()); +} + +std::string ColladaExporter::GetBoneUniqueId(const aiBone *bone) { + // Find the Node that is this Bone + const aiNode *boneNode = findBoneNode(mScene->mRootNode, bone); + if (boneNode == nullptr) + return std::string(); + + return GetNodeUniqueId(boneNode); +} + +std::string ColladaExporter::GetObjectUniqueId(AiObjectType type, size_t pIndex) { + auto idIt = GetObjectIdMap(type).find(pIndex); + if (idIt != GetObjectIdMap(type).cend()) + return idIt->second; + + // Not seen this object before, create and add + NameIdPair result = AddObjectIndexToMaps(type, pIndex); + return result.second; +} + +std::string ColladaExporter::GetObjectName(AiObjectType type, size_t pIndex) { + auto objectName = GetObjectNameMap(type).find(pIndex); + if (objectName != GetObjectNameMap(type).cend()) + return objectName->second; + + // Not seen this object before, create and add + NameIdPair result = AddObjectIndexToMaps(type, pIndex); + return result.first; +} + +// Determine unique id and add the name and id to the maps +// @param type object type +// @param index object index +// @param name in/out. Caller to set the original name if known. +// @param idStr in/out. Caller to set the preferred id if known. +ColladaExporter::NameIdPair ColladaExporter::AddObjectIndexToMaps(AiObjectType type, size_t index) { + + std::string name; + std::string idStr; + std::string idPostfix; + + // Get the name and id postfix + switch (type) { + case AiObjectType::Mesh: name = mScene->mMeshes[index]->mName.C_Str(); break; + case AiObjectType::Material: name = mScene->mMaterials[index]->GetName().C_Str(); break; + case AiObjectType::Animation: name = mScene->mAnimations[index]->mName.C_Str(); break; + case AiObjectType::Light: + name = mScene->mLights[index]->mName.C_Str(); + idPostfix = "-light"; + break; + case AiObjectType::Camera: + name = mScene->mCameras[index]->mName.C_Str(); + idPostfix = "-camera"; + break; + case AiObjectType::Count: throw std::logic_error("ColladaExporter::AiObjectType::Count is not an object type"); + } + + if (name.empty()) { + // Default ids if empty name + switch (type) { + case AiObjectType::Mesh: idStr = std::string("mesh_"); break; + case AiObjectType::Material: idStr = std::string("material_"); break; // This one should never happen + case AiObjectType::Animation: idStr = std::string("animation_"); break; + case AiObjectType::Light: idStr = std::string("light_"); break; + case AiObjectType::Camera: idStr = std::string("camera_"); break; + case AiObjectType::Count: throw std::logic_error("ColladaExporter::AiObjectType::Count is not an object type"); + } + idStr.append(to_string(index)); + } else { + idStr = XMLIDEncode(name); + } + + if (!name.empty()) + name = XMLEscape(name); + + idStr = MakeUniqueId(mUniqueIds, idStr, idPostfix); + + // Add to maps + mUniqueIds.insert(idStr); + GetObjectIdMap(type).insert(std::make_pair(index, idStr)); + GetObjectNameMap(type).insert(std::make_pair(index, name)); + + return std::make_pair(name, idStr); +} + +} // end of namespace Assimp + #endif #endif diff --git a/code/AssetLib/Collada/ColladaExporter.h b/code/AssetLib/Collada/ColladaExporter.h index f6c66e279..e9a3530ae 100644 --- a/code/AssetLib/Collada/ColladaExporter.h +++ b/code/AssetLib/Collada/ColladaExporter.h @@ -48,28 +48,28 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include struct aiScene; struct aiNode; +struct aiLight; +struct aiBone; -namespace Assimp -{ +namespace Assimp { + +class IOSystem; /// Helper class to export a given scene to a Collada file. Just for my personal /// comfort when implementing it. -class ColladaExporter -{ +class ColladaExporter { public: /// Constructor for a specific scene to export - ColladaExporter( const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file); + ColladaExporter(const aiScene *pScene, IOSystem *pIOSystem, const std::string &path, const std::string &file); /// Destructor virtual ~ColladaExporter(); @@ -107,51 +107,87 @@ protected: void WriteControllerLibrary(); /// Writes a skin controller of the given mesh - void WriteController( size_t pIndex); + void WriteController(size_t pIndex); /// Writes the geometry library void WriteGeometryLibrary(); /// Writes the given mesh - void WriteGeometry( size_t pIndex); + void WriteGeometry(size_t pIndex); //enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color, FloatType_Mat4x4, FloatType_Weight }; // customized to add animation related type - enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color, FloatType_Mat4x4, FloatType_Weight, FloatType_Time }; + enum FloatDataType { FloatType_Vector, + FloatType_TexCoord2, + FloatType_TexCoord3, + FloatType_Color, + FloatType_Mat4x4, + FloatType_Weight, + FloatType_Time }; /// Writes a float array of the given type - void WriteFloatArray( const std::string& pIdString, FloatDataType pType, const ai_real* pData, size_t pElementCount); + void WriteFloatArray(const std::string &pIdString, FloatDataType pType, const ai_real *pData, size_t pElementCount); /// Writes the scene library void WriteSceneLibrary(); - // customized, Writes the animation library - void WriteAnimationsLibrary(); - void WriteAnimationLibrary( size_t pIndex); - std::string mFoundSkeletonRootNodeID = "skeleton_root"; // will be replaced by found node id in the WriteNode call. - + // customized, Writes the animation library + void WriteAnimationsLibrary(); + void WriteAnimationLibrary(size_t pIndex); + std::string mFoundSkeletonRootNodeID = "skeleton_root"; // will be replaced by found node id in the WriteNode call. + /// Recursively writes the given node - void WriteNode( const aiScene* scene, aiNode* pNode); + void WriteNode(const aiNode *pNode); /// Enters a new xml element, which increases the indentation - void PushTag() { startstr.append( " "); } + void PushTag() { startstr.append(" "); } /// Leaves an element, decreasing the indentation - void PopTag() { - ai_assert( startstr.length() > 1); - startstr.erase( startstr.length() - 2); + void PopTag() { + ai_assert(startstr.length() > 1); + startstr.erase(startstr.length() - 2); } - /// Creates a mesh ID for the given mesh - std::string GetMeshId( size_t pIndex) const { - return std::string( "meshId" ) + to_string(pIndex); - } + void CreateNodeIds(const aiNode *node); + + /// Get or Create a unique Node ID string for the given Node + std::string GetNodeUniqueId(const aiNode *node); + std::string GetNodeName(const aiNode *node); + + std::string GetBoneUniqueId(const aiBone *bone); + + enum class AiObjectType { + Mesh, + Material, + Animation, + Light, + Camera, + Count, + }; + /// Get or Create a unique ID string for the given scene object index + std::string GetObjectUniqueId(AiObjectType type, size_t pIndex); + /// Get or Create a name string for the given scene object index + std::string GetObjectName(AiObjectType type, size_t pIndex); + + typedef std::map IndexIdMap; + typedef std::pair NameIdPair; + NameIdPair AddObjectIndexToMaps(AiObjectType type, size_t pIndex); + + // Helpers + inline IndexIdMap &GetObjectIdMap(AiObjectType type) { return mObjectIdMap[static_cast(type)]; } + inline IndexIdMap &GetObjectNameMap(AiObjectType type) { return mObjectNameMap[static_cast(type)]; } + +private: + std::unordered_set mUniqueIds; // Cache of used unique ids + std::map mNodeIdMap; // Cache of encoded node and bone ids + std::array(AiObjectType::Count)> mObjectIdMap; // Cache of encoded unique IDs + std::array(AiObjectType::Count)> mObjectNameMap; // Cache of encoded names public: /// Stringstream to write all output into std::stringstream mOutput; /// The IOSystem for output - IOSystem* mIOSystem; + IOSystem *mIOSystem; /// Path of the directory where the scene will be exported const std::string mPath; @@ -160,63 +196,62 @@ public: const std::string mFile; /// The scene to be written - const aiScene* mScene; - bool mSceneOwned; + const aiScene *const mScene; + std::string mSceneId; + bool mAdd_root_node = false; /// current line start string, contains the current indentation for simple stream insertion std::string startstr; /// current line end string for simple stream insertion - std::string endstr; + const std::string endstr; - // pair of color and texture - texture precedences color - struct Surface - { - bool exist; - aiColor4D color; - std::string texture; - size_t channel; - Surface() { exist = false; channel = 0; } - }; + // pair of color and texture - texture precedences color + struct Surface { + bool exist; + aiColor4D color; + std::string texture; + size_t channel; + Surface() { + exist = false; + channel = 0; + } + }; - struct Property - { - bool exist; - ai_real value; - Property() - : exist(false) - , value(0.0) - {} - }; + struct Property { + bool exist; + ai_real value; + Property() : + exist(false), + value(0.0) {} + }; - // summarize a material in an convenient way. - struct Material - { - std::string name; - std::string shading_model; - Surface ambient, diffuse, specular, emissive, reflective, transparent, normal; - Property shininess, transparency, index_refraction; + // summarize a material in an convenient way. + struct Material { + std::string id; + std::string name; + std::string shading_model; + Surface ambient, diffuse, specular, emissive, reflective, transparent, normal; + Property shininess, transparency, index_refraction; - Material() {} - }; + Material() {} + }; - std::vector materials; - - std::map textures; + std::map textures; public: - /// Dammit C++ - y u no compile two-pass? No I have to add all methods below the struct definitions - /// Reads a single surface entry from the given material keys - void ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex); - /// Writes an image entry for the given surface - void WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd); - /// Writes the two parameters necessary for referencing a texture in an effect entry - void WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName); - /// Writes a color-or-texture entry into an effect definition - void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName); - /// Writes a scalar property - void WriteFloatEntry( const Property& pProperty, const std::string& pTypeName); + /// Dammit C++ - y u no compile two-pass? No I have to add all methods below the struct definitions + /// Reads a single surface entry from the given material keys + bool ReadMaterialSurface(Surface &poSurface, const aiMaterial &pSrcMat, aiTextureType pTexture, const char *pKey, size_t pType, size_t pIndex); + /// Writes an image entry for the given surface + void WriteImageEntry(const Surface &pSurface, const std::string &imageId); + /// Writes the two parameters necessary for referencing a texture in an effect entry + void WriteTextureParamEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &materialId); + /// Writes a color-or-texture entry into an effect definition + void WriteTextureColorEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &imageId); + /// Writes a scalar property + void WriteFloatEntry(const Property &pProperty, const std::string &pTypeName); }; -} +} // namespace Assimp #endif // !! AI_COLLADAEXPORTER_H_INC diff --git a/code/AssetLib/Collada/ColladaHelper.cpp b/code/AssetLib/Collada/ColladaHelper.cpp index 6f1fed366..50d70e640 100644 --- a/code/AssetLib/Collada/ColladaHelper.cpp +++ b/code/AssetLib/Collada/ColladaHelper.cpp @@ -43,8 +43,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ColladaHelper.h" -#include #include +#include namespace Assimp { namespace Collada { @@ -63,42 +63,35 @@ const MetaKeyPairVector &GetColladaAssimpMetaKeys() { const MetaKeyPairVector MakeColladaAssimpMetaKeysCamelCase() { MetaKeyPairVector result = MakeColladaAssimpMetaKeys(); - for (auto &val : result) - { + for (auto &val : result) { ToCamelCase(val.first); } return result; }; -const MetaKeyPairVector &GetColladaAssimpMetaKeysCamelCase() -{ +const MetaKeyPairVector &GetColladaAssimpMetaKeysCamelCase() { static const MetaKeyPairVector result = MakeColladaAssimpMetaKeysCamelCase(); return result; } // ------------------------------------------------------------------------------------------------ // Convert underscore_separated to CamelCase: "authoring_tool" becomes "AuthoringTool" -void ToCamelCase(std::string &text) -{ +void ToCamelCase(std::string &text) { if (text.empty()) return; // Capitalise first character auto it = text.begin(); (*it) = ToUpper(*it); ++it; - for (/*started above*/ ; it != text.end(); /*iterated below*/) - { - if ((*it) == '_') - { + for (/*started above*/; it != text.end(); /*iterated below*/) { + if ((*it) == '_') { it = text.erase(it); if (it != text.end()) (*it) = ToUpper(*it); - } - else - { + } else { // Make lower case (*it) = ToLower(*it); - ++it; + ++it; } } } diff --git a/code/AssetLib/Collada/ColladaHelper.h b/code/AssetLib/Collada/ColladaHelper.h index c6e2e7ddd..f41691606 100644 --- a/code/AssetLib/Collada/ColladaHelper.h +++ b/code/AssetLib/Collada/ColladaHelper.h @@ -45,31 +45,28 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef AI_COLLADAHELPER_H_INC #define AI_COLLADAHELPER_H_INC -#include -#include -#include -#include #include -#include #include +#include +#include +#include +#include +#include struct aiMaterial; -namespace Assimp { -namespace Collada { +namespace Assimp { +namespace Collada { /** Collada file versions which evolved during the years ... */ -enum FormatVersion -{ +enum FormatVersion { FV_1_5_n, FV_1_4_n, FV_1_3_n }; - /** Transformation types that can be applied to a node */ -enum TransformType -{ +enum TransformType { TF_LOOKAT, TF_ROTATE, TF_TRANSLATE, @@ -79,10 +76,9 @@ enum TransformType }; /** Different types of input data to a vertex or face */ -enum InputType -{ +enum InputType { IT_Invalid, - IT_Vertex, // special type for per-index data referring to the element carrying the per-vertex data. + IT_Vertex, // special type for per-index data referring to the element carrying the per-vertex data. IT_Position, IT_Normal, IT_Texcoord, @@ -92,15 +88,13 @@ enum InputType }; /** Supported controller types */ -enum ControllerType -{ +enum ControllerType { Skin, Morph }; /** Supported morph methods */ -enum MorphMethod -{ +enum MorphMethod { Normalized, Relative }; @@ -118,24 +112,21 @@ const MetaKeyPairVector &GetColladaAssimpMetaKeysCamelCase(); void ToCamelCase(std::string &text); /** Contains all data for one of the different transformation types */ -struct Transform -{ - std::string mID; ///< SID of the transform step, by which anim channels address their target node +struct Transform { + std::string mID; ///< SID of the transform step, by which anim channels address their target node TransformType mType; ai_real f[16]; ///< Interpretation of data depends on the type of the transformation }; /** A collada camera. */ -struct Camera -{ - Camera() - : mOrtho (false) - , mHorFov (10e10f) - , mVerFov (10e10f) - , mAspect (10e10f) - , mZNear (0.1f) - , mZFar (1000.f) - {} +struct Camera { + Camera() : + mOrtho(false), + mHorFov(10e10f), + mVerFov(10e10f), + mAspect(10e10f), + mZNear(0.1f), + mZFar(1000.f) {} // Name of camera std::string mName; @@ -159,19 +150,17 @@ struct Camera #define ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET 1e9f /** A collada light source. */ -struct Light -{ - Light() - : mType (aiLightSource_UNDEFINED) - , mAttConstant (1.f) - , mAttLinear (0.f) - , mAttQuadratic (0.f) - , mFalloffAngle (180.f) - , mFalloffExponent (0.f) - , mPenumbraAngle (ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET) - , mOuterAngle (ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET) - , mIntensity (1.f) - {} +struct Light { + Light() : + mType(aiLightSource_UNDEFINED), + mAttConstant(1.f), + mAttLinear(0.f), + mAttQuadratic(0.f), + mFalloffAngle(180.f), + mFalloffExponent(0.f), + mPenumbraAngle(ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET), + mOuterAngle(ASSIMP_COLLADA_LIGHT_ANGLE_NOT_SET), + mIntensity(1.f) {} //! Type of the light source aiLightSourceType + ambient unsigned int mType; @@ -180,7 +169,7 @@ struct Light aiColor3D mColor; //! Light attenuation - ai_real mAttConstant,mAttLinear,mAttQuadratic; + ai_real mAttConstant, mAttLinear, mAttQuadratic; //! Spot light falloff ai_real mFalloffAngle; @@ -198,12 +187,10 @@ struct Light }; /** Short vertex index description */ -struct InputSemanticMapEntry -{ - InputSemanticMapEntry() - : mSet(0) - , mType(IT_Invalid) - {} +struct InputSemanticMapEntry { + InputSemanticMapEntry() : + mSet(0), + mType(IT_Invalid) {} //! Index of set, optional unsigned int mSet; @@ -213,8 +200,7 @@ struct InputSemanticMapEntry }; /** Table to map from effect to vertex input semantics */ -struct SemanticMappingTable -{ +struct SemanticMappingTable { //! Name of material std::string mMatName; @@ -222,7 +208,7 @@ struct SemanticMappingTable std::map mMap; //! For std::find - bool operator == (const std::string& s) const { + bool operator==(const std::string &s) const { return s == mMatName; } }; @@ -230,8 +216,7 @@ struct SemanticMappingTable /** A reference to a mesh inside a node, including materials assigned to the various subgroups. * The ID refers to either a mesh or a controller which specifies the mesh */ -struct MeshInstance -{ +struct MeshInstance { ///< ID of the mesh or controller to be instanced std::string mMeshOrController; @@ -240,34 +225,30 @@ struct MeshInstance }; /** A reference to a camera inside a node*/ -struct CameraInstance -{ - ///< ID of the camera +struct CameraInstance { + ///< ID of the camera std::string mCamera; }; /** A reference to a light inside a node*/ -struct LightInstance -{ - ///< ID of the camera +struct LightInstance { + ///< ID of the camera std::string mLight; }; /** A reference to a node inside a node*/ -struct NodeInstance -{ - ///< ID of the node +struct NodeInstance { + ///< ID of the node std::string mNode; }; /** A node in a scene hierarchy */ -struct Node -{ +struct Node { std::string mName; std::string mID; std::string mSID; - Node* mParent; - std::vector mChildren; + Node *mParent; + std::vector mChildren; /** Operations in order to calculate the resulting transformation to parent. */ std::vector mTransforms; @@ -288,80 +269,83 @@ struct Node std::string mPrimaryCamera; //! Constructor. Begin with a zero parent - Node() - : mParent( nullptr ){ + Node() : + mParent(nullptr) { // empty } //! Destructor: delete all children subsequently ~Node() { - for( std::vector::iterator it = mChildren.begin(); it != mChildren.end(); ++it) + for (std::vector::iterator it = mChildren.begin(); it != mChildren.end(); ++it) delete *it; } }; /** Data source array: either floats or strings */ -struct Data -{ +struct Data { bool mIsStringArray; std::vector mValues; std::vector mStrings; }; /** Accessor to a data array */ -struct Accessor -{ - size_t mCount; // in number of objects - size_t mSize; // size of an object, in elements (floats or strings, mostly 1) - size_t mOffset; // in number of values - size_t mStride; // Stride in number of values +struct Accessor { + size_t mCount; // in number of objects + size_t mSize; // size of an object, in elements (floats or strings, mostly 1) + size_t mOffset; // in number of values + size_t mStride; // Stride in number of values std::vector mParams; // names of the data streams in the accessors. Empty string tells to ignore. size_t mSubOffset[4]; // Suboffset inside the object for the common 4 elements. For a vector, that's XYZ, for a color RGBA and so on. - // For example, SubOffset[0] denotes which of the values inside the object is the vector X component. - std::string mSource; // URL of the source array - mutable const Data* mData; // Pointer to the source array, if resolved. NULL else + // For example, SubOffset[0] denotes which of the values inside the object is the vector X component. + std::string mSource; // URL of the source array + mutable const Data *mData; // Pointer to the source array, if resolved. NULL else - Accessor() - { - mCount = 0; mSize = 0; mOffset = 0; mStride = 0; mData = NULL; + Accessor() { + mCount = 0; + mSize = 0; + mOffset = 0; + mStride = 0; + mData = NULL; mSubOffset[0] = mSubOffset[1] = mSubOffset[2] = mSubOffset[3] = 0; } }; /** A single face in a mesh */ -struct Face -{ +struct Face { std::vector mIndices; }; /** An input channel for mesh data, referring to a single accessor */ -struct InputChannel -{ - InputType mType; // Type of the data - size_t mIndex; // Optional index, if multiple sets of the same data type are given - size_t mOffset; // Index offset in the indices array of per-face indices. Don't ask, can't explain that any better. +struct InputChannel { + InputType mType; // Type of the data + size_t mIndex; // Optional index, if multiple sets of the same data type are given + size_t mOffset; // Index offset in the indices array of per-face indices. Don't ask, can't explain that any better. std::string mAccessor; // ID of the accessor where to read the actual values from. - mutable const Accessor* mResolved; // Pointer to the accessor, if resolved. NULL else + mutable const Accessor *mResolved; // Pointer to the accessor, if resolved. NULL else - InputChannel() { mType = IT_Invalid; mIndex = 0; mOffset = 0; mResolved = NULL; } + InputChannel() { + mType = IT_Invalid; + mIndex = 0; + mOffset = 0; + mResolved = NULL; + } }; /** Subset of a mesh with a certain material */ -struct SubMesh -{ +struct SubMesh { std::string mMaterial; ///< subgroup identifier size_t mNumFaces; ///< number of faces in this submesh }; /** Contains data for a single mesh */ -struct Mesh -{ - Mesh() - { - for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i) +struct Mesh { + Mesh(const std::string &id) : + mId(id) { + for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) mNumUVComponents[i] = 2; } + const std::string mId; std::string mName; // just to check if there's some sophisticated addressing involved... @@ -377,7 +361,7 @@ struct Mesh std::vector mTangents; std::vector mBitangents; std::vector mTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS]; - std::vector mColors[AI_MAX_NUMBER_OF_COLOR_SETS]; + std::vector mColors[AI_MAX_NUMBER_OF_COLOR_SETS]; unsigned int mNumUVComponents[AI_MAX_NUMBER_OF_TEXTURECOORDS]; @@ -394,8 +378,7 @@ struct Mesh }; /** Which type of primitives the ReadPrimitives() function is going to read */ -enum PrimitiveType -{ +enum PrimitiveType { Prim_Invalid, Prim_Lines, Prim_LineStrip, @@ -407,8 +390,7 @@ enum PrimitiveType }; /** A skeleton controller to deform a mesh with the use of joints */ -struct Controller -{ +struct Controller { // controller type ControllerType mType; @@ -436,36 +418,32 @@ struct Controller std::vector mWeightCounts; // JointIndex-WeightIndex pairs for all vertices - std::vector< std::pair > mWeights; + std::vector> mWeights; std::string mMorphTarget; std::string mMorphWeight; }; /** A collada material. Pretty much the only member is a reference to an effect. */ -struct Material -{ +struct Material { std::string mName; std::string mEffect; }; /** Type of the effect param */ -enum ParamType -{ +enum ParamType { Param_Sampler, Param_Surface }; /** A param for an effect. Might be of several types, but they all just refer to each other, so I summarize them */ -struct EffectParam -{ +struct EffectParam { ParamType mType; std::string mReference; // to which other thing the param is referring to. }; /** Shading type supported by the standard effect spec of Collada */ -enum ShadeType -{ +enum ShadeType { Shade_Invalid, Shade_Constant, Shade_Lambert, @@ -474,18 +452,16 @@ enum ShadeType }; /** Represents a texture sampler in collada */ -struct Sampler -{ - Sampler() - : mWrapU (true) - , mWrapV (true) - , mMirrorU () - , mMirrorV () - , mOp (aiTextureOp_Multiply) - , mUVId (UINT_MAX) - , mWeighting (1.f) - , mMixWithPrevious (1.f) - {} +struct Sampler { + Sampler() : + mWrapU(true), + mWrapV(true), + mMirrorU(), + mMirrorV(), + mOp(aiTextureOp_Multiply), + mUVId(UINT_MAX), + mWeighting(1.f), + mMixWithPrevious(1.f) {} /** Name of image reference */ @@ -537,18 +513,17 @@ struct Sampler /** A collada effect. Can contain about anything according to the Collada spec, but we limit our version to a reasonable subset. */ -struct Effect -{ +struct Effect { // Shading mode ShadeType mShadeType; // Colors aiColor4D mEmissive, mAmbient, mDiffuse, mSpecular, - mTransparent, mReflective; + mTransparent, mReflective; // Textures Sampler mTexEmissive, mTexAmbient, mTexDiffuse, mTexSpecular, - mTexTransparent, mTexBump, mTexReflective; + mTexTransparent, mTexBump, mTexReflective; // Scalar factory ai_real mShininess, mRefractIndex, mReflectivity; @@ -566,30 +541,28 @@ struct Effect // Double-sided? bool mDoubleSided, mWireframe, mFaceted; - Effect() - : mShadeType (Shade_Phong) - , mEmissive ( 0, 0, 0, 1) - , mAmbient ( 0.1f, 0.1f, 0.1f, 1) - , mDiffuse ( 0.6f, 0.6f, 0.6f, 1) - , mSpecular ( 0.4f, 0.4f, 0.4f, 1) - , mTransparent ( 0, 0, 0, 1) - , mShininess (10.0f) - , mRefractIndex (1.f) - , mReflectivity (0.f) - , mTransparency (1.f) - , mHasTransparency (false) - , mRGBTransparency(false) - , mInvertTransparency(false) - , mDoubleSided (false) - , mWireframe (false) - , mFaceted (false) - { + Effect() : + mShadeType(Shade_Phong), + mEmissive(0, 0, 0, 1), + mAmbient(0.1f, 0.1f, 0.1f, 1), + mDiffuse(0.6f, 0.6f, 0.6f, 1), + mSpecular(0.4f, 0.4f, 0.4f, 1), + mTransparent(0, 0, 0, 1), + mShininess(10.0f), + mRefractIndex(1.f), + mReflectivity(0.f), + mTransparency(1.f), + mHasTransparency(false), + mRGBTransparency(false), + mInvertTransparency(false), + mDoubleSided(false), + mWireframe(false), + mFaceted(false) { } }; /** An image, meaning texture */ -struct Image -{ +struct Image { std::string mFileName; /** Embedded image data */ @@ -600,8 +573,7 @@ struct Image }; /** An animation channel. */ -struct AnimationChannel -{ +struct AnimationChannel { /** URL of the data to animate. Could be about anything, but we support only the * "NodeID/TransformID.SubElement" notation */ @@ -620,8 +592,7 @@ struct AnimationChannel }; /** An animation. Container for 0-x animation channels or 0-x animations */ -struct Animation -{ +struct Animation { /** Anim name */ std::string mName; @@ -629,96 +600,86 @@ struct Animation std::vector mChannels; /** the sub-animations, if any */ - std::vector mSubAnims; + std::vector mSubAnims; /** Destructor */ - ~Animation() - { - for( std::vector::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it) + ~Animation() { + for (std::vector::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it) delete *it; } - /** Collect all channels in the animation hierarchy into a single channel list. */ - void CollectChannelsRecursively(std::vector &channels) - { - channels.insert(channels.end(), mChannels.begin(), mChannels.end()); + /** Collect all channels in the animation hierarchy into a single channel list. */ + void CollectChannelsRecursively(std::vector &channels) { + channels.insert(channels.end(), mChannels.begin(), mChannels.end()); - for (std::vector::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it) - { - Animation *pAnim = (*it); + for (std::vector::iterator it = mSubAnims.begin(); it != mSubAnims.end(); ++it) { + Animation *pAnim = (*it); - pAnim->CollectChannelsRecursively(channels); - } - } + pAnim->CollectChannelsRecursively(channels); + } + } - /** Combine all single-channel animations' channel into the same (parent) animation channel list. */ - void CombineSingleChannelAnimations() - { - CombineSingleChannelAnimationsRecursively(this); - } + /** Combine all single-channel animations' channel into the same (parent) animation channel list. */ + void CombineSingleChannelAnimations() { + CombineSingleChannelAnimationsRecursively(this); + } - void CombineSingleChannelAnimationsRecursively(Animation *pParent) - { - std::set childrenTargets; - bool childrenAnimationsHaveDifferentChannels = true; + void CombineSingleChannelAnimationsRecursively(Animation *pParent) { + std::set childrenTargets; + bool childrenAnimationsHaveDifferentChannels = true; - for (std::vector::iterator it = pParent->mSubAnims.begin(); it != pParent->mSubAnims.end();) - { - Animation *anim = *it; - CombineSingleChannelAnimationsRecursively(anim); + for (std::vector::iterator it = pParent->mSubAnims.begin(); it != pParent->mSubAnims.end();) { + Animation *anim = *it; + CombineSingleChannelAnimationsRecursively(anim); - if (childrenAnimationsHaveDifferentChannels && anim->mChannels.size() == 1 && - childrenTargets.find(anim->mChannels[0].mTarget) == childrenTargets.end()) { - childrenTargets.insert(anim->mChannels[0].mTarget); - } else { - childrenAnimationsHaveDifferentChannels = false; - } + if (childrenAnimationsHaveDifferentChannels && anim->mChannels.size() == 1 && + childrenTargets.find(anim->mChannels[0].mTarget) == childrenTargets.end()) { + childrenTargets.insert(anim->mChannels[0].mTarget); + } else { + childrenAnimationsHaveDifferentChannels = false; + } - ++it; - } + ++it; + } - // We only want to combine animations if they have different channels - if (childrenAnimationsHaveDifferentChannels) - { - for (std::vector::iterator it = pParent->mSubAnims.begin(); it != pParent->mSubAnims.end();) - { - Animation *anim = *it; + // We only want to combine animations if they have different channels + if (childrenAnimationsHaveDifferentChannels) { + for (std::vector::iterator it = pParent->mSubAnims.begin(); it != pParent->mSubAnims.end();) { + Animation *anim = *it; - pParent->mChannels.push_back(anim->mChannels[0]); + pParent->mChannels.push_back(anim->mChannels[0]); - it = pParent->mSubAnims.erase(it); + it = pParent->mSubAnims.erase(it); - delete anim; - continue; - } - } - } + delete anim; + continue; + } + } + } }; /** Description of a collada animation channel which has been determined to affect the current node */ -struct ChannelEntry -{ - const Collada::AnimationChannel* mChannel; ///> the source channel +struct ChannelEntry { + const Collada::AnimationChannel *mChannel; ///> the source channel std::string mTargetId; - std::string mTransformId; // the ID of the transformation step of the node which is influenced + std::string mTransformId; // the ID of the transformation step of the node which is influenced size_t mTransformIndex; // Index into the node's transform chain to apply the channel to size_t mSubElement; // starting index inside the transform data // resolved data references - const Collada::Accessor* mTimeAccessor; ///> Collada accessor to the time values - const Collada::Data* mTimeData; ///> Source data array for the time values - const Collada::Accessor* mValueAccessor; ///> Collada accessor to the key value values - const Collada::Data* mValueData; ///> Source datat array for the key value values + const Collada::Accessor *mTimeAccessor; ///> Collada accessor to the time values + const Collada::Data *mTimeData; ///> Source data array for the time values + const Collada::Accessor *mValueAccessor; ///> Collada accessor to the key value values + const Collada::Data *mValueData; ///> Source datat array for the key value values - ChannelEntry() - : mChannel() - , mTransformIndex() - , mSubElement() - , mTimeAccessor() - , mTimeData() - , mValueAccessor() - , mValueData() - {} + ChannelEntry() : + mChannel(), + mTransformIndex(), + mSubElement(), + mTimeAccessor(), + mTimeData(), + mValueAccessor(), + mValueData() {} }; } // end of namespace Collada diff --git a/code/AssetLib/Collada/ColladaLoader.cpp b/code/AssetLib/Collada/ColladaLoader.cpp index b78fc0e3b..4e7434fe3 100644 --- a/code/AssetLib/Collada/ColladaLoader.cpp +++ b/code/AssetLib/Collada/ColladaLoader.cpp @@ -46,27 +46,28 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ColladaLoader.h" #include "ColladaParser.h" +#include +#include #include +#include #include #include #include -#include -#include -#include +#include #include #include -#include #include +#include -#include "time.h" #include "math.h" +#include "time.h" #include -#include #include +#include namespace Assimp { - + using namespace Assimp::Formatter; static const aiImporterDesc desc = { @@ -82,22 +83,34 @@ static const aiImporterDesc desc = { "dae zae" }; +static const float kMillisecondsFromSeconds = 1000.f; + +// Add an item of metadata to a node +// Assumes the key is not already in the list +template +inline void AddNodeMetaData(aiNode *node, const std::string &key, const T &value) { + if (nullptr == node->mMetaData) { + node->mMetaData = new aiMetadata(); + } + node->mMetaData->Add(key, value); +} + // ------------------------------------------------------------------------------------------------ // Constructor to be privately used by Importer -ColladaLoader::ColladaLoader() - : mFileName() - , mMeshIndexByID() - , mMaterialIndexByName() - , mMeshes() - , newMats() - , mCameras() - , mLights() - , mTextures() - , mAnims() - , noSkeletonMesh(false) - , ignoreUpDirection(false) - , useColladaName(false) - , mNodeNameCounter(0) { +ColladaLoader::ColladaLoader() : + mFileName(), + mMeshIndexByID(), + mMaterialIndexByName(), + mMeshes(), + newMats(), + mCameras(), + mLights(), + mTextures(), + mAnims(), + noSkeletonMesh(false), + ignoreUpDirection(false), + useColladaName(false), + mNodeNameCounter(0) { // empty } @@ -109,7 +122,7 @@ ColladaLoader::~ColladaLoader() { // ------------------------------------------------------------------------------------------------ // Returns whether the class can handle the format of the given file. -bool ColladaLoader::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const { +bool ColladaLoader::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const { // check file extension const std::string extension = GetExtension(pFile); @@ -137,7 +150,7 @@ bool ColladaLoader::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool if (!pIOHandler) { return true; } - static const char* tokens[] = { "GetPropertyInteger(AI_CONFIG_IMPORT_NO_SKELETON_MESHES, 0) != 0; ignoreUpDirection = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION, 0) != 0; useColladaName = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_USE_COLLADA_NAMES, 0) != 0; @@ -153,13 +166,13 @@ void ColladaLoader::SetupProperties(const Importer* pImp) { // ------------------------------------------------------------------------------------------------ // Get file extension list -const aiImporterDesc* ColladaLoader::GetInfo() const { +const aiImporterDesc *ColladaLoader::GetInfo() const { return &desc; } // ------------------------------------------------------------------------------------------------ // Imports the given file into the given scene structure. -void ColladaLoader::InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) { +void ColladaLoader::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) { mFileName = pFile; // clean all member arrays - just for safety, it should work even if we did not @@ -176,14 +189,13 @@ void ColladaLoader::InternReadFile(const std::string& pFile, aiScene* pScene, IO // parse the input file ColladaParser parser(pIOHandler, pFile); - - if( !parser.mRootNode) { - throw DeadlyImportError( "Collada: File came out empty. Something is wrong here."); + if (!parser.mRootNode) { + throw DeadlyImportError("Collada: File came out empty. Something is wrong here."); } // reserve some storage to avoid unnecessary reallocs - newMats.reserve(parser.mMaterialLibrary.size()*2u); - mMeshes.reserve(parser.mMeshLibrary.size()*2u); + newMats.reserve(parser.mMaterialLibrary.size() * 2u); + mMeshes.reserve(parser.mMeshLibrary.size() * 2u); mCameras.reserve(parser.mCameraLibrary.size()); mLights.reserve(parser.mLightLibrary.size()); @@ -199,24 +211,24 @@ void ColladaLoader::InternReadFile(const std::string& pFile, aiScene* pScene, IO // Apply unit-size scale calculation - pScene->mRootNode->mTransformation *= aiMatrix4x4(parser.mUnitSize, 0, 0, 0, - 0, parser.mUnitSize, 0, 0, - 0, 0, parser.mUnitSize, 0, - 0, 0, 0, 1); - if( !ignoreUpDirection ) { + pScene->mRootNode->mTransformation *= aiMatrix4x4(parser.mUnitSize, 0, 0, 0, + 0, parser.mUnitSize, 0, 0, + 0, 0, parser.mUnitSize, 0, + 0, 0, 0, 1); + if (!ignoreUpDirection) { // Convert to Y_UP, if different orientation - if( parser.mUpDirection == ColladaParser::UP_X) { + if (parser.mUpDirection == ColladaParser::UP_X) { pScene->mRootNode->mTransformation *= aiMatrix4x4( - 0, -1, 0, 0, - 1, 0, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1); - } else if( parser.mUpDirection == ColladaParser::UP_Z) { + 0, -1, 0, 0, + 1, 0, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); + } else if (parser.mUpDirection == ColladaParser::UP_Z) { pScene->mRootNode->mTransformation *= aiMatrix4x4( - 1, 0, 0, 0, - 0, 0, 1, 0, - 0, -1, 0, 0, - 0, 0, 0, 1); + 1, 0, 0, 0, + 0, 0, 1, 0, + 0, -1, 0, 0, + 0, 0, 0, 1); } } @@ -230,27 +242,15 @@ void ColladaLoader::InternReadFile(const std::string& pFile, aiScene* pScene, IO } } - // store all meshes StoreSceneMeshes(pScene); - - // store all materials StoreSceneMaterials(pScene); - - // store all textures StoreSceneTextures(pScene); - - // store all lights StoreSceneLights(pScene); - - // store all cameras StoreSceneCameras(pScene); - - // store all animations StoreAnimations(pScene, parser); // If no meshes have been loaded, it's probably just an animated skeleton. - if ( 0u == pScene->mNumMeshes) { - + if (0u == pScene->mNumMeshes) { if (!noSkeletonMesh) { SkeletonMeshBuilder hero(pScene); } @@ -260,23 +260,32 @@ void ColladaLoader::InternReadFile(const std::string& pFile, aiScene* pScene, IO // ------------------------------------------------------------------------------------------------ // Recursively constructs a scene node for the given parser node and returns it. -aiNode* ColladaLoader::BuildHierarchy(const ColladaParser& pParser, const Collada::Node* pNode) { +aiNode *ColladaLoader::BuildHierarchy(const ColladaParser &pParser, const Collada::Node *pNode) { // create a node for it - aiNode* node = new aiNode(); + aiNode *node = new aiNode(); // find a name for the new node. It's more complicated than you might think node->mName.Set(FindNameForNode(pNode)); + // if we're not using the unique IDs, hold onto them for reference and export + if (useColladaName) { + if (!pNode->mID.empty()) { + AddNodeMetaData(node, AI_METADATA_COLLADA_ID, aiString(pNode->mID)); + } + if (!pNode->mSID.empty()) { + AddNodeMetaData(node, AI_METADATA_COLLADA_SID, aiString(pNode->mSID)); + } + } // calculate the transformation matrix for it node->mTransformation = pParser.CalculateResultTransform(pNode->mTransforms); // now resolve node instances - std::vector instances; + std::vector instances; ResolveNodeInstances(pParser, pNode, instances); // add children. first the *real* ones node->mNumChildren = static_cast(pNode->mChildren.size() + instances.size()); - node->mChildren = new aiNode*[node->mNumChildren]; + node->mChildren = new aiNode *[node->mNumChildren]; for (size_t a = 0; a < pNode->mChildren.size(); ++a) { node->mChildren[a] = BuildHierarchy(pParser, pNode->mChildren[a]); @@ -289,13 +298,8 @@ aiNode* ColladaLoader::BuildHierarchy(const ColladaParser& pParser, const Collad node->mChildren[pNode->mChildren.size() + a]->mParent = node; } - // construct meshes BuildMeshesForNode(pParser, pNode, node); - - // construct cameras BuildCamerasForNode(pParser, pNode, node); - - // construct lights BuildLightsForNode(pParser, pNode, node); return node; @@ -303,8 +307,8 @@ aiNode* ColladaLoader::BuildHierarchy(const ColladaParser& pParser, const Collad // ------------------------------------------------------------------------------------------------ // Resolve node instances -void ColladaLoader::ResolveNodeInstances(const ColladaParser& pParser, const Collada::Node* pNode, - std::vector& resolved) { +void ColladaLoader::ResolveNodeInstances(const ColladaParser &pParser, const Collada::Node *pNode, + std::vector &resolved) { // reserve enough storage resolved.reserve(pNode->mNodeInstances.size()); @@ -312,7 +316,7 @@ void ColladaLoader::ResolveNodeInstances(const ColladaParser& pParser, const Col for (const auto &nodeInst : pNode->mNodeInstances) { // find the corresponding node in the library const ColladaParser::NodeLibrary::const_iterator itt = pParser.mNodeLibrary.find(nodeInst.mNode); - const Collada::Node* nd = itt == pParser.mNodeLibrary.end() ? NULL : (*itt).second; + const Collada::Node *nd = itt == pParser.mNodeLibrary.end() ? NULL : (*itt).second; // FIX for http://sourceforge.net/tracker/?func=detail&aid=3054873&group_id=226462&atid=1067632 // need to check for both name and ID to catch all. To avoid breaking valid files, @@ -331,9 +335,7 @@ void ColladaLoader::ResolveNodeInstances(const ColladaParser& pParser, const Col // ------------------------------------------------------------------------------------------------ // Resolve UV channels -void ColladaLoader::ApplyVertexToEffectSemanticMapping(Collada::Sampler& sampler, - - const Collada::SemanticMappingTable& table) { +void ColladaLoader::ApplyVertexToEffectSemanticMapping(Collada::Sampler &sampler, const Collada::SemanticMappingTable &table) { std::map::const_iterator it = table.mMap.find(sampler.mUVChannel); if (it != table.mMap.end()) { if (it->second.mType != Collada::IT_Texcoord) { @@ -346,18 +348,18 @@ void ColladaLoader::ApplyVertexToEffectSemanticMapping(Collada::Sampler& sampler // ------------------------------------------------------------------------------------------------ // Builds lights for the given node and references them -void ColladaLoader::BuildLightsForNode(const ColladaParser& pParser, const Collada::Node* pNode, aiNode* pTarget) { - for (const Collada::LightInstance& lid : pNode->mLights) { +void ColladaLoader::BuildLightsForNode(const ColladaParser &pParser, const Collada::Node *pNode, aiNode *pTarget) { + for (const Collada::LightInstance &lid : pNode->mLights) { // find the referred light ColladaParser::LightLibrary::const_iterator srcLightIt = pParser.mLightLibrary.find(lid.mLight); if (srcLightIt == pParser.mLightLibrary.end()) { ASSIMP_LOG_WARN_F("Collada: Unable to find light for ID \"", lid.mLight, "\". Skipping."); continue; } - const Collada::Light* srcLight = &srcLightIt->second; + const Collada::Light *srcLight = &srcLightIt->second; // now fill our ai data structure - aiLight* out = new aiLight(); + aiLight *out = new aiLight(); out->mName = pTarget->mName; out->mType = (aiLightSourceType)srcLight->mType; @@ -368,14 +370,13 @@ void ColladaLoader::BuildLightsForNode(const ColladaParser& pParser, const Colla out->mAttenuationLinear = srcLight->mAttLinear; out->mAttenuationQuadratic = srcLight->mAttQuadratic; - out->mColorDiffuse = out->mColorSpecular = out->mColorAmbient = srcLight->mColor*srcLight->mIntensity; + out->mColorDiffuse = out->mColorSpecular = out->mColorAmbient = srcLight->mColor * srcLight->mIntensity; if (out->mType == aiLightSource_AMBIENT) { out->mColorDiffuse = out->mColorSpecular = aiColor3D(0, 0, 0); - out->mColorAmbient = srcLight->mColor*srcLight->mIntensity; - } - else { + out->mColorAmbient = srcLight->mColor * srcLight->mIntensity; + } else { // collada doesn't differentiate between these color types - out->mColorDiffuse = out->mColorSpecular = srcLight->mColor*srcLight->mIntensity; + out->mColorDiffuse = out->mColorSpecular = srcLight->mColor * srcLight->mIntensity; out->mColorAmbient = aiColor3D(0, 0, 0); } @@ -390,15 +391,13 @@ void ColladaLoader::BuildLightsForNode(const ColladaParser& pParser, const Colla // Need to rely on falloff_exponent. I don't know how to interpret it, so I need to guess .... // epsilon chosen to be 0.1 out->mAngleOuterCone = std::acos(std::pow(0.1f, 1.f / srcLight->mFalloffExponent)) + - out->mAngleInnerCone; - } - else { + out->mAngleInnerCone; + } else { out->mAngleOuterCone = out->mAngleInnerCone + AI_DEG_TO_RAD(srcLight->mPenumbraAngle); if (out->mAngleOuterCone < out->mAngleInnerCone) std::swap(out->mAngleInnerCone, out->mAngleOuterCone); } - } - else { + } else { out->mAngleOuterCone = AI_DEG_TO_RAD(srcLight->mOuterAngle); } } @@ -410,15 +409,15 @@ void ColladaLoader::BuildLightsForNode(const ColladaParser& pParser, const Colla // ------------------------------------------------------------------------------------------------ // Builds cameras for the given node and references them -void ColladaLoader::BuildCamerasForNode(const ColladaParser& pParser, const Collada::Node* pNode, aiNode* pTarget) { - for (const Collada::CameraInstance& cid : pNode->mCameras) { +void ColladaLoader::BuildCamerasForNode(const ColladaParser &pParser, const Collada::Node *pNode, aiNode *pTarget) { + for (const Collada::CameraInstance &cid : pNode->mCameras) { // find the referred light ColladaParser::CameraLibrary::const_iterator srcCameraIt = pParser.mCameraLibrary.find(cid.mCamera); if (srcCameraIt == pParser.mCameraLibrary.end()) { ASSIMP_LOG_WARN_F("Collada: Unable to find camera for ID \"", cid.mCamera, "\". Skipping."); continue; } - const Collada::Camera* srcCamera = &srcCameraIt->second; + const Collada::Camera *srcCamera = &srcCameraIt->second; // orthographic cameras not yet supported in Assimp if (srcCamera->mOrtho) { @@ -426,7 +425,7 @@ void ColladaLoader::BuildCamerasForNode(const ColladaParser& pParser, const Coll } // now fill our ai data structure - aiCamera* out = new aiCamera(); + aiCamera *out = new aiCamera(); out->mName = pTarget->mName; // collada cameras point in -Z by default, rest is specified in node transform @@ -447,12 +446,12 @@ void ColladaLoader::BuildCamerasForNode(const ColladaParser& pParser, const Coll if (srcCamera->mVerFov != 10e10f && srcCamera->mAspect == 10e10f) { out->mAspect = std::tan(AI_DEG_TO_RAD(srcCamera->mHorFov)) / - std::tan(AI_DEG_TO_RAD(srcCamera->mVerFov)); + std::tan(AI_DEG_TO_RAD(srcCamera->mVerFov)); } - } else if (srcCamera->mAspect != 10e10f && srcCamera->mVerFov != 10e10f) { + } else if (srcCamera->mAspect != 10e10f && srcCamera->mVerFov != 10e10f) { out->mHorizontalFOV = 2.0f * AI_RAD_TO_DEG(std::atan(srcCamera->mAspect * - std::tan(AI_DEG_TO_RAD(srcCamera->mVerFov) * 0.5f))); + std::tan(AI_DEG_TO_RAD(srcCamera->mVerFov) * 0.5f))); } // Collada uses degrees, we use radians @@ -465,15 +464,15 @@ void ColladaLoader::BuildCamerasForNode(const ColladaParser& pParser, const Coll // ------------------------------------------------------------------------------------------------ // Builds meshes for the given node and references them -void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Collada::Node* pNode, aiNode* pTarget) { +void ColladaLoader::BuildMeshesForNode(const ColladaParser &pParser, const Collada::Node *pNode, aiNode *pTarget) { // accumulated mesh references by this node std::vector newMeshRefs; newMeshRefs.reserve(pNode->mMeshes.size()); // add a mesh for each subgroup in each collada mesh - for (const Collada::MeshInstance& mid : pNode->mMeshes) { - const Collada::Mesh* srcMesh = nullptr; - const Collada::Controller* srcController = nullptr; + for (const Collada::MeshInstance &mid : pNode->mMeshes) { + const Collada::Mesh *srcMesh = nullptr; + const Collada::Controller *srcController = nullptr; // find the referred mesh ColladaParser::MeshLibrary::const_iterator srcMeshIt = pParser.mMeshLibrary.find(mid.mMeshOrController); @@ -488,13 +487,11 @@ void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Colla } } - - if( nullptr == srcMesh) { - ASSIMP_LOG_WARN_F( "Collada: Unable to find geometry for ID \"", mid.mMeshOrController, "\". Skipping." ); + if (nullptr == srcMesh) { + ASSIMP_LOG_WARN_F("Collada: Unable to find geometry for ID \"", mid.mMeshOrController, "\". Skipping."); continue; } - } - else { + } else { // ID found in the mesh library -> direct reference to an unskinned mesh srcMesh = srcMeshIt->second; } @@ -502,23 +499,22 @@ void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Colla // build a mesh for each of its subgroups size_t vertexStart = 0, faceStart = 0; for (size_t sm = 0; sm < srcMesh->mSubMeshes.size(); ++sm) { - const Collada::SubMesh& submesh = srcMesh->mSubMeshes[sm]; + const Collada::SubMesh &submesh = srcMesh->mSubMeshes[sm]; if (submesh.mNumFaces == 0) { continue; } // find material assigned to this submesh std::string meshMaterial; - std::map::const_iterator meshMatIt = mid.mMaterials.find(submesh.mMaterial); + std::map::const_iterator meshMatIt = mid.mMaterials.find(submesh.mMaterial); - const Collada::SemanticMappingTable* table = nullptr; + const Collada::SemanticMappingTable *table = nullptr; if (meshMatIt != mid.mMaterials.end()) { table = &meshMatIt->second; meshMaterial = table->mMatName; - } - else { + } else { ASSIMP_LOG_WARN_F("Collada: No material specified for subgroup <", submesh.mMaterial, "> in geometry <", - mid.mMeshOrController, ">."); + mid.mMeshOrController, ">."); if (!mid.mMaterials.empty()) { meshMaterial = mid.mMaterials.begin()->second.mMatName; } @@ -533,7 +529,7 @@ void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Colla } if (table && !table->mMap.empty()) { - std::pair& mat = newMats[matIdx]; + std::pair &mat = newMats[matIdx]; // Iterate through all texture channels assigned to the effect and // check whether we have mapping information for it. @@ -552,16 +548,16 @@ void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Colla std::map::const_iterator dstMeshIt = mMeshIndexByID.find(index); if (dstMeshIt != mMeshIndexByID.end()) { newMeshRefs.push_back(dstMeshIt->second); - } - else { + } else { // else we have to add the mesh to the collection and store its newly assigned index at the node - aiMesh* dstMesh = CreateMesh(pParser, srcMesh, submesh, srcController, vertexStart, faceStart); + aiMesh *dstMesh = CreateMesh(pParser, srcMesh, submesh, srcController, vertexStart, faceStart); // store the mesh, and store its new index in the node newMeshRefs.push_back(mMeshes.size()); mMeshIndexByID[index] = mMeshes.size(); mMeshes.push_back(dstMesh); - vertexStart += dstMesh->mNumVertices; faceStart += submesh.mNumFaces; + vertexStart += dstMesh->mNumVertices; + faceStart += submesh.mNumFaces; // assign the material index dstMesh->mMaterialIndex = matIdx; @@ -576,7 +572,7 @@ void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Colla pTarget->mNumMeshes = static_cast(newMeshRefs.size()); if (newMeshRefs.size()) { struct UIntTypeConverter { - unsigned int operator()(const size_t& v) const { + unsigned int operator()(const size_t &v) const { return static_cast(v); } }; @@ -588,7 +584,11 @@ void ColladaLoader::BuildMeshesForNode(const ColladaParser& pParser, const Colla // ------------------------------------------------------------------------------------------------ // Find mesh from either meshes or morph target meshes -aiMesh *ColladaLoader::findMesh(const std::string& meshid) { +aiMesh *ColladaLoader::findMesh(const std::string &meshid) { + if ( meshid.empty()) { + return nullptr; + } + for (unsigned int i = 0; i < mMeshes.size(); ++i) { if (std::string(mMeshes[i]->mName.data) == meshid) { return mMeshes[i]; @@ -606,43 +606,43 @@ aiMesh *ColladaLoader::findMesh(const std::string& meshid) { // ------------------------------------------------------------------------------------------------ // Creates a mesh for the given ColladaMesh face subset and returns the newly created mesh -aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::Mesh* pSrcMesh, const Collada::SubMesh& pSubMesh, - const Collada::Controller* pSrcController, size_t pStartVertex, size_t pStartFace) { +aiMesh *ColladaLoader::CreateMesh(const ColladaParser &pParser, const Collada::Mesh *pSrcMesh, const Collada::SubMesh &pSubMesh, + const Collada::Controller *pSrcController, size_t pStartVertex, size_t pStartFace) { std::unique_ptr dstMesh(new aiMesh); - dstMesh->mName = pSrcMesh->mName; + if (useColladaName) { + dstMesh->mName = pSrcMesh->mName; + } else { + dstMesh->mName = pSrcMesh->mId; + } // count the vertices addressed by its faces const size_t numVertices = std::accumulate(pSrcMesh->mFaceSize.begin() + pStartFace, - pSrcMesh->mFaceSize.begin() + pStartFace + pSubMesh.mNumFaces, size_t(0)); + pSrcMesh->mFaceSize.begin() + pStartFace + pSubMesh.mNumFaces, size_t(0)); // copy positions dstMesh->mNumVertices = static_cast(numVertices); dstMesh->mVertices = new aiVector3D[numVertices]; - std::copy(pSrcMesh->mPositions.begin() + pStartVertex, pSrcMesh->mPositions.begin() + - pStartVertex + numVertices, dstMesh->mVertices); + std::copy(pSrcMesh->mPositions.begin() + pStartVertex, pSrcMesh->mPositions.begin() + pStartVertex + numVertices, dstMesh->mVertices); // normals, if given. HACK: (thom) Due to the glorious Collada spec we never // know if we have the same number of normals as there are positions. So we // also ignore any vertex attribute if it has a different count if (pSrcMesh->mNormals.size() >= pStartVertex + numVertices) { dstMesh->mNormals = new aiVector3D[numVertices]; - std::copy(pSrcMesh->mNormals.begin() + pStartVertex, pSrcMesh->mNormals.begin() + - pStartVertex + numVertices, dstMesh->mNormals); + std::copy(pSrcMesh->mNormals.begin() + pStartVertex, pSrcMesh->mNormals.begin() + pStartVertex + numVertices, dstMesh->mNormals); } // tangents, if given. if (pSrcMesh->mTangents.size() >= pStartVertex + numVertices) { dstMesh->mTangents = new aiVector3D[numVertices]; - std::copy(pSrcMesh->mTangents.begin() + pStartVertex, pSrcMesh->mTangents.begin() + - pStartVertex + numVertices, dstMesh->mTangents); + std::copy(pSrcMesh->mTangents.begin() + pStartVertex, pSrcMesh->mTangents.begin() + pStartVertex + numVertices, dstMesh->mTangents); } // bitangents, if given. if (pSrcMesh->mBitangents.size() >= pStartVertex + numVertices) { dstMesh->mBitangents = new aiVector3D[numVertices]; - std::copy(pSrcMesh->mBitangents.begin() + pStartVertex, pSrcMesh->mBitangents.begin() + - pStartVertex + numVertices, dstMesh->mBitangents); + std::copy(pSrcMesh->mBitangents.begin() + pStartVertex, pSrcMesh->mBitangents.begin() + pStartVertex + numVertices, dstMesh->mBitangents); } // same for texturecoords, as many as we have @@ -674,7 +674,7 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M dstMesh->mFaces = new aiFace[dstMesh->mNumFaces]; for (size_t a = 0; a < dstMesh->mNumFaces; ++a) { size_t s = pSrcMesh->mFaceSize[pStartFace + a]; - aiFace& face = dstMesh->mFaces[a]; + aiFace &face = dstMesh->mFaces[a]; face.mNumIndices = static_cast(s); face.mIndices = new unsigned int[s]; for (size_t b = 0; b < s; ++b) { @@ -683,20 +683,20 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M } // create morph target meshes if any - std::vector targetMeshes; + std::vector targetMeshes; std::vector targetWeights; Collada::MorphMethod method = Collada::Normalized; for (std::map::const_iterator it = pParser.mControllerLibrary.begin(); - it != pParser.mControllerLibrary.end(); ++it) { + it != pParser.mControllerLibrary.end(); ++it) { const Collada::Controller &c = it->second; - const Collada::Mesh* baseMesh = pParser.ResolveLibraryReference(pParser.mMeshLibrary, c.mMeshId); + const Collada::Mesh *baseMesh = pParser.ResolveLibraryReference(pParser.mMeshLibrary, c.mMeshId); if (c.mType == Collada::Morph && baseMesh->mName == pSrcMesh->mName) { - const Collada::Accessor& targetAccessor = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, c.mMorphTarget); - const Collada::Accessor& weightAccessor = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, c.mMorphWeight); - const Collada::Data& targetData = pParser.ResolveLibraryReference(pParser.mDataLibrary, targetAccessor.mSource); - const Collada::Data& weightData = pParser.ResolveLibraryReference(pParser.mDataLibrary, weightAccessor.mSource); + const Collada::Accessor &targetAccessor = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, c.mMorphTarget); + const Collada::Accessor &weightAccessor = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, c.mMorphWeight); + const Collada::Data &targetData = pParser.ResolveLibraryReference(pParser.mDataLibrary, targetAccessor.mSource); + const Collada::Data &weightData = pParser.ResolveLibraryReference(pParser.mDataLibrary, weightAccessor.mSource); // take method method = c.mMethod; @@ -709,12 +709,12 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M } for (unsigned int i = 0; i < targetData.mStrings.size(); ++i) { - const Collada::Mesh* targetMesh = pParser.ResolveLibraryReference(pParser.mMeshLibrary, targetData.mStrings.at(i)); + const Collada::Mesh *targetMesh = pParser.ResolveLibraryReference(pParser.mMeshLibrary, targetData.mStrings.at(i)); - aiMesh *aimesh = findMesh(targetMesh->mName); + aiMesh *aimesh = findMesh(useColladaName ? targetMesh->mName : targetMesh->mId); if (!aimesh) { if (targetMesh->mSubMeshes.size() > 1) { - throw DeadlyImportError("Morhing target mesh must be a single"); + throw DeadlyImportError("Morphing target mesh must be a single"); } aimesh = CreateMesh(pParser, targetMesh, targetMesh->mSubMeshes.at(0), NULL, 0, 0); mTargetMeshes.push_back(aimesh); @@ -727,19 +727,17 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M } } if (targetMeshes.size() > 0 && targetWeights.size() == targetMeshes.size()) { - std::vector animMeshes; + std::vector animMeshes; for (unsigned int i = 0; i < targetMeshes.size(); ++i) { - aiMesh* targetMesh = targetMeshes.at(i); + aiMesh *targetMesh = targetMeshes.at(i); aiAnimMesh *animMesh = aiCreateAnimMesh(targetMesh); float weight = targetWeights[i]; animMesh->mWeight = weight == 0 ? 1.0f : weight; animMesh->mName = targetMesh->mName; animMeshes.push_back(animMesh); } - dstMesh->mMethod = (method == Collada::Relative) - ? aiMorphingMethod_MORPH_RELATIVE - : aiMorphingMethod_MORPH_NORMALIZED; - dstMesh->mAnimMeshes = new aiAnimMesh*[animMeshes.size()]; + dstMesh->mMethod = (method == Collada::Relative) ? aiMorphingMethod_MORPH_RELATIVE : aiMorphingMethod_MORPH_NORMALIZED; + dstMesh->mAnimMeshes = new aiAnimMesh *[animMeshes.size()]; dstMesh->mNumAnimMeshes = static_cast(animMeshes.size()); for (unsigned int i = 0; i < animMeshes.size(); ++i) { dstMesh->mAnimMeshes[i] = animMeshes.at(i); @@ -749,18 +747,18 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M // create bones if given if (pSrcController && pSrcController->mType == Collada::Skin) { // resolve references - joint names - const Collada::Accessor& jointNamesAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mJointNameSource); - const Collada::Data& jointNames = pParser.ResolveLibraryReference(pParser.mDataLibrary, jointNamesAcc.mSource); + const Collada::Accessor &jointNamesAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mJointNameSource); + const Collada::Data &jointNames = pParser.ResolveLibraryReference(pParser.mDataLibrary, jointNamesAcc.mSource); // joint offset matrices - const Collada::Accessor& jointMatrixAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mJointOffsetMatrixSource); - const Collada::Data& jointMatrices = pParser.ResolveLibraryReference(pParser.mDataLibrary, jointMatrixAcc.mSource); + const Collada::Accessor &jointMatrixAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mJointOffsetMatrixSource); + const Collada::Data &jointMatrices = pParser.ResolveLibraryReference(pParser.mDataLibrary, jointMatrixAcc.mSource); // joint vertex_weight name list - should refer to the same list as the joint names above. If not, report and reconsider - const Collada::Accessor& weightNamesAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mWeightInputJoints.mAccessor); + const Collada::Accessor &weightNamesAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mWeightInputJoints.mAccessor); if (&weightNamesAcc != &jointNamesAcc) throw DeadlyImportError("Temporary implementational laziness. If you read this, please report to the author."); // vertex weights - const Collada::Accessor& weightsAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mWeightInputWeights.mAccessor); - const Collada::Data& weights = pParser.ResolveLibraryReference(pParser.mDataLibrary, weightsAcc.mSource); + const Collada::Accessor &weightsAcc = pParser.ResolveLibraryReference(pParser.mAccessorLibrary, pSrcController->mWeightInputWeights.mAccessor); + const Collada::Data &weights = pParser.ResolveLibraryReference(pParser.mDataLibrary, weightsAcc.mSource); if (!jointNames.mIsStringArray || jointMatrices.mIsStringArray || weights.mIsStringArray) throw DeadlyImportError("Data type mismatch while resolving mesh joints"); @@ -770,10 +768,10 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M // create containers to collect the weights for each bone size_t numBones = jointNames.mStrings.size(); - std::vector > dstBones(numBones); + std::vector> dstBones(numBones); // build a temporary array of pointers to the start of each vertex's weights - typedef std::vector< std::pair > IndexPairVector; + typedef std::vector> IndexPairVector; std::vector weightStartPerVertex; weightStartPerVertex.resize(pSrcController->mWeightCounts.size(), pSrcController->mWeights.end()); @@ -792,18 +790,16 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M IndexPairVector::const_iterator iit = weightStartPerVertex[orgIndex]; size_t pairCount = pSrcController->mWeightCounts[orgIndex]; - - for( size_t b = 0; b < pairCount; ++b, ++iit) { + for (size_t b = 0; b < pairCount; ++b, ++iit) { const size_t jointIndex = iit->first; - const size_t vertexIndex = iit->second; + const size_t vertexIndex = iit->second; ai_real weight = 1.0f; if (!weights.mValues.empty()) { weight = ReadFloat(weightsAcc, weights, vertexIndex, 0); } // one day I gonna kill that XSI Collada exporter - if (weight > 0.0f) - { + if (weight > 0.0f) { aiVertexWeight w; w.mVertexId = static_cast(a - pStartVertex); w.mWeight = weight; @@ -814,24 +810,24 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M // count the number of bones which influence vertices of the current submesh size_t numRemainingBones = 0; - for( std::vector >::const_iterator it = dstBones.begin(); it != dstBones.end(); ++it) { - if( it->size() > 0) { + for (std::vector>::const_iterator it = dstBones.begin(); it != dstBones.end(); ++it) { + if (it->size() > 0) { ++numRemainingBones; } } // create bone array and copy bone weights one by one dstMesh->mNumBones = static_cast(numRemainingBones); - dstMesh->mBones = new aiBone*[numRemainingBones]; + dstMesh->mBones = new aiBone *[numRemainingBones]; size_t boneCount = 0; - for( size_t a = 0; a < numBones; ++a) { + for (size_t a = 0; a < numBones; ++a) { // omit bones without weights - if( dstBones[a].empty() ) { + if (dstBones[a].empty()) { continue; } // create bone with its weights - aiBone* bone = new aiBone; + aiBone *bone = new aiBone; bone->mName = ReadString(jointNamesAcc, jointNames, a); bone->mOffsetMatrix.a1 = ReadFloat(jointMatrixAcc, jointMatrices, a, 0); bone->mOffsetMatrix.a2 = ReadFloat(jointMatrixAcc, jointMatrices, a, 1); @@ -873,16 +869,16 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M // Therefore I added a little name replacement here: I search for the bone's node by either name, ID or SID, // and replace the bone's name by the node's name so that the user can use the standard // find-by-name method to associate nodes with bones. - const Collada::Node* bnode = FindNode( pParser.mRootNode, bone->mName.data); - if( !bnode) { - bnode = FindNodeBySID( pParser.mRootNode, bone->mName.data); + const Collada::Node *bnode = FindNode(pParser.mRootNode, bone->mName.data); + if (!bnode) { + bnode = FindNodeBySID(pParser.mRootNode, bone->mName.data); } // assign the name that we would have assigned for the source node - if( bnode) { - bone->mName.Set( FindNameForNode( bnode)); + if (bnode) { + bone->mName.Set(FindNameForNode(bnode)); } else { - ASSIMP_LOG_WARN_F( "ColladaLoader::CreateMesh(): could not find corresponding node for joint \"", bone->mName.data, "\"." ); + ASSIMP_LOG_WARN_F("ColladaLoader::CreateMesh(): could not find corresponding node for joint \"", bone->mName.data, "\"."); } // and insert bone @@ -895,61 +891,61 @@ aiMesh* ColladaLoader::CreateMesh(const ColladaParser& pParser, const Collada::M // ------------------------------------------------------------------------------------------------ // Stores all meshes in the given scene -void ColladaLoader::StoreSceneMeshes( aiScene* pScene) { +void ColladaLoader::StoreSceneMeshes(aiScene *pScene) { pScene->mNumMeshes = static_cast(mMeshes.size()); - if( mMeshes.empty() ) { + if (mMeshes.empty()) { return; } - pScene->mMeshes = new aiMesh*[mMeshes.size()]; - std::copy( mMeshes.begin(), mMeshes.end(), pScene->mMeshes); + pScene->mMeshes = new aiMesh *[mMeshes.size()]; + std::copy(mMeshes.begin(), mMeshes.end(), pScene->mMeshes); mMeshes.clear(); } // ------------------------------------------------------------------------------------------------ // Stores all cameras in the given scene -void ColladaLoader::StoreSceneCameras( aiScene* pScene) { +void ColladaLoader::StoreSceneCameras(aiScene *pScene) { pScene->mNumCameras = static_cast(mCameras.size()); - if( mCameras.empty() ) { + if (mCameras.empty()) { return; } - pScene->mCameras = new aiCamera*[mCameras.size()]; - std::copy( mCameras.begin(), mCameras.end(), pScene->mCameras); + pScene->mCameras = new aiCamera *[mCameras.size()]; + std::copy(mCameras.begin(), mCameras.end(), pScene->mCameras); mCameras.clear(); } // ------------------------------------------------------------------------------------------------ // Stores all lights in the given scene -void ColladaLoader::StoreSceneLights( aiScene* pScene) { +void ColladaLoader::StoreSceneLights(aiScene *pScene) { pScene->mNumLights = static_cast(mLights.size()); - if( mLights.empty() ) { + if (mLights.empty()) { return; } - pScene->mLights = new aiLight*[mLights.size()]; - std::copy( mLights.begin(), mLights.end(), pScene->mLights); + pScene->mLights = new aiLight *[mLights.size()]; + std::copy(mLights.begin(), mLights.end(), pScene->mLights); mLights.clear(); } // ------------------------------------------------------------------------------------------------ // Stores all textures in the given scene -void ColladaLoader::StoreSceneTextures( aiScene* pScene) { +void ColladaLoader::StoreSceneTextures(aiScene *pScene) { pScene->mNumTextures = static_cast(mTextures.size()); - if( mTextures.empty() ) { + if (mTextures.empty()) { return; } - pScene->mTextures = new aiTexture*[mTextures.size()]; - std::copy( mTextures.begin(), mTextures.end(), pScene->mTextures); + pScene->mTextures = new aiTexture *[mTextures.size()]; + std::copy(mTextures.begin(), mTextures.end(), pScene->mTextures); mTextures.clear(); } // ------------------------------------------------------------------------------------------------ // Stores all materials in the given scene -void ColladaLoader::StoreSceneMaterials( aiScene* pScene) { +void ColladaLoader::StoreSceneMaterials(aiScene *pScene) { pScene->mNumMaterials = static_cast(newMats.size()); - if (newMats.empty() ) { + if (newMats.empty()) { return; } - pScene->mMaterials = new aiMaterial*[newMats.size()]; - for (unsigned int i = 0; i < newMats.size();++i) { + pScene->mMaterials = new aiMaterial *[newMats.size()]; + for (unsigned int i = 0; i < newMats.size(); ++i) { pScene->mMaterials[i] = newMats[i].second; } newMats.clear(); @@ -957,53 +953,51 @@ void ColladaLoader::StoreSceneMaterials( aiScene* pScene) { // ------------------------------------------------------------------------------------------------ // Stores all animations -void ColladaLoader::StoreAnimations( aiScene* pScene, const ColladaParser& pParser) { +void ColladaLoader::StoreAnimations(aiScene *pScene, const ColladaParser &pParser) { // recursively collect all animations from the collada scene StoreAnimations(pScene, pParser, &pParser.mAnims, ""); // catch special case: many animations with the same length, each affecting only a single node. // we need to unite all those single-node-anims to a proper combined animation - for(size_t a = 0; a < mAnims.size(); ++a) { - aiAnimation* templateAnim = mAnims[a]; + for (size_t a = 0; a < mAnims.size(); ++a) { + aiAnimation *templateAnim = mAnims[a]; if (templateAnim->mNumChannels == 1) { // search for other single-channel-anims with the same duration std::vector collectedAnimIndices; - for( size_t b = a+1; b < mAnims.size(); ++b) { - aiAnimation* other = mAnims[b]; + for (size_t b = a + 1; b < mAnims.size(); ++b) { + aiAnimation *other = mAnims[b]; if (other->mNumChannels == 1 && other->mDuration == templateAnim->mDuration && - other->mTicksPerSecond == templateAnim->mTicksPerSecond) - collectedAnimIndices.push_back(b); + other->mTicksPerSecond == templateAnim->mTicksPerSecond) + collectedAnimIndices.push_back(b); } - // We only want to combine the animations if they have different channels - std::set animTargets; - animTargets.insert(templateAnim->mChannels[0]->mNodeName.C_Str()); - bool collectedAnimationsHaveDifferentChannels = true; - for (size_t b = 0; b < collectedAnimIndices.size(); ++b) - { - aiAnimation* srcAnimation = mAnims[collectedAnimIndices[b]]; - std::string channelName = std::string(srcAnimation->mChannels[0]->mNodeName.C_Str()); - if (animTargets.find(channelName) == animTargets.end()) { - animTargets.insert(channelName); - } else { - collectedAnimationsHaveDifferentChannels = false; - break; - } - } + // We only want to combine the animations if they have different channels + std::set animTargets; + animTargets.insert(templateAnim->mChannels[0]->mNodeName.C_Str()); + bool collectedAnimationsHaveDifferentChannels = true; + for (size_t b = 0; b < collectedAnimIndices.size(); ++b) { + aiAnimation *srcAnimation = mAnims[collectedAnimIndices[b]]; + std::string channelName = std::string(srcAnimation->mChannels[0]->mNodeName.C_Str()); + if (animTargets.find(channelName) == animTargets.end()) { + animTargets.insert(channelName); + } else { + collectedAnimationsHaveDifferentChannels = false; + break; + } + } - if (!collectedAnimationsHaveDifferentChannels) - continue; + if (!collectedAnimationsHaveDifferentChannels) + continue; // if there are other animations which fit the template anim, combine all channels into a single anim - if (!collectedAnimIndices.empty()) - { - aiAnimation* combinedAnim = new aiAnimation(); + if (!collectedAnimIndices.empty()) { + aiAnimation *combinedAnim = new aiAnimation(); combinedAnim->mName = aiString(std::string("combinedAnim_") + char('0' + a)); combinedAnim->mDuration = templateAnim->mDuration; combinedAnim->mTicksPerSecond = templateAnim->mTicksPerSecond; combinedAnim->mNumChannels = static_cast(collectedAnimIndices.size() + 1); - combinedAnim->mChannels = new aiNodeAnim*[combinedAnim->mNumChannels]; + combinedAnim->mChannels = new aiNodeAnim *[combinedAnim->mNumChannels]; // add the template anim as first channel by moving its aiNodeAnim to the combined animation combinedAnim->mChannels[0] = templateAnim->mChannels[0]; templateAnim->mChannels[0] = NULL; @@ -1012,9 +1006,8 @@ void ColladaLoader::StoreAnimations( aiScene* pScene, const ColladaParser& pPars mAnims[a] = combinedAnim; // move the memory of all other anims to the combined anim and erase them from the source anims - for (size_t b = 0; b < collectedAnimIndices.size(); ++b) - { - aiAnimation* srcAnimation = mAnims[collectedAnimIndices[b]]; + for (size_t b = 0; b < collectedAnimIndices.size(); ++b) { + aiAnimation *srcAnimation = mAnims[collectedAnimIndices[b]]; combinedAnim->mChannels[1 + b] = srcAnimation->mChannels[0]; srcAnimation->mChannels[0] = NULL; delete srcAnimation; @@ -1022,8 +1015,7 @@ void ColladaLoader::StoreAnimations( aiScene* pScene, const ColladaParser& pPars // in a second go, delete all the single-channel-anims that we've stripped from their channels // back to front to preserve indices - you know, removing an element from a vector moves all elements behind the removed one - while (!collectedAnimIndices.empty()) - { + while (!collectedAnimIndices.empty()) { mAnims.erase(mAnims.begin() + collectedAnimIndices.back()); collectedAnimIndices.pop_back(); } @@ -1032,10 +1024,9 @@ void ColladaLoader::StoreAnimations( aiScene* pScene, const ColladaParser& pPars } // now store all anims in the scene - if (!mAnims.empty()) - { + if (!mAnims.empty()) { pScene->mNumAnimations = static_cast(mAnims.size()); - pScene->mAnimations = new aiAnimation*[mAnims.size()]; + pScene->mAnimations = new aiAnimation *[mAnims.size()]; std::copy(mAnims.begin(), mAnims.end(), pScene->mAnimations); } @@ -1044,12 +1035,11 @@ void ColladaLoader::StoreAnimations( aiScene* pScene, const ColladaParser& pPars // ------------------------------------------------------------------------------------------------ // Constructs the animations for the given source anim -void ColladaLoader::StoreAnimations(aiScene* pScene, const ColladaParser& pParser, const Collada::Animation* pSrcAnim, const std::string &pPrefix) -{ +void ColladaLoader::StoreAnimations(aiScene *pScene, const ColladaParser &pParser, const Collada::Animation *pSrcAnim, const std::string &pPrefix) { std::string animName = pPrefix.empty() ? pSrcAnim->mName : pPrefix + "_" + pSrcAnim->mName; // create nested animations, if given - for (std::vector::const_iterator it = pSrcAnim->mSubAnims.begin(); it != pSrcAnim->mSubAnims.end(); ++it) + for (std::vector::const_iterator it = pSrcAnim->mSubAnims.begin(); it != pSrcAnim->mSubAnims.end(); ++it) StoreAnimations(pScene, pParser, *it, animName); // create animation channels, if any @@ -1057,47 +1047,38 @@ void ColladaLoader::StoreAnimations(aiScene* pScene, const ColladaParser& pParse CreateAnimation(pScene, pParser, pSrcAnim, animName); } -struct MorphTimeValues -{ +struct MorphTimeValues { float mTime; - struct key - { + struct key { float mWeight; unsigned int mValue; }; std::vector mKeys; }; -void insertMorphTimeValue(std::vector &values, float time, float weight, unsigned int value) -{ +void insertMorphTimeValue(std::vector &values, float time, float weight, unsigned int value) { MorphTimeValues::key k; k.mValue = value; k.mWeight = weight; - if (values.size() == 0 || time < values[0].mTime) - { + if (values.size() == 0 || time < values[0].mTime) { MorphTimeValues val; val.mTime = time; val.mKeys.push_back(k); values.insert(values.begin(), val); return; } - if (time > values.back().mTime) - { + if (time > values.back().mTime) { MorphTimeValues val; val.mTime = time; val.mKeys.push_back(k); values.insert(values.end(), val); return; } - for (unsigned int i = 0; i < values.size(); i++) - { - if (std::abs(time - values[i].mTime) < 1e-6f) - { + for (unsigned int i = 0; i < values.size(); i++) { + if (std::abs(time - values[i].mTime) < 1e-6f) { values[i].mKeys.push_back(k); return; - } - else if (time > values[i].mTime && time < values[i + 1].mTime) - { + } else if (time > values[i].mTime && time < values[i + 1].mTime) { MorphTimeValues val; val.mTime = time; val.mKeys.push_back(k); @@ -1108,10 +1089,8 @@ void insertMorphTimeValue(std::vector &values, float time, floa // should not get here } -float getWeightAtKey(const std::vector &values, int key, unsigned int value) -{ - for (unsigned int i = 0; i < values[key].mKeys.size(); i++) - { +float getWeightAtKey(const std::vector &values, int key, unsigned int value) { + for (unsigned int i = 0; i < values[key].mKeys.size(); i++) { if (values[key].mKeys[i].mValue == value) return values[key].mKeys[i].mWeight; } @@ -1122,23 +1101,21 @@ float getWeightAtKey(const std::vector &values, int key, unsign // ------------------------------------------------------------------------------------------------ // Constructs the animation for the given source anim -void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParser, const Collada::Animation* pSrcAnim, const std::string& pName) -{ +void ColladaLoader::CreateAnimation(aiScene *pScene, const ColladaParser &pParser, const Collada::Animation *pSrcAnim, const std::string &pName) { // collect a list of animatable nodes - std::vector nodes; + std::vector nodes; CollectNodes(pScene->mRootNode, nodes); - std::vector anims; - std::vector morphAnims; + std::vector anims; + std::vector morphAnims; - for (std::vector::const_iterator nit = nodes.begin(); nit != nodes.end(); ++nit) - { + for (std::vector::const_iterator nit = nodes.begin(); nit != nodes.end(); ++nit) { // find all the collada anim channels which refer to the current node std::vector entries; std::string nodeName = (*nit)->mName.data; // find the collada node corresponding to the aiNode - const Collada::Node* srcNode = FindNode(pParser.mRootNode, nodeName); + const Collada::Node *srcNode = FindNode(pParser.mRootNode, nodeName); // ai_assert( srcNode != NULL); if (!srcNode) continue; @@ -1146,16 +1123,14 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // now check all channels if they affect the current node std::string targetID, subElement; for (std::vector::const_iterator cit = pSrcAnim->mChannels.begin(); - cit != pSrcAnim->mChannels.end(); ++cit) - { - const Collada::AnimationChannel& srcChannel = *cit; + cit != pSrcAnim->mChannels.end(); ++cit) { + const Collada::AnimationChannel &srcChannel = *cit; Collada::ChannelEntry entry; // we expect the animation target to be of type "nodeName/transformID.subElement". Ignore all others // find the slash that separates the node name - there should be only one std::string::size_type slashPos = srcChannel.mTarget.find('/'); - if (slashPos == std::string::npos) - { + if (slashPos == std::string::npos) { std::string::size_type targetPos = srcChannel.mTarget.find(srcNode->mID); if (targetPos == std::string::npos) continue; @@ -1163,7 +1138,7 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // not node transform, but something else. store as unknown animation channel for now entry.mChannel = &(*cit); entry.mTargetId = srcChannel.mTarget.substr(targetPos + pSrcAnim->mName.length(), - srcChannel.mTarget.length() - targetPos - pSrcAnim->mName.length()); + srcChannel.mTarget.length() - targetPos - pSrcAnim->mName.length()); if (entry.mTargetId.front() == '-') entry.mTargetId = entry.mTargetId.substr(1); entries.push_back(entry); @@ -1179,8 +1154,7 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // find the dot that separates the transformID - there should be only one or zero std::string::size_type dotPos = srcChannel.mTarget.find('.'); - if (dotPos != std::string::npos) - { + if (dotPos != std::string::npos) { if (srcChannel.mTarget.find('.', dotPos + 1) != std::string::npos) continue; @@ -1198,15 +1172,13 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse entry.mSubElement = 2; else ASSIMP_LOG_WARN_F("Unknown anim subelement <", subElement, ">. Ignoring"); - } - else { + } else { // no subelement following, transformId is remaining string entry.mTransformId = srcChannel.mTarget.substr(slashPos + 1); } std::string::size_type bracketPos = srcChannel.mTarget.find('('); - if (bracketPos != std::string::npos) - { + if (bracketPos != std::string::npos) { entry.mTransformId = srcChannel.mTarget.substr(slashPos + 1, bracketPos - slashPos - 1); subElement.clear(); subElement = srcChannel.mTarget.substr(bracketPos); @@ -1251,14 +1223,11 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse if (srcNode->mTransforms[a].mID == entry.mTransformId) entry.mTransformIndex = a; - if (entry.mTransformIndex == SIZE_MAX) - { - if (entry.mTransformId.find("morph-weights") != std::string::npos) - { + if (entry.mTransformIndex == SIZE_MAX) { + if (entry.mTransformId.find("morph-weights") != std::string::npos) { entry.mTargetId = entry.mTransformId; entry.mTransformId = ""; - } - else + } else continue; } @@ -1272,9 +1241,8 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // resolve the data pointers for all anim channels. Find the minimum time while we're at it ai_real startTime = ai_real(1e20), endTime = ai_real(-1e20); - for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) - { - Collada::ChannelEntry& e = *it; + for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) { + Collada::ChannelEntry &e = *it; e.mTimeAccessor = &pParser.ResolveLibraryReference(pParser.mAccessorLibrary, e.mChannel->mSourceTimes); e.mTimeData = &pParser.ResolveLibraryReference(pParser.mDataLibrary, e.mTimeAccessor->mSource); e.mValueAccessor = &pParser.ResolveLibraryReference(pParser.mAccessorLibrary, e.mChannel->mSourceValues); @@ -1284,8 +1252,7 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse if (e.mTimeAccessor->mCount != e.mValueAccessor->mCount) throw DeadlyImportError(format() << "Time count / value count mismatch in animation channel \"" << e.mChannel->mTarget << "\"."); - if (e.mTimeAccessor->mCount > 0) - { + if (e.mTimeAccessor->mCount > 0) { // find bounding times startTime = std::min(startTime, ReadFloat(*e.mTimeAccessor, *e.mTimeData, 0, 0)); endTime = std::max(endTime, ReadFloat(*e.mTimeAccessor, *e.mTimeData, e.mTimeAccessor->mCount - 1, 0)); @@ -1293,25 +1260,21 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse } std::vector resultTrafos; - if (!entries.empty() && entries.front().mTimeAccessor->mCount > 0) - { + if (!entries.empty() && entries.front().mTimeAccessor->mCount > 0) { // create a local transformation chain of the node's transforms std::vector transforms = srcNode->mTransforms; // now for every unique point in time, find or interpolate the key values for that time // and apply them to the transform chain. Then the node's present transformation can be calculated. ai_real time = startTime; - while (1) - { - for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) - { - Collada::ChannelEntry& e = *it; + while (1) { + for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) { + Collada::ChannelEntry &e = *it; // find the keyframe behind the current point in time size_t pos = 0; ai_real postTime = 0.0; - while (1) - { + while (1) { if (pos >= e.mTimeAccessor->mCount) break; postTime = ReadFloat(*e.mTimeAccessor, *e.mTimeData, pos, 0); @@ -1328,13 +1291,11 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse temp[c] = ReadFloat(*e.mValueAccessor, *e.mValueData, pos, c); // if not exactly at the key time, interpolate with previous value set - if (postTime > time && pos > 0) - { + if (postTime > time && pos > 0) { ai_real preTime = ReadFloat(*e.mTimeAccessor, *e.mTimeData, pos - 1, 0); ai_real factor = (time - postTime) / (preTime - postTime); - for (size_t c = 0; c < e.mValueAccessor->mSize; ++c) - { + for (size_t c = 0; c < e.mValueAccessor->mSize; ++c) { ai_real v = ReadFloat(*e.mValueAccessor, *e.mValueData, pos - 1, c); temp[c] += (v - temp[c]) * factor; } @@ -1353,17 +1314,14 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // find next point in time to evaluate. That's the closest frame larger than the current in any channel ai_real nextTime = ai_real(1e20); - for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) - { - Collada::ChannelEntry& channelElement = *it; + for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) { + Collada::ChannelEntry &channelElement = *it; // find the next time value larger than the current size_t pos = 0; - while (pos < channelElement.mTimeAccessor->mCount) - { + while (pos < channelElement.mTimeAccessor->mCount) { const ai_real t = ReadFloat(*channelElement.mTimeAccessor, *channelElement.mTimeData, pos, 0); - if (t > time) - { + if (t > time) { nextTime = std::min(nextTime, t); break; } @@ -1400,12 +1358,11 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse } // there should be some keyframes, but we aren't that fixated on valid input data -// ai_assert( resultTrafos.size() > 0); + // ai_assert( resultTrafos.size() > 0); // build an animation channel for the given node out of these trafo keys - if (!resultTrafos.empty()) - { - aiNodeAnim* dstAnim = new aiNodeAnim; + if (!resultTrafos.empty()) { + aiNodeAnim *dstAnim = new aiNodeAnim; dstAnim->mNodeName = nodeName; dstAnim->mNumPositionKeys = static_cast(resultTrafos.size()); dstAnim->mNumRotationKeys = static_cast(resultTrafos.size()); @@ -1414,31 +1371,26 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse dstAnim->mRotationKeys = new aiQuatKey[resultTrafos.size()]; dstAnim->mScalingKeys = new aiVectorKey[resultTrafos.size()]; - for (size_t a = 0; a < resultTrafos.size(); ++a) - { + for (size_t a = 0; a < resultTrafos.size(); ++a) { aiMatrix4x4 mat = resultTrafos[a]; double time = double(mat.d4); // remember? time is stored in mat.d4 mat.d4 = 1.0f; - dstAnim->mPositionKeys[a].mTime = time; - dstAnim->mRotationKeys[a].mTime = time; - dstAnim->mScalingKeys[a].mTime = time; + dstAnim->mPositionKeys[a].mTime = time * kMillisecondsFromSeconds ; + dstAnim->mRotationKeys[a].mTime = time * kMillisecondsFromSeconds ; + dstAnim->mScalingKeys[a].mTime = time * kMillisecondsFromSeconds ; mat.Decompose(dstAnim->mScalingKeys[a].mValue, dstAnim->mRotationKeys[a].mValue, dstAnim->mPositionKeys[a].mValue); } anims.push_back(dstAnim); - } - else - { + } else { ASSIMP_LOG_WARN("Collada loader: found empty animation channel, ignored. Please check your exporter."); } - if (!entries.empty() && entries.front().mTimeAccessor->mCount > 0) - { + if (!entries.empty() && entries.front().mTimeAccessor->mCount > 0) { std::vector morphChannels; - for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) - { - Collada::ChannelEntry& e = *it; + for (std::vector::iterator it = entries.begin(); it != entries.end(); ++it) { + Collada::ChannelEntry &e = *it; // skip non-transform types if (e.mTargetId.empty()) @@ -1447,8 +1399,7 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse if (e.mTargetId.find("morph-weights") != std::string::npos) morphChannels.push_back(e); } - if (morphChannels.size() > 0) - { + if (!morphChannels.empty() ) { // either 1) morph weight animation count should contain morph target count channels // or 2) one channel with morph target count arrays // assume first @@ -1457,11 +1408,9 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse morphAnim->mName.Set(nodeName); std::vector morphTimeValues; - int morphAnimChannelIndex = 0; - for (std::vector::iterator it = morphChannels.begin(); it != morphChannels.end(); ++it) - { - Collada::ChannelEntry& e = *it; + for (std::vector::iterator it = morphChannels.begin(); it != morphChannels.end(); ++it) { + Collada::ChannelEntry &e = *it; std::string::size_type apos = e.mTargetId.find('('); std::string::size_type bpos = e.mTargetId.find(')'); if (apos == std::string::npos || bpos == std::string::npos) @@ -1470,23 +1419,22 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // weight target can be in format Weight_M_N, Weight_N, WeightN, or some other way // we ignore the name and just assume the channels are in the right order - for (unsigned int i = 0; i < e.mTimeData->mValues.size(); i++) - insertMorphTimeValue(morphTimeValues, e.mTimeData->mValues.at(i), e.mValueData->mValues.at(i), morphAnimChannelIndex); + for (unsigned int i = 0; i < e.mTimeData->mValues.size(); i++) { + insertMorphTimeValue(morphTimeValues, e.mTimeData->mValues[i], e.mValueData->mValues[i], morphAnimChannelIndex); + } ++morphAnimChannelIndex; } morphAnim->mNumKeys = static_cast(morphTimeValues.size()); morphAnim->mKeys = new aiMeshMorphKey[morphAnim->mNumKeys]; - for (unsigned int key = 0; key < morphAnim->mNumKeys; key++) - { + for (unsigned int key = 0; key < morphAnim->mNumKeys; key++) { morphAnim->mKeys[key].mNumValuesAndWeights = static_cast(morphChannels.size()); morphAnim->mKeys[key].mValues = new unsigned int[morphChannels.size()]; morphAnim->mKeys[key].mWeights = new double[morphChannels.size()]; - morphAnim->mKeys[key].mTime = morphTimeValues[key].mTime; - for (unsigned int valueIndex = 0; valueIndex < morphChannels.size(); valueIndex++) - { + morphAnim->mKeys[key].mTime = morphTimeValues[key].mTime * kMillisecondsFromSeconds ; + for (unsigned int valueIndex = 0; valueIndex < morphChannels.size(); ++valueIndex ) { morphAnim->mKeys[key].mValues[valueIndex] = valueIndex; morphAnim->mKeys[key].mWeights[valueIndex] = getWeightAtKey(morphTimeValues, key, valueIndex); } @@ -1497,31 +1445,26 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse } } - if (!anims.empty() || !morphAnims.empty()) - { - aiAnimation* anim = new aiAnimation; + if (!anims.empty() || !morphAnims.empty()) { + aiAnimation *anim = new aiAnimation; anim->mName.Set(pName); anim->mNumChannels = static_cast(anims.size()); - if (anim->mNumChannels > 0) - { - anim->mChannels = new aiNodeAnim*[anims.size()]; + if (anim->mNumChannels > 0) { + anim->mChannels = new aiNodeAnim *[anims.size()]; std::copy(anims.begin(), anims.end(), anim->mChannels); } anim->mNumMorphMeshChannels = static_cast(morphAnims.size()); - if (anim->mNumMorphMeshChannels > 0) - { - anim->mMorphMeshChannels = new aiMeshMorphAnim*[anim->mNumMorphMeshChannels]; + if (anim->mNumMorphMeshChannels > 0) { + anim->mMorphMeshChannels = new aiMeshMorphAnim *[anim->mNumMorphMeshChannels]; std::copy(morphAnims.begin(), morphAnims.end(), anim->mMorphMeshChannels); } anim->mDuration = 0.0f; - for (size_t a = 0; a < anims.size(); ++a) - { + for (size_t a = 0; a < anims.size(); ++a) { anim->mDuration = std::max(anim->mDuration, anims[a]->mPositionKeys[anims[a]->mNumPositionKeys - 1].mTime); anim->mDuration = std::max(anim->mDuration, anims[a]->mRotationKeys[anims[a]->mNumRotationKeys - 1].mTime); anim->mDuration = std::max(anim->mDuration, anims[a]->mScalingKeys[anims[a]->mNumScalingKeys - 1].mTime); } - for (size_t a = 0; a < morphAnims.size(); ++a) - { + for (size_t a = 0; a < morphAnims.size(); ++a) { anim->mDuration = std::max(anim->mDuration, morphAnims[a]->mKeys[morphAnims[a]->mNumKeys - 1].mTime); } anim->mTicksPerSecond = 1; @@ -1531,52 +1474,55 @@ void ColladaLoader::CreateAnimation(aiScene* pScene, const ColladaParser& pParse // ------------------------------------------------------------------------------------------------ // Add a texture to a material structure -void ColladaLoader::AddTexture(aiMaterial& mat, const ColladaParser& pParser, - const Collada::Effect& effect, - const Collada::Sampler& sampler, - aiTextureType type, unsigned int idx) -{ +void ColladaLoader::AddTexture(aiMaterial &mat, const ColladaParser &pParser, + const Collada::Effect &effect, + const Collada::Sampler &sampler, + aiTextureType type, unsigned int idx) { // first of all, basic file name const aiString name = FindFilenameForEffectTexture(pParser, effect, sampler.mName); mat.AddProperty(&name, _AI_MATKEY_TEXTURE_BASE, type, idx); // mapping mode int map = aiTextureMapMode_Clamp; - if (sampler.mWrapU) + if (sampler.mWrapU) { map = aiTextureMapMode_Wrap; - if (sampler.mWrapU && sampler.mMirrorU) + } + if (sampler.mWrapU && sampler.mMirrorU) { map = aiTextureMapMode_Mirror; + } mat.AddProperty(&map, 1, _AI_MATKEY_MAPPINGMODE_U_BASE, type, idx); map = aiTextureMapMode_Clamp; - if (sampler.mWrapV) + if (sampler.mWrapV) { map = aiTextureMapMode_Wrap; - if (sampler.mWrapV && sampler.mMirrorV) + } + if (sampler.mWrapV && sampler.mMirrorV) { map = aiTextureMapMode_Mirror; + } mat.AddProperty(&map, 1, _AI_MATKEY_MAPPINGMODE_V_BASE, type, idx); // UV transformation mat.AddProperty(&sampler.mTransform, 1, - _AI_MATKEY_UVTRANSFORM_BASE, type, idx); + _AI_MATKEY_UVTRANSFORM_BASE, type, idx); // Blend mode - mat.AddProperty((int*)&sampler.mOp, 1, - _AI_MATKEY_TEXBLEND_BASE, type, idx); + mat.AddProperty((int *)&sampler.mOp, 1, + _AI_MATKEY_TEXBLEND_BASE, type, idx); // Blend factor - mat.AddProperty((ai_real*)&sampler.mWeighting, 1, - _AI_MATKEY_TEXBLEND_BASE, type, idx); + mat.AddProperty((ai_real *)&sampler.mWeighting, 1, + _AI_MATKEY_TEXBLEND_BASE, type, idx); // UV source index ... if we didn't resolve the mapping, it is actually just // a guess but it works in most cases. We search for the frst occurrence of a // number in the channel name. We assume it is the zero-based index into the // UV channel array of all corresponding meshes. It could also be one-based // for some exporters, but we won't care of it unless someone complains about. - if (sampler.mUVId != UINT_MAX) + if (sampler.mUVId != UINT_MAX) { map = sampler.mUVId; - else { + } else { map = -1; for (std::string::const_iterator it = sampler.mUVChannel.begin(); it != sampler.mUVChannel.end(); ++it) { if (IsNumeric(*it)) { @@ -1594,37 +1540,34 @@ void ColladaLoader::AddTexture(aiMaterial& mat, const ColladaParser& pParser, // ------------------------------------------------------------------------------------------------ // Fills materials from the collada material definitions -void ColladaLoader::FillMaterials(const ColladaParser& pParser, aiScene* /*pScene*/) -{ - for (auto &elem : newMats) - { - aiMaterial& mat = (aiMaterial&)*elem.second; - Collada::Effect& effect = *elem.first; +void ColladaLoader::FillMaterials(const ColladaParser &pParser, aiScene * /*pScene*/) { + for (auto &elem : newMats) { + aiMaterial &mat = (aiMaterial &)*elem.second; + Collada::Effect &effect = *elem.first; // resolve shading mode int shadeMode; - if (effect.mFaceted) /* fixme */ + if (effect.mFaceted) { shadeMode = aiShadingMode_Flat; - else { - switch (effect.mShadeType) - { - case Collada::Shade_Constant: - shadeMode = aiShadingMode_NoShading; - break; - case Collada::Shade_Lambert: - shadeMode = aiShadingMode_Gouraud; - break; - case Collada::Shade_Blinn: - shadeMode = aiShadingMode_Blinn; - break; - case Collada::Shade_Phong: - shadeMode = aiShadingMode_Phong; - break; + } else { + switch (effect.mShadeType) { + case Collada::Shade_Constant: + shadeMode = aiShadingMode_NoShading; + break; + case Collada::Shade_Lambert: + shadeMode = aiShadingMode_Gouraud; + break; + case Collada::Shade_Blinn: + shadeMode = aiShadingMode_Blinn; + break; + case Collada::Shade_Phong: + shadeMode = aiShadingMode_Phong; + break; - default: - ASSIMP_LOG_WARN("Collada: Unrecognized shading mode, using gouraud shading"); - shadeMode = aiShadingMode_Gouraud; - break; + default: + ASSIMP_LOG_WARN("Collada: Unrecognized shading mode, using gouraud shading"); + shadeMode = aiShadingMode_Gouraud; + break; } } mat.AddProperty(&shadeMode, 1, AI_MATKEY_SHADING_MODEL); @@ -1657,17 +1600,14 @@ void ColladaLoader::FillMaterials(const ColladaParser& pParser, aiScene* /*pScen // handle RGB transparency completely, cf Collada specs 1.5.0 pages 249 and 304 if (effect.mRGBTransparency) { // use luminance as defined by ISO/CIE color standards (see ITU-R Recommendation BT.709-4) - effect.mTransparency *= ( - 0.212671f * effect.mTransparent.r + - 0.715160f * effect.mTransparent.g + - 0.072169f * effect.mTransparent.b - ); + effect.mTransparency *= (0.212671f * effect.mTransparent.r + + 0.715160f * effect.mTransparent.g + + 0.072169f * effect.mTransparent.b); effect.mTransparent.a = 1.f; mat.AddProperty(&effect.mTransparent, 1, AI_MATKEY_COLOR_TRANSPARENT); - } - else { + } else { effect.mTransparency *= effect.mTransparent.a; } @@ -1709,62 +1649,42 @@ void ColladaLoader::FillMaterials(const ColladaParser& pParser, aiScene* /*pScen // ------------------------------------------------------------------------------------------------ // Constructs materials from the collada material definitions -void ColladaLoader::BuildMaterials(ColladaParser& pParser, aiScene* /*pScene*/) -{ +void ColladaLoader::BuildMaterials(ColladaParser &pParser, aiScene * /*pScene*/) { newMats.reserve(pParser.mMaterialLibrary.size()); for (ColladaParser::MaterialLibrary::const_iterator matIt = pParser.mMaterialLibrary.begin(); - matIt != pParser.mMaterialLibrary.end(); ++matIt) { - const Collada::Material& material = matIt->second; + matIt != pParser.mMaterialLibrary.end(); ++matIt) { + const Collada::Material &material = matIt->second; // a material is only a reference to an effect ColladaParser::EffectLibrary::iterator effIt = pParser.mEffectLibrary.find(material.mEffect); if (effIt == pParser.mEffectLibrary.end()) continue; - Collada::Effect& effect = effIt->second; + Collada::Effect &effect = effIt->second; // create material - aiMaterial* mat = new aiMaterial; + aiMaterial *mat = new aiMaterial; aiString name(material.mName.empty() ? matIt->first : material.mName); mat->AddProperty(&name, AI_MATKEY_NAME); // store the material mMaterialIndexByName[matIt->first] = newMats.size(); - newMats.push_back(std::pair(&effect, mat)); + newMats.push_back(std::pair(&effect, mat)); } // ScenePreprocessor generates a default material automatically if none is there. // All further code here in this loader works well without a valid material so // we can safely let it to ScenePreprocessor. -#if 0 - if (newMats.size() == 0) - { - aiMaterial* mat = new aiMaterial; - aiString name(AI_DEFAULT_MATERIAL_NAME); - mat->AddProperty(&name, AI_MATKEY_NAME); - - const int shadeMode = aiShadingMode_Phong; - mat->AddProperty(&shadeMode, 1, AI_MATKEY_SHADING_MODEL); - aiColor4D colAmbient(0.2, 0.2, 0.2, 1.0), colDiffuse(0.8, 0.8, 0.8, 1.0), colSpecular(0.5, 0.5, 0.5, 0.5); - mat->AddProperty(&colAmbient, 1, AI_MATKEY_COLOR_AMBIENT); - mat->AddProperty(&colDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE); - mat->AddProperty(&colSpecular, 1, AI_MATKEY_COLOR_SPECULAR); - const ai_real specExp = 5.0; - mat->AddProperty(&specExp, 1, AI_MATKEY_SHININESS); -} -#endif } // ------------------------------------------------------------------------------------------------ // Resolves the texture name for the given effect texture entry -// and loads the texture data -aiString ColladaLoader::FindFilenameForEffectTexture(const ColladaParser& pParser, - const Collada::Effect& pEffect, const std::string& pName) -{ +// and loads the texture data +aiString ColladaLoader::FindFilenameForEffectTexture(const ColladaParser &pParser, + const Collada::Effect &pEffect, const std::string &pName) { aiString result; // recurse through the param references until we end up at an image std::string name = pName; - while (1) - { + while (1) { // the given string is a param entry. Find it Collada::Effect::ParamLibrary::const_iterator it = pEffect.mParams.find(name); // if not found, we're at the end of the recursion. The resulting string should be the image ID @@ -1777,8 +1697,7 @@ aiString ColladaLoader::FindFilenameForEffectTexture(const ColladaParser& pParse // find the image referred by this name in the image library of the scene ColladaParser::ImageLibrary::const_iterator imIt = pParser.mImageLibrary.find(name); - if (imIt == pParser.mImageLibrary.end()) - { + if (imIt == pParser.mImageLibrary.end()) { ASSIMP_LOG_WARN_F("Collada: Unable to resolve effect texture entry \"", pName, "\", ended up at ID \"", name, "\"."); //set default texture file name @@ -1788,18 +1707,16 @@ aiString ColladaLoader::FindFilenameForEffectTexture(const ColladaParser& pParse } // if this is an embedded texture image setup an aiTexture for it - if (!imIt->second.mImageData.empty()) - { - aiTexture* tex = new aiTexture(); + if (!imIt->second.mImageData.empty()) { + aiTexture *tex = new aiTexture(); // Store embedded texture name reference tex->mFilename.Set(imIt->second.mFileName.c_str()); result.Set(imIt->second.mFileName); // TODO: check the possibility of using the flag "AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING" -// result.data[0] = '*'; -// result.length = 1 + ASSIMP_itoa10(result.data + 1, static_cast(MAXLEN - 1), static_cast(mTextures.size())); - + // result.data[0] = '*'; + // result.length = 1 + ASSIMP_itoa10(result.data + 1, static_cast(MAXLEN - 1), static_cast(mTextures.size())); // setup format hint if (imIt->second.mEmbeddedFormat.length() >= HINTMAXTEXTURELEN) { @@ -1810,28 +1727,26 @@ aiString ColladaLoader::FindFilenameForEffectTexture(const ColladaParser& pParse // and copy texture data tex->mHeight = 0; tex->mWidth = static_cast(imIt->second.mImageData.size()); - tex->pcData = (aiTexel*)new char[tex->mWidth]; + tex->pcData = (aiTexel *)new char[tex->mWidth]; memcpy(tex->pcData, &imIt->second.mImageData[0], tex->mWidth); // and add this texture to the list mTextures.push_back(tex); - } - else - { - if (imIt->second.mFileName.empty()) { - throw DeadlyImportError("Collada: Invalid texture, no data or file reference given"); - } + return result; + } - result.Set(imIt->second.mFileName); + if (imIt->second.mFileName.empty()) { + throw DeadlyImportError("Collada: Invalid texture, no data or file reference given"); } + + result.Set(imIt->second.mFileName); + return result; } // ------------------------------------------------------------------------------------------------ // Reads a float value from an accessor and its data array. -ai_real ColladaLoader::ReadFloat(const Collada::Accessor& pAccessor, const Collada::Data& pData, size_t pIndex, size_t pOffset) const -{ - // FIXME: (thom) Test for data type here in every access? For the moment, I leave this to the caller +ai_real ColladaLoader::ReadFloat(const Collada::Accessor &pAccessor, const Collada::Data &pData, size_t pIndex, size_t pOffset) const { size_t pos = pAccessor.mStride * pIndex + pAccessor.mOffset + pOffset; ai_assert(pos < pData.mValues.size()); return pData.mValues[pos]; @@ -1839,8 +1754,7 @@ ai_real ColladaLoader::ReadFloat(const Collada::Accessor& pAccessor, const Colla // ------------------------------------------------------------------------------------------------ // Reads a string value from an accessor and its data array. -const std::string& ColladaLoader::ReadString(const Collada::Accessor& pAccessor, const Collada::Data& pData, size_t pIndex) const -{ +const std::string &ColladaLoader::ReadString(const Collada::Accessor &pAccessor, const Collada::Data &pData, size_t pIndex) const { size_t pos = pAccessor.mStride * pIndex + pAccessor.mOffset; ai_assert(pos < pData.mStrings.size()); return pData.mStrings[pos]; @@ -1848,8 +1762,7 @@ const std::string& ColladaLoader::ReadString(const Collada::Accessor& pAccessor, // ------------------------------------------------------------------------------------------------ // Collects all nodes into the given array -void ColladaLoader::CollectNodes(const aiNode* pNode, std::vector& poNodes) const -{ +void ColladaLoader::CollectNodes(const aiNode *pNode, std::vector &poNodes) const { poNodes.push_back(pNode); for (size_t a = 0; a < pNode->mNumChildren; ++a) { CollectNodes(pNode->mChildren[a], poNodes); @@ -1858,14 +1771,12 @@ void ColladaLoader::CollectNodes(const aiNode* pNode, std::vector // ------------------------------------------------------------------------------------------------ // Finds a node in the collada scene by the given name -const Collada::Node* ColladaLoader::FindNode(const Collada::Node* pNode, const std::string& pName) const -{ +const Collada::Node *ColladaLoader::FindNode(const Collada::Node *pNode, const std::string &pName) const { if (pNode->mName == pName || pNode->mID == pName) return pNode; - for (size_t a = 0; a < pNode->mChildren.size(); ++a) - { - const Collada::Node* node = FindNode(pNode->mChildren[a], pName); + for (size_t a = 0; a < pNode->mChildren.size(); ++a) { + const Collada::Node *node = FindNode(pNode->mChildren[a], pName); if (node) return node; } @@ -1875,7 +1786,7 @@ const Collada::Node* ColladaLoader::FindNode(const Collada::Node* pNode, const s // ------------------------------------------------------------------------------------------------ // Finds a node in the collada scene by the given SID -const Collada::Node* ColladaLoader::FindNodeBySID( const Collada::Node* pNode, const std::string& pSID) const { +const Collada::Node *ColladaLoader::FindNodeBySID(const Collada::Node *pNode, const std::string &pSID) const { if (nullptr == pNode) { return nullptr; } @@ -1884,8 +1795,8 @@ const Collada::Node* ColladaLoader::FindNodeBySID( const Collada::Node* pNode, c return pNode; } - for( size_t a = 0; a < pNode->mChildren.size(); ++a) { - const Collada::Node* node = FindNodeBySID( pNode->mChildren[a], pSID); + for (size_t a = 0; a < pNode->mChildren.size(); ++a) { + const Collada::Node *node = FindNodeBySID(pNode->mChildren[a], pSID); if (node) { return node; } @@ -1897,28 +1808,22 @@ const Collada::Node* ColladaLoader::FindNodeBySID( const Collada::Node* pNode, c // ------------------------------------------------------------------------------------------------ // Finds a proper unique name for a node derived from the collada-node's properties. // The name must be unique for proper node-bone association. -std::string ColladaLoader::FindNameForNode(const Collada::Node* pNode) -{ +std::string ColladaLoader::FindNameForNode(const Collada::Node *pNode) { // If explicitly requested, just use the collada name. - if (useColladaName) - { + if (useColladaName) { if (!pNode->mName.empty()) { return pNode->mName; - } - else { + } else { return format() << "$ColladaAutoName$_" << mNodeNameCounter++; } - } - else - { + } else { // Now setup the name of the assimp node. The collada name might not be // unique, so we use the collada ID. if (!pNode->mID.empty()) return pNode->mID; else if (!pNode->mSID.empty()) return pNode->mSID; - else - { + else { // No need to worry. Unnamed nodes are no problem at all, except // if cameras or lights need to be assigned to them. return format() << "$ColladaAutoName$_" << mNodeNameCounter++; diff --git a/code/AssetLib/Collada/ColladaParser.cpp b/code/AssetLib/Collada/ColladaParser.cpp index 25ee2bc3c..04e6eb7d4 100644 --- a/code/AssetLib/Collada/ColladaParser.cpp +++ b/code/AssetLib/Collada/ColladaParser.cpp @@ -47,18 +47,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER -#include -#include #include "ColladaParser.h" -#include -#include #include #include -#include -#include -#include #include #include +#include +#include +#include +#include +#include +#include +#include #include @@ -68,24 +68,24 @@ using namespace Assimp::Formatter; // ------------------------------------------------------------------------------------------------ // Constructor to be privately used by Importer -ColladaParser::ColladaParser(IOSystem* pIOHandler, const std::string& pFile) - : mFileName(pFile) - , mReader(nullptr) - , mDataLibrary() - , mAccessorLibrary() - , mMeshLibrary() - , mNodeLibrary() - , mImageLibrary() - , mEffectLibrary() - , mMaterialLibrary() - , mLightLibrary() - , mCameraLibrary() - , mControllerLibrary() - , mRootNode(nullptr) - , mAnims() - , mUnitSize(1.0f) - , mUpDirection(UP_Y) - , mFormat(FV_1_5_n) // We assume the newest file format by default +ColladaParser::ColladaParser(IOSystem *pIOHandler, const std::string &pFile) : + mFileName(pFile), + mReader(nullptr), + mDataLibrary(), + mAccessorLibrary(), + mMeshLibrary(), + mNodeLibrary(), + mImageLibrary(), + mEffectLibrary(), + mMaterialLibrary(), + mLightLibrary(), + mCameraLibrary(), + mControllerLibrary(), + mRootNode(nullptr), + mAnims(), + mUnitSize(1.0f), + mUpDirection(UP_Y), + mFormat(FV_1_5_n) // We assume the newest file format by default { // validate io-handler instance if (nullptr == pIOHandler) { @@ -112,8 +112,7 @@ ColladaParser::ColladaParser(IOSystem* pIOHandler, const std::string& pFile) if (daefile == nullptr) { ThrowException(std::string("Invalid ZAE manifest: '") + std::string(dae_filename) + std::string("' is missing")); } - } - else { + } else { // attempt to open the file directly daefile.reset(pIOHandler->Open(pFile)); if (daefile.get() == nullptr) { @@ -139,8 +138,7 @@ ColladaParser::ColladaParser(IOSystem* pIOHandler, const std::string& pFile) // ------------------------------------------------------------------------------------------------ // Destructor, private as well -ColladaParser::~ColladaParser() -{ +ColladaParser::~ColladaParser() { delete mReader; for (NodeLibrary::iterator it = mNodeLibrary.begin(); it != mNodeLibrary.end(); ++it) delete it->second; @@ -153,8 +151,7 @@ ColladaParser::~ColladaParser() std::string ColladaParser::ReadZaeManifest(ZipArchiveIOSystem &zip_archive) { // Open the manifest std::unique_ptr manifestfile(zip_archive.Open("manifest.xml")); - if (manifestfile == nullptr) - { + if (manifestfile == nullptr) { // No manifest, hope there is only one .DAE inside std::vector file_list; zip_archive.getFileListExtension(file_list, "dae"); @@ -168,19 +165,16 @@ std::string ColladaParser::ReadZaeManifest(ZipArchiveIOSystem &zip_archive) { std::unique_ptr mIOWrapper(new CIrrXML_IOStreamReader(manifestfile.get())); std::unique_ptr manifest_reader(irr::io::createIrrXMLReader(mIOWrapper.get())); - while (manifest_reader->read()) - { + while (manifest_reader->read()) { // find the manifest "dae_root" element - if (manifest_reader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (::strcmp(manifest_reader->getNodeName(), "dae_root") == 0) - { + if (manifest_reader->getNodeType() == irr::io::EXN_ELEMENT) { + if (::strcmp(manifest_reader->getNodeName(), "dae_root") == 0) { if (!manifest_reader->read()) return std::string(); if (manifest_reader->getNodeType() != irr::io::EXN_TEXT && manifest_reader->getNodeType() != irr::io::EXN_CDATA) return std::string(); - const char* filepath = manifest_reader->getNodeData(); + const char *filepath = manifest_reader->getNodeData(); if (filepath == nullptr) return std::string(); @@ -196,14 +190,12 @@ std::string ColladaParser::ReadZaeManifest(ZipArchiveIOSystem &zip_archive) { // ------------------------------------------------------------------------------------------------ // Convert a path read from a collada file to the usual representation -void ColladaParser::UriDecodePath(aiString& ss) -{ +void ColladaParser::UriDecodePath(aiString &ss) { // TODO: collada spec, p 22. Handle URI correctly. // For the moment we're just stripping the file:// away to make it work. // Windows doesn't seem to be able to find stuff like // 'file://..\LWO\LWO2\MappingModes\earthSpherical.jpg' - if (0 == strncmp(ss.data, "file://", 7)) - { + if (0 == strncmp(ss.data, "file://", 7)) { ss.length -= 7; memmove(ss.data, ss.data + 7, ss.length); ss.data[ss.length] = '\0'; @@ -211,7 +203,7 @@ void ColladaParser::UriDecodePath(aiString& ss) // Maxon Cinema Collada Export writes "file:///C:\andsoon" with three slashes... // I need to filter it without destroying linux paths starting with "/somewhere" -#if defined( _MSC_VER ) +#if defined(_MSC_VER) if (ss.data[0] == '/' && isalpha((unsigned char)ss.data[1]) && ss.data[2] == ':') { #else if (ss.data[0] == '/' && isalpha(ss.data[1]) && ss.data[2] == ':') { @@ -222,19 +214,15 @@ void ColladaParser::UriDecodePath(aiString& ss) } // find and convert all %xy special chars - char* out = ss.data; - for (const char* it = ss.data; it != ss.data + ss.length; /**/) - { - if (*it == '%' && (it + 3) < ss.data + ss.length) - { + char *out = ss.data; + for (const char *it = ss.data; it != ss.data + ss.length; /**/) { + if (*it == '%' && (it + 3) < ss.data + ss.length) { // separate the number to avoid dragging in chars from behind into the parsing char mychar[3] = { it[1], it[2], 0 }; size_t nbr = strtoul16(mychar); it += 3; *out++ = (char)(nbr & 0xFF); - } - else - { + } else { *out++ = *it++; } } @@ -247,10 +235,9 @@ void ColladaParser::UriDecodePath(aiString& ss) // ------------------------------------------------------------------------------------------------ // Read bool from text contents of current element -bool ColladaParser::ReadBoolFromTextContent() -{ - const char* cur = GetTextContent(); - if ( nullptr == cur) { +bool ColladaParser::ReadBoolFromTextContent() { + const char *cur = GetTextContent(); + if (nullptr == cur) { return false; } return (!ASSIMP_strincmp(cur, "true", 4) || '0' != *cur); @@ -258,10 +245,9 @@ bool ColladaParser::ReadBoolFromTextContent() // ------------------------------------------------------------------------------------------------ // Read float from text contents of current element -ai_real ColladaParser::ReadFloatFromTextContent() -{ - const char* cur = GetTextContent(); - if ( nullptr == cur ) { +ai_real ColladaParser::ReadFloatFromTextContent() { + const char *cur = GetTextContent(); + if (nullptr == cur) { return 0.0; } return fast_atof(cur); @@ -269,49 +255,39 @@ ai_real ColladaParser::ReadFloatFromTextContent() // ------------------------------------------------------------------------------------------------ // Reads the contents of the file -void ColladaParser::ReadContents() -{ - while (mReader->read()) - { +void ColladaParser::ReadContents() { + while (mReader->read()) { // handle the root element "COLLADA" - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("COLLADA")) - { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("COLLADA")) { // check for 'version' attribute const int attrib = TestAttribute("version"); if (attrib != -1) { - const char* version = mReader->getAttributeValue(attrib); + const char *version = mReader->getAttributeValue(attrib); // Store declared format version string aiString v; v.Set(version); - mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, v ); + mAssetMetaData.emplace(AI_METADATA_SOURCE_FORMAT_VERSION, v); if (!::strncmp(version, "1.5", 3)) { mFormat = FV_1_5_n; ASSIMP_LOG_DEBUG("Collada schema version is 1.5.n"); - } - else if (!::strncmp(version, "1.4", 3)) { + } else if (!::strncmp(version, "1.4", 3)) { mFormat = FV_1_4_n; ASSIMP_LOG_DEBUG("Collada schema version is 1.4.n"); - } - else if (!::strncmp(version, "1.3", 3)) { + } else if (!::strncmp(version, "1.3", 3)) { mFormat = FV_1_3_n; ASSIMP_LOG_DEBUG("Collada schema version is 1.3.n"); } } ReadStructure(); - } - else - { + } else { ASSIMP_LOG_DEBUG_F("Ignoring global element <", mReader->getNodeName(), ">."); SkipElement(); } - } - else - { + } else { // skip everything else silently } } @@ -319,13 +295,10 @@ void ColladaParser::ReadContents() // ------------------------------------------------------------------------------------------------ // Reads the structure of the file -void ColladaParser::ReadStructure() -{ - while (mReader->read()) - { +void ColladaParser::ReadStructure() { + while (mReader->read()) { // beginning of elements - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("asset")) ReadAssetInfo(); else if (IsElement("library_animations")) @@ -354,9 +327,7 @@ void ColladaParser::ReadStructure() ReadScene(); else SkipElement(); - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { break; } } @@ -367,34 +338,27 @@ void ColladaParser::ReadStructure() // ------------------------------------------------------------------------------------------------ // Reads asset information such as coordinate system information and legal blah -void ColladaParser::ReadAssetInfo() -{ +void ColladaParser::ReadAssetInfo() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("unit")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("unit")) { // read unit data from the element's attributes const int attrIndex = TestAttribute("meter"); if (attrIndex == -1) { mUnitSize = 1.f; - } - else { + } else { mUnitSize = mReader->getAttributeValueAsFloat(attrIndex); } // consume the trailing stuff if (!mReader->isEmptyElement()) SkipElement(); - } - else if (IsElement("up_axis")) - { + } else if (IsElement("up_axis")) { // read content, strip whitespace, compare - const char* content = GetTextContent(); + const char *content = GetTextContent(); if (strncmp(content, "X_UP", 4) == 0) mUpDirection = UP_X; else if (strncmp(content, "Z_UP", 4) == 0) @@ -404,18 +368,12 @@ void ColladaParser::ReadAssetInfo() // check element end TestClosing("up_axis"); - } - else if (IsElement("contributor")) - { + } else if (IsElement("contributor")) { ReadContributorInfo(); - } - else - { + } else { ReadMetaDataItem(mAssetMetaData); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "asset") != 0) ThrowException("Expected end of element."); @@ -426,19 +384,14 @@ void ColladaParser::ReadAssetInfo() // ------------------------------------------------------------------------------------------------ // Reads the contributor info -void ColladaParser::ReadContributorInfo() -{ +void ColladaParser::ReadContributorInfo() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { ReadMetaDataItem(mAssetMetaData); - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "contributor") != 0) ThrowException("Expected end of element."); break; @@ -448,11 +401,11 @@ void ColladaParser::ReadContributorInfo() static bool FindCommonKey(const std::string &collada_key, const MetaKeyPairVector &key_renaming, size_t &found_index) { for (size_t i = 0; i < key_renaming.size(); ++i) { - if (key_renaming[i].first == collada_key) { + if (key_renaming[i].first == collada_key) { found_index = i; return true; - } - } + } + } found_index = std::numeric_limits::max(); return false; } @@ -461,44 +414,39 @@ static bool FindCommonKey(const std::string &collada_key, const MetaKeyPairVecto // Reads a single string metadata item void ColladaParser::ReadMetaDataItem(StringMetaData &metadata) { const Collada::MetaKeyPairVector &key_renaming = GetColladaAssimpMetaKeysCamelCase(); - // Metadata such as created, keywords, subject etc - const char *key_char = mReader->getNodeName(); - if (key_char != nullptr) { + // Metadata such as created, keywords, subject etc + const char *key_char = mReader->getNodeName(); + if (key_char != nullptr) { const std::string key_str(key_char); - const char *value_char = TestTextContent(); - if (value_char != nullptr) { + const char *value_char = TestTextContent(); + if (value_char != nullptr) { aiString aistr; - aistr.Set(value_char); + aistr.Set(value_char); std::string camel_key_str(key_str); - ToCamelCase(camel_key_str); + ToCamelCase(camel_key_str); - size_t found_index; - if (FindCommonKey(camel_key_str, key_renaming, found_index)) { + size_t found_index; + if (FindCommonKey(camel_key_str, key_renaming, found_index)) { metadata.emplace(key_renaming[found_index].second, aistr); } else { - metadata.emplace(camel_key_str, aistr); - } + metadata.emplace(camel_key_str, aistr); + } } TestClosing(key_str.c_str()); - } - else + } else SkipElement(); } // ------------------------------------------------------------------------------------------------ // Reads the animation clips -void ColladaParser::ReadAnimationClipLibrary() -{ +void ColladaParser::ReadAnimationClipLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("animation_clip")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("animation_clip")) { // optional name given as an attribute std::string animName; int indexName = TestAttribute("name"); @@ -510,20 +458,16 @@ void ColladaParser::ReadAnimationClipLibrary() else animName = std::string("animation_") + to_string(mAnimationClipLibrary.size()); - std::pair > clip; + std::pair> clip; clip.first = animName; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("instance_animation")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("instance_animation")) { int indexUrl = TestAttribute("url"); - if (indexUrl >= 0) - { - const char* url = mReader->getAttributeValue(indexUrl); + if (indexUrl >= 0) { + const char *url = mReader->getAttributeValue(indexUrl); if (url[0] != '#') ThrowException("Unknown reference format"); @@ -531,15 +475,11 @@ void ColladaParser::ReadAnimationClipLibrary() clip.second.push_back(url); } - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "animation_clip") != 0) ThrowException("Expected end of element."); @@ -547,19 +487,14 @@ void ColladaParser::ReadAnimationClipLibrary() } } - if (clip.second.size() > 0) - { + if (clip.second.size() > 0) { mAnimationClipLibrary.push_back(clip); } - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_animation_clips") != 0) ThrowException("Expected end of element."); @@ -568,8 +503,7 @@ void ColladaParser::ReadAnimationClipLibrary() } } -void ColladaParser::PostProcessControllers() -{ +void ColladaParser::PostProcessControllers() { std::string meshId; for (ControllerLibrary::iterator it = mControllerLibrary.begin(); it != mControllerLibrary.end(); ++it) { meshId = it->second.mMeshId; @@ -585,14 +519,11 @@ void ColladaParser::PostProcessControllers() // ------------------------------------------------------------------------------------------------ // Re-build animations from animation clip library, if present, otherwise combine single-channel animations -void ColladaParser::PostProcessRootAnimations() -{ - if (mAnimationClipLibrary.size() > 0) - { +void ColladaParser::PostProcessRootAnimations() { + if (mAnimationClipLibrary.size() > 0) { Animation temp; - for (AnimationClipLibrary::iterator it = mAnimationClipLibrary.begin(); it != mAnimationClipLibrary.end(); ++it) - { + for (AnimationClipLibrary::iterator it = mAnimationClipLibrary.begin(); it != mAnimationClipLibrary.end(); ++it) { std::string clipName = it->first; Animation *clip = new Animation(); @@ -600,14 +531,12 @@ void ColladaParser::PostProcessRootAnimations() temp.mSubAnims.push_back(clip); - for (std::vector::iterator a = it->second.begin(); a != it->second.end(); ++a) - { + for (std::vector::iterator a = it->second.begin(); a != it->second.end(); ++a) { std::string animationID = *a; AnimationLibrary::iterator animation = mAnimationLibrary.find(animationID); - if (animation != mAnimationLibrary.end()) - { + if (animation != mAnimationLibrary.end()) { Animation *pSourceAnimation = animation->second; pSourceAnimation->CollectChannelsRecursively(clip->mChannels); @@ -619,37 +548,27 @@ void ColladaParser::PostProcessRootAnimations() // Ensure no double deletes. temp.mSubAnims.clear(); - } - else - { + } else { mAnims.CombineSingleChannelAnimations(); } } // ------------------------------------------------------------------------------------------------ // Reads the animation library -void ColladaParser::ReadAnimationLibrary() -{ +void ColladaParser::ReadAnimationLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("animation")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("animation")) { // delegate the reading. Depending on the inner elements it will be a container or a anim channel ReadAnimation(&mAnims); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_animations") != 0) ThrowException("Expected end of element."); @@ -660,8 +579,7 @@ void ColladaParser::ReadAnimationLibrary() // ------------------------------------------------------------------------------------------------ // Reads an animation into the given parent structure -void ColladaParser::ReadAnimation(Collada::Animation* pParent) -{ +void ColladaParser::ReadAnimation(Collada::Animation *pParent) { if (mReader->isEmptyElement()) return; @@ -670,7 +588,7 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) typedef std::map ChannelMap; ChannelMap channels; // this is the anim container in case we're a container - Animation* anim = NULL; + Animation *anim = NULL; // optional name given as an attribute std::string animName; @@ -688,16 +606,12 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) else animName = "animation"; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // we have subanimations - if (IsElement("animation")) - { + if (IsElement("animation")) { // create container from our element - if (!anim) - { + if (!anim) { anim = new Animation; anim->mName = animName; pParent->mSubAnims.push_back(anim); @@ -705,14 +619,10 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) // recurse into the subelement ReadAnimation(anim); - } - else if (IsElement("source")) - { + } else if (IsElement("source")) { // possible animation data - we'll never know. Better store it ReadSource(); - } - else if (IsElement("sampler")) - { + } else if (IsElement("sampler")) { // read the ID to assign the corresponding collada channel afterwards. int indexId = GetAttribute("id"); std::string id = mReader->getAttributeValue(indexId); @@ -720,15 +630,13 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) // have it read into a channel ReadAnimationSampler(newChannel->second); - } - else if (IsElement("channel")) - { + } else if (IsElement("channel")) { // the binding element whose whole purpose is to provide the target to animate // Thanks, Collada! A directly posted information would have been too simple, I guess. // Better add another indirection to that! Can't have enough of those. int indexTarget = GetAttribute("target"); int indexSource = GetAttribute("source"); - const char* sourceId = mReader->getAttributeValue(indexSource); + const char *sourceId = mReader->getAttributeValue(indexSource); if (sourceId[0] == '#') sourceId++; ChannelMap::iterator cit = channels.find(sourceId); @@ -737,15 +645,11 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) if (!mReader->isEmptyElement()) SkipElement(); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "animation") != 0) ThrowException("Expected end of element."); @@ -754,15 +658,14 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) } // it turned out to have channels - add them - if (!channels.empty()) - { + if (!channels.empty()) { // FIXME: Is this essentially doing the same as "single-anim-node" codepath in // ColladaLoader::StoreAnimations? For now, this has been deferred to after // all animations and all clips have been read. Due to handling of // this cannot be done here, as the channel owner // is lost, and some exporters make up animations by referring to multiple // single-channel animations from an . -/* + /* // special filtering for stupid exporters packing each channel into a separate animation if( channels.size() == 1) { @@ -771,8 +674,7 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) */ { // else create the animation, if not done yet, and store the channels - if (!anim) - { + if (!anim) { anim = new Animation; anim->mName = animName; pParent->mSubAnims.push_back(anim); @@ -780,8 +682,7 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) for (ChannelMap::const_iterator it = channels.begin(); it != channels.end(); ++it) anim->mChannels.push_back(it->second); - if (indexID >= 0) - { + if (indexID >= 0) { mAnimationLibrary[animID] = anim; } } @@ -790,18 +691,14 @@ void ColladaParser::ReadAnimation(Collada::Animation* pParent) // ------------------------------------------------------------------------------------------------ // Reads an animation sampler into the given anim channel -void ColladaParser::ReadAnimationSampler(Collada::AnimationChannel& pChannel) -{ - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("input")) - { +void ColladaParser::ReadAnimationSampler(Collada::AnimationChannel &pChannel) { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("input")) { int indexSemantic = GetAttribute("semantic"); - const char* semantic = mReader->getAttributeValue(indexSemantic); + const char *semantic = mReader->getAttributeValue(indexSemantic); int indexSource = GetAttribute("source"); - const char* source = mReader->getAttributeValue(indexSource); + const char *source = mReader->getAttributeValue(indexSource); if (source[0] != '#') ThrowException("Unsupported URL format"); source++; @@ -819,15 +716,11 @@ void ColladaParser::ReadAnimationSampler(Collada::AnimationChannel& pChannel) if (!mReader->isEmptyElement()) SkipElement(); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "sampler") != 0) ThrowException("Expected end of element."); @@ -838,17 +731,13 @@ void ColladaParser::ReadAnimationSampler(Collada::AnimationChannel& pChannel) // ------------------------------------------------------------------------------------------------ // Reads the skeleton controller library -void ColladaParser::ReadControllerLibrary() -{ +void ColladaParser::ReadControllerLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("controller")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("controller")) { // read ID. Ask the spec if it's necessary or optional... you might be surprised. int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); @@ -858,15 +747,11 @@ void ColladaParser::ReadControllerLibrary() // read on from there ReadController(mControllerLibrary[id]); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_controllers") != 0) ThrowException("Expected end of element."); @@ -877,18 +762,14 @@ void ColladaParser::ReadControllerLibrary() // ------------------------------------------------------------------------------------------------ // Reads a controller into the given mesh structure -void ColladaParser::ReadController(Collada::Controller& pController) -{ +void ColladaParser::ReadController(Collada::Controller &pController) { // initial values pController.mType = Skin; pController.mMethod = Normalized; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // two types of controllers: "skin" and "morph". Only the first one is relevant, we skip the other - if (IsElement("morph")) - { + if (IsElement("morph")) { pController.mType = Morph; int baseIndex = GetAttribute("source"); pController.mMeshId = mReader->getAttributeValue(baseIndex) + 1; @@ -898,22 +779,17 @@ void ColladaParser::ReadController(Collada::Controller& pController) if (strcmp(method, "RELATIVE") == 0) pController.mMethod = Relative; } - } - else if (IsElement("skin")) - { + } else if (IsElement("skin")) { // read the mesh it refers to. According to the spec this could also be another // controller, but I refuse to implement every single idea they've come up with int sourceIndex = GetAttribute("source"); pController.mMeshId = mReader->getAttributeValue(sourceIndex) + 1; - } - else if (IsElement("bind_shape_matrix")) - { + } else if (IsElement("bind_shape_matrix")) { // content is 16 floats to define a matrix... it seems to be important for some models - const char* content = GetTextContent(); + const char *content = GetTextContent(); // read the 16 floats - for (unsigned int a = 0; a < 16; a++) - { + for (unsigned int a = 0; a < 16; a++) { // read a number content = fast_atoreal_move(content, pController.mBindShapeMatrix[a]); // skip whitespace after it @@ -921,22 +797,14 @@ void ColladaParser::ReadController(Collada::Controller& pController) } TestClosing("bind_shape_matrix"); - } - else if (IsElement("source")) - { + } else if (IsElement("source")) { // data array - we have specialists to handle this ReadSource(); - } - else if (IsElement("joints")) - { + } else if (IsElement("joints")) { ReadControllerJoints(pController); - } - else if (IsElement("vertex_weights")) - { + } else if (IsElement("vertex_weights")) { ReadControllerWeights(pController); - } - else if (IsElement("targets")) - { + } else if (IsElement("targets")) { while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("input")) { @@ -947,29 +815,22 @@ void ColladaParser::ReadController(Collada::Controller& pController) const char *source = mReader->getAttributeValue(sourceIndex); if (strcmp(semantics, "MORPH_TARGET") == 0) { pController.mMorphTarget = source + 1; - } - else if (strcmp(semantics, "MORPH_WEIGHT") == 0) - { + } else if (strcmp(semantics, "MORPH_WEIGHT") == 0) { pController.mMorphWeight = source + 1; } } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "targets") == 0) break; else ThrowException("Expected end of element."); } } - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "controller") == 0) break; else if (strcmp(mReader->getNodeName(), "skin") != 0 && strcmp(mReader->getNodeName(), "morph") != 0) @@ -980,19 +841,15 @@ void ColladaParser::ReadController(Collada::Controller& pController) // ------------------------------------------------------------------------------------------------ // Reads the joint definitions for the given controller -void ColladaParser::ReadControllerJoints(Collada::Controller& pController) -{ - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { +void ColladaParser::ReadControllerJoints(Collada::Controller &pController) { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // Input channels for joint data. Two possible semantics: "JOINT" and "INV_BIND_MATRIX" - if (IsElement("input")) - { + if (IsElement("input")) { int indexSemantic = GetAttribute("semantic"); - const char* attrSemantic = mReader->getAttributeValue(indexSemantic); + const char *attrSemantic = mReader->getAttributeValue(indexSemantic); int indexSource = GetAttribute("source"); - const char* attrSource = mReader->getAttributeValue(indexSource); + const char *attrSource = mReader->getAttributeValue(indexSource); // local URLS always start with a '#'. We don't support global URLs if (attrSource[0] != '#') @@ -1010,15 +867,11 @@ void ColladaParser::ReadControllerJoints(Collada::Controller& pController) // skip inner data, if present if (!mReader->isEmptyElement()) SkipElement(); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "joints") != 0) ThrowException("Expected end of element."); @@ -1029,26 +882,22 @@ void ColladaParser::ReadControllerJoints(Collada::Controller& pController) // ------------------------------------------------------------------------------------------------ // Reads the joint weights for the given controller -void ColladaParser::ReadControllerWeights(Collada::Controller& pController) -{ +void ColladaParser::ReadControllerWeights(Collada::Controller &pController) { // read vertex count from attributes and resize the array accordingly int indexCount = GetAttribute("count"); size_t vertexCount = (size_t)mReader->getAttributeValueAsInt(indexCount); pController.mWeightCounts.resize(vertexCount); - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // Input channels for weight data. Two possible semantics: "JOINT" and "WEIGHT" - if (IsElement("input") && vertexCount > 0) - { + if (IsElement("input") && vertexCount > 0) { InputChannel channel; int indexSemantic = GetAttribute("semantic"); - const char* attrSemantic = mReader->getAttributeValue(indexSemantic); + const char *attrSemantic = mReader->getAttributeValue(indexSemantic); int indexSource = GetAttribute("source"); - const char* attrSource = mReader->getAttributeValue(indexSource); + const char *attrSource = mReader->getAttributeValue(indexSource); int indexOffset = TestAttribute("offset"); if (indexOffset >= 0) channel.mOffset = mReader->getAttributeValueAsInt(indexOffset); @@ -1069,14 +918,11 @@ void ColladaParser::ReadControllerWeights(Collada::Controller& pController) // skip inner data, if present if (!mReader->isEmptyElement()) SkipElement(); - } - else if (IsElement("vcount") && vertexCount > 0) - { + } else if (IsElement("vcount") && vertexCount > 0) { // read weight count per vertex - const char* text = GetTextContent(); + const char *text = GetTextContent(); size_t numWeights = 0; - for (std::vector::iterator it = pController.mWeightCounts.begin(); it != pController.mWeightCounts.end(); ++it) - { + for (std::vector::iterator it = pController.mWeightCounts.begin(); it != pController.mWeightCounts.end(); ++it) { if (*text == 0) ThrowException("Out of data while reading "); @@ -1089,14 +935,11 @@ void ColladaParser::ReadControllerWeights(Collada::Controller& pController) // reserve weight count pController.mWeights.resize(numWeights); - } - else if (IsElement("v") && vertexCount > 0) - { + } else if (IsElement("v") && vertexCount > 0) { // read JointIndex - WeightIndex pairs - const char* text = GetTextContent(); + const char *text = GetTextContent(); - for (std::vector< std::pair >::iterator it = pController.mWeights.begin(); it != pController.mWeights.end(); ++it) - { + for (std::vector>::iterator it = pController.mWeights.begin(); it != pController.mWeights.end(); ++it) { if (*text == 0) ThrowException("Out of data while reading "); it->first = strtoul10(text, &text); @@ -1108,15 +951,11 @@ void ColladaParser::ReadControllerWeights(Collada::Controller& pController) } TestClosing("v"); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "vertex_weights") != 0) ThrowException("Expected end of element."); @@ -1127,16 +966,13 @@ void ColladaParser::ReadControllerWeights(Collada::Controller& pController) // ------------------------------------------------------------------------------------------------ // Reads the image library contents -void ColladaParser::ReadImageLibrary() -{ +void ColladaParser::ReadImageLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("image")) - { + if (IsElement("image")) { // read ID. Another entry which is "optional" by design but obligatory in reality int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); @@ -1146,14 +982,11 @@ void ColladaParser::ReadImageLibrary() // read on from there ReadImage(mImageLibrary[id]); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_images") != 0) ThrowException("Expected end of element."); @@ -1164,25 +997,19 @@ void ColladaParser::ReadImageLibrary() // ------------------------------------------------------------------------------------------------ // Reads an image entry into the given image -void ColladaParser::ReadImage(Collada::Image& pImage) -{ - while (mReader->read()) - { +void ColladaParser::ReadImage(Collada::Image &pImage) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // Need to run different code paths here, depending on the Collada XSD version if (IsElement("image")) { SkipElement(); - } - else if (IsElement("init_from")) - { - if (mFormat == FV_1_4_n) - { + } else if (IsElement("init_from")) { + if (mFormat == FV_1_4_n) { // FIX: C4D exporter writes empty tags if (!mReader->isEmptyElement()) { // element content is filename - hopefully - const char* sz = TestTextContent(); - if (sz) - { + const char *sz = TestTextContent(); + if (sz) { aiString filepath(sz); UriDecodePath(filepath); pImage.mFileName = filepath.C_Str(); @@ -1192,9 +1019,7 @@ void ColladaParser::ReadImage(Collada::Image& pImage) if (!pImage.mFileName.length()) { pImage.mFileName = "unknown_texture"; } - } - else if (mFormat == FV_1_5_n) - { + } else if (mFormat == FV_1_5_n) { // make sure we skip over mip and array initializations, which // we don't support, but which could confuse the loader if // they're not skipped. @@ -1212,35 +1037,31 @@ void ColladaParser::ReadImage(Collada::Image& pImage) // TODO: correctly jump over cube and volume maps? } - } - else if (mFormat == FV_1_5_n) - { - if (IsElement("ref")) - { + } else if (mFormat == FV_1_5_n) { + if (IsElement("ref")) { // element content is filename - hopefully - const char* sz = TestTextContent(); - if (sz) - { + const char *sz = TestTextContent(); + if (sz) { aiString filepath(sz); UriDecodePath(filepath); pImage.mFileName = filepath.C_Str(); } TestClosing("ref"); - } - else if (IsElement("hex") && !pImage.mFileName.length()) - { + } else if (IsElement("hex") && !pImage.mFileName.length()) { // embedded image. get format const int attrib = TestAttribute("format"); if (-1 == attrib) ASSIMP_LOG_WARN("Collada: Unknown image file format"); - else pImage.mEmbeddedFormat = mReader->getAttributeValue(attrib); + else + pImage.mEmbeddedFormat = mReader->getAttributeValue(attrib); - const char* data = GetTextContent(); + const char *data = GetTextContent(); // hexadecimal-encoded binary octets. First of all, find the // required buffer size to reserve enough storage. - const char* cur = data; - while (!IsSpaceOrNewLine(*cur)) cur++; + const char *cur = data; + while (!IsSpaceOrNewLine(*cur)) + cur++; const unsigned int size = (unsigned int)(cur - data) * 2; pImage.mImageData.resize(size); @@ -1249,14 +1070,11 @@ void ColladaParser::ReadImage(Collada::Image& pImage) TestClosing("hex"); } - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "image") == 0) break; } @@ -1265,18 +1083,14 @@ void ColladaParser::ReadImage(Collada::Image& pImage) // ------------------------------------------------------------------------------------------------ // Reads the material library -void ColladaParser::ReadMaterialLibrary() -{ +void ColladaParser::ReadMaterialLibrary() { if (mReader->isEmptyElement()) return; std::map names; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("material")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("material")) { // read ID. By now you probably know my opinion about this "specification" int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); @@ -1289,17 +1103,13 @@ void ColladaParser::ReadMaterialLibrary() // create an entry and store it in the library under its ID mMaterialLibrary[id] = Material(); - if (!name.empty()) - { + if (!name.empty()) { std::map::iterator it = names.find(name); - if (it != names.end()) - { + if (it != names.end()) { std::ostringstream strStream; strStream << ++it->second; name.append(" " + strStream.str()); - } - else - { + } else { names[name] = 0; } @@ -1307,15 +1117,11 @@ void ColladaParser::ReadMaterialLibrary() } ReadMaterial(mMaterialLibrary[id]); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_materials") != 0) ThrowException("Expected end of element."); @@ -1326,16 +1132,13 @@ void ColladaParser::ReadMaterialLibrary() // ------------------------------------------------------------------------------------------------ // Reads the light library -void ColladaParser::ReadLightLibrary() -{ +void ColladaParser::ReadLightLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("light")) - { + if (IsElement("light")) { // read ID. By now you probably know my opinion about this "specification" int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); @@ -1343,14 +1146,11 @@ void ColladaParser::ReadLightLibrary() // create an entry and store it in the library under its ID ReadLight(mLightLibrary[id] = Light()); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_lights") != 0) ThrowException("Expected end of element."); @@ -1361,36 +1161,30 @@ void ColladaParser::ReadLightLibrary() // ------------------------------------------------------------------------------------------------ // Reads the camera library -void ColladaParser::ReadCameraLibrary() -{ +void ColladaParser::ReadCameraLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("camera")) - { + if (IsElement("camera")) { // read ID. By now you probably know my opinion about this "specification" int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); // create an entry and store it in the library under its ID - Camera& cam = mCameraLibrary[id]; + Camera &cam = mCameraLibrary[id]; attrID = TestAttribute("name"); if (attrID != -1) cam.mName = mReader->getAttributeValue(attrID); ReadCamera(cam); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_cameras") != 0) ThrowException("Expected end of element."); @@ -1401,33 +1195,26 @@ void ColladaParser::ReadCameraLibrary() // ------------------------------------------------------------------------------------------------ // Reads a material entry into the given material -void ColladaParser::ReadMaterial(Collada::Material& pMaterial) -{ - while (mReader->read()) - { +void ColladaParser::ReadMaterial(Collada::Material &pMaterial) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("material")) { SkipElement(); - } - else if (IsElement("instance_effect")) - { + } else if (IsElement("instance_effect")) { // referred effect by URL int attrUrl = GetAttribute("url"); - const char* url = mReader->getAttributeValue(attrUrl); + const char *url = mReader->getAttributeValue(attrUrl); if (url[0] != '#') ThrowException("Unknown reference format"); pMaterial.mEffect = url + 1; SkipElement(); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "material") != 0) ThrowException("Expected end of element."); @@ -1438,58 +1225,46 @@ void ColladaParser::ReadMaterial(Collada::Material& pMaterial) // ------------------------------------------------------------------------------------------------ // Reads a light entry into the given light -void ColladaParser::ReadLight(Collada::Light& pLight) -{ - while (mReader->read()) - { +void ColladaParser::ReadLight(Collada::Light &pLight) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("light")) { SkipElement(); - } - else if (IsElement("spot")) { + } else if (IsElement("spot")) { pLight.mType = aiLightSource_SPOT; - } - else if (IsElement("ambient")) { + } else if (IsElement("ambient")) { pLight.mType = aiLightSource_AMBIENT; - } - else if (IsElement("directional")) { + } else if (IsElement("directional")) { pLight.mType = aiLightSource_DIRECTIONAL; - } - else if (IsElement("point")) { + } else if (IsElement("point")) { pLight.mType = aiLightSource_POINT; - } - else if (IsElement("color")) { + } else if (IsElement("color")) { // text content contains 3 floats - const char* content = GetTextContent(); + const char *content = GetTextContent(); - content = fast_atoreal_move(content, (ai_real&)pLight.mColor.r); + content = fast_atoreal_move(content, (ai_real &)pLight.mColor.r); SkipSpacesAndLineEnd(&content); - content = fast_atoreal_move(content, (ai_real&)pLight.mColor.g); + content = fast_atoreal_move(content, (ai_real &)pLight.mColor.g); SkipSpacesAndLineEnd(&content); - content = fast_atoreal_move(content, (ai_real&)pLight.mColor.b); + content = fast_atoreal_move(content, (ai_real &)pLight.mColor.b); SkipSpacesAndLineEnd(&content); TestClosing("color"); - } - else if (IsElement("constant_attenuation")) { + } else if (IsElement("constant_attenuation")) { pLight.mAttConstant = ReadFloatFromTextContent(); TestClosing("constant_attenuation"); - } - else if (IsElement("linear_attenuation")) { + } else if (IsElement("linear_attenuation")) { pLight.mAttLinear = ReadFloatFromTextContent(); TestClosing("linear_attenuation"); - } - else if (IsElement("quadratic_attenuation")) { + } else if (IsElement("quadratic_attenuation")) { pLight.mAttQuadratic = ReadFloatFromTextContent(); TestClosing("quadratic_attenuation"); - } - else if (IsElement("falloff_angle")) { + } else if (IsElement("falloff_angle")) { pLight.mFalloffAngle = ReadFloatFromTextContent(); TestClosing("falloff_angle"); - } - else if (IsElement("falloff_exponent")) { + } else if (IsElement("falloff_exponent")) { pLight.mFalloffExponent = ReadFloatFromTextContent(); TestClosing("falloff_exponent"); } @@ -1503,16 +1278,13 @@ void ColladaParser::ReadLight(Collada::Light& pLight) else if (IsElement("penumbra_angle")) { pLight.mPenumbraAngle = ReadFloatFromTextContent(); TestClosing("penumbra_angle"); - } - else if (IsElement("intensity")) { + } else if (IsElement("intensity")) { pLight.mIntensity = ReadFloatFromTextContent(); TestClosing("intensity"); - } - else if (IsElement("falloff")) { + } else if (IsElement("falloff")) { pLight.mOuterAngle = ReadFloatFromTextContent(); TestClosing("falloff"); - } - else if (IsElement("hotspot_beam")) { + } else if (IsElement("hotspot_beam")) { pLight.mFalloffAngle = ReadFloatFromTextContent(); TestClosing("hotspot_beam"); } @@ -1522,8 +1294,7 @@ void ColladaParser::ReadLight(Collada::Light& pLight) pLight.mOuterAngle = ReadFloatFromTextContent(); TestClosing("decay_falloff"); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "light") == 0) break; } @@ -1532,39 +1303,30 @@ void ColladaParser::ReadLight(Collada::Light& pLight) // ------------------------------------------------------------------------------------------------ // Reads a camera entry into the given light -void ColladaParser::ReadCamera(Collada::Camera& pCamera) -{ - while (mReader->read()) - { +void ColladaParser::ReadCamera(Collada::Camera &pCamera) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("camera")) { SkipElement(); - } - else if (IsElement("orthographic")) { + } else if (IsElement("orthographic")) { pCamera.mOrtho = true; - } - else if (IsElement("xfov") || IsElement("xmag")) { + } else if (IsElement("xfov") || IsElement("xmag")) { pCamera.mHorFov = ReadFloatFromTextContent(); TestClosing((pCamera.mOrtho ? "xmag" : "xfov")); - } - else if (IsElement("yfov") || IsElement("ymag")) { + } else if (IsElement("yfov") || IsElement("ymag")) { pCamera.mVerFov = ReadFloatFromTextContent(); TestClosing((pCamera.mOrtho ? "ymag" : "yfov")); - } - else if (IsElement("aspect_ratio")) { + } else if (IsElement("aspect_ratio")) { pCamera.mAspect = ReadFloatFromTextContent(); TestClosing("aspect_ratio"); - } - else if (IsElement("znear")) { + } else if (IsElement("znear")) { pCamera.mZNear = ReadFloatFromTextContent(); TestClosing("znear"); - } - else if (IsElement("zfar")) { + } else if (IsElement("zfar")) { pCamera.mZFar = ReadFloatFromTextContent(); TestClosing("zfar"); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "camera") == 0) break; } @@ -1573,17 +1335,14 @@ void ColladaParser::ReadCamera(Collada::Camera& pCamera) // ------------------------------------------------------------------------------------------------ // Reads the effect library -void ColladaParser::ReadEffectLibrary() -{ +void ColladaParser::ReadEffectLibrary() { if (mReader->isEmptyElement()) { return; } - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("effect")) - { + if (IsElement("effect")) { // read ID. Do I have to repeat my ranting about "optional" attributes? int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); @@ -1592,14 +1351,11 @@ void ColladaParser::ReadEffectLibrary() mEffectLibrary[id] = Effect(); // read on from there ReadEffect(mEffectLibrary[id]); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_effects") != 0) ThrowException("Expected end of element."); @@ -1610,20 +1366,15 @@ void ColladaParser::ReadEffectLibrary() // ------------------------------------------------------------------------------------------------ // Reads an effect entry into the given effect -void ColladaParser::ReadEffect(Collada::Effect& pEffect) -{ +void ColladaParser::ReadEffect(Collada::Effect &pEffect) { // for the moment we don't support any other type of effect. - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("profile_COMMON")) ReadEffectProfileCommon(pEffect); else SkipElement(); - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "effect") != 0) ThrowException("Expected end of element."); @@ -1634,26 +1385,20 @@ void ColladaParser::ReadEffect(Collada::Effect& pEffect) // ------------------------------------------------------------------------------------------------ // Reads an COMMON effect profile -void ColladaParser::ReadEffectProfileCommon(Collada::Effect& pEffect) -{ - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { +void ColladaParser::ReadEffectProfileCommon(Collada::Effect &pEffect) { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { if (IsElement("newparam")) { // save ID int attrSID = GetAttribute("sid"); std::string sid = mReader->getAttributeValue(attrSID); pEffect.mParams[sid] = EffectParam(); ReadEffectParam(pEffect.mParams[sid]); - } - else if (IsElement("technique") || IsElement("extra")) - { + } else if (IsElement("technique") || IsElement("extra")) { // just syntactic sugar } - else if (mFormat == FV_1_4_n && IsElement("image")) - { + else if (mFormat == FV_1_4_n && IsElement("image")) { // read ID. Another entry which is "optional" by design but obligatory in reality int attrID = GetAttribute("id"); std::string id = mReader->getAttributeValue(attrID); @@ -1686,11 +1431,10 @@ void ColladaParser::ReadEffectProfileCommon(Collada::Effect& pEffect) ReadEffectColor(pEffect.mSpecular, pEffect.mTexSpecular); else if (IsElement("reflective")) { ReadEffectColor(pEffect.mReflective, pEffect.mTexReflective); - } - else if (IsElement("transparent")) { + } else if (IsElement("transparent")) { pEffect.mHasTransparency = true; - const char* opaque = mReader->getAttributeValueSafe("opaque"); + const char *opaque = mReader->getAttributeValueSafe("opaque"); if (::strcmp(opaque, "RGB_ZERO") == 0 || ::strcmp(opaque, "RGB_ONE") == 0) { pEffect.mRGBTransparency = true; @@ -1702,8 +1446,7 @@ void ColladaParser::ReadEffectProfileCommon(Collada::Effect& pEffect) } ReadEffectColor(pEffect.mTransparent, pEffect.mTexTransparent); - } - else if (IsElement("shininess")) + } else if (IsElement("shininess")) ReadEffectFloat(pEffect.mShininess); else if (IsElement("reflectivity")) ReadEffectFloat(pEffect.mReflectivity); @@ -1731,20 +1474,15 @@ void ColladaParser::ReadEffectProfileCommon(Collada::Effect& pEffect) else if (IsElement("wireframe")) { pEffect.mWireframe = ReadBoolFromTextContent(); TestClosing("wireframe"); - } - else if (IsElement("faceted")) { + } else if (IsElement("faceted")) { pEffect.mFaceted = ReadBoolFromTextContent(); TestClosing("faceted"); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { - if (strcmp(mReader->getNodeName(), "profile_COMMON") == 0) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + if (strcmp(mReader->getNodeName(), "profile_COMMON") == 0) { break; } } @@ -1753,14 +1491,12 @@ void ColladaParser::ReadEffectProfileCommon(Collada::Effect& pEffect) // ------------------------------------------------------------------------------------------------ // Read texture wrapping + UV transform settings from a profile==Maya chunk -void ColladaParser::ReadSamplerProperties(Sampler& out) -{ +void ColladaParser::ReadSamplerProperties(Sampler &out) { if (mReader->isEmptyElement()) { return; } - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // MAYA extensions @@ -1768,42 +1504,33 @@ void ColladaParser::ReadSamplerProperties(Sampler& out) if (IsElement("wrapU")) { out.mWrapU = ReadBoolFromTextContent(); TestClosing("wrapU"); - } - else if (IsElement("wrapV")) { + } else if (IsElement("wrapV")) { out.mWrapV = ReadBoolFromTextContent(); TestClosing("wrapV"); - } - else if (IsElement("mirrorU")) { + } else if (IsElement("mirrorU")) { out.mMirrorU = ReadBoolFromTextContent(); TestClosing("mirrorU"); - } - else if (IsElement("mirrorV")) { + } else if (IsElement("mirrorV")) { out.mMirrorV = ReadBoolFromTextContent(); TestClosing("mirrorV"); - } - else if (IsElement("repeatU")) { + } else if (IsElement("repeatU")) { out.mTransform.mScaling.x = ReadFloatFromTextContent(); TestClosing("repeatU"); - } - else if (IsElement("repeatV")) { + } else if (IsElement("repeatV")) { out.mTransform.mScaling.y = ReadFloatFromTextContent(); TestClosing("repeatV"); - } - else if (IsElement("offsetU")) { + } else if (IsElement("offsetU")) { out.mTransform.mTranslation.x = ReadFloatFromTextContent(); TestClosing("offsetU"); - } - else if (IsElement("offsetV")) { + } else if (IsElement("offsetV")) { out.mTransform.mTranslation.y = ReadFloatFromTextContent(); TestClosing("offsetV"); - } - else if (IsElement("rotateUV")) { + } else if (IsElement("rotateUV")) { out.mTransform.mRotation = ReadFloatFromTextContent(); TestClosing("rotateUV"); - } - else if (IsElement("blend_mode")) { + } else if (IsElement("blend_mode")) { - const char* sz = GetTextContent(); + const char *sz = GetTextContent(); // http://www.feelingsoftware.com/content/view/55/72/lang,en/ // NONE, OVER, IN, OUT, ADD, SUBTRACT, MULTIPLY, DIFFERENCE, LIGHTEN, DARKEN, SATURATE, DESATURATE and ILLUMINATE if (0 == ASSIMP_strincmp(sz, "ADD", 3)) @@ -1825,8 +1552,7 @@ void ColladaParser::ReadSamplerProperties(Sampler& out) else if (IsElement("weighting")) { out.mWeighting = ReadFloatFromTextContent(); TestClosing("weighting"); - } - else if (IsElement("mix_with_previous_layer")) { + } else if (IsElement("mix_with_previous_layer")) { out.mMixWithPrevious = ReadFloatFromTextContent(); TestClosing("mix_with_previous_layer"); } @@ -1836,8 +1562,7 @@ void ColladaParser::ReadSamplerProperties(Sampler& out) out.mWeighting = ReadFloatFromTextContent(); TestClosing("amount"); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "technique") == 0) break; } @@ -1846,37 +1571,32 @@ void ColladaParser::ReadSamplerProperties(Sampler& out) // ------------------------------------------------------------------------------------------------ // Reads an effect entry containing a color or a texture defining that color -void ColladaParser::ReadEffectColor(aiColor4D& pColor, Sampler& pSampler) -{ +void ColladaParser::ReadEffectColor(aiColor4D &pColor, Sampler &pSampler) { if (mReader->isEmptyElement()) return; // Save current element name const std::string curElem = mReader->getNodeName(); - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("color")) - { + if (IsElement("color")) { // text content contains 4 floats - const char* content = GetTextContent(); + const char *content = GetTextContent(); - content = fast_atoreal_move(content, (ai_real&)pColor.r); + content = fast_atoreal_move(content, (ai_real &)pColor.r); SkipSpacesAndLineEnd(&content); - content = fast_atoreal_move(content, (ai_real&)pColor.g); + content = fast_atoreal_move(content, (ai_real &)pColor.g); SkipSpacesAndLineEnd(&content); - content = fast_atoreal_move(content, (ai_real&)pColor.b); + content = fast_atoreal_move(content, (ai_real &)pColor.b); SkipSpacesAndLineEnd(&content); - content = fast_atoreal_move(content, (ai_real&)pColor.a); + content = fast_atoreal_move(content, (ai_real &)pColor.a); SkipSpacesAndLineEnd(&content); TestClosing("color"); - } - else if (IsElement("texture")) - { + } else if (IsElement("texture")) { // get name of source texture/sampler int attrTex = GetAttribute("texture"); pSampler.mName = mReader->getAttributeValue(attrTex); @@ -1890,28 +1610,22 @@ void ColladaParser::ReadEffectColor(aiColor4D& pColor, Sampler& pSampler) // as we've read texture, the color needs to be 1,1,1,1 pColor = aiColor4D(1.f, 1.f, 1.f, 1.f); - } - else if (IsElement("technique")) - { + } else if (IsElement("technique")) { const int _profile = GetAttribute("profile"); - const char* profile = mReader->getAttributeValue(_profile); + const char *profile = mReader->getAttributeValue(_profile); // Some extensions are quite useful ... ReadSamplerProperties processes // several extensions in MAYA, OKINO and MAX3D profiles. - if (!::strcmp(profile, "MAYA") || !::strcmp(profile, "MAX3D") || !::strcmp(profile, "OKINO")) - { + if (!::strcmp(profile, "MAYA") || !::strcmp(profile, "MAX3D") || !::strcmp(profile, "OKINO")) { // get more information on this sampler ReadSamplerProperties(pSampler); - } - else SkipElement(); - } - else if (!IsElement("extra")) - { + } else + SkipElement(); + } else if (!IsElement("extra")) { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (mReader->getNodeName() == curElem) break; } @@ -1920,27 +1634,21 @@ void ColladaParser::ReadEffectColor(aiColor4D& pColor, Sampler& pSampler) // ------------------------------------------------------------------------------------------------ // Reads an effect entry containing a float -void ColladaParser::ReadEffectFloat(ai_real& pFloat) -{ - while (mReader->read()) - { +void ColladaParser::ReadEffectFloat(ai_real &pFloat) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("float")) - { + if (IsElement("float")) { // text content contains a single floats - const char* content = GetTextContent(); + const char *content = GetTextContent(); content = fast_atoreal_move(content, pFloat); SkipSpacesAndLineEnd(&content); TestClosing("float"); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { break; } } @@ -1948,55 +1656,45 @@ void ColladaParser::ReadEffectFloat(ai_real& pFloat) // ------------------------------------------------------------------------------------------------ // Reads an effect parameter specification of any kind -void ColladaParser::ReadEffectParam(Collada::EffectParam& pParam) -{ - while (mReader->read()) - { +void ColladaParser::ReadEffectParam(Collada::EffectParam &pParam) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("surface")) - { + if (IsElement("surface")) { // image ID given inside tags TestOpening("init_from"); - const char* content = GetTextContent(); + const char *content = GetTextContent(); pParam.mType = Param_Surface; pParam.mReference = content; TestClosing("init_from"); // don't care for remaining stuff SkipElement("surface"); - } - else if (IsElement("sampler2D") && (FV_1_4_n == mFormat || FV_1_3_n == mFormat)) - { + } else if (IsElement("sampler2D") && (FV_1_4_n == mFormat || FV_1_3_n == mFormat)) { // surface ID is given inside tags TestOpening("source"); - const char* content = GetTextContent(); + const char *content = GetTextContent(); pParam.mType = Param_Sampler; pParam.mReference = content; TestClosing("source"); // don't care for remaining stuff SkipElement("sampler2D"); - } - else if (IsElement("sampler2D")) - { + } else if (IsElement("sampler2D")) { // surface ID is given inside tags TestOpening("instance_image"); int attrURL = GetAttribute("url"); - const char* url = mReader->getAttributeValue(attrURL); + const char *url = mReader->getAttributeValue(attrURL); if (url[0] != '#') ThrowException("Unsupported URL format in instance_image"); url++; pParam.mType = Param_Sampler; pParam.mReference = url; SkipElement("sampler2D"); - } - else - { + } else { // ignore unknown element SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { break; } } @@ -2004,17 +1702,13 @@ void ColladaParser::ReadEffectParam(Collada::EffectParam& pParam) // ------------------------------------------------------------------------------------------------ // Reads the geometry library contents -void ColladaParser::ReadGeometryLibrary() -{ +void ColladaParser::ReadGeometryLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("geometry")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("geometry")) { // read ID. Another entry which is "optional" by design but obligatory in reality int indexID = GetAttribute("id"); std::string id = mReader->getAttributeValue(indexID); @@ -2022,28 +1716,30 @@ void ColladaParser::ReadGeometryLibrary() // TODO: (thom) support SIDs // ai_assert( TestAttribute( "sid") == -1); - // create a mesh and store it in the library under its ID - Mesh* mesh = new Mesh; - mMeshLibrary[id] = mesh; + // create a mesh and store it in the library under its (resolved) ID + // Skip and warn if ID is not unique + if (mMeshLibrary.find(id) == mMeshLibrary.cend()) { + std::unique_ptr mesh(new Mesh(id)); - // read the mesh name if it exists - const int nameIndex = TestAttribute("name"); - if (nameIndex != -1) - { - mesh->mName = mReader->getAttributeValue(nameIndex); + // read the mesh name if it exists + const int nameIndex = TestAttribute("name"); + if (nameIndex != -1) { + mesh->mName = mReader->getAttributeValue(nameIndex); + } + + // read on from there + ReadGeometry(*mesh); + // Read successfully, add to library + mMeshLibrary.insert({ id, mesh.release() }); + } else { + ASSIMP_LOG_ERROR_F("Collada: Skipped duplicate geometry id: \"", id, "\""); + SkipElement(); } - - // read on from there - ReadGeometry(mesh); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_geometries") != 0) ThrowException("Expected end of element."); @@ -2054,28 +1750,20 @@ void ColladaParser::ReadGeometryLibrary() // ------------------------------------------------------------------------------------------------ // Reads a geometry from the geometry library. -void ColladaParser::ReadGeometry(Collada::Mesh* pMesh) -{ +void ColladaParser::ReadGeometry(Collada::Mesh &pMesh) { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("mesh")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("mesh")) { // read on from there ReadMesh(pMesh); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "geometry") != 0) ThrowException("Expected end of element."); @@ -2086,50 +1774,32 @@ void ColladaParser::ReadGeometry(Collada::Mesh* pMesh) // ------------------------------------------------------------------------------------------------ // Reads a mesh from the geometry library -void ColladaParser::ReadMesh(Mesh* pMesh) -{ +void ColladaParser::ReadMesh(Mesh &pMesh) { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("source")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("source")) { // we have professionals dealing with this ReadSource(); - } - else if (IsElement("vertices")) - { + } else if (IsElement("vertices")) { // read per-vertex mesh data ReadVertexData(pMesh); - } - else if (IsElement("triangles") || IsElement("lines") || IsElement("linestrips") - || IsElement("polygons") || IsElement("polylist") || IsElement("trifans") || IsElement("tristrips")) - { + } else if (IsElement("triangles") || IsElement("lines") || IsElement("linestrips") || IsElement("polygons") || IsElement("polylist") || IsElement("trifans") || IsElement("tristrips")) { // read per-index mesh data and faces setup ReadIndexData(pMesh); - } - else - { + } else { // ignore the restf SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { - if (strcmp(mReader->getNodeName(), "technique_common") == 0) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + if (strcmp(mReader->getNodeName(), "technique_common") == 0) { // end of another meaningless element - read over it - } - else if (strcmp(mReader->getNodeName(), "mesh") == 0) - { + } else if (strcmp(mReader->getNodeName(), "mesh") == 0) { // end of element - we're done here break; - } - else - { + } else { // everything else should be punished ThrowException("Expected end of element."); } @@ -2139,46 +1809,29 @@ void ColladaParser::ReadMesh(Mesh* pMesh) // ------------------------------------------------------------------------------------------------ // Reads a source element -void ColladaParser::ReadSource() -{ +void ColladaParser::ReadSource() { int indexID = GetAttribute("id"); std::string sourceID = mReader->getAttributeValue(indexID); - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("float_array") || IsElement("IDREF_array") || IsElement("Name_array")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("float_array") || IsElement("IDREF_array") || IsElement("Name_array")) { ReadDataArray(); - } - else if (IsElement("technique_common")) - { + } else if (IsElement("technique_common")) { // I don't care for your profiles - } - else if (IsElement("accessor")) - { + } else if (IsElement("accessor")) { ReadAccessor(sourceID); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { - if (strcmp(mReader->getNodeName(), "source") == 0) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + if (strcmp(mReader->getNodeName(), "source") == 0) { // end of - we're done break; - } - else if (strcmp(mReader->getNodeName(), "technique_common") == 0) - { + } else if (strcmp(mReader->getNodeName(), "technique_common") == 0) { // end of another meaningless element - read over it - } - else - { + } else { // everything else should be punished ThrowException("Expected end of element."); } @@ -2188,8 +1841,7 @@ void ColladaParser::ReadSource() // ------------------------------------------------------------------------------------------------ // Reads a data array holding a number of floats, and stores it in the global library -void ColladaParser::ReadDataArray() -{ +void ColladaParser::ReadDataArray() { std::string elmName = mReader->getNodeName(); bool isStringArray = (elmName == "IDREF_array" || elmName == "Name_array"); bool isEmptyElement = mReader->isEmptyElement(); @@ -2199,23 +1851,20 @@ void ColladaParser::ReadDataArray() std::string id = mReader->getAttributeValue(indexID); int indexCount = GetAttribute("count"); unsigned int count = (unsigned int)mReader->getAttributeValueAsInt(indexCount); - const char* content = TestTextContent(); + const char *content = TestTextContent(); // read values and store inside an array in the data library mDataLibrary[id] = Data(); - Data& data = mDataLibrary[id]; + Data &data = mDataLibrary[id]; data.mIsStringArray = isStringArray; // some exporters write empty data arrays, but we need to conserve them anyways because others might reference them - if (content) - { - if (isStringArray) - { + if (content) { + if (isStringArray) { data.mStrings.reserve(count); std::string s; - for (unsigned int a = 0; a < count; a++) - { + for (unsigned int a = 0; a < count; a++) { if (*content == 0) ThrowException("Expected more values while reading IDREF_array contents."); @@ -2226,13 +1875,10 @@ void ColladaParser::ReadDataArray() SkipSpacesAndLineEnd(&content); } - } - else - { + } else { data.mValues.reserve(count); - for (unsigned int a = 0; a < count; a++) - { + for (unsigned int a = 0; a < count; a++) { if (*content == 0) ThrowException("Expected more values while reading float_array contents."); @@ -2253,11 +1899,10 @@ void ColladaParser::ReadDataArray() // ------------------------------------------------------------------------------------------------ // Reads an accessor and stores it in the global library -void ColladaParser::ReadAccessor(const std::string& pID) -{ +void ColladaParser::ReadAccessor(const std::string &pID) { // read accessor attributes int attrSource = GetAttribute("source"); - const char* source = mReader->getAttributeValue(attrSource); + const char *source = mReader->getAttributeValue(attrSource); if (source[0] != '#') ThrowException(format() << "Unknown reference format in url \"" << source << "\" in source attribute of element."); int attrCount = GetAttribute("count"); @@ -2273,7 +1918,7 @@ void ColladaParser::ReadAccessor(const std::string& pID) // store in the library under the given ID mAccessorLibrary[pID] = Accessor(); - Accessor& acc = mAccessorLibrary[pID]; + Accessor &acc = mAccessorLibrary[pID]; acc.mCount = count; acc.mOffset = offset; acc.mStride = stride; @@ -2281,50 +1926,57 @@ void ColladaParser::ReadAccessor(const std::string& pID) acc.mSize = 0; // gets incremented with every param // and read the components - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("param")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("param")) { // read data param int attrName = TestAttribute("name"); std::string name; - if (attrName > -1) - { + if (attrName > -1) { name = mReader->getAttributeValue(attrName); // analyse for common type components and store it's sub-offset in the corresponding field /* Cartesian coordinates */ - if (name == "X") acc.mSubOffset[0] = acc.mParams.size(); - else if (name == "Y") acc.mSubOffset[1] = acc.mParams.size(); - else if (name == "Z") acc.mSubOffset[2] = acc.mParams.size(); + if (name == "X") + acc.mSubOffset[0] = acc.mParams.size(); + else if (name == "Y") + acc.mSubOffset[1] = acc.mParams.size(); + else if (name == "Z") + acc.mSubOffset[2] = acc.mParams.size(); /* RGBA colors */ - else if (name == "R") acc.mSubOffset[0] = acc.mParams.size(); - else if (name == "G") acc.mSubOffset[1] = acc.mParams.size(); - else if (name == "B") acc.mSubOffset[2] = acc.mParams.size(); - else if (name == "A") acc.mSubOffset[3] = acc.mParams.size(); + else if (name == "R") + acc.mSubOffset[0] = acc.mParams.size(); + else if (name == "G") + acc.mSubOffset[1] = acc.mParams.size(); + else if (name == "B") + acc.mSubOffset[2] = acc.mParams.size(); + else if (name == "A") + acc.mSubOffset[3] = acc.mParams.size(); /* UVWQ (STPQ) texture coordinates */ - else if (name == "S") acc.mSubOffset[0] = acc.mParams.size(); - else if (name == "T") acc.mSubOffset[1] = acc.mParams.size(); - else if (name == "P") acc.mSubOffset[2] = acc.mParams.size(); + else if (name == "S") + acc.mSubOffset[0] = acc.mParams.size(); + else if (name == "T") + acc.mSubOffset[1] = acc.mParams.size(); + else if (name == "P") + acc.mSubOffset[2] = acc.mParams.size(); // else if( name == "Q") acc.mSubOffset[3] = acc.mParams.size(); - /* 4D uv coordinates are not supported in Assimp */ + /* 4D uv coordinates are not supported in Assimp */ - /* Generic extra data, interpreted as UV data, too*/ - else if (name == "U") acc.mSubOffset[0] = acc.mParams.size(); - else if (name == "V") acc.mSubOffset[1] = acc.mParams.size(); + /* Generic extra data, interpreted as UV data, too*/ + else if (name == "U") + acc.mSubOffset[0] = acc.mParams.size(); + else if (name == "V") + acc.mSubOffset[1] = acc.mParams.size(); //else // DefaultLogger::get()->warn( format() << "Unknown accessor parameter \"" << name << "\". Ignoring data channel." ); } // read data type int attrType = TestAttribute("type"); - if (attrType > -1) - { + if (attrType > -1) { // for the moment we only distinguish between a 4x4 matrix and anything else. // TODO: (thom) I don't have a spec here at work. Check if there are other multi-value types // which should be tested for here. @@ -2339,14 +1991,10 @@ void ColladaParser::ReadAccessor(const std::string& pID) // skip remaining stuff of this element, if any SkipElement(); - } - else - { + } else { ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag "); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "accessor") != 0) ThrowException("Expected end of element."); break; @@ -2356,28 +2004,20 @@ void ColladaParser::ReadAccessor(const std::string& pID) // ------------------------------------------------------------------------------------------------ // Reads input declarations of per-vertex mesh data into the given mesh -void ColladaParser::ReadVertexData(Mesh* pMesh) -{ +void ColladaParser::ReadVertexData(Mesh &pMesh) { // extract the ID of the element. Not that we care, but to catch strange referencing schemes we should warn about int attrID = GetAttribute("id"); - pMesh->mVertexID = mReader->getAttributeValue(attrID); + pMesh.mVertexID = mReader->getAttributeValue(attrID); // a number of elements - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("input")) - { - ReadInputChannel(pMesh->mPerVertexData); - } - else - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("input")) { + ReadInputChannel(pMesh.mPerVertexData); + } else { ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag "); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "vertices") != 0) ThrowException("Expected end of element."); @@ -2388,8 +2028,7 @@ void ColladaParser::ReadVertexData(Mesh* pMesh) // ------------------------------------------------------------------------------------------------ // Reads input declarations of per-index mesh data into the given mesh -void ColladaParser::ReadIndexData(Mesh* pMesh) -{ +void ColladaParser::ReadIndexData(Mesh &pMesh) { std::vector vcount; std::vector perIndexData; @@ -2427,25 +2066,18 @@ void ColladaParser::ReadIndexData(Mesh* pMesh) ai_assert(primType != Prim_Invalid); // also a number of elements, but in addition a

primitive collection and probably index counts for all primitives - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("input")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("input")) { ReadInputChannel(perIndexData); - } - else if (IsElement("vcount")) - { - if (!mReader->isEmptyElement()) - { - if (numPrimitives) // It is possible to define a mesh without any primitives + } else if (IsElement("vcount")) { + if (!mReader->isEmptyElement()) { + if (numPrimitives) // It is possible to define a mesh without any primitives { // case - specifies the number of indices for each polygon - const char* content = GetTextContent(); + const char *content = GetTextContent(); vcount.reserve(numPrimitives); - for (unsigned int a = 0; a < numPrimitives; a++) - { + for (unsigned int a = 0; a < numPrimitives; a++) { if (*content == 0) ThrowException("Expected more values while reading contents."); // read a number @@ -2457,28 +2089,19 @@ void ColladaParser::ReadIndexData(Mesh* pMesh) TestClosing("vcount"); } - } - else if (IsElement("p")) - { - if (!mReader->isEmptyElement()) - { + } else if (IsElement("p")) { + if (!mReader->isEmptyElement()) { // now here the actual fun starts - these are the indices to construct the mesh data from actualPrimitives += ReadPrimitives(pMesh, perIndexData, numPrimitives, vcount, primType); } - } - else if (IsElement("extra")) - { + } else if (IsElement("extra")) { SkipElement("extra"); - } - else if (IsElement("ph")) { + } else if (IsElement("ph")) { SkipElement("ph"); - } - else { + } else { ThrowException(format() << "Unexpected sub element <" << mReader->getNodeName() << "> in tag <" << elementName << ">"); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (mReader->getNodeName() != elementName) ThrowException(format() << "Expected end of <" << elementName << "> element."); @@ -2488,20 +2111,19 @@ void ColladaParser::ReadIndexData(Mesh* pMesh) #ifdef ASSIMP_BUILD_DEBUG if (primType != Prim_TriFans && primType != Prim_TriStrips && primType != Prim_LineStrip && - primType != Prim_Lines) { // this is ONLY to workaround a bug in SketchUp 15.3.331 where it writes the wrong 'count' when it writes out the 'lines'. + primType != Prim_Lines) { // this is ONLY to workaround a bug in SketchUp 15.3.331 where it writes the wrong 'count' when it writes out the 'lines'. ai_assert(actualPrimitives == numPrimitives); } #endif // only when we're done reading all

tags (and thus know the final vertex count) can we commit the submesh subgroup.mNumFaces = actualPrimitives; - pMesh->mSubMeshes.push_back(subgroup); + pMesh.mSubMeshes.push_back(subgroup); } // ------------------------------------------------------------------------------------------------ // Reads a single input channel element and stores it in the given array, if valid -void ColladaParser::ReadInputChannel(std::vector& poChannels) -{ +void ColladaParser::ReadInputChannel(std::vector &poChannels) { InputChannel channel; // read semantic @@ -2511,7 +2133,7 @@ void ColladaParser::ReadInputChannel(std::vector& poChannels) // read source int attrSource = GetAttribute("source"); - const char* source = mReader->getAttributeValue(attrSource); + const char *source = mReader->getAttributeValue(attrSource); if (source[0] != '#') ThrowException(format() << "Unknown reference format in url \"" << source << "\" in source attribute of element."); channel.mAccessor = source + 1; // skipping the leading #, hopefully the remaining text is the accessor ID only @@ -2543,15 +2165,13 @@ void ColladaParser::ReadInputChannel(std::vector& poChannels) // ------------------------------------------------------------------------------------------------ // Reads a

primitive index list and assembles the mesh data into the given mesh -size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPerIndexChannels, - size_t pNumPrimitives, const std::vector& pVCount, PrimitiveType pPrimType) -{ +size_t ColladaParser::ReadPrimitives(Mesh &pMesh, std::vector &pPerIndexChannels, + size_t pNumPrimitives, const std::vector &pVCount, PrimitiveType pPrimType) { // determine number of indices coming per vertex // find the offset index for all per-vertex channels size_t numOffsets = 1; size_t perVertexOffset = SIZE_MAX; // invalid value - for (const InputChannel& channel : pPerIndexChannels) - { + for (const InputChannel &channel : pPerIndexChannels) { numOffsets = std::max(numOffsets, channel.mOffset + 1); if (channel.mType == IT_Vertex) perVertexOffset = channel.mOffset; @@ -2559,10 +2179,8 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe // determine the expected number of indices size_t expectedPointCount = 0; - switch (pPrimType) - { - case Prim_Polylist: - { + switch (pPrimType) { + case Prim_Polylist: { for (size_t i : pVCount) expectedPointCount += i; break; @@ -2585,9 +2203,8 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe if (pNumPrimitives > 0) // It is possible to not contain any indices { - const char* content = GetTextContent(); - while (*content != 0) - { + const char *content = GetTextContent(); + while (*content != 0) { // read a value. // Hack: (thom) Some exporters put negative indices sometimes. We just try to carry on anyways. int value = std::max(0, strtol10(content, &content)); @@ -2603,40 +2220,35 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe // HACK: We just fix this number since SketchUp 15.3.331 writes the wrong 'count' for 'lines' ReportWarning("Expected different index count in

element, %zu instead of %zu.", indices.size(), expectedPointCount * numOffsets); pNumPrimitives = (indices.size() / numOffsets) / 2; - } - else + } else ThrowException("Expected different index count in

element."); - } - else if (expectedPointCount == 0 && (indices.size() % numOffsets) != 0) + } else if (expectedPointCount == 0 && (indices.size() % numOffsets) != 0) ThrowException("Expected different index count in

element."); // find the data for all sources - for (std::vector::iterator it = pMesh->mPerVertexData.begin(); it != pMesh->mPerVertexData.end(); ++it) - { - InputChannel& input = *it; + for (std::vector::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it) { + InputChannel &input = *it; if (input.mResolved) continue; // find accessor input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor); // resolve accessor's data pointer as well, if necessary - const Accessor* acc = input.mResolved; + const Accessor *acc = input.mResolved; if (!acc->mData) acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource); } // and the same for the per-index channels - for (std::vector::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) - { - InputChannel& input = *it; + for (std::vector::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) { + InputChannel &input = *it; if (input.mResolved) continue; // ignore vertex pointer, it doesn't refer to an accessor - if (input.mType == IT_Vertex) - { + if (input.mType == IT_Vertex) { // warn if the vertex channel does not refer to the element in the same mesh - if (input.mAccessor != pMesh->mVertexID) + if (input.mAccessor != pMesh.mVertexID) ThrowException("Unsupported vertex referencing scheme."); continue; } @@ -2644,7 +2256,7 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe // find accessor input.mResolved = &ResolveLibraryReference(mAccessorLibrary, input.mAccessor); // resolve accessor's data pointer as well, if necessary - const Accessor* acc = input.mResolved; + const Accessor *acc = input.mResolved; if (!acc->mData) acc->mData = &ResolveLibraryReference(mDataLibrary, acc->mSource); } @@ -2663,16 +2275,14 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe numPrimitives = numberOfVertices - 1; } - pMesh->mFaceSize.reserve(numPrimitives); - pMesh->mFacePosIndices.reserve(indices.size() / numOffsets); + pMesh.mFaceSize.reserve(numPrimitives); + pMesh.mFacePosIndices.reserve(indices.size() / numOffsets); size_t polylistStartVertex = 0; - for (size_t currentPrimitive = 0; currentPrimitive < numPrimitives; currentPrimitive++) - { + for (size_t currentPrimitive = 0; currentPrimitive < numPrimitives; currentPrimitive++) { // determine number of points for this primitive size_t numPoints = 0; - switch (pPrimType) - { + switch (pPrimType) { case Prim_Lines: numPoints = 2; for (size_t currentVertex = 0; currentVertex < numPoints; currentVertex++) @@ -2711,7 +2321,7 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe } // store the face size to later reconstruct the face from - pMesh->mFaceSize.push_back(numPoints); + pMesh.mFaceSize.push_back(numPoints); } // if I ever get my hands on that guy who invented this steaming pile of indirection... @@ -2722,7 +2332,7 @@ size_t ColladaParser::ReadPrimitives(Mesh* pMesh, std::vector& pPe ///@note This function willn't work correctly if both PerIndex and PerVertex channels have same channels. ///For example if TEXCOORD present in both and tags this function will create wrong uv coordinates. ///It's not clear from COLLADA documentation is this allowed or not. For now only exporter fixed to avoid such behavior -void ColladaParser::CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, Mesh* pMesh, std::vector& pPerIndexChannels, size_t currentPrimitive, const std::vector& indices) { +void ColladaParser::CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, Mesh &pMesh, std::vector &pPerIndexChannels, size_t currentPrimitive, const std::vector &indices) { // calculate the base offset of the vertex whose attributes we ant to copy size_t baseOffset = currentPrimitive * numOffsets * numPoints + currentVertex * numOffsets; @@ -2730,24 +2340,23 @@ void ColladaParser::CopyVertex(size_t currentVertex, size_t numOffsets, size_t n ai_assert((baseOffset + numOffsets - 1) < indices.size()); // extract per-vertex channels using the global per-vertex offset - for (std::vector::iterator it = pMesh->mPerVertexData.begin(); it != pMesh->mPerVertexData.end(); ++it) + for (std::vector::iterator it = pMesh.mPerVertexData.begin(); it != pMesh.mPerVertexData.end(); ++it) ExtractDataObjectFromChannel(*it, indices[baseOffset + perVertexOffset], pMesh); // and extract per-index channels using there specified offset for (std::vector::iterator it = pPerIndexChannels.begin(); it != pPerIndexChannels.end(); ++it) ExtractDataObjectFromChannel(*it, indices[baseOffset + it->mOffset], pMesh); // store the vertex-data index for later assignment of bone vertex weights - pMesh->mFacePosIndices.push_back(indices[baseOffset + perVertexOffset]); + pMesh.mFacePosIndices.push_back(indices[baseOffset + perVertexOffset]); } -void ColladaParser::ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Mesh* pMesh, std::vector& pPerIndexChannels, size_t currentPrimitive, const std::vector& indices) { +void ColladaParser::ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Mesh &pMesh, std::vector &pPerIndexChannels, size_t currentPrimitive, const std::vector &indices) { if (currentPrimitive % 2 != 0) { //odd tristrip triangles need their indices mangled, to preserve winding direction CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); - } - else {//for non tristrips or even tristrip triangles + } else { //for non tristrips or even tristrip triangles CopyVertex(0, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); CopyVertex(1, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); CopyVertex(2, numOffsets, 1, perVertexOffset, pMesh, pPerIndexChannels, currentPrimitive, indices); @@ -2756,18 +2365,17 @@ void ColladaParser::ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, // ------------------------------------------------------------------------------------------------ // Extracts a single object from an input channel and stores it in the appropriate mesh data array -void ColladaParser::ExtractDataObjectFromChannel(const InputChannel& pInput, size_t pLocalIndex, Mesh* pMesh) -{ +void ColladaParser::ExtractDataObjectFromChannel(const InputChannel &pInput, size_t pLocalIndex, Mesh &pMesh) { // ignore vertex referrer - we handle them that separate if (pInput.mType == IT_Vertex) return; - const Accessor& acc = *pInput.mResolved; + const Accessor &acc = *pInput.mResolved; if (pLocalIndex >= acc.mCount) ThrowException(format() << "Invalid data index (" << pLocalIndex << "/" << acc.mCount << ") in primitive specification"); // get a pointer to the start of the data object referred to by the accessor and the local index - const ai_real* dataObject = &(acc.mData->mValues[0]) + acc.mOffset + pLocalIndex * acc.mStride; + const ai_real *dataObject = &(acc.mData->mValues[0]) + acc.mOffset + pLocalIndex * acc.mStride; // assemble according to the accessors component sub-offset list. We don't care, yet, // what kind of object exactly we're extracting here @@ -2776,83 +2384,75 @@ void ColladaParser::ExtractDataObjectFromChannel(const InputChannel& pInput, siz obj[c] = dataObject[acc.mSubOffset[c]]; // now we reinterpret it according to the type we're reading here - switch (pInput.mType) - { + switch (pInput.mType) { case IT_Position: // ignore all position streams except 0 - there can be only one position if (pInput.mIndex == 0) - pMesh->mPositions.push_back(aiVector3D(obj[0], obj[1], obj[2])); + pMesh.mPositions.push_back(aiVector3D(obj[0], obj[1], obj[2])); else ASSIMP_LOG_ERROR("Collada: just one vertex position stream supported"); break; case IT_Normal: // pad to current vertex count if necessary - if (pMesh->mNormals.size() < pMesh->mPositions.size() - 1) - pMesh->mNormals.insert(pMesh->mNormals.end(), pMesh->mPositions.size() - pMesh->mNormals.size() - 1, aiVector3D(0, 1, 0)); + if (pMesh.mNormals.size() < pMesh.mPositions.size() - 1) + pMesh.mNormals.insert(pMesh.mNormals.end(), pMesh.mPositions.size() - pMesh.mNormals.size() - 1, aiVector3D(0, 1, 0)); // ignore all normal streams except 0 - there can be only one normal if (pInput.mIndex == 0) - pMesh->mNormals.push_back(aiVector3D(obj[0], obj[1], obj[2])); + pMesh.mNormals.push_back(aiVector3D(obj[0], obj[1], obj[2])); else ASSIMP_LOG_ERROR("Collada: just one vertex normal stream supported"); break; case IT_Tangent: // pad to current vertex count if necessary - if (pMesh->mTangents.size() < pMesh->mPositions.size() - 1) - pMesh->mTangents.insert(pMesh->mTangents.end(), pMesh->mPositions.size() - pMesh->mTangents.size() - 1, aiVector3D(1, 0, 0)); + if (pMesh.mTangents.size() < pMesh.mPositions.size() - 1) + pMesh.mTangents.insert(pMesh.mTangents.end(), pMesh.mPositions.size() - pMesh.mTangents.size() - 1, aiVector3D(1, 0, 0)); // ignore all tangent streams except 0 - there can be only one tangent if (pInput.mIndex == 0) - pMesh->mTangents.push_back(aiVector3D(obj[0], obj[1], obj[2])); + pMesh.mTangents.push_back(aiVector3D(obj[0], obj[1], obj[2])); else ASSIMP_LOG_ERROR("Collada: just one vertex tangent stream supported"); break; case IT_Bitangent: // pad to current vertex count if necessary - if (pMesh->mBitangents.size() < pMesh->mPositions.size() - 1) - pMesh->mBitangents.insert(pMesh->mBitangents.end(), pMesh->mPositions.size() - pMesh->mBitangents.size() - 1, aiVector3D(0, 0, 1)); + if (pMesh.mBitangents.size() < pMesh.mPositions.size() - 1) + pMesh.mBitangents.insert(pMesh.mBitangents.end(), pMesh.mPositions.size() - pMesh.mBitangents.size() - 1, aiVector3D(0, 0, 1)); // ignore all bitangent streams except 0 - there can be only one bitangent if (pInput.mIndex == 0) - pMesh->mBitangents.push_back(aiVector3D(obj[0], obj[1], obj[2])); + pMesh.mBitangents.push_back(aiVector3D(obj[0], obj[1], obj[2])); else ASSIMP_LOG_ERROR("Collada: just one vertex bitangent stream supported"); break; case IT_Texcoord: // up to 4 texture coord sets are fine, ignore the others - if (pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS) - { + if (pInput.mIndex < AI_MAX_NUMBER_OF_TEXTURECOORDS) { // pad to current vertex count if necessary - if (pMesh->mTexCoords[pInput.mIndex].size() < pMesh->mPositions.size() - 1) - pMesh->mTexCoords[pInput.mIndex].insert(pMesh->mTexCoords[pInput.mIndex].end(), - pMesh->mPositions.size() - pMesh->mTexCoords[pInput.mIndex].size() - 1, aiVector3D(0, 0, 0)); + if (pMesh.mTexCoords[pInput.mIndex].size() < pMesh.mPositions.size() - 1) + pMesh.mTexCoords[pInput.mIndex].insert(pMesh.mTexCoords[pInput.mIndex].end(), + pMesh.mPositions.size() - pMesh.mTexCoords[pInput.mIndex].size() - 1, aiVector3D(0, 0, 0)); - pMesh->mTexCoords[pInput.mIndex].push_back(aiVector3D(obj[0], obj[1], obj[2])); + pMesh.mTexCoords[pInput.mIndex].push_back(aiVector3D(obj[0], obj[1], obj[2])); if (0 != acc.mSubOffset[2] || 0 != acc.mSubOffset[3]) /* hack ... consider cleaner solution */ - pMesh->mNumUVComponents[pInput.mIndex] = 3; - } - else - { + pMesh.mNumUVComponents[pInput.mIndex] = 3; + } else { ASSIMP_LOG_ERROR("Collada: too many texture coordinate sets. Skipping."); } break; case IT_Color: // up to 4 color sets are fine, ignore the others - if (pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS) - { + if (pInput.mIndex < AI_MAX_NUMBER_OF_COLOR_SETS) { // pad to current vertex count if necessary - if (pMesh->mColors[pInput.mIndex].size() < pMesh->mPositions.size() - 1) - pMesh->mColors[pInput.mIndex].insert(pMesh->mColors[pInput.mIndex].end(), - pMesh->mPositions.size() - pMesh->mColors[pInput.mIndex].size() - 1, aiColor4D(0, 0, 0, 1)); + if (pMesh.mColors[pInput.mIndex].size() < pMesh.mPositions.size() - 1) + pMesh.mColors[pInput.mIndex].insert(pMesh.mColors[pInput.mIndex].end(), + pMesh.mPositions.size() - pMesh.mColors[pInput.mIndex].size() - 1, aiColor4D(0, 0, 0, 1)); aiColor4D result(0, 0, 0, 1); - for (size_t i = 0; i < pInput.mResolved->mSize; ++i) - { + for (size_t i = 0; i < pInput.mResolved->mSize; ++i) { result[static_cast(i)] = obj[pInput.mResolved->mSubOffset[i]]; } - pMesh->mColors[pInput.mIndex].push_back(result); - } - else - { + pMesh.mColors[pInput.mIndex].push_back(result); + } else { ASSIMP_LOG_ERROR("Collada: too many vertex color sets. Skipping."); } @@ -2865,44 +2465,36 @@ void ColladaParser::ExtractDataObjectFromChannel(const InputChannel& pInput, siz // ------------------------------------------------------------------------------------------------ // Reads the library of node hierarchies and scene parts -void ColladaParser::ReadSceneLibrary() -{ +void ColladaParser::ReadSceneLibrary() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { // a visual scene - generate root node under its ID and let ReadNode() do the recursive work - if (IsElement("visual_scene")) - { + if (IsElement("visual_scene")) { // read ID. Is optional according to the spec, but how on earth should a scene_instance refer to it then? int indexID = GetAttribute("id"); - const char* attrID = mReader->getAttributeValue(indexID); + const char *attrID = mReader->getAttributeValue(indexID); // read name if given. int indexName = TestAttribute("name"); - const char* attrName = "unnamed"; + const char *attrName = "Scene"; if (indexName > -1) attrName = mReader->getAttributeValue(indexName); // create a node and store it in the library under its ID - Node* node = new Node; + Node *node = new Node; node->mID = attrID; node->mName = attrName; mNodeLibrary[node->mID] = node; ReadSceneNode(node); - } - else - { + } else { // ignore the rest SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "library_visual_scenes") == 0) //ThrowException( "Expected end of \"library_visual_scenes\" element."); @@ -2913,19 +2505,15 @@ void ColladaParser::ReadSceneLibrary() // ------------------------------------------------------------------------------------------------ // Reads a scene node's contents including children and stores it in the given node -void ColladaParser::ReadSceneNode(Node* pNode) -{ +void ColladaParser::ReadSceneNode(Node *pNode) { // quit immediately on elements if (mReader->isEmptyElement()) return; - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("node")) - { - Node* child = new Node; + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("node")) { + Node *child = new Node; int attrID = TestAttribute("id"); if (attrID > -1) child->mID = mReader->getAttributeValue(attrID); @@ -2940,13 +2528,10 @@ void ColladaParser::ReadSceneNode(Node* pNode) // TODO: (thom) support SIDs // ai_assert( TestAttribute( "sid") == -1); - if (pNode) - { + if (pNode) { pNode->mChildren.push_back(child); child->mParent = pNode; - } - else - { + } else { // no parent node given, probably called from element. // create new node in node library mNodeLibrary[child->mID] = child; @@ -2972,82 +2557,65 @@ void ColladaParser::ReadSceneNode(Node* pNode) ReadNodeTransformation(pNode, TF_SKEW); else if (IsElement("translate")) ReadNodeTransformation(pNode, TF_TRANSLATE); - else if (IsElement("render") && pNode->mParent == NULL && 0 == pNode->mPrimaryCamera.length()) - { + else if (IsElement("render") && pNode->mParent == NULL && 0 == pNode->mPrimaryCamera.length()) { // ... scene evaluation or, in other words, postprocessing pipeline, // or, again in other words, a turing-complete description how to // render a Collada scene. The only thing that is interesting for // us is the primary camera. int attrId = TestAttribute("camera_node"); - if (-1 != attrId) - { - const char* s = mReader->getAttributeValue(attrId); + if (-1 != attrId) { + const char *s = mReader->getAttributeValue(attrId); if (s[0] != '#') ASSIMP_LOG_ERROR("Collada: Unresolved reference format of camera"); else pNode->mPrimaryCamera = s + 1; } - } - else if (IsElement("instance_node")) - { + } else if (IsElement("instance_node")) { // find the node in the library int attrID = TestAttribute("url"); - if (attrID != -1) - { - const char* s = mReader->getAttributeValue(attrID); + if (attrID != -1) { + const char *s = mReader->getAttributeValue(attrID); if (s[0] != '#') ASSIMP_LOG_ERROR("Collada: Unresolved reference format of node"); - else - { + else { pNode->mNodeInstances.push_back(NodeInstance()); pNode->mNodeInstances.back().mNode = s + 1; } } - } - else if (IsElement("instance_geometry") || IsElement("instance_controller")) - { + } else if (IsElement("instance_geometry") || IsElement("instance_controller")) { // Reference to a mesh or controller, with possible material associations ReadNodeGeometry(pNode); - } - else if (IsElement("instance_light")) - { + } else if (IsElement("instance_light")) { // Reference to a light, name given in 'url' attribute int attrID = TestAttribute("url"); if (-1 == attrID) ASSIMP_LOG_WARN("Collada: Expected url attribute in element"); - else - { - const char* url = mReader->getAttributeValue(attrID); + else { + const char *url = mReader->getAttributeValue(attrID); if (url[0] != '#') ThrowException("Unknown reference format in element"); pNode->mLights.push_back(LightInstance()); pNode->mLights.back().mLight = url + 1; } - } - else if (IsElement("instance_camera")) - { + } else if (IsElement("instance_camera")) { // Reference to a camera, name given in 'url' attribute int attrID = TestAttribute("url"); if (-1 == attrID) ASSIMP_LOG_WARN("Collada: Expected url attribute in element"); - else - { - const char* url = mReader->getAttributeValue(attrID); + else { + const char *url = mReader->getAttributeValue(attrID); if (url[0] != '#') ThrowException("Unknown reference format in element"); pNode->mCameras.push_back(CameraInstance()); pNode->mCameras.back().mCamera = url + 1; } - } - else - { + } else { // skip everything else for the moment SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { break; } } @@ -3055,8 +2623,7 @@ void ColladaParser::ReadSceneNode(Node* pNode) // ------------------------------------------------------------------------------------------------ // Reads a node transformation entry of the given type and adds it to the given node's transformation list. -void ColladaParser::ReadNodeTransformation(Node* pNode, TransformType pType) -{ +void ColladaParser::ReadNodeTransformation(Node *pNode, TransformType pType) { if (mReader->isEmptyElement()) return; @@ -3072,11 +2639,10 @@ void ColladaParser::ReadNodeTransformation(Node* pNode, TransformType pType) // how many parameters to read per transformation type static const unsigned int sNumParameters[] = { 9, 4, 3, 3, 7, 16 }; - const char* content = GetTextContent(); + const char *content = GetTextContent(); // read as many parameters and store in the transformation - for (unsigned int a = 0; a < sNumParameters[pType]; a++) - { + for (unsigned int a = 0; a < sNumParameters[pType]; a++) { // read a number content = fast_atoreal_move(content, tf.f[a]); // skip whitespace after it @@ -3092,13 +2658,10 @@ void ColladaParser::ReadNodeTransformation(Node* pNode, TransformType pType) // ------------------------------------------------------------------------------------------------ // Processes bind_vertex_input and bind elements -void ColladaParser::ReadMaterialVertexInputBinding(Collada::SemanticMappingTable& tbl) -{ - while (mReader->read()) - { +void ColladaParser::ReadMaterialVertexInputBinding(Collada::SemanticMappingTable &tbl) { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("bind_vertex_input")) - { + if (IsElement("bind_vertex_input")) { Collada::InputSemanticMapEntry vn; // effect semantic @@ -3115,20 +2678,17 @@ void ColladaParser::ReadMaterialVertexInputBinding(Collada::SemanticMappingTable vn.mSet = mReader->getAttributeValueAsInt(n); tbl.mMap[s] = vn; - } - else if (IsElement("bind")) { + } else if (IsElement("bind")) { ASSIMP_LOG_WARN("Collada: Found unsupported element"); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { if (strcmp(mReader->getNodeName(), "instance_material") == 0) break; } } } -void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem& zip_archive) -{ +void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive) { // Attempt to load any undefined Collada::Image in ImageLibrary for (ImageLibrary::iterator it = mImageLibrary.begin(); it != mImageLibrary.end(); ++it) { Collada::Image &image = (*it).second; @@ -3149,31 +2709,26 @@ void ColladaParser::ReadEmbeddedTextures(ZipArchiveIOSystem& zip_archive) // ------------------------------------------------------------------------------------------------ // Reads a mesh reference in a node and adds it to the node's mesh list -void ColladaParser::ReadNodeGeometry(Node* pNode) -{ +void ColladaParser::ReadNodeGeometry(Node *pNode) { // referred mesh is given as an attribute of the element int attrUrl = GetAttribute("url"); - const char* url = mReader->getAttributeValue(attrUrl); + const char *url = mReader->getAttributeValue(attrUrl); if (url[0] != '#') ThrowException("Unknown reference format"); Collada::MeshInstance instance; instance.mMeshOrController = url + 1; // skipping the leading # - if (!mReader->isEmptyElement()) - { + if (!mReader->isEmptyElement()) { // read material associations. Ignore additional elements in between - while (mReader->read()) - { - if (mReader->getNodeType() == irr::io::EXN_ELEMENT) - { - if (IsElement("instance_material")) - { + while (mReader->read()) { + if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { + if (IsElement("instance_material")) { // read ID of the geometry subgroup and the target material int attrGroup = GetAttribute("symbol"); std::string group = mReader->getAttributeValue(attrGroup); int attrMaterial = GetAttribute("target"); - const char* urlMat = mReader->getAttributeValue(attrMaterial); + const char *urlMat = mReader->getAttributeValue(attrMaterial); Collada::SemanticMappingTable s; if (urlMat[0] == '#') urlMat++; @@ -3187,11 +2742,8 @@ void ColladaParser::ReadNodeGeometry(Node* pNode) // store the association instance.mMaterials[group] = s; } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) - { - if (strcmp(mReader->getNodeName(), "instance_geometry") == 0 - || strcmp(mReader->getNodeName(), "instance_controller") == 0) + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + if (strcmp(mReader->getNodeName(), "instance_geometry") == 0 || strcmp(mReader->getNodeName(), "instance_controller") == 0) break; } } @@ -3203,23 +2755,20 @@ void ColladaParser::ReadNodeGeometry(Node* pNode) // ------------------------------------------------------------------------------------------------ // Reads the collada scene -void ColladaParser::ReadScene() -{ +void ColladaParser::ReadScene() { if (mReader->isEmptyElement()) return; - while (mReader->read()) - { + while (mReader->read()) { if (mReader->getNodeType() == irr::io::EXN_ELEMENT) { - if (IsElement("instance_visual_scene")) - { + if (IsElement("instance_visual_scene")) { // should be the first and only occurrence if (mRootNode) ThrowException("Invalid scene containing multiple root nodes in element"); // read the url of the scene to instance. Should be of format "#some_name" int urlIndex = GetAttribute("url"); - const char* url = mReader->getAttributeValue(urlIndex); + const char *url = mReader->getAttributeValue(urlIndex); if (url[0] != '#') ThrowException("Unknown reference format in element"); @@ -3228,12 +2777,10 @@ void ColladaParser::ReadScene() if (sit == mNodeLibrary.end()) ThrowException("Unable to resolve visual_scene reference \"" + std::string(url) + "\" in element."); mRootNode = sit->second; - } - else { + } else { SkipElement(); } - } - else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { + } else if (mReader->getNodeType() == irr::io::EXN_ELEMENT_END) { break; } } @@ -3241,11 +2788,11 @@ void ColladaParser::ReadScene() // ------------------------------------------------------------------------------------------------ // Aborts the file reading with an exception -AI_WONT_RETURN void ColladaParser::ThrowException(const std::string& pError) const { +AI_WONT_RETURN void ColladaParser::ThrowException(const std::string &pError) const { throw DeadlyImportError(format() << "Collada: " << mFileName << " - " << pError); } -void ColladaParser::ReportWarning(const char* msg, ...) { +void ColladaParser::ReportWarning(const char *msg, ...) { ai_assert(nullptr != msg); va_list args; @@ -3273,7 +2820,7 @@ void ColladaParser::SkipElement() { // ------------------------------------------------------------------------------------------------ // Skips all data until the end node of the given element -void ColladaParser::SkipElement(const char* pElement) { +void ColladaParser::SkipElement(const char *pElement) { // copy the current node's name because it'a pointer to the reader's internal buffer, // which is going to change with the upcoming parsing std::string element = pElement; @@ -3288,7 +2835,7 @@ void ColladaParser::SkipElement(const char* pElement) { // ------------------------------------------------------------------------------------------------ // Tests for an opening element of the given name, throws an exception if not found -void ColladaParser::TestOpening(const char* pName) { +void ColladaParser::TestOpening(const char *pName) { // read element start if (!mReader->read()) { ThrowException(format() << "Unexpected end of file while beginning of <" << pName << "> element."); @@ -3307,7 +2854,7 @@ void ColladaParser::TestOpening(const char* pName) { // ------------------------------------------------------------------------------------------------ // Tests for the closing tag of the given element, throws an exception if not found -void ColladaParser::TestClosing(const char* pName) { +void ColladaParser::TestClosing(const char *pName) { // check if we have an empty (self-closing) element if (mReader->isEmptyElement()) { return; @@ -3337,7 +2884,7 @@ void ColladaParser::TestClosing(const char* pName) { // ------------------------------------------------------------------------------------------------ // Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes -int ColladaParser::GetAttribute(const char* pAttr) const { +int ColladaParser::GetAttribute(const char *pAttr) const { int index = TestAttribute(pAttr); if (index == -1) { ThrowException(format() << "Expected attribute \"" << pAttr << "\" for element <" << mReader->getNodeName() << ">."); @@ -3349,8 +2896,7 @@ int ColladaParser::GetAttribute(const char* pAttr) const { // ------------------------------------------------------------------------------------------------ // Tests the present element for the presence of one attribute, returns its index or throws an exception if not found -int ColladaParser::TestAttribute(const char* pAttr) const -{ +int ColladaParser::TestAttribute(const char *pAttr) const { for (int a = 0; a < mReader->getAttributeCount(); a++) if (strcmp(mReader->getAttributeName(a), pAttr) == 0) return a; @@ -3360,9 +2906,8 @@ int ColladaParser::TestAttribute(const char* pAttr) const // ------------------------------------------------------------------------------------------------ // Reads the text contents of an element, throws an exception if not given. Skips leading whitespace. -const char* ColladaParser::GetTextContent() -{ - const char* sz = TestTextContent(); +const char *ColladaParser::GetTextContent() { + const char *sz = TestTextContent(); if (!sz) { ThrowException("Invalid contents in element \"n\"."); } @@ -3371,8 +2916,7 @@ const char* ColladaParser::GetTextContent() // ------------------------------------------------------------------------------------------------ // Reads the text contents of an element, returns NULL if not given. Skips leading whitespace. -const char* ColladaParser::TestTextContent() -{ +const char *ColladaParser::TestTextContent() { // present node should be the beginning of an element if (mReader->getNodeType() != irr::io::EXN_ELEMENT || mReader->isEmptyElement()) return NULL; @@ -3384,7 +2928,7 @@ const char* ColladaParser::TestTextContent() return NULL; // skip leading whitespace - const char* text = mReader->getNodeData(); + const char *text = mReader->getNodeData(); SkipSpacesAndLineEnd(&text); return text; @@ -3392,17 +2936,13 @@ const char* ColladaParser::TestTextContent() // ------------------------------------------------------------------------------------------------ // Calculates the resulting transformation fromm all the given transform steps -aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector& pTransforms) const -{ +aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector &pTransforms) const { aiMatrix4x4 res; - for (std::vector::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it) - { - const Transform& tf = *it; - switch (tf.mType) - { - case TF_LOOKAT: - { + for (std::vector::const_iterator it = pTransforms.begin(); it != pTransforms.end(); ++it) { + const Transform &tf = *it; + switch (tf.mType) { + case TF_LOOKAT: { aiVector3D pos(tf.f[0], tf.f[1], tf.f[2]); aiVector3D dstPos(tf.f[3], tf.f[4], tf.f[5]); aiVector3D up = aiVector3D(tf.f[6], tf.f[7], tf.f[8]).Normalize(); @@ -3410,14 +2950,13 @@ aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector aiVector3D right = (dir ^ up).Normalize(); res *= aiMatrix4x4( - right.x, up.x, -dir.x, pos.x, - right.y, up.y, -dir.y, pos.y, - right.z, up.z, -dir.z, pos.z, - 0, 0, 0, 1); + right.x, up.x, -dir.x, pos.x, + right.y, up.y, -dir.y, pos.y, + right.z, up.z, -dir.z, pos.z, + 0, 0, 0, 1); break; } - case TF_ROTATE: - { + case TF_ROTATE: { aiMatrix4x4 rot; ai_real angle = tf.f[3] * ai_real(AI_MATH_PI) / ai_real(180.0); aiVector3D axis(tf.f[0], tf.f[1], tf.f[2]); @@ -3425,17 +2964,15 @@ aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector res *= rot; break; } - case TF_TRANSLATE: - { + case TF_TRANSLATE: { aiMatrix4x4 trans; aiMatrix4x4::Translation(aiVector3D(tf.f[0], tf.f[1], tf.f[2]), trans); res *= trans; break; } - case TF_SCALE: - { + case TF_SCALE: { aiMatrix4x4 scale(tf.f[0], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[1], 0.0f, 0.0f, 0.0f, 0.0f, tf.f[2], 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f); + 0.0f, 0.0f, 0.0f, 1.0f); res *= scale; break; } @@ -3443,10 +2980,9 @@ aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector // TODO: (thom) ai_assert(false); break; - case TF_MATRIX: - { + case TF_MATRIX: { aiMatrix4x4 mat(tf.f[0], tf.f[1], tf.f[2], tf.f[3], tf.f[4], tf.f[5], tf.f[6], tf.f[7], - tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]); + tf.f[8], tf.f[9], tf.f[10], tf.f[11], tf.f[12], tf.f[13], tf.f[14], tf.f[15]); res *= mat; break; } @@ -3461,8 +2997,7 @@ aiMatrix4x4 ColladaParser::CalculateResultTransform(const std::vector // ------------------------------------------------------------------------------------------------ // Determines the input data type for the given semantic string -Collada::InputType ColladaParser::GetTypeForSemantic(const std::string& semantic) -{ +Collada::InputType ColladaParser::GetTypeForSemantic(const std::string &semantic) { if (semantic.empty()) { ASSIMP_LOG_WARN("Vertex input type is empty."); return IT_Invalid; diff --git a/code/AssetLib/Collada/ColladaParser.h b/code/AssetLib/Collada/ColladaParser.h index d1e812bd2..a84a59354 100644 --- a/code/AssetLib/Collada/ColladaParser.h +++ b/code/AssetLib/Collada/ColladaParser.h @@ -47,346 +47,345 @@ #ifndef AI_COLLADAPARSER_H_INC #define AI_COLLADAPARSER_H_INC -#include #include "ColladaHelper.h" -#include #include +#include +#include -namespace Assimp -{ - class ZipArchiveIOSystem; +namespace Assimp { +class ZipArchiveIOSystem; - // ------------------------------------------------------------------------------------------ - /** Parser helper class for the Collada loader. +// ------------------------------------------------------------------------------------------ +/** Parser helper class for the Collada loader. * * Does all the XML reading and builds internal data structures from it, * but leaves the resolving of all the references to the loader. */ - class ColladaParser - { - friend class ColladaLoader; +class ColladaParser { + friend class ColladaLoader; - /** Converts a path read from a collada file to the usual representation */ - static void UriDecodePath(aiString& ss); + /** Converts a path read from a collada file to the usual representation */ + static void UriDecodePath(aiString &ss); - protected: - /** Map for generic metadata as aiString */ - typedef std::map StringMetaData; +protected: + /** Map for generic metadata as aiString */ + typedef std::map StringMetaData; - /** Constructor from XML file */ - ColladaParser(IOSystem* pIOHandler, const std::string& pFile); + /** Constructor from XML file */ + ColladaParser(IOSystem *pIOHandler, const std::string &pFile); - /** Destructor */ - ~ColladaParser(); + /** Destructor */ + ~ColladaParser(); - /** Attempts to read the ZAE manifest and returns the DAE to open */ - static std::string ReadZaeManifest(ZipArchiveIOSystem &zip_archive); + /** Attempts to read the ZAE manifest and returns the DAE to open */ + static std::string ReadZaeManifest(ZipArchiveIOSystem &zip_archive); - /** Reads the contents of the file */ - void ReadContents(); + /** Reads the contents of the file */ + void ReadContents(); - /** Reads the structure of the file */ - void ReadStructure(); + /** Reads the structure of the file */ + void ReadStructure(); - /** Reads asset information such as coordinate system information and legal blah */ - void ReadAssetInfo(); + /** Reads asset information such as coordinate system information and legal blah */ + void ReadAssetInfo(); - /** Reads contributor information such as author and legal blah */ - void ReadContributorInfo(); + /** Reads contributor information such as author and legal blah */ + void ReadContributorInfo(); - /** Reads generic metadata into provided map and renames keys for Assimp */ - void ReadMetaDataItem(StringMetaData &metadata); + /** Reads generic metadata into provided map and renames keys for Assimp */ + void ReadMetaDataItem(StringMetaData &metadata); - /** Reads the animation library */ - void ReadAnimationLibrary(); + /** Reads the animation library */ + void ReadAnimationLibrary(); - /** Reads the animation clip library */ - void ReadAnimationClipLibrary(); + /** Reads the animation clip library */ + void ReadAnimationClipLibrary(); - /** Unwrap controllers dependency hierarchy */ - void PostProcessControllers(); - - /** Re-build animations from animation clip library, if present, otherwise combine single-channel animations */ - void PostProcessRootAnimations(); + /** Unwrap controllers dependency hierarchy */ + void PostProcessControllers(); - /** Reads an animation into the given parent structure */ - void ReadAnimation( Collada::Animation* pParent); + /** Re-build animations from animation clip library, if present, otherwise combine single-channel animations */ + void PostProcessRootAnimations(); - /** Reads an animation sampler into the given anim channel */ - void ReadAnimationSampler( Collada::AnimationChannel& pChannel); + /** Reads an animation into the given parent structure */ + void ReadAnimation(Collada::Animation *pParent); - /** Reads the skeleton controller library */ - void ReadControllerLibrary(); + /** Reads an animation sampler into the given anim channel */ + void ReadAnimationSampler(Collada::AnimationChannel &pChannel); - /** Reads a controller into the given mesh structure */ - void ReadController( Collada::Controller& pController); + /** Reads the skeleton controller library */ + void ReadControllerLibrary(); - /** Reads the joint definitions for the given controller */ - void ReadControllerJoints( Collada::Controller& pController); + /** Reads a controller into the given mesh structure */ + void ReadController(Collada::Controller &pController); - /** Reads the joint weights for the given controller */ - void ReadControllerWeights( Collada::Controller& pController); + /** Reads the joint definitions for the given controller */ + void ReadControllerJoints(Collada::Controller &pController); - /** Reads the image library contents */ - void ReadImageLibrary(); + /** Reads the joint weights for the given controller */ + void ReadControllerWeights(Collada::Controller &pController); - /** Reads an image entry into the given image */ - void ReadImage( Collada::Image& pImage); + /** Reads the image library contents */ + void ReadImageLibrary(); - /** Reads the material library */ - void ReadMaterialLibrary(); + /** Reads an image entry into the given image */ + void ReadImage(Collada::Image &pImage); - /** Reads a material entry into the given material */ - void ReadMaterial( Collada::Material& pMaterial); + /** Reads the material library */ + void ReadMaterialLibrary(); - /** Reads the camera library */ - void ReadCameraLibrary(); + /** Reads a material entry into the given material */ + void ReadMaterial(Collada::Material &pMaterial); - /** Reads a camera entry into the given camera */ - void ReadCamera( Collada::Camera& pCamera); + /** Reads the camera library */ + void ReadCameraLibrary(); - /** Reads the light library */ - void ReadLightLibrary(); + /** Reads a camera entry into the given camera */ + void ReadCamera(Collada::Camera &pCamera); - /** Reads a light entry into the given light */ - void ReadLight( Collada::Light& pLight); + /** Reads the light library */ + void ReadLightLibrary(); - /** Reads the effect library */ - void ReadEffectLibrary(); + /** Reads a light entry into the given light */ + void ReadLight(Collada::Light &pLight); - /** Reads an effect entry into the given effect*/ - void ReadEffect( Collada::Effect& pEffect); + /** Reads the effect library */ + void ReadEffectLibrary(); - /** Reads an COMMON effect profile */ - void ReadEffectProfileCommon( Collada::Effect& pEffect); + /** Reads an effect entry into the given effect*/ + void ReadEffect(Collada::Effect &pEffect); - /** Read sampler properties */ - void ReadSamplerProperties( Collada::Sampler& pSampler); + /** Reads an COMMON effect profile */ + void ReadEffectProfileCommon(Collada::Effect &pEffect); - /** Reads an effect entry containing a color or a texture defining that color */ - void ReadEffectColor( aiColor4D& pColor, Collada::Sampler& pSampler); + /** Read sampler properties */ + void ReadSamplerProperties(Collada::Sampler &pSampler); - /** Reads an effect entry containing a float */ - void ReadEffectFloat( ai_real& pFloat); + /** Reads an effect entry containing a color or a texture defining that color */ + void ReadEffectColor(aiColor4D &pColor, Collada::Sampler &pSampler); - /** Reads an effect parameter specification of any kind */ - void ReadEffectParam( Collada::EffectParam& pParam); + /** Reads an effect entry containing a float */ + void ReadEffectFloat(ai_real &pFloat); - /** Reads the geometry library contents */ - void ReadGeometryLibrary(); + /** Reads an effect parameter specification of any kind */ + void ReadEffectParam(Collada::EffectParam &pParam); - /** Reads a geometry from the geometry library. */ - void ReadGeometry( Collada::Mesh* pMesh); + /** Reads the geometry library contents */ + void ReadGeometryLibrary(); - /** Reads a mesh from the geometry library */ - void ReadMesh( Collada::Mesh* pMesh); + /** Reads a geometry from the geometry library. */ + void ReadGeometry(Collada::Mesh &pMesh); - /** Reads a source element - a combination of raw data and an accessor defining + /** Reads a mesh from the geometry library */ + void ReadMesh(Collada::Mesh &pMesh); + + /** Reads a source element - a combination of raw data and an accessor defining * things that should not be redefinable. Yes, that's another rant. */ - void ReadSource(); + void ReadSource(); - /** Reads a data array holding a number of elements, and stores it in the global library. + /** Reads a data array holding a number of elements, and stores it in the global library. * Currently supported are array of floats and arrays of strings. */ - void ReadDataArray(); + void ReadDataArray(); - /** Reads an accessor and stores it in the global library under the given ID - + /** Reads an accessor and stores it in the global library under the given ID - * accessors use the ID of the parent element */ - void ReadAccessor( const std::string& pID); + void ReadAccessor(const std::string &pID); - /** Reads input declarations of per-vertex mesh data into the given mesh */ - void ReadVertexData( Collada::Mesh* pMesh); + /** Reads input declarations of per-vertex mesh data into the given mesh */ + void ReadVertexData(Collada::Mesh &pMesh); - /** Reads input declarations of per-index mesh data into the given mesh */ - void ReadIndexData( Collada::Mesh* pMesh); + /** Reads input declarations of per-index mesh data into the given mesh */ + void ReadIndexData(Collada::Mesh &pMesh); - /** Reads a single input channel element and stores it in the given array, if valid */ - void ReadInputChannel( std::vector& poChannels); + /** Reads a single input channel element and stores it in the given array, if valid */ + void ReadInputChannel(std::vector &poChannels); - /** Reads a

primitive index list and assembles the mesh data into the given mesh */ - size_t ReadPrimitives( Collada::Mesh* pMesh, std::vector& pPerIndexChannels, - size_t pNumPrimitives, const std::vector& pVCount, Collada::PrimitiveType pPrimType); + /** Reads a

primitive index list and assembles the mesh data into the given mesh */ + size_t ReadPrimitives(Collada::Mesh &pMesh, std::vector &pPerIndexChannels, + size_t pNumPrimitives, const std::vector &pVCount, Collada::PrimitiveType pPrimType); - /** Copies the data for a single primitive into the mesh, based on the InputChannels */ - void CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, - Collada::Mesh* pMesh, std::vector& pPerIndexChannels, - size_t currentPrimitive, const std::vector& indices); + /** Copies the data for a single primitive into the mesh, based on the InputChannels */ + void CopyVertex(size_t currentVertex, size_t numOffsets, size_t numPoints, size_t perVertexOffset, + Collada::Mesh &pMesh, std::vector &pPerIndexChannels, + size_t currentPrimitive, const std::vector &indices); - /** Reads one triangle of a tristrip into the mesh */ - void ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Collada::Mesh* pMesh, - std::vector& pPerIndexChannels, size_t currentPrimitive, const std::vector& indices); + /** Reads one triangle of a tristrip into the mesh */ + void ReadPrimTriStrips(size_t numOffsets, size_t perVertexOffset, Collada::Mesh &pMesh, + std::vector &pPerIndexChannels, size_t currentPrimitive, const std::vector &indices); - /** Extracts a single object from an input channel and stores it in the appropriate mesh data array */ - void ExtractDataObjectFromChannel( const Collada::InputChannel& pInput, size_t pLocalIndex, Collada::Mesh* pMesh); + /** Extracts a single object from an input channel and stores it in the appropriate mesh data array */ + void ExtractDataObjectFromChannel(const Collada::InputChannel &pInput, size_t pLocalIndex, Collada::Mesh &pMesh); - /** Reads the library of node hierarchies and scene parts */ - void ReadSceneLibrary(); + /** Reads the library of node hierarchies and scene parts */ + void ReadSceneLibrary(); - /** Reads a scene node's contents including children and stores it in the given node */ - void ReadSceneNode( Collada::Node* pNode); + /** Reads a scene node's contents including children and stores it in the given node */ + void ReadSceneNode(Collada::Node *pNode); - /** Reads a node transformation entry of the given type and adds it to the given node's transformation list. */ - void ReadNodeTransformation( Collada::Node* pNode, Collada::TransformType pType); + /** Reads a node transformation entry of the given type and adds it to the given node's transformation list. */ + void ReadNodeTransformation(Collada::Node *pNode, Collada::TransformType pType); - /** Reads a mesh reference in a node and adds it to the node's mesh list */ - void ReadNodeGeometry( Collada::Node* pNode); + /** Reads a mesh reference in a node and adds it to the node's mesh list */ + void ReadNodeGeometry(Collada::Node *pNode); - /** Reads the collada scene */ - void ReadScene(); + /** Reads the collada scene */ + void ReadScene(); - // Processes bind_vertex_input and bind elements - void ReadMaterialVertexInputBinding( Collada::SemanticMappingTable& tbl); + // Processes bind_vertex_input and bind elements + void ReadMaterialVertexInputBinding(Collada::SemanticMappingTable &tbl); - /** Reads embedded textures from a ZAE archive*/ - void ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive); + /** Reads embedded textures from a ZAE archive*/ + void ReadEmbeddedTextures(ZipArchiveIOSystem &zip_archive); - protected: - /** Aborts the file reading with an exception */ - AI_WONT_RETURN void ThrowException( const std::string& pError) const AI_WONT_RETURN_SUFFIX; - void ReportWarning(const char* msg,...); +protected: + /** Aborts the file reading with an exception */ + AI_WONT_RETURN void ThrowException(const std::string &pError) const AI_WONT_RETURN_SUFFIX; + void ReportWarning(const char *msg, ...); - /** Skips all data until the end node of the current element */ - void SkipElement(); + /** Skips all data until the end node of the current element */ + void SkipElement(); - /** Skips all data until the end node of the given element */ - void SkipElement( const char* pElement); + /** Skips all data until the end node of the given element */ + void SkipElement(const char *pElement); - /** Compares the current xml element name to the given string and returns true if equal */ - bool IsElement( const char* pName) const; + /** Compares the current xml element name to the given string and returns true if equal */ + bool IsElement(const char *pName) const; - /** Tests for the opening tag of the given element, throws an exception if not found */ - void TestOpening( const char* pName); + /** Tests for the opening tag of the given element, throws an exception if not found */ + void TestOpening(const char *pName); - /** Tests for the closing tag of the given element, throws an exception if not found */ - void TestClosing( const char* pName); + /** Tests for the closing tag of the given element, throws an exception if not found */ + void TestClosing(const char *pName); - /** Checks the present element for the presence of the attribute, returns its index + /** Checks the present element for the presence of the attribute, returns its index or throws an exception if not found */ - int GetAttribute( const char* pAttr) const; + int GetAttribute(const char *pAttr) const; - /** Returns the index of the named attribute or -1 if not found. Does not throw, + /** Returns the index of the named attribute or -1 if not found. Does not throw, therefore useful for optional attributes */ - int TestAttribute( const char* pAttr) const; + int TestAttribute(const char *pAttr) const; - /** Reads the text contents of an element, throws an exception if not given. + /** Reads the text contents of an element, throws an exception if not given. Skips leading whitespace. */ - const char* GetTextContent(); + const char *GetTextContent(); - /** Reads the text contents of an element, returns NULL if not given. + /** Reads the text contents of an element, returns NULL if not given. Skips leading whitespace. */ - const char* TestTextContent(); + const char *TestTextContent(); - /** Reads a single bool from current text content */ - bool ReadBoolFromTextContent(); + /** Reads a single bool from current text content */ + bool ReadBoolFromTextContent(); - /** Reads a single float from current text content */ - ai_real ReadFloatFromTextContent(); + /** Reads a single float from current text content */ + ai_real ReadFloatFromTextContent(); - /** Calculates the resulting transformation from all the given transform steps */ - aiMatrix4x4 CalculateResultTransform( const std::vector& pTransforms) const; + /** Calculates the resulting transformation from all the given transform steps */ + aiMatrix4x4 CalculateResultTransform(const std::vector &pTransforms) const; - /** Determines the input data type for the given semantic string */ - Collada::InputType GetTypeForSemantic( const std::string& pSemantic); + /** Determines the input data type for the given semantic string */ + Collada::InputType GetTypeForSemantic(const std::string &pSemantic); - /** Finds the item in the given library by its reference, throws if not found */ - template const Type& ResolveLibraryReference( const std::map& pLibrary, const std::string& pURL) const; - - protected: - /** Filename, for a verbose error message */ - std::string mFileName; - - /** XML reader, member for everyday use */ - irr::io::IrrXMLReader* mReader; - - /** All data arrays found in the file by ID. Might be referred to by actually - everyone. Collada, you are a steaming pile of indirection. */ - typedef std::map DataLibrary; - DataLibrary mDataLibrary; - - /** Same for accessors which define how the data in a data array is accessed. */ - typedef std::map AccessorLibrary; - AccessorLibrary mAccessorLibrary; - - /** Mesh library: mesh by ID */ - typedef std::map MeshLibrary; - MeshLibrary mMeshLibrary; - - /** node library: root node of the hierarchy part by ID */ - typedef std::map NodeLibrary; - NodeLibrary mNodeLibrary; - - /** Image library: stores texture properties by ID */ - typedef std::map ImageLibrary; - ImageLibrary mImageLibrary; - - /** Effect library: surface attributes by ID */ - typedef std::map EffectLibrary; - EffectLibrary mEffectLibrary; - - /** Material library: surface material by ID */ - typedef std::map MaterialLibrary; - MaterialLibrary mMaterialLibrary; - - /** Light library: surface light by ID */ - typedef std::map LightLibrary; - LightLibrary mLightLibrary; - - /** Camera library: surface material by ID */ - typedef std::map CameraLibrary; - CameraLibrary mCameraLibrary; - - /** Controller library: joint controllers by ID */ - typedef std::map ControllerLibrary; - ControllerLibrary mControllerLibrary; - - /** Animation library: animation references by ID */ - typedef std::map AnimationLibrary; - AnimationLibrary mAnimationLibrary; - - /** Animation clip library: clip animation references by ID */ - typedef std::vector > > AnimationClipLibrary; - AnimationClipLibrary mAnimationClipLibrary; - - /** Pointer to the root node. Don't delete, it just points to one of - the nodes in the node library. */ - Collada::Node* mRootNode; - - /** Root animation container */ - Collada::Animation mAnims; - - /** Size unit: how large compared to a meter */ - ai_real mUnitSize; - - /** Which is the up vector */ - enum { UP_X, UP_Y, UP_Z } mUpDirection; - - /** Asset metadata (global for scene) */ - StringMetaData mAssetMetaData; - - /** Collada file format version */ - Collada::FormatVersion mFormat; - }; - - // ------------------------------------------------------------------------------------------------ - // Check for element match - inline bool ColladaParser::IsElement( const char* pName) const - { - ai_assert( mReader->getNodeType() == irr::io::EXN_ELEMENT); - return ::strcmp( mReader->getNodeName(), pName) == 0; - } - - // ------------------------------------------------------------------------------------------------ - // Finds the item in the given library by its reference, throws if not found + /** Finds the item in the given library by its reference, throws if not found */ template - const Type& ColladaParser::ResolveLibraryReference( const std::map& pLibrary, const std::string& pURL) const - { - typename std::map::const_iterator it = pLibrary.find( pURL); - if( it == pLibrary.end()) - ThrowException( Formatter::format() << "Unable to resolve library reference \"" << pURL << "\"." ); - return it->second; - } + const Type &ResolveLibraryReference(const std::map &pLibrary, const std::string &pURL) const; + +protected: + /** Filename, for a verbose error message */ + std::string mFileName; + + /** XML reader, member for everyday use */ + irr::io::IrrXMLReader *mReader; + + /** All data arrays found in the file by ID. Might be referred to by actually + everyone. Collada, you are a steaming pile of indirection. */ + typedef std::map DataLibrary; + DataLibrary mDataLibrary; + + /** Same for accessors which define how the data in a data array is accessed. */ + typedef std::map AccessorLibrary; + AccessorLibrary mAccessorLibrary; + + /** Mesh library: mesh by ID */ + typedef std::map MeshLibrary; + MeshLibrary mMeshLibrary; + + /** node library: root node of the hierarchy part by ID */ + typedef std::map NodeLibrary; + NodeLibrary mNodeLibrary; + + /** Image library: stores texture properties by ID */ + typedef std::map ImageLibrary; + ImageLibrary mImageLibrary; + + /** Effect library: surface attributes by ID */ + typedef std::map EffectLibrary; + EffectLibrary mEffectLibrary; + + /** Material library: surface material by ID */ + typedef std::map MaterialLibrary; + MaterialLibrary mMaterialLibrary; + + /** Light library: surface light by ID */ + typedef std::map LightLibrary; + LightLibrary mLightLibrary; + + /** Camera library: surface material by ID */ + typedef std::map CameraLibrary; + CameraLibrary mCameraLibrary; + + /** Controller library: joint controllers by ID */ + typedef std::map ControllerLibrary; + ControllerLibrary mControllerLibrary; + + /** Animation library: animation references by ID */ + typedef std::map AnimationLibrary; + AnimationLibrary mAnimationLibrary; + + /** Animation clip library: clip animation references by ID */ + typedef std::vector>> AnimationClipLibrary; + AnimationClipLibrary mAnimationClipLibrary; + + /** Pointer to the root node. Don't delete, it just points to one of + the nodes in the node library. */ + Collada::Node *mRootNode; + + /** Root animation container */ + Collada::Animation mAnims; + + /** Size unit: how large compared to a meter */ + ai_real mUnitSize; + + /** Which is the up vector */ + enum { UP_X, + UP_Y, + UP_Z } mUpDirection; + + /** Asset metadata (global for scene) */ + StringMetaData mAssetMetaData; + + /** Collada file format version */ + Collada::FormatVersion mFormat; +}; + +// ------------------------------------------------------------------------------------------------ +// Check for element match +inline bool ColladaParser::IsElement(const char *pName) const { + ai_assert(mReader->getNodeType() == irr::io::EXN_ELEMENT); + return ::strcmp(mReader->getNodeName(), pName) == 0; +} + +// ------------------------------------------------------------------------------------------------ +// Finds the item in the given library by its reference, throws if not found +template +const Type &ColladaParser::ResolveLibraryReference(const std::map &pLibrary, const std::string &pURL) const { + typename std::map::const_iterator it = pLibrary.find(pURL); + if (it == pLibrary.end()) + ThrowException(Formatter::format() << "Unable to resolve library reference \"" << pURL << "\"."); + return it->second; +} } // end of namespace Assimp diff --git a/code/AssetLib/glTF/glTFAssetWriter.inl b/code/AssetLib/glTF/glTFAssetWriter.inl index 5e4416ee9..d8d2556fa 100644 --- a/code/AssetLib/glTF/glTFAssetWriter.inl +++ b/code/AssetLib/glTF/glTFAssetWriter.inl @@ -59,7 +59,7 @@ namespace glTF { namespace { template - inline + inline Value& MakeValue(Value& val, T(&r)[N], MemoryPoolAllocator<>& al) { val.SetArray(); val.Reserve(N, al); @@ -70,7 +70,7 @@ namespace glTF { } template - inline + inline Value& MakeValue(Value& val, const std::vector & r, MemoryPoolAllocator<>& al) { val.SetArray(); val.Reserve(static_cast(r.size()), al); @@ -530,7 +530,9 @@ namespace glTF { StringBuffer docBuffer; PrettyWriter writer(docBuffer); - mDoc.Accept(writer); + if (!mDoc.Accept(writer)) { + throw DeadlyExportError("Failed to write scene data!"); + } if (jsonOutFile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) { throw DeadlyExportError("Failed to write scene data!"); @@ -569,7 +571,9 @@ namespace glTF { StringBuffer docBuffer; Writer writer(docBuffer); - mDoc.Accept(writer); + if (!mDoc.Accept(writer)) { + throw DeadlyExportError("Failed to write scene data!"); + } if (outfile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) { throw DeadlyExportError("Failed to write scene data!"); diff --git a/code/AssetLib/glTF/glTFCommon.cpp b/code/AssetLib/glTF/glTFCommon.cpp index e0ce4fed8..2c46a46e3 100644 --- a/code/AssetLib/glTF/glTFCommon.cpp +++ b/code/AssetLib/glTF/glTFCommon.cpp @@ -145,13 +145,13 @@ bool ParseDataURI(const char *const_uri, size_t uriLen, DataURI &out) { size_t i = 5, j; if (uri[i] != ';' && uri[i] != ',') { // has media type? uri[1] = char(i); - for (; uri[i] != ';' && uri[i] != ',' && i < uriLen; ++i) { + for (;i < uriLen && uri[i] != ';' && uri[i] != ','; ++i) { // nothing to do! } } - while (uri[i] == ';' && i < uriLen) { + while (i < uriLen && uri[i] == ';') { uri[i++] = '\0'; - for (j = i; uri[i] != ';' && uri[i] != ',' && i < uriLen; ++i) { + for (j = i; i < uriLen && uri[i] != ';' && uri[i] != ','; ++i) { // nothing to do! } diff --git a/code/AssetLib/glTF/glTFExporter.cpp b/code/AssetLib/glTF/glTFExporter.cpp index e6c14e7dd..b85affc08 100644 --- a/code/AssetLib/glTF/glTFExporter.cpp +++ b/code/AssetLib/glTF/glTFExporter.cpp @@ -348,7 +348,7 @@ void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& pr if (path[0] == '*') { // embedded aiTexture* curTex = mScene->mTextures[atoi(&path[1])]; - + prop.texture->source->name = curTex->mFilename.C_Str(); uint8_t *data = reinterpret_cast(curTex->pcData); diff --git a/code/AssetLib/glTF2/glTF2Asset.h b/code/AssetLib/glTF2/glTF2Asset.h index b413f1fe7..58e5ee88b 100644 --- a/code/AssetLib/glTF2/glTF2Asset.h +++ b/code/AssetLib/glTF2/glTF2Asset.h @@ -175,19 +175,19 @@ enum ComponentType { inline unsigned int ComponentTypeSize(ComponentType t) { switch (t) { - case ComponentType_SHORT: - case ComponentType_UNSIGNED_SHORT: - return 2; + case ComponentType_SHORT: + case ComponentType_UNSIGNED_SHORT: + return 2; - case ComponentType_UNSIGNED_INT: - case ComponentType_FLOAT: - return 4; + case ComponentType_UNSIGNED_INT: + case ComponentType_FLOAT: + return 4; - case ComponentType_BYTE: - case ComponentType_UNSIGNED_BYTE: - return 1; - default: - throw DeadlyImportError("GLTF: Unsupported Component Type " + to_string(t)); + case ComponentType_BYTE: + case ComponentType_UNSIGNED_BYTE: + return 1; + default: + throw DeadlyImportError("GLTF: Unsupported Component Type " + to_string(t)); } } @@ -318,9 +318,11 @@ class Ref { public: Ref() : - vector(0), index(0) {} + vector(0), + index(0) {} Ref(std::vector &vec, unsigned int idx) : - vector(&vec), index(idx) {} + vector(&vec), + index(idx) {} inline unsigned int GetIndex() const { return index; } @@ -340,7 +342,8 @@ struct Nullable { Nullable() : isPresent(false) {} Nullable(T &val) : - value(val), isPresent(true) {} + value(val), + isPresent(true) {} }; //! Base class for all glTF top-level objects @@ -368,6 +371,7 @@ struct Object { //! similar to how WebGL's vertexAttribPointer() defines an attribute in a buffer. struct Accessor : public Object { struct Sparse; + Ref bufferView; //!< The ID of the bufferView. (required) size_t byteOffset; //!< The offset relative to the start of the bufferView in bytes. (required) ComponentType componentType; //!< The datatype of components in the attribute. (required) @@ -383,20 +387,19 @@ struct Accessor : public Object { inline uint8_t *GetPointer(); - template + template void ExtractData(T *&outData); void WriteData(size_t count, const void *src_buffer, size_t src_stride); void WriteSparseValues(size_t count, const void *src_data, size_t src_dataStride); void WriteSparseIndices(size_t count, const void *src_idx, size_t src_idxStride); - //! Helper class to iterate the data class Indexer { friend struct Accessor; - // This field is reported as not used, making it protectd is the easiest way to work around it without going to the bottom of what the problem is: - // ../code/glTF2/glTF2Asset.h:392:19: error: private field 'accessor' is not used [-Werror,-Wunused-private-field] + // This field is reported as not used, making it protectd is the easiest way to work around it without going to the bottom of what the problem is: + // ../code/glTF2/glTF2Asset.h:392:19: error: private field 'accessor' is not used [-Werror,-Wunused-private-field] protected: Accessor &accessor; @@ -405,8 +408,7 @@ struct Accessor : public Object { size_t elemSize, stride; Indexer(Accessor &acc); - - + public: //! Accesses the i-th value as defined by the accessor template @@ -471,7 +473,11 @@ public: /// \param [in] pDecodedData_Length - size of encoded region, in bytes. /// \param [in] pID - ID of the region. SEncodedRegion(const size_t pOffset, const size_t pEncodedData_Length, uint8_t *pDecodedData, const size_t pDecodedData_Length, const std::string pID) : - Offset(pOffset), EncodedData_Length(pEncodedData_Length), DecodedData(pDecodedData), DecodedData_Length(pDecodedData_Length), ID(pID) {} + Offset(pOffset), + EncodedData_Length(pEncodedData_Length), + DecodedData(pDecodedData), + DecodedData_Length(pDecodedData_Length), + ID(pID) {} /// \fn ~SEncodedRegion() /// Destructor. @@ -603,7 +609,8 @@ struct Camera : public Object { } cameraProperties; Camera() : - type(Perspective), cameraProperties() { + type(Perspective), + cameraProperties() { // empty } void Read(Value &obj, Asset &r); @@ -908,7 +915,7 @@ class LazyDict : public LazyDictBase { Value *mDict; //! JSON dictionary object Asset &mAsset; //! The asset instance - std::gltf_unordered_set mRecursiveReferenceCheck; //! Used by Retrieve to prevent recursive lookups + std::gltf_unordered_set mRecursiveReferenceCheck; //! Used by Retrieve to prevent recursive lookups void AttachToDocument(Document &doc); void DetachFromDocument(); @@ -1022,7 +1029,22 @@ public: public: Asset(IOSystem *io = 0) : - mIOSystem(io), asset(), accessors(*this, "accessors"), animations(*this, "animations"), buffers(*this, "buffers"), bufferViews(*this, "bufferViews"), cameras(*this, "cameras"), lights(*this, "lights", "KHR_lights_punctual"), images(*this, "images"), materials(*this, "materials"), meshes(*this, "meshes"), nodes(*this, "nodes"), samplers(*this, "samplers"), scenes(*this, "scenes"), skins(*this, "skins"), textures(*this, "textures") { + mIOSystem(io), + asset(), + accessors(*this, "accessors"), + animations(*this, "animations"), + buffers(*this, "buffers"), + bufferViews(*this, "bufferViews"), + cameras(*this, "cameras"), + lights(*this, "lights", "KHR_lights_punctual"), + images(*this, "images"), + materials(*this, "materials"), + meshes(*this, "meshes"), + nodes(*this, "nodes"), + samplers(*this, "samplers"), + scenes(*this, "scenes"), + skins(*this, "skins"), + textures(*this, "textures") { memset(&extensionsUsed, 0, sizeof(extensionsUsed)); memset(&extensionsRequired, 0, sizeof(extensionsRequired)); } diff --git a/code/AssetLib/glTF2/glTF2Asset.inl b/code/AssetLib/glTF2/glTF2Asset.inl index b81d73eda..400d44670 100644 --- a/code/AssetLib/glTF2/glTF2Asset.inl +++ b/code/AssetLib/glTF2/glTF2Asset.inl @@ -180,7 +180,10 @@ inline Value *FindObject(Value &val, const char *id) { template inline LazyDict::LazyDict(Asset &asset, const char *dictId, const char *extId) : - mDictId(dictId), mExtId(extId), mDict(0), mAsset(asset) { + mDictId(dictId), + mExtId(extId), + mDict(0), + mAsset(asset) { asset.mDicts.push_back(this); // register to the list of dictionaries } @@ -342,7 +345,10 @@ Ref LazyDict::Create(const char *id) { // inline Buffer::Buffer() : - byteLength(0), type(Type_arraybuffer), EncodedRegion_Current(nullptr), mIsSpecial(false) {} + byteLength(0), + type(Type_arraybuffer), + EncodedRegion_Current(nullptr), + mIsSpecial(false) {} inline Buffer::~Buffer() { for (SEncodedRegion *reg : EncodedRegion_List) @@ -517,7 +523,7 @@ inline void Buffer::Grow(size_t amount) { if (amount <= 0) { return; } - + // Capacity is big enough if (capacity >= byteLength + amount) { byteLength += amount; @@ -569,7 +575,6 @@ inline uint8_t *BufferView::GetPointer(size_t accOffset) { // // struct Accessor // - inline void Accessor::Sparse::PopulateData(size_t numBytes, uint8_t *bytes) { if (bytes) { data.assign(bytes, bytes + numBytes); @@ -713,10 +718,9 @@ inline void CopyData(size_t count, } } // namespace -template -void Accessor::ExtractData(T *&outData) -{ - uint8_t* data = GetPointer(); +template +void Accessor::ExtractData(T *&outData) { + uint8_t *data = GetPointer(); if (!data) { throw DeadlyImportError("GLTF: data is NULL"); } @@ -781,7 +785,10 @@ inline void Accessor::WriteSparseIndices(size_t _count, const void *src_idx, siz CopyData(_count, indices_src, src_idxStride, indices_dst, indices_dst_stride); } inline Accessor::Indexer::Indexer(Accessor &acc) : - accessor(acc), data(acc.GetPointer()), elemSize(acc.GetElementSize()), stride(acc.bufferView && acc.bufferView->byteStride ? acc.bufferView->byteStride : elemSize) { + accessor(acc), + data(acc.GetPointer()), + elemSize(acc.GetElementSize()), + stride(acc.bufferView && acc.bufferView->byteStride ? acc.bufferView->byteStride : elemSize) { } //! Accesses the i-th value as defined by the accessor @@ -796,7 +803,9 @@ T Accessor::Indexer::GetValue(int i) { } inline Image::Image() : - width(0), height(0), mDataLength(0) { + width(0), + height(0), + mDataLength(0) { } inline void Image::Read(Value &obj, Asset &r) { @@ -1034,8 +1043,8 @@ inline int Compare(const char *attr, const char (&str)[N]) { } #ifdef _WIN32 -# pragma warning(push) -# pragma warning(disable : 4706) +#pragma warning(push) +#pragma warning(disable : 4706) #endif // _WIN32 inline bool GetAttribVector(Mesh::Primitive &p, const char *attr, Mesh::AccessorList *&v, int &pos) { @@ -1155,11 +1164,11 @@ inline void Mesh::Read(Value &pJSON_Object, Asset &pAsset_Root) { } Value *extras = FindObject(pJSON_Object, "extras"); - if (nullptr != extras ) { - if (Value* curTargetNames = FindArray(*extras, "targetNames")) { + if (nullptr != extras) { + if (Value *curTargetNames = FindArray(*extras, "targetNames")) { this->targetNames.resize(curTargetNames->Size()); for (unsigned int i = 0; i < curTargetNames->Size(); ++i) { - Value& targetNameValue = (*curTargetNames)[i]; + Value &targetNameValue = (*curTargetNames)[i]; if (targetNameValue.IsString()) { this->targetNames[i] = targetNameValue.GetString(); } @@ -1187,10 +1196,10 @@ inline void Camera::Read(Value &obj, Asset & /*r*/) { cameraProperties.perspective.zfar = MemberOrDefault(*it, "zfar", 100.f); cameraProperties.perspective.znear = MemberOrDefault(*it, "znear", 0.01f); } else { - cameraProperties.ortographic.xmag = MemberOrDefault(obj, "xmag", 1.f); - cameraProperties.ortographic.ymag = MemberOrDefault(obj, "ymag", 1.f); - cameraProperties.ortographic.zfar = MemberOrDefault(obj, "zfar", 100.f); - cameraProperties.ortographic.znear = MemberOrDefault(obj, "znear", 0.01f); + cameraProperties.ortographic.xmag = MemberOrDefault(*it, "xmag", 1.f); + cameraProperties.ortographic.ymag = MemberOrDefault(*it, "ymag", 1.f); + cameraProperties.ortographic.zfar = MemberOrDefault(*it, "zfar", 100.f); + cameraProperties.ortographic.znear = MemberOrDefault(*it, "znear", 0.01f); } } @@ -1264,9 +1273,11 @@ inline void Node::Read(Value &obj, Asset &r) { } } + // Do not retrieve a skin here, just take a reference, to avoid infinite recursion + // Skins will be properly loaded later Value *curSkin = FindUInt(obj, "skin"); if (nullptr != curSkin) { - this->skin = r.skins.Retrieve(curSkin->GetUint()); + this->skin = r.skins.Get(curSkin->GetUint()); } Value *curCamera = FindUInt(obj, "camera"); @@ -1557,7 +1568,6 @@ 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); @@ -1669,7 +1679,7 @@ inline std::string Asset::FindUniqueID(const std::string &str, const char *suffi } #ifdef _WIN32 -# pragma warning(pop) +#pragma warning(pop) #endif // _WIN32 } // namespace glTF2 diff --git a/code/AssetLib/glTF2/glTF2AssetWriter.inl b/code/AssetLib/glTF2/glTF2AssetWriter.inl index 6d7e28738..b0b7f9b37 100644 --- a/code/AssetLib/glTF2/glTF2AssetWriter.inl +++ b/code/AssetLib/glTF2/glTF2AssetWriter.inl @@ -639,7 +639,9 @@ namespace glTF2 { StringBuffer docBuffer; PrettyWriter writer(docBuffer); - mDoc.Accept(writer); + if (!mDoc.Accept(writer)) { + throw DeadlyExportError("Failed to write scene data!"); + } if (jsonOutFile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) { throw DeadlyExportError("Failed to write scene data!"); @@ -690,7 +692,9 @@ namespace glTF2 { StringBuffer docBuffer; Writer writer(docBuffer); - mDoc.Accept(writer); + if (!mDoc.Accept(writer)) { + throw DeadlyExportError("Failed to write scene data!"); + } uint32_t jsonChunkLength = (docBuffer.GetSize() + 3) & ~3; // Round up to next multiple of 4 auto paddingLength = jsonChunkLength - docBuffer.GetSize(); @@ -842,5 +846,3 @@ namespace glTF2 { } } - - diff --git a/code/AssetLib/glTF2/glTF2Exporter.cpp b/code/AssetLib/glTF2/glTF2Exporter.cpp index 41fe4c9c8..088503636 100644 --- a/code/AssetLib/glTF2/glTF2Exporter.cpp +++ b/code/AssetLib/glTF2/glTF2Exporter.cpp @@ -1,4 +1,4 @@ -/* +/* Open Asset Import Library (assimp) ---------------------------------------------------------------------- @@ -172,6 +172,13 @@ void SetAccessorRange(Ref acc, void* data, size_t count, for (unsigned int j = 0 ; j < numCompsOut ; j++) { double valueTmp = buffer_ptr[j]; + // Gracefully tolerate rogue NaN's in buffer data + // Any NaNs/Infs introduced in accessor bounds will end up in + // document and prevent rapidjson from writing out valid JSON + if (!std::isfinite(valueTmp)) { + continue; + } + if (valueTmp < acc->min[j]) { acc->min[j] = valueTmp; } @@ -493,7 +500,7 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref& texture, aiTe if (path[0] == '*') { // embedded aiTexture* curTex = mScene->mTextures[atoi(&path[1])]; - + texture->source->name = curTex->mFilename.C_Str(); // The asset has its own buffer, see Image::SetData @@ -896,7 +903,7 @@ void glTF2Exporter::ExportMeshes() // Normalize all normals as the validator can emit a warning otherwise if ( nullptr != aim->mNormals) { for ( auto i = 0u; i < aim->mNumVertices; ++i ) { - aim->mNormals[ i ].Normalize(); + aim->mNormals[ i ].NormalizeSafe(); } } @@ -907,7 +914,7 @@ void glTF2Exporter::ExportMeshes() for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { if (!aim->HasTextureCoords(i)) continue; - + // Flip UV y coords if (aim -> mNumUVComponents[i] > 1) { for (unsigned int j = 0; j < aim->mNumVertices; ++j) { diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 9f347f0a5..000ebad6c 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -66,6 +66,7 @@ SET( PUBLIC_HEADERS ${HEADER_PATH}/color4.h ${HEADER_PATH}/color4.inl ${CMAKE_CURRENT_BINARY_DIR}/../include/assimp/config.h + ${HEADER_PATH}/ColladaMetaData.h ${HEADER_PATH}/commonMetaData.h ${HEADER_PATH}/defs.h ${HEADER_PATH}/Defines.h diff --git a/code/Collada/ColladaExporter.cpp b/code/Collada/ColladaExporter.cpp new file mode 100644 index 000000000..05df6fc94 --- /dev/null +++ b/code/Collada/ColladaExporter.cpp @@ -0,0 +1,1704 @@ +/* +Open Asset Import Library (assimp) +---------------------------------------------------------------------- + +Copyright (c) 2006-2020, assimp team + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the +following conditions are met: + +* Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + +* Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the + following disclaimer in the documentation and/or other + materials provided with the distribution. + +* Neither the name of the assimp team, nor the names of its + contributors may be used to endorse or promote products + derived from this software without specific prior + written permission of the assimp team. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- +*/ + +#ifndef ASSIMP_BUILD_NO_EXPORT +#ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER + +#include "ColladaExporter.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +using namespace Assimp; + +namespace Assimp { + +// ------------------------------------------------------------------------------------------------ +// Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp +void ExportSceneCollada(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* /*pProperties*/) { + std::string path = DefaultIOSystem::absolutePath(std::string(pFile)); + std::string file = DefaultIOSystem::completeBaseName(std::string(pFile)); + + // invoke the exporter + ColladaExporter iDoTheExportThing( pScene, pIOSystem, path, file); + + if (iDoTheExportThing.mOutput.fail()) { + throw DeadlyExportError("output data creation failed. Most likely the file became too large: " + std::string(pFile)); + } + + // we're still here - export successfully completed. Write result to the given IOSYstem + std::unique_ptr outfile (pIOSystem->Open(pFile,"wt")); + if(outfile == NULL) { + throw DeadlyExportError("could not open output .dae file: " + std::string(pFile)); + } + + // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy. + outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast(iDoTheExportThing.mOutput.tellp()),1); +} + +} // end of namespace Assimp + +// ------------------------------------------------------------------------------------------------ +// Encodes a string into a valid XML ID using the xsd:ID schema qualifications. +static const std::string XMLIDEncode(const std::string& name) { + const char XML_ID_CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-."; + const unsigned int XML_ID_CHARS_COUNT = sizeof(XML_ID_CHARS) / sizeof(char); + + if (name.length() == 0) { + return name; + } + + std::stringstream idEncoded; + + // xsd:ID must start with letter or underscore + if (!((name[0] >= 'A' && name[0] <= 'z') || name[0] == '_')) { + idEncoded << '_'; + } + + for (std::string::const_iterator it = name.begin(); it != name.end(); ++it) { + // xsd:ID can only contain letters, digits, underscores, hyphens and periods + if (strchr(XML_ID_CHARS, *it) != nullptr) { + idEncoded << *it; + } else { + // Select placeholder character based on invalid character to prevent name collisions + idEncoded << XML_ID_CHARS[(*it) % XML_ID_CHARS_COUNT]; + } + } + + return idEncoded.str(); +} + +// ------------------------------------------------------------------------------------------------ +// Constructor for a specific scene to export +ColladaExporter::ColladaExporter( const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file) +: mIOSystem(pIOSystem) +, mPath(path) +, mFile(file) { + // make sure that all formatting happens using the standard, C locale and not the user's current locale + mOutput.imbue( std::locale("C") ); + mOutput.precision(ASSIMP_AI_REAL_TEXT_PRECISION); + + mScene = pScene; + mSceneOwned = false; + + // set up strings + endstr = "\n"; + + // start writing the file + WriteFile(); +} + +// ------------------------------------------------------------------------------------------------ +// Destructor +ColladaExporter::~ColladaExporter() { + if ( mSceneOwned ) { + delete mScene; + } +} + +// ------------------------------------------------------------------------------------------------ +// Starts writing the contents +void ColladaExporter::WriteFile() { + // write the DTD + mOutput << "" << endstr; + // COLLADA element start + mOutput << "" << endstr; + PushTag(); + + WriteTextures(); + WriteHeader(); + + WriteCamerasLibrary(); + WriteLightsLibrary(); + WriteMaterials(); + WriteGeometryLibrary(); + WriteControllerLibrary(); + + WriteSceneLibrary(); + + // customized, Writes the animation library + WriteAnimationsLibrary(); + + // useless Collada fu at the end, just in case we haven't had enough indirections, yet. + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "mRootNode->mName.C_Str()) + "\" />" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Writes the asset header +void ColladaExporter::WriteHeader() { + static const ai_real epsilon = Math::getEpsilon(); + static const aiQuaternion x_rot(aiMatrix3x3( + 0, -1, 0, + 1, 0, 0, + 0, 0, 1)); + static const aiQuaternion y_rot(aiMatrix3x3( + 1, 0, 0, + 0, 1, 0, + 0, 0, 1)); + static const aiQuaternion z_rot(aiMatrix3x3( + 1, 0, 0, + 0, 0, 1, + 0, -1, 0)); + + static const unsigned int date_nb_chars = 20; + char date_str[date_nb_chars]; + std::time_t date = std::time(NULL); + std::strftime(date_str, date_nb_chars, "%Y-%m-%dT%H:%M:%S", std::localtime(&date)); + + aiVector3D scaling; + aiQuaternion rotation; + aiVector3D position; + mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position); + rotation.Normalize(); + + bool add_root_node = false; + + ai_real scale = 1.0; + if(std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) { + scale = (ai_real) ((((double) scaling.x) + ((double) scaling.y) + ((double) scaling.z)) / 3.0); + } else { + add_root_node = true; + } + + std::string up_axis = "Y_UP"; + if(rotation.Equal(x_rot, epsilon)) { + up_axis = "X_UP"; + } else if(rotation.Equal(y_rot, epsilon)) { + up_axis = "Y_UP"; + } else if(rotation.Equal(z_rot, epsilon)) { + up_axis = "Z_UP"; + } else { + add_root_node = true; + } + + if(! position.Equal(aiVector3D(0, 0, 0))) { + add_root_node = true; + } + + if(mScene->mRootNode->mNumChildren == 0) { + add_root_node = true; + } + + if(add_root_node) { + aiScene* scene; + SceneCombiner::CopyScene(&scene, mScene); + + aiNode* root = new aiNode("Scene"); + + root->mNumChildren = 1; + root->mChildren = new aiNode*[root->mNumChildren]; + + root->mChildren[0] = scene->mRootNode; + scene->mRootNode->mParent = root; + scene->mRootNode = root; + + mScene = scene; + mSceneOwned = true; + + up_axis = "Y_UP"; + scale = 1.0; + } + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + + // If no Scene metadata, use root node metadata + aiMetadata* meta = mScene->mMetaData; + if (nullptr == meta) { + meta = mScene->mRootNode->mMetaData; + } + + aiString value; + if (!meta || !meta->Get("Author", value)) { + mOutput << startstr << "" << "Assimp" << "" << endstr; + } else { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + + if (nullptr == meta || !meta->Get(AI_METADATA_SOURCE_GENERATOR, value)) { + mOutput << startstr << "" << "Assimp Exporter" << "" << endstr; + } else { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + + if (meta) { + if (meta->Get("Comments", value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + if (meta->Get(AI_METADATA_SOURCE_COPYRIGHT, value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + if (meta->Get("SourceData", value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + } + + PopTag(); + mOutput << startstr << "" << endstr; + + if (nullptr == meta || !meta->Get("Created", value)) { + mOutput << startstr << "" << date_str << "" << endstr; + } else { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + + // Modified date is always the date saved + mOutput << startstr << "" << date_str << "" << endstr; + + if (meta) { + if (meta->Get("Keywords", value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + if (meta->Get("Revision", value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + if (meta->Get("Subject", value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + if (meta->Get("Title", value)) { + mOutput << startstr << "" << XMLEscape(value.C_Str()) << "" << endstr; + } + } + + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << up_axis << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Write the embedded textures +void ColladaExporter::WriteTextures() { + static const unsigned int buffer_size = 1024; + char str[buffer_size]; + + if (mScene->HasTextures()) { + for(unsigned int i = 0; i < mScene->mNumTextures; i++) { + // It would be great to be able to create a directory in portable standard C++, but it's not the case, + // so we just write the textures in the current directory. + + aiTexture* texture = mScene->mTextures[i]; + if ( nullptr == texture ) { + continue; + } + + ASSIMP_itoa10(str, buffer_size, i + 1); + + std::string name = mFile + "_texture_" + (i < 1000 ? "0" : "") + (i < 100 ? "0" : "") + (i < 10 ? "0" : "") + str + "." + ((const char*) texture->achFormatHint); + + std::unique_ptr outfile(mIOSystem->Open(mPath + mIOSystem->getOsSeparator() + name, "wb")); + if(outfile == NULL) { + throw DeadlyExportError("could not open output texture file: " + mPath + name); + } + + if(texture->mHeight == 0) { + outfile->Write((void*) texture->pcData, texture->mWidth, 1); + } else { + Bitmap::Save(texture, outfile.get()); + } + + outfile->Flush(); + + textures.insert(std::make_pair(i, name)); + } + } +} + +// ------------------------------------------------------------------------------------------------ +// Write the embedded textures +void ColladaExporter::WriteCamerasLibrary() { + if(mScene->HasCameras()) { + + mOutput << startstr << "" << endstr; + PushTag(); + + for( size_t a = 0; a < mScene->mNumCameras; ++a) + WriteCamera( a); + + PopTag(); + mOutput << startstr << "" << endstr; + + } +} + +void ColladaExporter::WriteCamera(size_t pIndex){ + + const aiCamera *cam = mScene->mCameras[pIndex]; + const std::string cameraName = XMLEscape(cam->mName.C_Str()); + const std::string cameraId = XMLIDEncode(cam->mName.C_Str()); + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + //assimp doesn't support the import of orthographic cameras! se we write + //always perspective + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << ""<< + AI_RAD_TO_DEG(cam->mHorizontalFOV) + <<"" << endstr; + mOutput << startstr << "" + << cam->mAspect + << "" << endstr; + mOutput << startstr << "" + << cam->mClipPlaneNear + << "" << endstr; + mOutput << startstr << "" + << cam->mClipPlaneFar + << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + +} + + +// ------------------------------------------------------------------------------------------------ +// Write the embedded textures +void ColladaExporter::WriteLightsLibrary() { + if(mScene->HasLights()) { + + mOutput << startstr << "" << endstr; + PushTag(); + + for( size_t a = 0; a < mScene->mNumLights; ++a) + WriteLight( a); + + PopTag(); + mOutput << startstr << "" << endstr; + + } +} + +void ColladaExporter::WriteLight(size_t pIndex){ + + const aiLight *light = mScene->mLights[pIndex]; + const std::string lightName = XMLEscape(light->mName.C_Str()); + const std::string lightId = XMLIDEncode(light->mName.C_Str()); + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + switch(light->mType){ + case aiLightSource_AMBIENT: + WriteAmbienttLight(light); + break; + case aiLightSource_DIRECTIONAL: + WriteDirectionalLight(light); + break; + case aiLightSource_POINT: + WritePointLight(light); + break; + case aiLightSource_SPOT: + WriteSpotLight(light); + break; + case aiLightSource_AREA: + case aiLightSource_UNDEFINED: + case _aiLightSource_Force32Bit: + break; + } + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + +} + +void ColladaExporter::WritePointLight(const aiLight *const light){ + const aiColor3D &color= light->mColorDiffuse; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + mOutput << startstr << "" + << light->mAttenuationConstant + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationLinear + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationQuadratic + <<"" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + +} + +void ColladaExporter::WriteDirectionalLight(const aiLight *const light){ + const aiColor3D &color= light->mColorDiffuse; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + +} + +void ColladaExporter::WriteSpotLight(const aiLight *const light){ + + const aiColor3D &color= light->mColorDiffuse; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + mOutput << startstr << "" + << light->mAttenuationConstant + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationLinear + <<"" << endstr; + mOutput << startstr << "" + << light->mAttenuationQuadratic + <<"" << endstr; + + const ai_real fallOffAngle = AI_RAD_TO_DEG(light->mAngleInnerCone); + mOutput << startstr <<"" + << fallOffAngle + <<"" << endstr; + double temp = light->mAngleOuterCone-light->mAngleInnerCone; + + temp = std::cos(temp); + temp = std::log(temp)/std::log(0.1); + temp = 1/temp; + mOutput << startstr << "" + << temp + <<"" << endstr; + + + PopTag(); + mOutput << startstr << "" << endstr; + +} + +void ColladaExporter::WriteAmbienttLight(const aiLight *const light){ + + const aiColor3D &color= light->mColorAmbient; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" + << color.r<<" "<" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Reads a single surface entry from the given material keys +void ColladaExporter::ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, + aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex) { + if( pSrcMat->GetTextureCount( pTexture) > 0 ) { + aiString texfile; + unsigned int uvChannel = 0; + pSrcMat->GetTexture( pTexture, 0, &texfile, NULL, &uvChannel); + + std::string index_str(texfile.C_Str()); + + if(index_str.size() != 0 && index_str[0] == '*') { + unsigned int index; + + index_str = index_str.substr(1, std::string::npos); + + try { + index = (unsigned int) strtoul10_64(index_str.c_str()); + } catch(std::exception& error) { + throw DeadlyExportError(error.what()); + } + + std::map::const_iterator name = textures.find(index); + + if(name != textures.end()) { + poSurface.texture = name->second; + } else { + throw DeadlyExportError("could not find embedded texture at index " + index_str); + } + } else { + poSurface.texture = texfile.C_Str(); + } + + poSurface.channel = uvChannel; + poSurface.exist = true; + } else { + if( pKey ) + poSurface.exist = pSrcMat->Get( pKey, static_cast(pType), static_cast(pIndex), poSurface.color) == aiReturn_SUCCESS; + } +} + +static bool isalnum_C(char c) { + return ( nullptr != strchr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",c) ); +} + +// ------------------------------------------------------------------------------------------------ +// Writes an image entry for the given surface +void ColladaExporter::WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd) { + if( !pSurface.texture.empty() ) + { + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << ""; + + // URL encode image file name first, then XML encode on top + std::stringstream imageUrlEncoded; + for( std::string::const_iterator it = pSurface.texture.begin(); it != pSurface.texture.end(); ++it ) + { + if( isalnum_C( (unsigned char) *it) || *it == ':' || *it == '_' || *it == '-' || *it == '.' || *it == '/' || *it == '\\' ) + imageUrlEncoded << *it; + else + imageUrlEncoded << '%' << std::hex << size_t( (unsigned char) *it) << std::dec; + } + mOutput << XMLEscape(imageUrlEncoded.str()); + mOutput << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + } +} + +// ------------------------------------------------------------------------------------------------ +// Writes a color-or-texture entry into an effect definition +void ColladaExporter::WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName) +{ + if(pSurface.exist) { + mOutput << startstr << "<" << pTypeName << ">" << endstr; + PushTag(); + if( pSurface.texture.empty() ) + { + mOutput << startstr << "" << pSurface.color.r << " " << pSurface.color.g << " " << pSurface.color.b << " " << pSurface.color.a << "" << endstr; + } + else + { + mOutput << startstr << "" << endstr; + } + PopTag(); + mOutput << startstr << "" << endstr; + } +} + +// ------------------------------------------------------------------------------------------------ +// Writes the two parameters necessary for referencing a texture in an effect entry +void ColladaExporter::WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName) +{ + // if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture + if( !pSurface.texture.empty() ) + { + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << XMLIDEncode(pMatName) << "-" << pTypeName << "-image" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << XMLIDEncode(pMatName) << "-" << pTypeName << "-surface" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + } +} + +// ------------------------------------------------------------------------------------------------ +// Writes a scalar property +void ColladaExporter::WriteFloatEntry( const Property& pProperty, const std::string& pTypeName) +{ + if(pProperty.exist) { + mOutput << startstr << "<" << pTypeName << ">" << endstr; + PushTag(); + mOutput << startstr << "" << pProperty.value << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + } +} + +// ------------------------------------------------------------------------------------------------ +// Writes the material setup +void ColladaExporter::WriteMaterials() +{ + materials.resize( mScene->mNumMaterials); + + /// collect all materials from the scene + size_t numTextures = 0; + for( size_t a = 0; a < mScene->mNumMaterials; ++a ) + { + const aiMaterial* mat = mScene->mMaterials[a]; + + aiString name; + if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS ) { + name = "mat"; + materials[a].name = std::string( "m") + to_string(a) + name.C_Str(); + } else { + // try to use the material's name if no other material has already taken it, else append # + std::string testName = name.C_Str(); + size_t materialCountWithThisName = 0; + for( size_t i = 0; i < a; i ++ ) { + if( materials[i].name == testName ) { + materialCountWithThisName ++; + } + } + if( materialCountWithThisName == 0 ) { + materials[a].name = name.C_Str(); + } else { + materials[a].name = std::string(name.C_Str()) + to_string(materialCountWithThisName); + } + } + + aiShadingMode shading = aiShadingMode_Flat; + materials[a].shading_model = "phong"; + if(mat->Get( AI_MATKEY_SHADING_MODEL, shading) == aiReturn_SUCCESS) { + if(shading == aiShadingMode_Phong) { + materials[a].shading_model = "phong"; + } else if(shading == aiShadingMode_Blinn) { + materials[a].shading_model = "blinn"; + } else if(shading == aiShadingMode_NoShading) { + materials[a].shading_model = "constant"; + } else if(shading == aiShadingMode_Gouraud) { + materials[a].shading_model = "lambert"; + } + } + + ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT); + if( !materials[a].ambient.texture.empty() ) numTextures++; + ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE); + if( !materials[a].diffuse.texture.empty() ) numTextures++; + ReadMaterialSurface( materials[a].specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR); + if( !materials[a].specular.texture.empty() ) numTextures++; + ReadMaterialSurface( materials[a].emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE); + if( !materials[a].emissive.texture.empty() ) numTextures++; + ReadMaterialSurface( materials[a].reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE); + if( !materials[a].reflective.texture.empty() ) numTextures++; + ReadMaterialSurface( materials[a].transparent, mat, aiTextureType_OPACITY, AI_MATKEY_COLOR_TRANSPARENT); + if( !materials[a].transparent.texture.empty() ) numTextures++; + ReadMaterialSurface( materials[a].normal, mat, aiTextureType_NORMALS, NULL, 0, 0); + if( !materials[a].normal.texture.empty() ) numTextures++; + + materials[a].shininess.exist = mat->Get( AI_MATKEY_SHININESS, materials[a].shininess.value) == aiReturn_SUCCESS; + materials[a].transparency.exist = mat->Get( AI_MATKEY_OPACITY, materials[a].transparency.value) == aiReturn_SUCCESS; + materials[a].index_refraction.exist = mat->Get( AI_MATKEY_REFRACTI, materials[a].index_refraction.value) == aiReturn_SUCCESS; + } + + // output textures if present + if( numTextures > 0 ) + { + mOutput << startstr << "" << endstr; + PushTag(); + for( std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it ) + { + const Material& mat = *it; + WriteImageEntry( mat.ambient, mat.name + "-ambient-image"); + WriteImageEntry( mat.diffuse, mat.name + "-diffuse-image"); + WriteImageEntry( mat.specular, mat.name + "-specular-image"); + WriteImageEntry( mat.emissive, mat.name + "-emission-image"); + WriteImageEntry( mat.reflective, mat.name + "-reflective-image"); + WriteImageEntry( mat.transparent, mat.name + "-transparent-image"); + WriteImageEntry( mat.normal, mat.name + "-normal-image"); + } + PopTag(); + mOutput << startstr << "" << endstr; + } + + // output effects - those are the actual carriers of information + if( !materials.empty() ) + { + mOutput << startstr << "" << endstr; + PushTag(); + for( std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it ) + { + const Material& mat = *it; + // this is so ridiculous it must be right + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + + // write sampler- and surface params for the texture entries + WriteTextureParamEntry( mat.emissive, "emission", mat.name); + WriteTextureParamEntry( mat.ambient, "ambient", mat.name); + WriteTextureParamEntry( mat.diffuse, "diffuse", mat.name); + WriteTextureParamEntry( mat.specular, "specular", mat.name); + WriteTextureParamEntry( mat.reflective, "reflective", mat.name); + WriteTextureParamEntry( mat.transparent, "transparent", mat.name); + WriteTextureParamEntry( mat.normal, "normal", mat.name); + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "<" << mat.shading_model << ">" << endstr; + PushTag(); + + WriteTextureColorEntry( mat.emissive, "emission", mat.name + "-emission-sampler"); + WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "-ambient-sampler"); + WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "-diffuse-sampler"); + WriteTextureColorEntry( mat.specular, "specular", mat.name + "-specular-sampler"); + WriteFloatEntry(mat.shininess, "shininess"); + WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "-reflective-sampler"); + WriteTextureColorEntry( mat.transparent, "transparent", mat.name + "-transparent-sampler"); + WriteFloatEntry(mat.transparency, "transparency"); + WriteFloatEntry(mat.index_refraction, "index_of_refraction"); + + if(! mat.normal.texture.empty()) { + WriteTextureColorEntry( mat.normal, "bump", mat.name + "-normal-sampler"); + } + + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + } + PopTag(); + mOutput << startstr << "" << endstr; + + // write materials - they're just effect references + mOutput << startstr << "" << endstr; + PushTag(); + for( std::vector::const_iterator it = materials.begin(); it != materials.end(); ++it ) + { + const Material& mat = *it; + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + } + PopTag(); + mOutput << startstr << "" << endstr; + } +} + +// ------------------------------------------------------------------------------------------------ +// Writes the controller library +void ColladaExporter::WriteControllerLibrary() +{ + mOutput << startstr << "" << endstr; + PushTag(); + + for( size_t a = 0; a < mScene->mNumMeshes; ++a) { + WriteController( a); + } + + PopTag(); + mOutput << startstr << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Writes a skin controller of the given mesh +void ColladaExporter::WriteController( size_t pIndex) +{ + const aiMesh* mesh = mScene->mMeshes[pIndex]; + const std::string idstr = mesh->mName.length == 0 ? GetMeshId(pIndex) : mesh->mName.C_Str(); + const std::string idstrEscaped = XMLIDEncode(idstr); + + if ( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 ) + return; + + if ( mesh->mNumBones == 0 ) + return; + + mOutput << startstr << ""<< endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + PushTag(); + + // bind pose matrix + mOutput << startstr << "" << endstr; + PushTag(); + + // I think it is identity in general cases. + aiMatrix4x4 mat; + mOutput << startstr << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << endstr; + mOutput << startstr << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << endstr; + mOutput << startstr << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << endstr; + mOutput << startstr << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4 << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "mNumBones << "\">"; + + for( size_t i = 0; i < mesh->mNumBones; ++i ) + mOutput << XMLIDEncode(mesh->mBones[i]->mName.C_Str()) << " "; + + mOutput << "" << endstr; + + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "mNumBones << "\" stride=\"" << 1 << "\">" << endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + std::vector bind_poses; + bind_poses.reserve(mesh->mNumBones * 16); + for(unsigned int i = 0; i < mesh->mNumBones; ++i) + for( unsigned int j = 0; j < 4; ++j) + bind_poses.insert(bind_poses.end(), mesh->mBones[i]->mOffsetMatrix[j], mesh->mBones[i]->mOffsetMatrix[j] + 4); + + WriteFloatArray( idstr + "-skin-bind_poses", FloatType_Mat4x4, (const ai_real*) bind_poses.data(), bind_poses.size() / 16); + + bind_poses.clear(); + + std::vector skin_weights; + skin_weights.reserve(mesh->mNumVertices * mesh->mNumBones); + for( size_t i = 0; i < mesh->mNumBones; ++i) + for( size_t j = 0; j < mesh->mBones[i]->mNumWeights; ++j) + skin_weights.push_back(mesh->mBones[i]->mWeights[j].mWeight); + + WriteFloatArray( idstr + "-skin-weights", FloatType_Weight, (const ai_real*) skin_weights.data(), skin_weights.size()); + + skin_weights.clear(); + + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + mOutput << startstr << "mNumVertices << "\">" << endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + + mOutput << startstr << ""; + + std::vector num_influences(mesh->mNumVertices, (ai_uint)0); + for( size_t i = 0; i < mesh->mNumBones; ++i) + for( size_t j = 0; j < mesh->mBones[i]->mNumWeights; ++j) + ++num_influences[mesh->mBones[i]->mWeights[j].mVertexId]; + + for( size_t i = 0; i < mesh->mNumVertices; ++i) + mOutput << num_influences[i] << " "; + + mOutput << "" << endstr; + + mOutput << startstr << ""; + + ai_uint joint_weight_indices_length = 0; + std::vector accum_influences; + accum_influences.reserve(num_influences.size()); + for( size_t i = 0; i < num_influences.size(); ++i) + { + accum_influences.push_back(joint_weight_indices_length); + joint_weight_indices_length += num_influences[i]; + } + + ai_uint weight_index = 0; + std::vector joint_weight_indices(2 * joint_weight_indices_length, (ai_int)-1); + for( unsigned int i = 0; i < mesh->mNumBones; ++i) + for( unsigned j = 0; j < mesh->mBones[i]->mNumWeights; ++j) + { + unsigned int vId = mesh->mBones[i]->mWeights[j].mVertexId; + for( ai_uint k = 0; k < num_influences[vId]; ++k) + { + if (joint_weight_indices[2 * (accum_influences[vId] + k)] == -1) + { + joint_weight_indices[2 * (accum_influences[vId] + k)] = i; + joint_weight_indices[2 * (accum_influences[vId] + k) + 1] = weight_index; + break; + } + } + ++weight_index; + } + + for( size_t i = 0; i < joint_weight_indices.size(); ++i) + mOutput << joint_weight_indices[i] << " "; + + num_influences.clear(); + accum_influences.clear(); + joint_weight_indices.clear(); + + mOutput << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Writes the geometry library +void ColladaExporter::WriteGeometryLibrary() +{ + mOutput << startstr << "" << endstr; + PushTag(); + + for( size_t a = 0; a < mScene->mNumMeshes; ++a) + WriteGeometry( a); + + PopTag(); + mOutput << startstr << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Writes the given mesh +void ColladaExporter::WriteGeometry( size_t pIndex) +{ + const aiMesh* mesh = mScene->mMeshes[pIndex]; + const std::string idstr = mesh->mName.length == 0 ? GetMeshId(pIndex) : mesh->mName.C_Str(); + const std::string geometryName = XMLEscape(idstr); + const std::string geometryId = XMLIDEncode(idstr); + + if ( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 ) + return; + + // opening tag + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + PushTag(); + + // Positions + WriteFloatArray( idstr + "-positions", FloatType_Vector, (ai_real*) mesh->mVertices, mesh->mNumVertices); + // Normals, if any + if( mesh->HasNormals() ) + WriteFloatArray( idstr + "-normals", FloatType_Vector, (ai_real*) mesh->mNormals, mesh->mNumVertices); + + // texture coords + for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) + { + if( mesh->HasTextureCoords(static_cast(a)) ) + { + WriteFloatArray( idstr + "-tex" + to_string(a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2, + (ai_real*) mesh->mTextureCoords[a], mesh->mNumVertices); + } + } + + // vertex colors + for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) + { + if( mesh->HasVertexColors(static_cast(a)) ) + WriteFloatArray( idstr + "-color" + to_string(a), FloatType_Color, (ai_real*) mesh->mColors[a], mesh->mNumVertices); + } + + // assemble vertex structure + // Only write input for POSITION since we will write other as shared inputs in polygon definition + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + + // count the number of lines, triangles and polygon meshes + int countLines = 0; + int countPoly = 0; + for( size_t a = 0; a < mesh->mNumFaces; ++a ) + { + if (mesh->mFaces[a].mNumIndices == 2) countLines++; + else if (mesh->mFaces[a].mNumIndices >= 3) countPoly++; + } + + // lines + if (countLines) + { + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + if( mesh->HasNormals() ) + mOutput << startstr << "" << endstr; + for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a ) + { + if( mesh->HasTextureCoords(static_cast(a)) ) + mOutput << startstr << "" << endstr; + } + for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a ) + { + if( mesh->HasVertexColors(static_cast(a) ) ) + mOutput << startstr << "" << endstr; + } + + mOutput << startstr << "

"; + for( size_t a = 0; a < mesh->mNumFaces; ++a ) + { + const aiFace& face = mesh->mFaces[a]; + if (face.mNumIndices != 2) continue; + for( size_t b = 0; b < face.mNumIndices; ++b ) + mOutput << face.mIndices[b] << " "; + } + mOutput << "

" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + } + + // triangle - don't use it, because compatibility problems + + // polygons + if (countPoly) + { + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + if( mesh->HasNormals() ) + mOutput << startstr << "" << endstr; + for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a ) + { + if( mesh->HasTextureCoords(static_cast(a)) ) + mOutput << startstr << "" << endstr; + } + for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a ) + { + if( mesh->HasVertexColors(static_cast(a) ) ) + mOutput << startstr << "" << endstr; + } + + mOutput << startstr << ""; + for( size_t a = 0; a < mesh->mNumFaces; ++a ) + { + if (mesh->mFaces[a].mNumIndices < 3) continue; + mOutput << mesh->mFaces[a].mNumIndices << " "; + } + mOutput << "" << endstr; + + mOutput << startstr << "

"; + for( size_t a = 0; a < mesh->mNumFaces; ++a ) + { + const aiFace& face = mesh->mFaces[a]; + if (face.mNumIndices < 3) continue; + for( size_t b = 0; b < face.mNumIndices; ++b ) + mOutput << face.mIndices[b] << " "; + } + mOutput << "

" << endstr; + PopTag(); + mOutput << startstr << "
" << endstr; + } + + // closing tags + PopTag(); + mOutput << startstr << "
" << endstr; + PopTag(); + mOutput << startstr << "
" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Writes a float array of the given type +void ColladaExporter::WriteFloatArray( const std::string& pIdString, FloatDataType pType, const ai_real* pData, size_t pElementCount) +{ + size_t floatsPerElement = 0; + switch( pType ) + { + case FloatType_Vector: floatsPerElement = 3; break; + case FloatType_TexCoord2: floatsPerElement = 2; break; + case FloatType_TexCoord3: floatsPerElement = 3; break; + case FloatType_Color: floatsPerElement = 3; break; + case FloatType_Mat4x4: floatsPerElement = 16; break; + case FloatType_Weight: floatsPerElement = 1; break; + case FloatType_Time: floatsPerElement = 1; break; + default: + return; + } + + std::string arrayId = XMLIDEncode(pIdString) + "-array"; + + mOutput << startstr << "" << endstr; + PushTag(); + + // source array + mOutput << startstr << " "; + PushTag(); + + if( pType == FloatType_TexCoord2 ) + { + for( size_t a = 0; a < pElementCount; ++a ) + { + mOutput << pData[a*3+0] << " "; + mOutput << pData[a*3+1] << " "; + } + } + else if( pType == FloatType_Color ) + { + for( size_t a = 0; a < pElementCount; ++a ) + { + mOutput << pData[a*4+0] << " "; + mOutput << pData[a*4+1] << " "; + mOutput << pData[a*4+2] << " "; + } + } + else + { + for( size_t a = 0; a < pElementCount * floatsPerElement; ++a ) + mOutput << pData[a] << " "; + } + mOutput << "" << endstr; + PopTag(); + + // the usual Collada fun. Let's bloat it even more! + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + + switch( pType ) + { + case FloatType_Vector: + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + break; + + case FloatType_TexCoord2: + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + break; + + case FloatType_TexCoord3: + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + break; + + case FloatType_Color: + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + mOutput << startstr << "" << endstr; + break; + + case FloatType_Mat4x4: + mOutput << startstr << "" << endstr; + break; + + case FloatType_Weight: + mOutput << startstr << "" << endstr; + break; + + // customized, add animation related + case FloatType_Time: + mOutput << startstr << "" << endstr; + break; + + } + + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; +} + +// ------------------------------------------------------------------------------------------------ +// Writes the scene library +void ColladaExporter::WriteSceneLibrary() +{ + const std::string sceneName = XMLEscape(mScene->mRootNode->mName.C_Str()); + const std::string sceneId = XMLIDEncode(mScene->mRootNode->mName.C_Str()); + + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + + // start recursive write at the root node + for( size_t a = 0; a < mScene->mRootNode->mNumChildren; ++a ) + WriteNode( mScene, mScene->mRootNode->mChildren[a]); + + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; +} +// ------------------------------------------------------------------------------------------------ +void ColladaExporter::WriteAnimationLibrary(size_t pIndex) +{ + static const float kSecondsFromMilliseconds = .001f; + + const aiAnimation * anim = mScene->mAnimations[pIndex]; + + if ( anim->mNumChannels == 0 && anim->mNumMeshChannels == 0 && anim->mNumMorphMeshChannels ==0 ) { + return; + } + + const std::string animation_name_escaped = XMLEscape( anim->mName.C_Str() ); + std::string idstr = anim->mName.C_Str(); + std::string ending = std::string( "AnimId" ) + to_string(pIndex); + if (idstr.length() >= ending.length()) { + if (0 != idstr.compare (idstr.length() - ending.length(), ending.length(), ending)) { + idstr = idstr + ending; + } + } else { + idstr = idstr + ending; + } + + const std::string idstrEscaped = XMLIDEncode(idstr); + + mOutput << startstr << "" << endstr; + PushTag(); + + std::string cur_node_idstr; + for (size_t a = 0; a < anim->mNumChannels; ++a) { + const aiNodeAnim * nodeAnim = anim->mChannels[a]; + + // sanity check + if (nodeAnim->mNumPositionKeys != nodeAnim->mNumScalingKeys || nodeAnim->mNumPositionKeys != nodeAnim->mNumRotationKeys) { + continue; + } + + { + cur_node_idstr.clear(); + cur_node_idstr += nodeAnim->mNodeName.data; + cur_node_idstr += std::string("_matrix-input"); + + std::vector frames; + for( size_t i = 0; i < nodeAnim->mNumPositionKeys; ++i) { + frames.push_back(static_cast(nodeAnim->mPositionKeys[i].mTime) * kSecondsFromMilliseconds); + } + + WriteFloatArray(cur_node_idstr, FloatType_Time, (const ai_real *)frames.data(), frames.size()); + frames.clear(); + } + + { + cur_node_idstr.clear(); + + cur_node_idstr += nodeAnim->mNodeName.data; + cur_node_idstr += std::string("_matrix-output"); + + std::vector keyframes; + keyframes.reserve(nodeAnim->mNumPositionKeys * 16); + for( size_t i = 0; i < nodeAnim->mNumPositionKeys; ++i) { + aiVector3D Scaling = nodeAnim->mScalingKeys[i].mValue; + aiMatrix4x4 ScalingM; // identity + ScalingM[0][0] = Scaling.x; ScalingM[1][1] = Scaling.y; ScalingM[2][2] = Scaling.z; + + aiQuaternion RotationQ = nodeAnim->mRotationKeys[i].mValue; + aiMatrix4x4 s = aiMatrix4x4( RotationQ.GetMatrix() ); + aiMatrix4x4 RotationM(s.a1, s.a2, s.a3, 0, s.b1, s.b2, s.b3, 0, s.c1, s.c2, s.c3, 0, 0, 0, 0, 1); + + aiVector3D Translation = nodeAnim->mPositionKeys[i].mValue; + aiMatrix4x4 TranslationM; // identity + TranslationM[0][3] = Translation.x; TranslationM[1][3] = Translation.y; TranslationM[2][3] = Translation.z; + + // Combine the above transformations + aiMatrix4x4 mat = TranslationM * RotationM * ScalingM; + + for( unsigned int j = 0; j < 4; ++j) { + keyframes.insert(keyframes.end(), mat[j], mat[j] + 4); + } + } + + WriteFloatArray(cur_node_idstr, FloatType_Mat4x4, (const ai_real *)keyframes.data(), keyframes.size() / 16); + } + + { + std::vector names; + for ( size_t i = 0; i < nodeAnim->mNumPositionKeys; ++i) { + if ( nodeAnim->mPreState == aiAnimBehaviour_DEFAULT + || nodeAnim->mPreState == aiAnimBehaviour_LINEAR + || nodeAnim->mPreState == aiAnimBehaviour_REPEAT + ) { + names.push_back( "LINEAR" ); + } else if (nodeAnim->mPostState == aiAnimBehaviour_CONSTANT) { + names.push_back( "STEP" ); + } + } + + const std::string cur_node_idstr2 = nodeAnim->mNodeName.data + std::string("_matrix-interpolation"); + std::string arrayId = XMLIDEncode(cur_node_idstr2) + "-array"; + + mOutput << startstr << "" << endstr; + PushTag(); + + // source array + mOutput << startstr << " "; + for( size_t aa = 0; aa < names.size(); ++aa ) { + mOutput << names[aa] << " "; + } + mOutput << "" << endstr; + + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + } + } + + for (size_t a = 0; a < anim->mNumChannels; ++a) { + const aiNodeAnim * nodeAnim = anim->mChannels[a]; + + { + // samplers + const std::string node_idstr = nodeAnim->mNodeName.data + std::string("_matrix-sampler"); + mOutput << startstr << "" << endstr; + PushTag(); + + mOutput << startstr << "mNodeName.data + std::string("_matrix-input") ) << "\"/>" << endstr; + mOutput << startstr << "mNodeName.data + std::string("_matrix-output") ) << "\"/>" << endstr; + mOutput << startstr << "mNodeName.data + std::string("_matrix-interpolation") ) << "\"/>" << endstr; + + PopTag(); + mOutput << startstr << "" << endstr; + } + } + + for (size_t a = 0; a < anim->mNumChannels; ++a) { + const aiNodeAnim * nodeAnim = anim->mChannels[a]; + + { + // channels + mOutput << startstr << "mNodeName.data + std::string("_matrix-sampler") ) << "\" target=\"" << XMLIDEncode(nodeAnim->mNodeName.data) << "/matrix\"/>" << endstr; + } + } + + PopTag(); + mOutput << startstr << "" << endstr; + +} +// ------------------------------------------------------------------------------------------------ +void ColladaExporter::WriteAnimationsLibrary() +{ + if ( mScene->mNumAnimations > 0 ) { + mOutput << startstr << "" << endstr; + PushTag(); + + // start recursive write at the root node + for( size_t a = 0; a < mScene->mNumAnimations; ++a) + WriteAnimationLibrary( a ); + + PopTag(); + mOutput << startstr << "" << endstr; + } +} +// ------------------------------------------------------------------------------------------------ +// Helper to find a bone by name in the scene +aiBone* findBone( const aiScene* scene, const char * name) { + for (size_t m=0; mmNumMeshes; m++) { + aiMesh * mesh = scene->mMeshes[m]; + for (size_t b=0; bmNumBones; b++) { + aiBone * bone = mesh->mBones[b]; + if (0 == strcmp(name, bone->mName.C_Str())) { + return bone; + } + } + } + return NULL; +} + +// ------------------------------------------------------------------------------------------------ +const aiNode * findBoneNode( const aiNode* aNode, const aiBone* bone) +{ + if ( aNode && bone && aNode->mName == bone->mName ) { + return aNode; + } + + if ( aNode && bone ) { + for (unsigned int i=0; i < aNode->mNumChildren; ++i) { + aiNode * aChild = aNode->mChildren[i]; + const aiNode * foundFromChild = 0; + if ( aChild ) { + foundFromChild = findBoneNode( aChild, bone ); + if ( foundFromChild ) return foundFromChild; + } + } + } + + return NULL; +} + +const aiNode * findSkeletonRootNode( const aiScene* scene, const aiMesh * mesh) +{ + std::set topParentBoneNodes; + if ( mesh && mesh->mNumBones > 0 ) { + for (unsigned int i=0; i < mesh->mNumBones; ++i) { + aiBone * bone = mesh->mBones[i]; + + const aiNode * node = findBoneNode( scene->mRootNode, bone); + if ( node ) { + while ( node->mParent && findBone(scene, node->mParent->mName.C_Str() ) != 0 ) { + node = node->mParent; + } + topParentBoneNodes.insert( node ); + } + } + } + + if ( !topParentBoneNodes.empty() ) { + const aiNode * parentBoneNode = *topParentBoneNodes.begin(); + if ( topParentBoneNodes.size() == 1 ) { + return parentBoneNode; + } else { + for (auto it : topParentBoneNodes) { + if ( it->mParent ) return it->mParent; + } + return parentBoneNode; + } + } + + return NULL; +} + +// ------------------------------------------------------------------------------------------------ +// Recursively writes the given node +void ColladaExporter::WriteNode( const aiScene* pScene, aiNode* pNode) +{ + // the node must have a name + if (pNode->mName.length == 0) + { + std::stringstream ss; + ss << "Node_" << pNode; + pNode->mName.Set(ss.str()); + } + + // If the node is associated with a bone, it is a joint node (JOINT) + // otherwise it is a normal node (NODE) + const char * node_type; + bool is_joint, is_skeleton_root = false; + if (nullptr == findBone(pScene, pNode->mName.C_Str())) { + node_type = "NODE"; + is_joint = false; + } else { + node_type = "JOINT"; + is_joint = true; + if (!pNode->mParent || nullptr == findBone(pScene, pNode->mParent->mName.C_Str())) { + is_skeleton_root = true; + } + } + + const std::string node_id = XMLIDEncode(pNode->mName.data); + const std::string node_name = XMLEscape(pNode->mName.data); + mOutput << startstr << "" << endstr; + PushTag(); + + // write transformation - we can directly put the matrix there + // TODO: (thom) decompose into scale - rot - quad to allow addressing it by animations afterwards + aiMatrix4x4 mat = pNode->mTransformation; + + // If this node is a Camera node, the camera coordinate system needs to be multiplied in. + // When importing from Collada, the mLookAt is set to 0, 0, -1, and the node transform is unchanged. + // When importing from a different format, mLookAt is set to 0, 0, 1. Therefore, the local camera + // coordinate system must be changed to matche the Collada specification. + for (size_t i = 0; imNumCameras; i++){ + if (mScene->mCameras[i]->mName == pNode->mName){ + aiMatrix4x4 sourceView; + mScene->mCameras[i]->GetCameraMatrix(sourceView); + + aiMatrix4x4 colladaView; + colladaView.a1 = colladaView.c3 = -1; // move into -z space. + mat *= (sourceView * colladaView); + break; + } + } + + // customized, sid should be 'matrix' to match with loader code. + //mOutput << startstr << ""; + mOutput << startstr << ""; + + mOutput << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << " "; + mOutput << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << " "; + mOutput << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << " "; + mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4; + mOutput << "" << endstr; + + if(pNode->mNumMeshes==0){ + //check if it is a camera node + for(size_t i=0; imNumCameras; i++){ + if(mScene->mCameras[i]->mName == pNode->mName){ + mOutput << startstr <<"" << endstr; + break; + } + } + //check if it is a light node + for(size_t i=0; imNumLights; i++){ + if(mScene->mLights[i]->mName == pNode->mName){ + mOutput << startstr <<"" << endstr; + break; + } + } + + }else + // instance every geometry + for( size_t a = 0; a < pNode->mNumMeshes; ++a ) + { + const aiMesh* mesh = mScene->mMeshes[pNode->mMeshes[a]]; + // do not instantiate mesh if empty. I wonder how this could happen + if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 ) + continue; + + const std::string meshName = mesh->mName.length == 0 ? GetMeshId(pNode->mMeshes[a]) : mesh->mName.C_Str(); + + if( mesh->mNumBones == 0 ) + { + mOutput << startstr << "" << endstr; + PushTag(); + } + else + { + mOutput << startstr + << "" + << endstr; + PushTag(); + + // note! this mFoundSkeletonRootNodeID some how affects animation, it makes the mesh attaches to armature skeleton root node. + // use the first bone to find skeleton root + const aiNode * skeletonRootBoneNode = findSkeletonRootNode( pScene, mesh ); + if ( skeletonRootBoneNode ) { + mFoundSkeletonRootNodeID = XMLIDEncode( skeletonRootBoneNode->mName.C_Str() ); + } + mOutput << startstr << "#" << mFoundSkeletonRootNodeID << "" << endstr; + } + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "" << endstr; + PushTag(); + mOutput << startstr << "mMaterialIndex].name) << "\">" << endstr; + PushTag(); + for( size_t aa = 0; aa < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++aa ) + { + if( mesh->HasTextureCoords( static_cast(aa) ) ) + // semantic as in + // input_semantic as in + // input_set as in + mOutput << startstr << "" << endstr; + } + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + PopTag(); + mOutput << startstr << "" << endstr; + + PopTag(); + if( mesh->mNumBones == 0) + mOutput << startstr << "" << endstr; + else + mOutput << startstr << "" << endstr; + } + + // recurse into subnodes + for( size_t a = 0; a < pNode->mNumChildren; ++a ) + WriteNode( pScene, pNode->mChildren[a]); + + PopTag(); + mOutput << startstr << "" << endstr; +} + +#endif +#endif diff --git a/include/assimp/ColladaMetaData.h b/include/assimp/ColladaMetaData.h new file mode 100644 index 000000000..82aee78d0 --- /dev/null +++ b/include/assimp/ColladaMetaData.h @@ -0,0 +1,53 @@ +/* +Open Asset Import Library (assimp) +---------------------------------------------------------------------- + +Copyright (c) 2006-2020, assimp team + + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the +following conditions are met: + +* Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + +* Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the + following disclaimer in the documentation and/or other + materials provided with the distribution. + +* Neither the name of the assimp team, nor the names of its + contributors may be used to endorse or promote products + derived from this software without specific prior + written permission of the assimp team. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +---------------------------------------------------------------------- +*/ + +/** @file ColladaMetaData.h + * Declares common metadata constants used by Collada files + */ +#pragma once +#ifndef AI_COLLADAMETADATA_H_INC +#define AI_COLLADAMETADATA_H_INC + +#define AI_METADATA_COLLADA_ID "Collada_id" +#define AI_METADATA_COLLADA_SID "Collada_sid" + +#endif diff --git a/include/assimp/config.h.in b/include/assimp/config.h.in index e2f2a3888..c26dcc77f 100644 --- a/include/assimp/config.h.in +++ b/include/assimp/config.h.in @@ -1030,10 +1030,10 @@ enum aiComponent #define AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION "IMPORT_COLLADA_IGNORE_UP_DIRECTION" // --------------------------------------------------------------------------- -/** @brief Specifies whether the Collada loader should use Collada names as node names. +/** @brief Specifies whether the Collada loader should use Collada names. * - * If this property is set to true, the Collada names will be used as the - * node name. The default is to use the id tag (resp. sid tag, if no id tag is present) + * If this property is set to true, the Collada names will be used as the node and + * mesh names. The default is to use the id tag (resp. sid tag, if no id tag is present) * instead. * Property type: Bool. Default value: false. */ diff --git a/port/assimp_rs/Cargo.lock b/port/assimp_rs/Cargo.lock new file mode 100644 index 000000000..4f571f362 --- /dev/null +++ b/port/assimp_rs/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "assimp_rs" +version = "0.1.0" + diff --git a/port/assimp_rs/Cargo.toml b/port/assimp_rs/Cargo.toml new file mode 100644 index 000000000..073a2b283 --- /dev/null +++ b/port/assimp_rs/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "assimp_rs" +version = "0.1.0" +authors = ["David Golembiowski "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/port/assimp_rs/src/camera/mod.rs b/port/assimp_rs/src/camera/mod.rs new file mode 100644 index 000000000..26ca1185b --- /dev/null +++ b/port/assimp_rs/src/camera/mod.rs @@ -0,0 +1 @@ +pub use self::structs::{Camera}; diff --git a/port/assimp_rs/src/core/mod.rs b/port/assimp_rs/src/core/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/errors/mod.rs b/port/assimp_rs/src/errors/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/formats/mod.rs b/port/assimp_rs/src/formats/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/lib.rs b/port/assimp_rs/src/lib.rs new file mode 100644 index 000000000..dbb648876 --- /dev/null +++ b/port/assimp_rs/src/lib.rs @@ -0,0 +1,17 @@ +pub mod camera; +pub mod core; +pub mod errors; +pub mod formats; +pub mod material; +pub mod postprocess; +pub mod shims; +pub mod socket; +pub mod structs; + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(true, true); + } +} diff --git a/port/assimp_rs/src/material/mod.rs b/port/assimp_rs/src/material/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/postprocess/mod.rs b/port/assimp_rs/src/postprocess/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/shims/mod.rs b/port/assimp_rs/src/shims/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/socket/mod.rs b/port/assimp_rs/src/socket/mod.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/anim/anim.rs b/port/assimp_rs/src/structs/anim/anim.rs new file mode 100644 index 000000000..5374151eb --- /dev/null +++ b/port/assimp_rs/src/structs/anim/anim.rs @@ -0,0 +1,44 @@ +pub struct Animation<'mA, 'mMA, 'nA> { + /* The name of the animation. If the modeling package this data was + * exported from does support only a single animation channel, this + * name is usually empty (length is zero). + */ + m_name: Option, + // Duration of the animation in ticks + m_duration: f64, + // Ticks per second. Zero (0.000... ticks/second) if not + // specified in the imported file + m_ticks_per_second: Option, + /* Number of bone animation channels. + Each channel affects a single node. + */ + m_num_channels: u64, + /* Node animation channels. Each channel + affects a single node. + ?? -> The array is m_num_channels in size. + (maybe refine to a derivative type of usize?) + */ + m_channels: &'nA NodeAnim, + /* Number of mesh animation channels. Each + channel affects a single mesh and defines + vertex-based animation. + */ + m_num_mesh_channels: u64, + /* The mesh animation channels. Each channel + affects a single mesh. + The array is m_num_mesh_channels in size + (maybe refine to a derivative of usize?) + */ + m_mesh_channels: &'mA MeshAnim, + /* The number of mesh animation channels. Each channel + affects a single mesh and defines some morphing animation. + */ + m_num_morph_mesh_channels: u64, + /* The morph mesh animation channels. Each channel affects a single mesh. + The array is mNumMorphMeshChannels in size. + */ + m_morph_mesh_channels: &'mMA MeshMorphAnim +} +pub struct NodeAnim {} +pub struct MeshAnim {} +pub struct MeshMorphAnim {} diff --git a/port/assimp_rs/src/structs/anim/mod.rs b/port/assimp_rs/src/structs/anim/mod.rs new file mode 100644 index 000000000..a0d4b7daf --- /dev/null +++ b/port/assimp_rs/src/structs/anim/mod.rs @@ -0,0 +1,6 @@ +mod anim; +pub use self::anim::{ + Animation, + NodeAnim, + MeshAnim, + MeshMorphAnim}; diff --git a/port/assimp_rs/src/structs/blob/blob.rs b/port/assimp_rs/src/structs/blob/blob.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/blob/mod.rs b/port/assimp_rs/src/structs/blob/mod.rs new file mode 100644 index 000000000..ad7612c0a --- /dev/null +++ b/port/assimp_rs/src/structs/blob/mod.rs @@ -0,0 +1,2 @@ +mod blob; + diff --git a/port/assimp_rs/src/structs/bone/bone.rs b/port/assimp_rs/src/structs/bone/bone.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/bone/mod.rs b/port/assimp_rs/src/structs/bone/mod.rs new file mode 100644 index 000000000..758a15afc --- /dev/null +++ b/port/assimp_rs/src/structs/bone/mod.rs @@ -0,0 +1,2 @@ +mod bone; + diff --git a/port/assimp_rs/src/structs/camera/camera.rs b/port/assimp_rs/src/structs/camera/camera.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/camera/mod.rs b/port/assimp_rs/src/structs/camera/mod.rs new file mode 100644 index 000000000..d4d79d026 --- /dev/null +++ b/port/assimp_rs/src/structs/camera/mod.rs @@ -0,0 +1,2 @@ +mod camera; + diff --git a/port/assimp_rs/src/structs/color/color.rs b/port/assimp_rs/src/structs/color/color.rs new file mode 100644 index 000000000..0b5fc6413 --- /dev/null +++ b/port/assimp_rs/src/structs/color/color.rs @@ -0,0 +1,27 @@ +#[derive(Clone, Debug, Copy)] +struct Color3D { + r: f32, + g: f32, + b: f32 +} + +impl Color3D { + pub fn new(r_f32: f32, g_f32: f32, b_f32: f32) -> Color3D { + Color3D {r: r_f32, g: g_f32, b: b_f32 } + } +} + +#[derive(Clone, Debug, Copy)] +struct Color4D { + r: f32, + g: f32, + b: f32, + a: f32 +} + +impl Color4D { + pub fn new(r_f32: f32, g_f32: f32, b_f32: f32, a_f32: f32) -> Color4D { + Color4D {r: r_f32, g: g_f32, b: b_f32, a: a_f32 } + } +} + diff --git a/port/assimp_rs/src/structs/color/mod.rs b/port/assimp_rs/src/structs/color/mod.rs new file mode 100644 index 000000000..d88527ed1 --- /dev/null +++ b/port/assimp_rs/src/structs/color/mod.rs @@ -0,0 +1,5 @@ +mod color; +pub use self::color::{ + Color3D, + Color4D +}; diff --git a/port/assimp_rs/src/structs/face/face.rs b/port/assimp_rs/src/structs/face/face.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/face/mod.rs b/port/assimp_rs/src/structs/face/mod.rs new file mode 100644 index 000000000..ae5aa5bdb --- /dev/null +++ b/port/assimp_rs/src/structs/face/mod.rs @@ -0,0 +1,2 @@ +mod face; + diff --git a/port/assimp_rs/src/structs/key/key.rs b/port/assimp_rs/src/structs/key/key.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/key/mod.rs b/port/assimp_rs/src/structs/key/mod.rs new file mode 100644 index 000000000..b23779d35 --- /dev/null +++ b/port/assimp_rs/src/structs/key/mod.rs @@ -0,0 +1,2 @@ +mod key; + diff --git a/port/assimp_rs/src/structs/light/light.rs b/port/assimp_rs/src/structs/light/light.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/light/mod.rs b/port/assimp_rs/src/structs/light/mod.rs new file mode 100644 index 000000000..a68b51970 --- /dev/null +++ b/port/assimp_rs/src/structs/light/mod.rs @@ -0,0 +1,2 @@ +mod light; + diff --git a/port/assimp_rs/src/structs/material/material.rs b/port/assimp_rs/src/structs/material/material.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/material/mod.rs b/port/assimp_rs/src/structs/material/mod.rs new file mode 100644 index 000000000..54de8b399 --- /dev/null +++ b/port/assimp_rs/src/structs/material/mod.rs @@ -0,0 +1,2 @@ +mod material; + diff --git a/port/assimp_rs/src/structs/matrix/matrix.rs b/port/assimp_rs/src/structs/matrix/matrix.rs new file mode 100644 index 000000000..4673b2d69 --- /dev/null +++ b/port/assimp_rs/src/structs/matrix/matrix.rs @@ -0,0 +1,64 @@ +#[derive(Clone, Debug, Copy)] +struct Matrix3x3 { + a1: f32, + a2: f32, + a3: f32, + b1: f32, + b2: f32, + b3: f32, + c1: f32, + c2: f32, + c3: f32 +} + +#[derive(Clone, Debug, Copy)] +struct Matrix4x4 { + a1: f32, + a2: f32, + a3: f32, + a4: f32, + b1: f32, + b2: f32, + b3: f32, + b4: f32, + c1: f32, + c2: f32, + c3: f32, + c4: f32, + d1: f32, + d2: f32, + d3: f32, + d4: f32 +} + +impl Matrix3x3 { + pub fn new( + a1_f32: f32, a2_f32: f32, a3_f32: f32, + b1_f32: f32, b2_f32: f32, b3_f32: f32, + c1_f32: f32, c2_f32: f32, c3_f32: f32 + ) -> Matrix3x3 { + Matrix3x3 { + a1: a1_f32, a2: a2_f32, a3: a3_f32, + b1: b1_f32, b2: b2_f32, b3: b3_f32, + c1: c1_f32, c2: c2_f32, c3: c3_f32 + } + } +} + +impl Matrix4x4 { + pub fn new( + a1_f32: f32, a2_f32: f32, a3_f32: f32, a4_f32: f32, + b1_f32: f32, b2_f32: f32, b3_f32: f32, b4_f32: f32, + c1_f32: f32, c2_f32: f32, c3_f32: f32, c4_f32: f32, + d1_f32: f32, d2_f32: f32, d3_f32: f32, d4_f32: f32 + ) -> Matrix4x4 { + Matrix4x4 { + a1: a1_f32, a2: a2_f32, a3: a3_f32, a4: a4_f32, + b1: b1_f32, b2: b2_f32, b3: b3_f32, b4: b4_f32, + c1: c1_f32, c2: c2_f32, c3: c3_f32, c4: c4_f32, + d1: d1_f32, d2: d2_f32, d3: d3_f32, d4: d4_f32 + } + } +} + + diff --git a/port/assimp_rs/src/structs/matrix/mod.rs b/port/assimp_rs/src/structs/matrix/mod.rs new file mode 100644 index 000000000..b0fb1e1f9 --- /dev/null +++ b/port/assimp_rs/src/structs/matrix/mod.rs @@ -0,0 +1,4 @@ +mod matrix; +pub use self::matrix::{ + Matrix3x3, + Matrix4x4}; diff --git a/port/assimp_rs/src/structs/memory/memory.rs b/port/assimp_rs/src/structs/memory/memory.rs new file mode 100644 index 000000000..c076f172a --- /dev/null +++ b/port/assimp_rs/src/structs/memory/memory.rs @@ -0,0 +1,35 @@ +#[derive(Clone, Debug, Copy)] +struct MemoryInfo { + textures: u32, + materials: u32, + meshes: u32, + nodes: u32, + animations: u32, + cameras: u32, + lights: u32, + total: u32 +} + +impl MemoryInfo { + pub fn new( + textures_uint: u32, + materials_uint: u32, + meshes_uint: u32, + nodes_uint: u32, + animations_uint: u32, + cameras_uint: u32, + lights_uint: u32, + total_uint: u32) -> MemoryInfo { + + MemoryInfo { + textures: textures_uint, + materials: materials_uint, + meshes: meshes_uint, + nodes: nodes_uint, + animations: animations_uint, + cameras: cameras_uint, + lights: lights_uint, + total: total_uint + } + } +} diff --git a/port/assimp_rs/src/structs/memory/mod.rs b/port/assimp_rs/src/structs/memory/mod.rs new file mode 100644 index 000000000..8c8c31ce8 --- /dev/null +++ b/port/assimp_rs/src/structs/memory/mod.rs @@ -0,0 +1,2 @@ +mod memory; +pub use self::memory::MemoryInfo; diff --git a/port/assimp_rs/src/structs/mesh/mesh.rs b/port/assimp_rs/src/structs/mesh/mesh.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/mesh/mod.rs b/port/assimp_rs/src/structs/mesh/mod.rs new file mode 100644 index 000000000..1c3ef651d --- /dev/null +++ b/port/assimp_rs/src/structs/mesh/mod.rs @@ -0,0 +1,3 @@ +mod mesh; + + diff --git a/port/assimp_rs/src/structs/meta/meta.rs b/port/assimp_rs/src/structs/meta/meta.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/meta/mod.rs b/port/assimp_rs/src/structs/meta/mod.rs new file mode 100644 index 000000000..045294772 --- /dev/null +++ b/port/assimp_rs/src/structs/meta/mod.rs @@ -0,0 +1,2 @@ +mod meta; + diff --git a/port/assimp_rs/src/structs/mod.rs b/port/assimp_rs/src/structs/mod.rs new file mode 100644 index 000000000..fd9087648 --- /dev/null +++ b/port/assimp_rs/src/structs/mod.rs @@ -0,0 +1,61 @@ +mod anim; +/* Animation + * NodeAnim + * MeshAnim + * MeshMorphAnim + */ +mod blob; +/* ExportDataBlob + */ +mod vec; +/* Vector2d + * Vector3d + * */ +mod matrix; +/* Matrix3by3 + * Matrix4by4 + */ +mod camera; +/* Camera */ +mod color; +/* Color3d + * Color4d + */ +mod key; +/* MeshKey + * MeshMorphKey + * QuatKey + * VectorKey + */ +mod texel; +mod plane; +mod string; +/* String + */ +mod material; +/* Material + * MaterialPropery + * MaterialPropertyString + */ +mod mem; +mod quaternion; +mod face; +mod vertex_weight; +mod mesh; +/* Mesh + */ +mod meta; +/* Metadata + * MetadataEntry + */ +mod node; +/* Node + * */ +mod light; +mod texture; +mod ray; +mod transform; +/* UVTransform */ +mod bone; +mod scene; +/* Scene */ diff --git a/port/assimp_rs/src/structs/node/mod.rs b/port/assimp_rs/src/structs/node/mod.rs new file mode 100644 index 000000000..c1fc34cdb --- /dev/null +++ b/port/assimp_rs/src/structs/node/mod.rs @@ -0,0 +1,2 @@ +mod node; + diff --git a/port/assimp_rs/src/structs/node/node.rs b/port/assimp_rs/src/structs/node/node.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/plane/mod.rs b/port/assimp_rs/src/structs/plane/mod.rs new file mode 100644 index 000000000..c73a8ed45 --- /dev/null +++ b/port/assimp_rs/src/structs/plane/mod.rs @@ -0,0 +1,2 @@ +mod plane; + diff --git a/port/assimp_rs/src/structs/plane/plane.rs b/port/assimp_rs/src/structs/plane/plane.rs new file mode 100644 index 000000000..2b0b74499 --- /dev/null +++ b/port/assimp_rs/src/structs/plane/plane.rs @@ -0,0 +1,23 @@ +#[derive(Clone, Debug, Copy)] +struct Plane { + a: f32, + b: f32, + c: f32, + d: f32 +} + +impl Plane { + pub fn new( + a_f32: f32, + b_f32: f32, + c_f32: f32, + d_f32: f32 + ) -> Plane { + Plane { + a: a_f32, + b: b_f32, + c: b_f32, + d: d_f32 + } + } +} diff --git a/port/assimp_rs/src/structs/quaternion/mod.rs b/port/assimp_rs/src/structs/quaternion/mod.rs new file mode 100644 index 000000000..bb2c0616c --- /dev/null +++ b/port/assimp_rs/src/structs/quaternion/mod.rs @@ -0,0 +1,3 @@ +mod quaternion; + +pub use self::quaternion::Quaternion; diff --git a/port/assimp_rs/src/structs/quaternion/quaternion.rs b/port/assimp_rs/src/structs/quaternion/quaternion.rs new file mode 100644 index 000000000..970f5cce5 --- /dev/null +++ b/port/assimp_rs/src/structs/quaternion/quaternion.rs @@ -0,0 +1,7 @@ +use crate::vec; + +#[derive(Clone, Debug, Copy)] +pub struct Quaternion { + _coordinates: vec::Vector4d + +} diff --git a/port/assimp_rs/src/structs/ray/mod.rs b/port/assimp_rs/src/structs/ray/mod.rs new file mode 100644 index 000000000..7f0be074e --- /dev/null +++ b/port/assimp_rs/src/structs/ray/mod.rs @@ -0,0 +1,2 @@ +mod ray; + diff --git a/port/assimp_rs/src/structs/ray/ray.rs b/port/assimp_rs/src/structs/ray/ray.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/scene/mod.rs b/port/assimp_rs/src/structs/scene/mod.rs new file mode 100644 index 000000000..5aea638ee --- /dev/null +++ b/port/assimp_rs/src/structs/scene/mod.rs @@ -0,0 +1,2 @@ +mod scene; + diff --git a/port/assimp_rs/src/structs/scene/scene.rs b/port/assimp_rs/src/structs/scene/scene.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/string/mod.rs b/port/assimp_rs/src/structs/string/mod.rs new file mode 100644 index 000000000..f599ba7cb --- /dev/null +++ b/port/assimp_rs/src/structs/string/mod.rs @@ -0,0 +1,3 @@ +mod string; +pub use self::string::MAXLEN; +pub use self::string::Str; diff --git a/port/assimp_rs/src/structs/string/string.rs b/port/assimp_rs/src/structs/string/string.rs new file mode 100644 index 000000000..b88457df4 --- /dev/null +++ b/port/assimp_rs/src/structs/string/string.rs @@ -0,0 +1,41 @@ +pub const MAXLEN: usize = 1024; + +/// Want to consider replacing `Vec` +/// with a comparable definition at +/// https://doc.rust-lang.org/src/alloc/string.rs.html#415-417 +#[derive(Clone, Debug)] +struct Str { + length: usize, + data: Vec +} + +impl Str { + pub fn new(len_u32: usize, data_string: String) -> Str { + Str { + length: len_u32, + data: data_string.chars().collect() + } + } +} + +/// MaterialPropertyStr +/// The size of length is truncated to 4 bytes on a 64-bit platform when used as a +/// material property (see MaterialSystem.cpp, as aiMaterial::AddProperty() ). +#[derive(Clone, Debug)] +struct MaterialPropertyStr { + length: usize, + data: Vec +} + + +impl MaterialPropertyStr { + pub fn new(len_u32: usize, data_string: String) -> MaterialPropertyStr { + MaterialPropertyStr { + length: len_u32, + data: data_string.chars().collect() + } + } +} + + + diff --git a/port/assimp_rs/src/structs/texture/mod.rs b/port/assimp_rs/src/structs/texture/mod.rs new file mode 100644 index 000000000..1b5c9308d --- /dev/null +++ b/port/assimp_rs/src/structs/texture/mod.rs @@ -0,0 +1,3 @@ +mod texture; +pub use self::texture::Texel; + diff --git a/port/assimp_rs/src/structs/texture/texture.rs b/port/assimp_rs/src/structs/texture/texture.rs new file mode 100644 index 000000000..b2c72f30e --- /dev/null +++ b/port/assimp_rs/src/structs/texture/texture.rs @@ -0,0 +1,19 @@ +#[derive(Clone, Debug, Copy)] +struct Texel { + b: u32, + g: u32, + r: u32, + a: u32 +} + +impl Texel { + pub fn new(b_u32: u32, g_u32: u32, + r_u32: u32, a_u32: u32) -> Texel { + Texel { + b: b_u32, + g: g_u32, + r: r_u32, + a: a_u32 + } + } +} diff --git a/port/assimp_rs/src/structs/transform/mod.rs b/port/assimp_rs/src/structs/transform/mod.rs new file mode 100644 index 000000000..b80c43dd0 --- /dev/null +++ b/port/assimp_rs/src/structs/transform/mod.rs @@ -0,0 +1,2 @@ +mod transform; + diff --git a/port/assimp_rs/src/structs/transform/transform.rs b/port/assimp_rs/src/structs/transform/transform.rs new file mode 100644 index 000000000..e69de29bb diff --git a/port/assimp_rs/src/structs/vec/mod.rs b/port/assimp_rs/src/structs/vec/mod.rs new file mode 100644 index 000000000..3613d5d48 --- /dev/null +++ b/port/assimp_rs/src/structs/vec/mod.rs @@ -0,0 +1,2 @@ +mod vec; + diff --git a/port/assimp_rs/src/structs/vec/vec.rs b/port/assimp_rs/src/structs/vec/vec.rs new file mode 100644 index 000000000..ee0d194de --- /dev/null +++ b/port/assimp_rs/src/structs/vec/vec.rs @@ -0,0 +1,48 @@ +struct Vector2d { + x: f32, + y: f32 +} + +struct Vector3d { + x: f32, + y: f32, + z: f32 +} + +struct Vector4d { + x: f32, + y: f32, + z: f32, + w: f32 +} + +impl Vector2d { + pub fn new(x_f32: f32, y_f32: f32) -> Vector2d { + Vector2d { + x: x_f32, + y: y_f32 + } + } +} + +impl Vector3d { + pub fn new(x_f32: f32, y_f32: f32, z_f32: f32) -> Vector3d { + Vector3d { + x: x_f32, + y: y_f32, + z: z_f32 + } + } +} + +impl Vector4d { + pub fn new(x_f32: f32, y_f32: f32, z_f32: f32, w_f32: f32) -> Vector4d { + Vector4d { + x: x_f32, + y: y_f32, + z: z_f32, + w: w_f32 + } + } +} + diff --git a/port/assimp_rs/src/structs/vertex/mod.rs b/port/assimp_rs/src/structs/vertex/mod.rs new file mode 100644 index 000000000..97ae3eced --- /dev/null +++ b/port/assimp_rs/src/structs/vertex/mod.rs @@ -0,0 +1,2 @@ +mod vertex; +// pub use self::vertex:: diff --git a/port/assimp_rs/src/structs/vertex/vertex.rs b/port/assimp_rs/src/structs/vertex/vertex.rs new file mode 100644 index 000000000..e69de29bb diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bf6694845..a5f8086e9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -124,8 +124,7 @@ SET( IMPORTERS unit/utBlendImportMaterials.cpp unit/utBlenderWork.cpp unit/utBVHImportExport.cpp - unit/utColladaExportCamera.cpp - unit/utColladaExportLight.cpp + unit/utColladaExport.cpp unit/utColladaImportExport.cpp unit/utCSMImportExport.cpp unit/utB3DImportExport.cpp diff --git a/test/models/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb b/test/models/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb new file mode 100644 index 000000000..c36727d12 Binary files /dev/null and b/test/models/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb differ diff --git a/test/models/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb b/test/models/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb new file mode 100644 index 000000000..ae83f1f06 Binary files /dev/null and b/test/models/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb differ diff --git a/test/unit/utColladaExportLight.cpp b/test/unit/utColladaExport.cpp similarity index 78% rename from test/unit/utColladaExportLight.cpp rename to test/unit/utColladaExport.cpp index 0327b296e..efb2d7f17 100644 --- a/test/unit/utColladaExportLight.cpp +++ b/test/unit/utColladaExport.cpp @@ -49,7 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef ASSIMP_BUILD_NO_EXPORT -class ColladaExportLight : public ::testing::Test { +class utColladaExport : public ::testing::Test { public: void SetUp() override { ex = new Assimp::Exporter(); @@ -58,7 +58,9 @@ public: void TearDown() override { delete ex; + ex = nullptr; delete im; + im = nullptr; } protected: @@ -66,8 +68,53 @@ protected: Assimp::Importer *im; }; +TEST_F(utColladaExport, testExportCamera) { + const char *file = "cameraExp.dae"; + + const aiScene *pTest = im->ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/cameras.dae", aiProcess_ValidateDataStructure); + ASSERT_NE(nullptr, pTest); + ASSERT_TRUE(pTest->HasCameras()); + + EXPECT_EQ(AI_SUCCESS, ex->Export(pTest, "collada", file)); + const unsigned int origNumCams(pTest->mNumCameras); + std::unique_ptr origFOV(new float[origNumCams]); + std::unique_ptr orifClipPlaneNear(new float[origNumCams]); + std::unique_ptr orifClipPlaneFar(new float[origNumCams]); + std::unique_ptr names(new aiString[origNumCams]); + std::unique_ptr pos(new aiVector3D[origNumCams]); + for (size_t i = 0; i < origNumCams; i++) { + const aiCamera *orig = pTest->mCameras[i]; + ASSERT_NE(nullptr, orig); + + origFOV[i] = orig->mHorizontalFOV; + orifClipPlaneNear[i] = orig->mClipPlaneNear; + orifClipPlaneFar[i] = orig->mClipPlaneFar; + names[i] = orig->mName; + pos[i] = orig->mPosition; + } + const aiScene *imported = im->ReadFile(file, aiProcess_ValidateDataStructure); + + ASSERT_NE(nullptr, imported); + + EXPECT_TRUE(imported->HasCameras()); + EXPECT_EQ(origNumCams, imported->mNumCameras); + + for (size_t i = 0; i < imported->mNumCameras; i++) { + const aiCamera *read = imported->mCameras[i]; + + EXPECT_TRUE(names[i] == read->mName); + EXPECT_NEAR(origFOV[i], read->mHorizontalFOV, 0.0001f); + EXPECT_FLOAT_EQ(orifClipPlaneNear[i], read->mClipPlaneNear); + EXPECT_FLOAT_EQ(orifClipPlaneFar[i], read->mClipPlaneFar); + + EXPECT_FLOAT_EQ(pos[i].x, read->mPosition.x); + EXPECT_FLOAT_EQ(pos[i].y, read->mPosition.y); + EXPECT_FLOAT_EQ(pos[i].z, read->mPosition.z); + } +} + // ------------------------------------------------------------------------------------------------ -TEST_F(ColladaExportLight, testExportLight) { +TEST_F(utColladaExport, testExportLight) { const char *file = "lightsExp.dae"; const aiScene *pTest = im->ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/lights.dae", aiProcess_ValidateDataStructure); diff --git a/test/unit/utColladaExportCamera.cpp b/test/unit/utColladaExportCamera.cpp deleted file mode 100644 index c2c704056..000000000 --- a/test/unit/utColladaExportCamera.cpp +++ /dev/null @@ -1,115 +0,0 @@ -/* ---------------------------------------------------------------------------- -Open Asset Import Library (assimp) ---------------------------------------------------------------------------- - -Copyright (c) 2006-2020, assimp team - -All rights reserved. - -Redistribution and use of this software in source and binary forms, -with or without modification, are permitted provided that the following -conditions are met: - -* Redistributions of source code must retain the above -copyright notice, this list of conditions and the -following disclaimer. - -* Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the -following disclaimer in the documentation and/or other -materials provided with the distribution. - -* Neither the name of the assimp team, nor the names of its -contributors may be used to endorse or promote products -derived from this software without specific prior -written permission of the assimp team. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------------- -*/ -#include "UnitTestPCH.h" - -#include -#include -#include -#include -#include - -#ifndef ASSIMP_BUILD_NO_EXPORT - -class ColladaExportCamera : public ::testing::Test { -public: - void SetUp() override { - ex = new Assimp::Exporter(); - im = new Assimp::Importer(); - } - - void TearDown() override { - delete ex; - ex = nullptr; - delete im; - im = nullptr; - } - -protected: - Assimp::Exporter *ex; - Assimp::Importer *im; -}; - -TEST_F(ColladaExportCamera, testExportCamera) { - const char *file = "cameraExp.dae"; - - const aiScene *pTest = im->ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/cameras.dae", aiProcess_ValidateDataStructure); - ASSERT_NE(nullptr, pTest); - ASSERT_TRUE(pTest->HasCameras()); - - EXPECT_EQ(AI_SUCCESS, ex->Export(pTest, "collada", file)); - const unsigned int origNumCams(pTest->mNumCameras); - std::unique_ptr origFOV(new float[origNumCams]); - std::unique_ptr orifClipPlaneNear(new float[origNumCams]); - std::unique_ptr orifClipPlaneFar(new float[origNumCams]); - std::unique_ptr names(new aiString[origNumCams]); - std::unique_ptr pos(new aiVector3D[origNumCams]); - for (size_t i = 0; i < origNumCams; i++) { - const aiCamera *orig = pTest->mCameras[i]; - ASSERT_NE(nullptr, orig); - - origFOV[i] = orig->mHorizontalFOV; - orifClipPlaneNear[i] = orig->mClipPlaneNear; - orifClipPlaneFar[i] = orig->mClipPlaneFar; - names[i] = orig->mName; - pos[i] = orig->mPosition; - } - const aiScene *imported = im->ReadFile(file, aiProcess_ValidateDataStructure); - - ASSERT_NE(nullptr, imported); - - EXPECT_TRUE(imported->HasCameras()); - EXPECT_EQ(origNumCams, imported->mNumCameras); - - for (size_t i = 0; i < imported->mNumCameras; i++) { - const aiCamera *read = imported->mCameras[i]; - - EXPECT_TRUE(names[i] == read->mName); - EXPECT_NEAR(origFOV[i], read->mHorizontalFOV, 0.0001f); - EXPECT_FLOAT_EQ(orifClipPlaneNear[i], read->mClipPlaneNear); - EXPECT_FLOAT_EQ(orifClipPlaneFar[i], read->mClipPlaneFar); - - EXPECT_FLOAT_EQ(pos[i].x, read->mPosition.x); - EXPECT_FLOAT_EQ(pos[i].y, read->mPosition.y); - EXPECT_FLOAT_EQ(pos[i].z, read->mPosition.z); - } -} - -#endif // ASSIMP_BUILD_NO_EXPORT diff --git a/test/unit/utColladaImportExport.cpp b/test/unit/utColladaImportExport.cpp index 549ff68fb..451c8e235 100644 --- a/test/unit/utColladaImportExport.cpp +++ b/test/unit/utColladaImportExport.cpp @@ -41,16 +41,33 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "AbstractImportExportBase.h" #include "UnitTestPCH.h" +#include +#include #include #include #include +#include #include using namespace Assimp; class utColladaImportExport : public AbstractImportExportBase { public: - virtual bool importerTest() { + // Clones the scene in an exception-safe way + struct SceneCloner { + SceneCloner(const aiScene *scene) { + sceneCopy = nullptr; + SceneCombiner::CopyScene(&sceneCopy, scene); + } + + ~SceneCloner() { + delete sceneCopy; + sceneCopy = nullptr; + } + aiScene *sceneCopy; + }; + + virtual bool importerTest() final { Assimp::Importer importer; const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck.dae", aiProcess_ValidateDataStructure); if (scene == nullptr) @@ -80,15 +97,255 @@ public: return true; } + + typedef std::pair IdNameString; + typedef std::map IdNameMap; + + template + static inline IdNameString GetColladaIdName(const T *item, size_t index) { + std::ostringstream stream; + stream << typeid(T).name() << "@" << index; + if (item->mMetaData) { + aiString aiStr; + if (item->mMetaData->Get(AI_METADATA_COLLADA_ID, aiStr)) + return std::make_pair(std::string(aiStr.C_Str()), stream.str()); + } + return std::make_pair(std::string(), stream.str()); + } + + template + static inline IdNameString GetItemIdName(const T *item, size_t index) { + std::ostringstream stream; + stream << typeid(T).name() << "@" << index; + return std::make_pair(std::string(item->mName.C_Str()), stream.str()); + } + + // Specialisations + static inline IdNameString GetItemIdName(aiMaterial *item, size_t index) { + std::ostringstream stream; + stream << typeid(aiMaterial).name() << "@" << index; + return std::make_pair(std::string(item->GetName().C_Str()), stream.str()); + } + + static inline IdNameString GetItemIdName(aiTexture *item, size_t index) { + std::ostringstream stream; + stream << typeid(aiTexture).name() << "@" << index; + return std::make_pair(std::string(item->mFilename.C_Str()), stream.str()); + } + + static inline void ReportDuplicate(IdNameMap &itemIdMap, const IdNameString &namePair, const char *typeNameStr) { + const auto result = itemIdMap.insert(namePair); + EXPECT_TRUE(result.second) << "Duplicate '" << typeNameStr << "' name: '" << namePair.first << "'. " << namePair.second << " == " << result.first->second; + } + + template + static inline void CheckUniqueIds(IdNameMap &itemIdMap, unsigned int itemCount, T **itemArray) { + for (size_t idx = 0; idx < itemCount; ++idx) { + IdNameString namePair = GetItemIdName(itemArray[idx], idx); + ReportDuplicate(itemIdMap, namePair, typeid(T).name()); + } + } + + static inline void CheckUniqueIds(IdNameMap &itemIdMap, const aiNode *parent, size_t index) { + IdNameString namePair = GetItemIdName(parent, index); + ReportDuplicate(itemIdMap, namePair, typeid(aiNode).name()); + + for (size_t idx = 0; idx < parent->mNumChildren; ++idx) { + CheckUniqueIds(itemIdMap, parent->mChildren[idx], idx); + } + } + + static inline void CheckNodeIdNames(IdNameMap &nodeIdMap, IdNameMap &nodeNameMap, const aiNode *parent, size_t index) { + IdNameString namePair = GetItemIdName(parent, index); + const auto result = nodeNameMap.insert(namePair); + IdNameString idPair = GetColladaIdName(parent, index); + ReportDuplicate(nodeIdMap, idPair, typeid(aiNode).name()); + + for (size_t idx = 0; idx < parent->mNumChildren; ++idx) { + CheckNodeIdNames(nodeIdMap, nodeNameMap, parent->mChildren[idx], idx); + } + } + + static inline void SetAllNodeNames(const aiString &newName, aiNode *node) { + node->mName = newName; + for (size_t idx = 0; idx < node->mNumChildren; ++idx) { + SetAllNodeNames(newName, node->mChildren[idx]); + } + } + + void ImportAndCheckIds(const char *file, const aiScene *origScene) { + // Import the Collada using the 'default' where aiNode and aiMesh names are the Collada ids + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(file, aiProcess_ValidateDataStructure); + ASSERT_TRUE(scene != nullptr) << "Fatal: could not re-import " << file; + EXPECT_EQ(origScene->mNumMeshes, scene->mNumMeshes) << "in " << file; + + // Check the ids are unique + IdNameMap itemIdMap; + // Recurse the Nodes + CheckUniqueIds(itemIdMap, scene->mRootNode, 0); + // Check the lists + CheckUniqueIds(itemIdMap, scene->mNumMeshes, scene->mMeshes); + // The remaining will come in using the name, which may not be unique + // Check we have the right number + EXPECT_EQ(origScene->mNumAnimations, scene->mNumAnimations); + EXPECT_EQ(origScene->mNumMaterials, scene->mNumMaterials); + EXPECT_EQ(origScene->mNumTextures, scene->mNumTextures); + EXPECT_EQ(origScene->mNumLights, scene->mNumLights); + EXPECT_EQ(origScene->mNumCameras, scene->mNumCameras); + } + + void ImportAsNames(const char *file, const aiScene *origScene) { + // Import the Collada but using the user-visible names for aiNode and aiMesh + // Note that this mode may not support bones or animations + Assimp::Importer importer; + importer.SetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_USE_COLLADA_NAMES, 1); + + const aiScene *scene = importer.ReadFile(file, aiProcess_ValidateDataStructure); + ASSERT_TRUE(scene != nullptr) << "Fatal: could not re-import " << file; + EXPECT_EQ(origScene->mNumMeshes, scene->mNumMeshes) << "in " << file; + + // Check the node ids are unique but the node names are not + IdNameMap nodeIdMap; + IdNameMap nodeNameMap; + // Recurse the Nodes + CheckNodeIdNames(nodeIdMap, nodeNameMap, scene->mRootNode, 0); + + // nodeNameMap should have fewer than nodeIdMap + EXPECT_LT(nodeNameMap.size(), nodeIdMap.size()) << "Some nodes should have the same names"; + // Check the counts haven't changed + EXPECT_EQ(origScene->mNumAnimations, scene->mNumAnimations); + EXPECT_EQ(origScene->mNumMaterials, scene->mNumMaterials); + EXPECT_EQ(origScene->mNumTextures, scene->mNumTextures); + EXPECT_EQ(origScene->mNumLights, scene->mNumLights); + EXPECT_EQ(origScene->mNumCameras, scene->mNumCameras); + } }; -TEST_F(utColladaImportExport, importBlenFromFileTest) { +TEST_F(utColladaImportExport, importDaeFromFileTest) { EXPECT_TRUE(importerTest()); } +unsigned int GetMeshUseCount(const aiNode *rootNode) { + unsigned int result = rootNode->mNumMeshes; + for (unsigned int i = 0; i < rootNode->mNumChildren; ++i) { + result += GetMeshUseCount(rootNode->mChildren[i]); + } + return result; +} + +TEST_F(utColladaImportExport, exportRootNodeMeshTest) { + Assimp::Importer importer; + Assimp::Exporter exporter; + const char *outFile = "exportRootNodeMeshTest_out.dae"; + + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck.dae", aiProcess_ValidateDataStructure); + ASSERT_TRUE(scene != nullptr) << "Fatal: could not import duck.dae!"; + + ASSERT_EQ(0u, scene->mRootNode->mNumMeshes) << "Collada import should not give the root node a mesh"; + + { + SceneCloner clone(scene); + ASSERT_TRUE(clone.sceneCopy != nullptr) << "Fatal: could not copy scene!"; + // Do this by moving the meshes from the first child that has some + aiNode *rootNode = clone.sceneCopy->mRootNode; + ASSERT_TRUE(rootNode->mNumChildren > 0) << "Fatal: root has no children"; + aiNode *meshNode = rootNode->mChildren[0]; + ASSERT_EQ(1u, meshNode->mNumMeshes) << "Fatal: First child node has no duck mesh"; + + // Move the meshes to the parent + rootNode->mNumMeshes = meshNode->mNumMeshes; + rootNode->mMeshes = new unsigned int[rootNode->mNumMeshes]; + for (unsigned int i = 0; i < rootNode->mNumMeshes; ++i) { + rootNode->mMeshes[i] = meshNode->mMeshes[i]; + } + + // Remove the meshes from the original node + meshNode->mNumMeshes = 0; + delete[] meshNode->mMeshes; + meshNode->mMeshes = nullptr; + + ASSERT_EQ(AI_SUCCESS, exporter.Export(clone.sceneCopy, "collada", outFile)) << "Fatal: Could not export file"; + } + + // Reimport and look for meshes + scene = importer.ReadFile(outFile, aiProcess_ValidateDataStructure); + ASSERT_TRUE(scene != nullptr) << "Fatal: could not reimport!"; + + // A Collada root node is not allowed to have a mesh + ASSERT_EQ(0u, scene->mRootNode->mNumMeshes) << "Collada reimport should not give the root node a mesh"; + + // Walk nodes and counts used meshes + // Should be exactly one + EXPECT_EQ(1u, GetMeshUseCount(scene->mRootNode)) << "Nodes had unexpected number of meshes in use"; +} + +TEST_F(utColladaImportExport, exporterUniqueIdsTest) { + Assimp::Importer importer; + Assimp::Exporter exporter; + const char *outFileEmpty = "exportMeshIdTest_empty_out.dae"; + const char *outFileNamed = "exportMeshIdTest_named_out.dae"; + + // Load a sample file containing multiple meshes + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/teapots.DAE", aiProcess_ValidateDataStructure); + + ASSERT_TRUE(scene != nullptr) << "Fatal: could not import teapots.DAE!"; + ASSERT_EQ(3u, scene->mNumMeshes) << "Fatal: teapots.DAE initial load failed"; + + // Clear all the names + for (size_t idx = 0; idx < scene->mNumMeshes; ++idx) { + scene->mMeshes[idx]->mName.Clear(); + } + for (size_t idx = 0; idx < scene->mNumMaterials; ++idx) { + scene->mMaterials[idx]->RemoveProperty(AI_MATKEY_NAME); + } + for (size_t idx = 0; idx < scene->mNumAnimations; ++idx) { + scene->mAnimations[idx]->mName.Clear(); + } + // Can't clear texture names + for (size_t idx = 0; idx < scene->mNumLights; ++idx) { + scene->mLights[idx]->mName.Clear(); + } + for (size_t idx = 0; idx < scene->mNumCameras; ++idx) { + scene->mCameras[idx]->mName.Clear(); + } + + SetAllNodeNames(aiString(), scene->mRootNode); + + ASSERT_EQ(AI_SUCCESS, exporter.Export(scene, "collada", outFileEmpty)) << "Fatal: Could not export un-named meshes file"; + + ImportAndCheckIds(outFileEmpty, scene); + + // Force everything to have the same non-empty name + aiString testName("test_name"); + for (size_t idx = 0; idx < scene->mNumMeshes; ++idx) { + scene->mMeshes[idx]->mName = testName; + } + for (size_t idx = 0; idx < scene->mNumMaterials; ++idx) { + scene->mMaterials[idx]->AddProperty(&testName, AI_MATKEY_NAME); + } + for (size_t idx = 0; idx < scene->mNumAnimations; ++idx) { + scene->mAnimations[idx]->mName = testName; + } + // Can't clear texture names + for (size_t idx = 0; idx < scene->mNumLights; ++idx) { + scene->mLights[idx]->mName = testName; + } + for (size_t idx = 0; idx < scene->mNumCameras; ++idx) { + scene->mCameras[idx]->mName = testName; + } + + SetAllNodeNames(testName, scene->mRootNode); + + ASSERT_EQ(AI_SUCCESS, exporter.Export(scene, "collada", outFileNamed)) << "Fatal: Could not export named meshes file"; + + ImportAndCheckIds(outFileNamed, scene); + ImportAsNames(outFileNamed, scene); +} + class utColladaZaeImportExport : public AbstractImportExportBase { public: - virtual bool importerTest() { + virtual bool importerTest() final { { Assimp::Importer importer; const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/Collada/duck.zae", aiProcess_ValidateDataStructure); diff --git a/test/unit/utglTF2ImportExport.cpp b/test/unit/utglTF2ImportExport.cpp index 55cd2ef6a..f0f18d503 100644 --- a/test/unit/utglTF2ImportExport.cpp +++ b/test/unit/utglTF2ImportExport.cpp @@ -436,6 +436,31 @@ TEST_F(utglTF2ImportExport, error_string_preserved) { ASSERT_NE(error.find("BoxTextured0.bin"), std::string::npos) << "Error string should contain an error about missing .bin file"; } +TEST_F(utglTF2ImportExport, export_bad_accessor_bounds) { + Assimp::Importer importer; + Assimp::Exporter exporter; + const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb", aiProcess_ValidateDataStructure); + ASSERT_NE(scene, nullptr); + + EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.glb")); + EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.gltf")); +} + +TEST_F(utglTF2ImportExport, export_normalized_normals) { + Assimp::Importer importer; + Assimp::Exporter exporter; + const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb", aiProcess_ValidateDataStructure); + ASSERT_NE(scene, nullptr); + EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb")); + + // load in again and ensure normal-length normals but no Nan's or Inf's introduced + scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb", aiProcess_ValidateDataStructure); + for ( auto i = 0u; i < scene->mMeshes[0]->mNumVertices; ++i ) { + const auto length = scene->mMeshes[0]->mNormals[i].Length(); + EXPECT_TRUE(abs(length) < 1e-6 || abs(length - 1) < 1e-6); + } +} + #endif // ASSIMP_BUILD_NO_EXPORT TEST_F(utglTF2ImportExport, sceneMetadata) {