From a56134ba330775415dd6148d8667e9a3c7ce2537 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 09:05:09 +0100 Subject: [PATCH 01/24] Drop faces when indices are out of range. --- code/AssetLib/glTF2/glTF2Importer.cpp | 117 +++++++++++++++----------- 1 file changed, 67 insertions(+), 50 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 4d740d8c1..1c32b48e2 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -298,25 +298,37 @@ void glTF2Importer::ImportMaterials(glTF2::Asset &r) { } } -static inline void SetFace(aiFace &face, int a) { - face.mNumIndices = 1; - face.mIndices = new unsigned int[1]; - face.mIndices[0] = a; +static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a) { + if (a >= numVertices) { + return; + } + face->mNumIndices = 1; + face->mIndices = new unsigned int[1]; + face->mIndices[0] = a; + ++face; } -static inline void SetFace(aiFace &face, int a, int b) { - face.mNumIndices = 2; - face.mIndices = new unsigned int[2]; - face.mIndices[0] = a; - face.mIndices[1] = b; +static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b) { + if ((a >= numVertices) || (b >= numVertices)) { + return; + } + face->mNumIndices = 2; + face->mIndices = new unsigned int[2]; + face->mIndices[0] = a; + face->mIndices[1] = b; + ++face; } -static inline void SetFace(aiFace &face, int a, int b, int c) { - face.mNumIndices = 3; - face.mIndices = new unsigned int[3]; - face.mIndices[0] = a; - face.mIndices[1] = b; - face.mIndices[2] = c; +static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b, unsigned int c) { + if ((a >= numVertices) || (b >= numVertices) || (c >= numVertices)) { + return; + } + face->mNumIndices = 3; + face->mIndices = new unsigned int[3]; + face->mIndices[0] = a; + face->mIndices[1] = b; + face->mIndices[2] = c; + ++face; } #ifdef ASSIMP_BUILD_DEBUG @@ -486,6 +498,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { } aiFace *faces = nullptr; + aiFace *facePtr = nullptr; size_t nFaces = 0; if (prim.indices) { @@ -497,9 +510,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { switch (prim.mode) { case PrimitiveMode_POINTS: { nFaces = count; - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; ++i) { - SetFace(faces[i], data.GetUInt(i)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i)); } break; } @@ -510,9 +523,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped."); count = nFaces * 2; } - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 2) { - SetFace(faces[i / 2], data.GetUInt(i), data.GetUInt(i + 1)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1)); } break; } @@ -520,13 +533,13 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_LINE_LOOP: case PrimitiveMode_LINE_STRIP: { nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); - faces = new aiFace[nFaces]; - SetFace(faces[0], data.GetUInt(0), data.GetUInt(1)); + facePtr = faces = new aiFace[nFaces]; + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1)); for (unsigned int i = 2; i < count; ++i) { - SetFace(faces[i - 1], faces[i - 2].mIndices[1], data.GetUInt(i)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], data.GetUInt(i)); } if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop - SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]); + SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); } break; } @@ -537,33 +550,33 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped."); count = nFaces * 3; } - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 3) { - SetFace(faces[i / 3], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); } break; } case PrimitiveMode_TRIANGLE_STRIP: { nFaces = count - 2; - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < nFaces; ++i) { //The ordering is to ensure that the triangles are all drawn with the same orientation if ((i + 1) % 2 == 0) { //For even n, vertices n + 1, n, and n + 2 define triangle n - SetFace(faces[i], data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2)); } else { //For odd n, vertices n, n+1, and n+2 define triangle n - SetFace(faces[i], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); } } break; } case PrimitiveMode_TRIANGLE_FAN: nFaces = count - 2; - faces = new aiFace[nFaces]; - SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); + facePtr = faces = new aiFace[nFaces]; + SetFaceAndAdvance(facePtr, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); for (unsigned int i = 1; i < nFaces; ++i) { - SetFace(faces[i], faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); } break; } @@ -575,9 +588,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { switch (prim.mode) { case PrimitiveMode_POINTS: { nFaces = count; - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; ++i) { - SetFace(faces[i], i); + SetFaceAndAdvance(facePtr, aim->mNumVertices, i); } break; } @@ -588,9 +601,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped."); count = (unsigned int)nFaces * 2; } - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 2) { - SetFace(faces[i / 2], i, i + 1); + SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1); } break; } @@ -598,13 +611,13 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_LINE_LOOP: case PrimitiveMode_LINE_STRIP: { nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); - faces = new aiFace[nFaces]; - SetFace(faces[0], 0, 1); + facePtr = faces = new aiFace[nFaces]; + SetFaceAndAdvance(facePtr, aim->mNumVertices, 0, 1); for (unsigned int i = 2; i < count; ++i) { - SetFace(faces[i - 1], faces[i - 2].mIndices[1], i); + SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], i); } if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop - SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]); + SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); } break; } @@ -615,42 +628,46 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped."); count = (unsigned int)nFaces * 3; } - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 3) { - SetFace(faces[i / 3], i, i + 1, i + 2); + SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1, i + 2); } break; } case PrimitiveMode_TRIANGLE_STRIP: { nFaces = count - 2; - faces = new aiFace[nFaces]; + facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < nFaces; ++i) { //The ordering is to ensure that the triangles are all drawn with the same orientation if ((i + 1) % 2 == 0) { //For even n, vertices n + 1, n, and n + 2 define triangle n - SetFace(faces[i], i + 1, i, i + 2); + SetFaceAndAdvance(facePtr, aim->mNumVertices, i + 1, i, i + 2); } else { //For odd n, vertices n, n+1, and n+2 define triangle n - SetFace(faces[i], i, i + 1, i + 2); + SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1, i + 2); } } break; } case PrimitiveMode_TRIANGLE_FAN: nFaces = count - 2; - faces = new aiFace[nFaces]; - SetFace(faces[0], 0, 1, 2); + facePtr = faces = new aiFace[nFaces]; + SetFaceAndAdvance(facePtr, aim->mNumVertices, 0, 1, 2); for (unsigned int i = 1; i < nFaces; ++i) { - SetFace(faces[i], faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); + SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); } break; } } - if (nullptr != faces) { + if (faces) { aim->mFaces = faces; - aim->mNumFaces = static_cast(nFaces); - ai_assert(CheckValidFacesIndices(faces, static_cast(nFaces), aim->mNumVertices)); + const unsigned int actualNumFaces = facePtr - faces; + if (actualNumFaces < nFaces) { + ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped."); + } + aim->mNumFaces = actualNumFaces; + ai_assert(CheckValidFacesIndices(faces, actualNumFaces, aim->mNumVertices)); } if (prim.material) { From c0d978786e3714aaeb2934540b6c425823509c13 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 09:12:52 +0100 Subject: [PATCH 02/24] Fix warning --- code/AssetLib/glTF2/glTF2Importer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 1c32b48e2..9ce488cbd 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -662,7 +662,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { if (faces) { aim->mFaces = faces; - const unsigned int actualNumFaces = facePtr - faces; + const unsigned int actualNumFaces = static_cast(facePtr - faces); if (actualNumFaces < nFaces) { ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped."); } From 7e7161852ae2a724e1f44667294b048a3281f670 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 11:19:10 +0100 Subject: [PATCH 03/24] Add a unit test. --- .../glTF2/IndexOutOfRange/IndexOutOfRange.bin | Bin 0 -> 648 bytes .../IndexOutOfRange/IndexOutOfRange.gltf | 142 ++++++++++++++++++ test/unit/utglTF2ImportExport.cpp | 28 ++++ 3 files changed, 170 insertions(+) create mode 100644 test/models/glTF2/IndexOutOfRange/IndexOutOfRange.bin create mode 100644 test/models/glTF2/IndexOutOfRange/IndexOutOfRange.gltf diff --git a/test/models/glTF2/IndexOutOfRange/IndexOutOfRange.bin b/test/models/glTF2/IndexOutOfRange/IndexOutOfRange.bin new file mode 100644 index 0000000000000000000000000000000000000000..b7cfe377b9cdabc5bc9ed9879730941916b3946e GIT binary patch literal 648 zcmb7;0Sbap5JaDqm6es1bsVqe%{p4`2t&(9kc7v~?hM}rf8$^WOMZN(?t))>OE2Y4 zIp=K7|8vXl>iFlv-P0ZFm?6B-oYniRxnr+fzC`YAz-dW4cdufTg sR^{^3{GnpSI;hxCvt<|5>}fb~3>r?foVf%oS2}LogN_G1PhLUK7moElJ^%m! literal 0 HcmV?d00001 diff --git a/test/models/glTF2/IndexOutOfRange/IndexOutOfRange.gltf b/test/models/glTF2/IndexOutOfRange/IndexOutOfRange.gltf new file mode 100644 index 000000000..67561c591 --- /dev/null +++ b/test/models/glTF2/IndexOutOfRange/IndexOutOfRange.gltf @@ -0,0 +1,142 @@ +{ + "asset": { + "generator": "COLLADA2GLTF", + "version": "2.0" + }, + "scene": 0, + "scenes": [ + { + "nodes": [ + 0 + ] + } + ], + "nodes": [ + { + "children": [ + 1 + ], + "matrix": [ + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + -1.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0 + ] + }, + { + "mesh": 0 + } + ], + "meshes": [ + { + "primitives": [ + { + "attributes": { + "NORMAL": 1, + "POSITION": 2 + }, + "indices": 0, + "mode": 4, + "material": 0 + } + ], + "name": "Mesh" + } + ], + "accessors": [ + { + "bufferView": 0, + "byteOffset": 0, + "componentType": 5123, + "count": 36, + "max": [ + 255 + ], + "min": [ + 0 + ], + "type": "SCALAR" + }, + { + "bufferView": 1, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "max": [ + 1.0, + 1.0, + 1.0 + ], + "min": [ + -1.0, + -1.0, + -1.0 + ], + "type": "VEC3" + }, + { + "bufferView": 1, + "byteOffset": 288, + "componentType": 5126, + "count": 24, + "max": [ + 0.5, + 0.5, + 0.5 + ], + "min": [ + -0.5, + -0.5, + -0.5 + ], + "type": "VEC3" + } + ], + "materials": [ + { + "pbrMetallicRoughness": { + "baseColorFactor": [ + 0.800000011920929, + 0.0, + 0.0, + 1.0 + ], + "metallicFactor": 0.0 + }, + "name": "Red" + } + ], + "bufferViews": [ + { + "buffer": 0, + "byteOffset": 576, + "byteLength": 72, + "target": 34963 + }, + { + "buffer": 0, + "byteOffset": 0, + "byteLength": 576, + "byteStride": 12, + "target": 34962 + } + ], + "buffers": [ + { + "byteLength": 648, + "uri": "IndexOutOfRange.bin" + } + ] +} diff --git a/test/unit/utglTF2ImportExport.cpp b/test/unit/utglTF2ImportExport.cpp index 6791d5f89..8bc20e950 100644 --- a/test/unit/utglTF2ImportExport.cpp +++ b/test/unit/utglTF2ImportExport.cpp @@ -541,3 +541,31 @@ TEST_F(utglTF2ImportExport, norootnode_issue_3269) { const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/issue_3269/texcoord_crash.gltf", aiProcess_ValidateDataStructure); ASSERT_EQ(scene, nullptr); } + +#include +#include + +TEST_F(utglTF2ImportExport, indexOutOfRange) { + // The contents of an asset should not lead to an assert. + Assimp::Importer importer; + + struct WarningObserver : Assimp::LogStream + { + bool m_observedWarning = false; + void write(const char *message) override + { + m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped"); + } + }; + WarningObserver warningObserver; + + DefaultLogger::get()->attachStream(&warningObserver); + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure); + ASSERT_NE(scene, nullptr); + ASSERT_NE(scene->mRootNode, nullptr); + ASSERT_EQ(scene->mNumMeshes, 1); + EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11); + DefaultLogger::get()->detachStream(&warningObserver); + EXPECT_TRUE(warningObserver.m_observedWarning); +} + From 212903e935d64e8d0d3f20c68603744ddbc213f9 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 12:19:00 +0100 Subject: [PATCH 04/24] Unit test for all indices out of range, and fix. --- code/AssetLib/glTF2/glTF2Importer.cpp | 8 ++++++-- test/unit/utglTF2ImportExport.cpp | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 9ce488cbd..0b784e0d4 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -574,7 +574,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_TRIANGLE_FAN: nFaces = count - 2; facePtr = faces = new aiFace[nFaces]; - SetFaceAndAdvance(facePtr, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); + SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); for (unsigned int i = 1; i < nFaces; ++i) { SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); } @@ -664,7 +664,11 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { aim->mFaces = faces; const unsigned int actualNumFaces = static_cast(facePtr - faces); if (actualNumFaces < nFaces) { - ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped."); + ASSIMP_LOG_WARN("Some faces in mesh had out-of-range indices. Those faces were dropped."); + } + if (actualNumFaces == 0) + { + throw DeadlyImportError(std::string("Mesh \"") + aim->mName.C_Str() + "\" has no faces"); } aim->mNumFaces = actualNumFaces; ai_assert(CheckValidFacesIndices(faces, actualNumFaces, aim->mNumVertices)); diff --git a/test/unit/utglTF2ImportExport.cpp b/test/unit/utglTF2ImportExport.cpp index 8bc20e950..c3ad2d994 100644 --- a/test/unit/utglTF2ImportExport.cpp +++ b/test/unit/utglTF2ImportExport.cpp @@ -46,11 +46,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include + #include #include - using namespace Assimp; class utglTF2ImportExport : public AbstractImportExportBase { @@ -542,14 +544,11 @@ TEST_F(utglTF2ImportExport, norootnode_issue_3269) { ASSERT_EQ(scene, nullptr); } -#include -#include - TEST_F(utglTF2ImportExport, indexOutOfRange) { // The contents of an asset should not lead to an assert. Assimp::Importer importer; - struct WarningObserver : Assimp::LogStream + struct LogObserver : Assimp::LogStream { bool m_observedWarning = false; void write(const char *message) override @@ -557,15 +556,23 @@ TEST_F(utglTF2ImportExport, indexOutOfRange) { m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped"); } }; - WarningObserver warningObserver; + LogObserver logObserver; - DefaultLogger::get()->attachStream(&warningObserver); + DefaultLogger::get()->attachStream(&logObserver); const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure); ASSERT_NE(scene, nullptr); ASSERT_NE(scene->mRootNode, nullptr); ASSERT_EQ(scene->mNumMeshes, 1); EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11); - DefaultLogger::get()->detachStream(&warningObserver); - EXPECT_TRUE(warningObserver.m_observedWarning); + DefaultLogger::get()->detachStream(&logObserver); + EXPECT_TRUE(logObserver.m_observedWarning); } +TEST_F(utglTF2ImportExport, allIndicesOutOfRange) { + // The contents of an asset should not lead to an assert. + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf", aiProcess_ValidateDataStructure); + ASSERT_EQ(scene, nullptr); + std::string error = importer.GetErrorString(); + ASSERT_NE(error.find("Mesh \"Mesh\" has no faces"), std::string::npos); +} From fff6396e3c997b2a4368e597511495967e1eca82 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 12:22:28 +0100 Subject: [PATCH 05/24] Rename to avoid overloading problems. --- code/AssetLib/glTF2/glTF2Importer.cpp | 46 +++++++++++++-------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 0b784e0d4..fae87a990 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -298,7 +298,7 @@ void glTF2Importer::ImportMaterials(glTF2::Asset &r) { } } -static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a) { +static inline void SetFaceAndAdvance1(aiFace*& face, unsigned int numVertices, unsigned int a) { if (a >= numVertices) { return; } @@ -308,7 +308,7 @@ static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, un ++face; } -static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b) { +static inline void SetFaceAndAdvance2(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b) { if ((a >= numVertices) || (b >= numVertices)) { return; } @@ -319,7 +319,7 @@ static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, un ++face; } -static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b, unsigned int c) { +static inline void SetFaceAndAdvance3(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b, unsigned int c) { if ((a >= numVertices) || (b >= numVertices) || (c >= numVertices)) { return; } @@ -512,7 +512,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { nFaces = count; facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; ++i) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i)); + SetFaceAndAdvance1(facePtr, aim->mNumVertices, data.GetUInt(i)); } break; } @@ -525,7 +525,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { } facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 2) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1)); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1)); } break; } @@ -534,12 +534,12 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_LINE_STRIP: { nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); facePtr = faces = new aiFace[nFaces]; - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1)); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1)); for (unsigned int i = 2; i < count; ++i) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], data.GetUInt(i)); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], data.GetUInt(i)); } if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop - SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); } break; } @@ -552,7 +552,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { } facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 3) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); } break; } @@ -563,10 +563,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { //The ordering is to ensure that the triangles are all drawn with the same orientation if ((i + 1) % 2 == 0) { //For even n, vertices n + 1, n, and n + 2 define triangle n - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2)); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2)); } else { //For odd n, vertices n, n+1, and n+2 define triangle n - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); } } break; @@ -574,9 +574,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_TRIANGLE_FAN: nFaces = count - 2; facePtr = faces = new aiFace[nFaces]; - SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); for (unsigned int i = 1; i < nFaces; ++i) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); } break; } @@ -590,7 +590,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { nFaces = count; facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; ++i) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, i); + SetFaceAndAdvance1(facePtr, aim->mNumVertices, i); } break; } @@ -603,7 +603,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { } facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 2) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, i, i + 1); } break; } @@ -612,12 +612,12 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_LINE_STRIP: { nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); facePtr = faces = new aiFace[nFaces]; - SetFaceAndAdvance(facePtr, aim->mNumVertices, 0, 1); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, 0, 1); for (unsigned int i = 2; i < count; ++i) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], i); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], i); } if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop - SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); } break; } @@ -630,7 +630,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { } facePtr = faces = new aiFace[nFaces]; for (unsigned int i = 0; i < count; i += 3) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1, i + 2); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, i, i + 1, i + 2); } break; } @@ -641,10 +641,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { //The ordering is to ensure that the triangles are all drawn with the same orientation if ((i + 1) % 2 == 0) { //For even n, vertices n + 1, n, and n + 2 define triangle n - SetFaceAndAdvance(facePtr, aim->mNumVertices, i + 1, i, i + 2); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, i + 1, i, i + 2); } else { //For odd n, vertices n, n+1, and n+2 define triangle n - SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1, i + 2); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, i, i + 1, i + 2); } } break; @@ -652,9 +652,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { case PrimitiveMode_TRIANGLE_FAN: nFaces = count - 2; facePtr = faces = new aiFace[nFaces]; - SetFaceAndAdvance(facePtr, aim->mNumVertices, 0, 1, 2); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, 0, 1, 2); for (unsigned int i = 1; i < nFaces; ++i) { - SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); } break; } From d4f5f29b44d64f3dd44b5e68e90e6d395f528f5b Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 12:22:39 +0100 Subject: [PATCH 06/24] Add missing asset --- .../IndexOutOfRange/AllIndicesOutOfRange.bin | Bin 0 -> 648 bytes .../IndexOutOfRange/AllIndicesOutOfRange.gltf | 142 ++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.bin create mode 100644 test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf diff --git a/test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.bin b/test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.bin new file mode 100644 index 0000000000000000000000000000000000000000..8fef5c7c04bc230c538c8bf7aed41e73d302acb9 GIT binary patch literal 648 zcmb`BfeFAc2n5|Mt7Wr}*5(37MFLW2;Q7-RV}x%GDWcg4dQ@1NcSS)YSh literal 0 HcmV?d00001 diff --git a/test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf b/test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf new file mode 100644 index 000000000..3dbb6efe4 --- /dev/null +++ b/test/models/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf @@ -0,0 +1,142 @@ +{ + "asset": { + "generator": "COLLADA2GLTF", + "version": "2.0" + }, + "scene": 0, + "scenes": [ + { + "nodes": [ + 0 + ] + } + ], + "nodes": [ + { + "children": [ + 1 + ], + "matrix": [ + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + -1.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0 + ] + }, + { + "mesh": 0 + } + ], + "meshes": [ + { + "primitives": [ + { + "attributes": { + "NORMAL": 1, + "POSITION": 2 + }, + "indices": 0, + "mode": 4, + "material": 0 + } + ], + "name": "Mesh" + } + ], + "accessors": [ + { + "bufferView": 0, + "byteOffset": 0, + "componentType": 5123, + "count": 36, + "max": [ + 255 + ], + "min": [ + 0 + ], + "type": "SCALAR" + }, + { + "bufferView": 1, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "max": [ + 1.0, + 1.0, + 1.0 + ], + "min": [ + -1.0, + -1.0, + -1.0 + ], + "type": "VEC3" + }, + { + "bufferView": 1, + "byteOffset": 288, + "componentType": 5126, + "count": 24, + "max": [ + 0.5, + 0.5, + 0.5 + ], + "min": [ + -0.5, + -0.5, + -0.5 + ], + "type": "VEC3" + } + ], + "materials": [ + { + "pbrMetallicRoughness": { + "baseColorFactor": [ + 0.800000011920929, + 0.0, + 0.0, + 1.0 + ], + "metallicFactor": 0.0 + }, + "name": "Red" + } + ], + "bufferViews": [ + { + "buffer": 0, + "byteOffset": 576, + "byteLength": 72, + "target": 34963 + }, + { + "buffer": 0, + "byteOffset": 0, + "byteLength": 576, + "byteStride": 12, + "target": 34962 + } + ], + "buffers": [ + { + "byteLength": 648, + "uri": "AllIndicesOutOfRange.bin" + } + ] +} From f3170a96ba48b6938ff455d6ea5a2cfd5919c884 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 12:36:48 +0100 Subject: [PATCH 07/24] Ensure data does not depend on faces we may not have created. --- code/AssetLib/glTF2/glTF2Importer.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index fae87a990..566e48a69 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -536,10 +536,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { facePtr = faces = new aiFace[nFaces]; SetFaceAndAdvance2(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1)); for (unsigned int i = 2; i < count; ++i) { - SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], data.GetUInt(i)); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, data.GetUInt(i - 1), data.GetUInt(i)); } if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop - SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, data.GetUInt(static_cast(count) - 1), faces[0].mIndices[0]); } break; } @@ -576,7 +576,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { facePtr = faces = new aiFace[nFaces]; SetFaceAndAdvance3(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); for (unsigned int i = 1; i < nFaces; ++i) { - SetFaceAndAdvance3(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(i + 1), data.GetUInt(i + 2)); } break; } @@ -614,10 +614,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { facePtr = faces = new aiFace[nFaces]; SetFaceAndAdvance2(facePtr, aim->mNumVertices, 0, 1); for (unsigned int i = 2; i < count; ++i) { - SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], i); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, i - 1, i); } if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop - SetFaceAndAdvance2(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]); + SetFaceAndAdvance2(facePtr, aim->mNumVertices, count - 1, 0); } break; } @@ -654,7 +654,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { facePtr = faces = new aiFace[nFaces]; SetFaceAndAdvance3(facePtr, aim->mNumVertices, 0, 1, 2); for (unsigned int i = 1; i < nFaces; ++i) { - SetFaceAndAdvance3(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); + SetFaceAndAdvance3(facePtr, aim->mNumVertices, 0, i + 1, i + 2); } break; } From 37e1fb9cd7a85e0716cc17175ac914f403cf25e2 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 14:19:13 +0100 Subject: [PATCH 08/24] Fix message --- code/AssetLib/glTF2/glTF2Importer.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 566e48a69..441219feb 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -299,9 +299,9 @@ void glTF2Importer::ImportMaterials(glTF2::Asset &r) { } static inline void SetFaceAndAdvance1(aiFace*& face, unsigned int numVertices, unsigned int a) { - if (a >= numVertices) { - return; - } + if (a >= numVertices) { + return; + } face->mNumIndices = 1; face->mIndices = new unsigned int[1]; face->mIndices[0] = a; @@ -309,14 +309,14 @@ static inline void SetFaceAndAdvance1(aiFace*& face, unsigned int numVertices, u } static inline void SetFaceAndAdvance2(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b) { - if ((a >= numVertices) || (b >= numVertices)) { - return; - } + if ((a >= numVertices) || (b >= numVertices)) { + return; + } face->mNumIndices = 2; face->mIndices = new unsigned int[2]; face->mIndices[0] = a; face->mIndices[1] = b; - ++face; + ++face; } static inline void SetFaceAndAdvance3(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b, unsigned int c) { @@ -328,7 +328,7 @@ static inline void SetFaceAndAdvance3(aiFace*& face, unsigned int numVertices, u face->mIndices[0] = a; face->mIndices[1] = b; face->mIndices[2] = c; - ++face; + ++face; } #ifdef ASSIMP_BUILD_DEBUG @@ -498,7 +498,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { } aiFace *faces = nullptr; - aiFace *facePtr = nullptr; + aiFace *facePtr = nullptr; size_t nFaces = 0; if (prim.indices) { @@ -664,7 +664,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { aim->mFaces = faces; const unsigned int actualNumFaces = static_cast(facePtr - faces); if (actualNumFaces < nFaces) { - ASSIMP_LOG_WARN("Some faces in mesh had out-of-range indices. Those faces were dropped."); + ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped."); } if (actualNumFaces == 0) { From e1bab44e19d797a9372669a3d3dd333784e26000 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 15:02:27 +0100 Subject: [PATCH 09/24] Exception safety --- code/AssetLib/glTF2/glTF2Importer.cpp | 6 +++--- include/assimp/BaseImporter.h | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 441219feb..18c95d42b 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -347,7 +347,7 @@ static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsign void glTF2Importer::ImportMeshes(glTF2::Asset &r) { ASSIMP_LOG_DEBUG_F("Importing ", r.meshes.Size(), " meshes"); - std::vector meshes; + std::vector> meshes; unsigned int k = 0; meshOffsets.clear(); @@ -361,8 +361,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { for (unsigned int p = 0; p < mesh.primitives.size(); ++p) { Mesh::Primitive &prim = mesh.primitives[p]; - aiMesh *aim = new aiMesh(); - meshes.push_back(aim); + meshes.emplace_back(std::make_unique()); + aiMesh *aim = meshes.back().get(); aim->mName = mesh.name.empty() ? mesh.id : mesh.name; diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index ed146168d..cc14f2f03 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -57,6 +57,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include struct aiScene; struct aiImporterDesc; @@ -391,6 +392,24 @@ public: // static utilities } } + // ------------------------------------------------------------------- + /** Utility function to move a std::vector of unique_ptrs into a aiScene array + * @param vec The vector of unique_ptrs to be moved + * @param out The output pointer to the allocated array. + * @param numOut The output count of elements copied. */ + template + AI_FORCE_INLINE static void CopyVector( + std::vector> &vec, + T **&out, + unsigned int &outLength) { + outLength = unsigned(vec.size()); + if (outLength) { + out = new T*[outLength]; + T** outPtr = out; + std::for_each(vec.begin(), vec.end(), [&outPtr](auto& uPtr){*outPtr = uPtr.release(); ++outPtr; }); + } + } + protected: /// Error description in case there was one. std::string m_ErrorText; From e51e07982d3ae6056f84a9881d6ab3a1d34f935e Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 15:05:42 +0100 Subject: [PATCH 10/24] Remove generic lambda usage. --- include/assimp/BaseImporter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index cc14f2f03..e6ff2e68d 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -406,7 +406,7 @@ public: // static utilities if (outLength) { out = new T*[outLength]; T** outPtr = out; - std::for_each(vec.begin(), vec.end(), [&outPtr](auto& uPtr){*outPtr = uPtr.release(); ++outPtr; }); + std::for_each(vec.begin(), vec.end(), [&outPtr](std::unique_ptr& uPtr){*outPtr = uPtr.release(); ++outPtr; }); } } From 04df5f8b1e585fbe74a0934b07308cb9784fa9c6 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 15:47:25 +0100 Subject: [PATCH 11/24] Don't use make_unique --- code/AssetLib/glTF2/glTF2Importer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 18c95d42b..23d42ba97 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -361,8 +361,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { for (unsigned int p = 0; p < mesh.primitives.size(); ++p) { Mesh::Primitive &prim = mesh.primitives[p]; - meshes.emplace_back(std::make_unique()); - aiMesh *aim = meshes.back().get(); + aiMesh *aim = new aiMesh(); + meshes.push_back(std::unique_ptr(aim)); aim->mName = mesh.name.empty() ? mesh.id : mesh.name; From 719cc82a1f1270396a1d6465a7250ea13bc5a371 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Fri, 17 Jul 2020 10:29:44 +0100 Subject: [PATCH 12/24] Apply clangformat --- code/AssetLib/AC/ACLoader.h | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/code/AssetLib/AC/ACLoader.h b/code/AssetLib/AC/ACLoader.h index e330ddf1b..69f6ae1b0 100644 --- a/code/AssetLib/AC/ACLoader.h +++ b/code/AssetLib/AC/ACLoader.h @@ -69,7 +69,10 @@ public: // Represents an AC3D material struct Material { Material() : - rgb(0.6f, 0.6f, 0.6f), spec(1.f, 1.f, 1.f), shin(0.f), trans(0.f) {} + rgb(0.6f, 0.6f, 0.6f), + spec(1.f, 1.f, 1.f), + shin(0.f), + trans(0.f) {} // base color of the material aiColor3D rgb; @@ -96,7 +99,8 @@ public: // Represents an AC3D surface struct Surface { Surface() : - mat(0), flags(0) {} + mat(0), + flags(0) {} unsigned int mat, flags; @@ -107,7 +111,19 @@ public: // Represents an AC3D object struct Object { Object() : - type(World), name(""), children(), texture(""), texRepeat(1.f, 1.f), texOffset(0.0f, 0.0f), rotation(), translation(), vertices(), surfaces(), numRefs(0), subDiv(0), crease() {} + type(World), + name(""), + children(), + texture(""), + texRepeat(1.f, 1.f), + texOffset(0.0f, 0.0f), + rotation(), + translation(), + vertices(), + surfaces(), + numRefs(0), + subDiv(0), + crease() {} // Type description enum Type { From 17b9403b7a6d5f38066a033e7cd0118af891107c Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Fri, 17 Jul 2020 11:23:50 +0100 Subject: [PATCH 13/24] ACLoader: Use enum for Surface flags --- code/AssetLib/AC/ACLoader.cpp | 33 +++++++++++++++++---------------- code/AssetLib/AC/ACLoader.h | 12 ++++++++++++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/code/AssetLib/AC/ACLoader.cpp b/code/AssetLib/AC/ACLoader.cpp index 5b63d315e..4291ce8d1 100644 --- a/code/AssetLib/AC/ACLoader.cpp +++ b/code/AssetLib/AC/ACLoader.cpp @@ -471,32 +471,33 @@ aiNode *AC3DImporter::ConvertObjectSection(Object &object, ++node->mNumMeshes; } - switch ((*it).flags & 0xf) { + switch ((*it).GetType()) { // closed line - case 0x1: + case Surface::ClosedLine: needMat[idx].first += (unsigned int)(*it).entries.size(); needMat[idx].second += (unsigned int)(*it).entries.size() << 1u; break; // unclosed line - case 0x2: + case Surface::OpenLine: needMat[idx].first += (unsigned int)(*it).entries.size() - 1; needMat[idx].second += ((unsigned int)(*it).entries.size() - 1) << 1u; break; // triangle strip - case 0x4: + case Surface::TriangleStrip: needMat[idx].first += (unsigned int)(*it).entries.size() - 2; needMat[idx].second += ((unsigned int)(*it).entries.size() - 2) * 3; break; - // 0 == polygon, else unknown default: - if ((*it).flags & 0xf) { - ASSIMP_LOG_WARN("AC3D: The type flag of a surface is unknown"); - (*it).flags &= ~(0xf); - } + // Coerce unknowns to a polygon and warn + ASSIMP_LOG_WARN_F("AC3D: The type flag of a surface is unknown: ", (*it).flags); + (*it).flags &= ~(Surface::Mask); + // fallthrough + // polygon + case Surface::Polygon: // the number of faces increments by one, the number // of vertices by surface.numref. needMat[idx].first++; @@ -552,8 +553,8 @@ aiNode *AC3DImporter::ConvertObjectSection(Object &object, const Surface &src = *it; // closed polygon - unsigned int type = (*it).flags & 0xf; - if (!type) { + uint8_t type = (*it).GetType(); + if (type == Surface::Polygon) { aiFace &face = *faces++; face.mNumIndices = (unsigned int)src.entries.size(); if (0 != face.mNumIndices) { @@ -576,7 +577,7 @@ aiNode *AC3DImporter::ConvertObjectSection(Object &object, } } } - } else if (type == 0x4) { + } else if (type == Surface::TriangleStrip) { for (unsigned int i = 0; i < (unsigned int)src.entries.size() - 2; ++i) { const Surface::SurfaceEntry &entry1 = src.entries[i]; const Surface::SurfaceEntry &entry2 = src.entries[i + 1]; @@ -584,8 +585,8 @@ aiNode *AC3DImporter::ConvertObjectSection(Object &object, // skip degenerate triangles if (object.vertices[entry1.first] == object.vertices[entry2.first] || - object.vertices[entry1.first] == object.vertices[entry3.first] || - object.vertices[entry2.first] == object.vertices[entry3.first]) { + object.vertices[entry1.first] == object.vertices[entry3.first] || + object.vertices[entry2.first] == object.vertices[entry3.first]) { mesh->mNumFaces--; mesh->mNumVertices -= 3; continue; @@ -640,7 +641,7 @@ aiNode *AC3DImporter::ConvertObjectSection(Object &object, // either a closed or an unclosed line unsigned int tmp = (unsigned int)(*it).entries.size(); - if (0x2 == type) --tmp; + if (Surface::OpenLine == type) --tmp; for (unsigned int m = 0; m < tmp; ++m) { aiFace &face = *faces++; @@ -663,7 +664,7 @@ aiNode *AC3DImporter::ConvertObjectSection(Object &object, ++uv; } - if (0x1 == type && tmp - 1 == m) { + if (Surface::ClosedLine == type && tmp - 1 == m) { // if this is a closed line repeat its beginning now it2 = (*it).entries.begin(); } else diff --git a/code/AssetLib/AC/ACLoader.h b/code/AssetLib/AC/ACLoader.h index 69f6ae1b0..92a5114f1 100644 --- a/code/AssetLib/AC/ACLoader.h +++ b/code/AssetLib/AC/ACLoader.h @@ -106,6 +106,18 @@ public: typedef std::pair SurfaceEntry; std::vector entries; + + // Type is low nibble of flags + enum Type : uint8_t { + Polygon = 0x0, + ClosedLine = 0x1, + OpenLine = 0x2, + TriangleStrip = 0x4, // ACC extension (TORCS and Speed Dreams) + + Mask = 0xf, + }; + + inline constexpr uint8_t GetType() const { return (flags & Mask); } }; // Represents an AC3D object From ce133e5add2732511365845a8749674723efce10 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Fri, 17 Jul 2020 11:58:17 +0100 Subject: [PATCH 14/24] Set CMake policy CMP0092 Disable cmake's automatic /W3 for MSVC --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b350f6e3..5c1a697ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #---------------------------------------------------------------------- SET(CMAKE_POLICY_DEFAULT_CMP0074 NEW) +cmake_policy(SET CMP0092 NEW) CMAKE_MINIMUM_REQUIRED( VERSION 3.0 ) From a4eceb7b3ff19a86236a56a92d05a0817fd03833 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Fri, 17 Jul 2020 12:06:52 +0100 Subject: [PATCH 15/24] Set CMP0092 the other way --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00819679c..43b9d9d1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #---------------------------------------------------------------------- SET(CMAKE_POLICY_DEFAULT_CMP0074 NEW) -cmake_policy(SET CMP0092 NEW) +SET(CMAKE_POLICY_DEFAULT_CMP0092 NEW) CMAKE_MINIMUM_REQUIRED( VERSION 3.0 ) From 9cad10a99553c1eafd299f68649f15433659fbb4 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Fri, 17 Jul 2020 14:58:51 +0100 Subject: [PATCH 16/24] Disable MSVC warnings on all MSVC Fixes the build on MSVC 2017 (and probably MSVC 2015) --- code/AssetLib/3DS/3DSHelper.h | 9 +- code/AssetLib/IFC/IFCReaderGen_2x3.h | 9 +- code/AssetLib/M3D/m3d.h | 1308 +++++++++---------- code/AssetLib/MDL/HalfLife/HL1MDLLoader.cpp | 4 +- code/AssetLib/Step/STEPFile.h | 8 +- code/Common/Exporter.cpp | 4 +- code/Common/Subdivision.cpp | 4 +- 7 files changed, 678 insertions(+), 668 deletions(-) diff --git a/code/AssetLib/3DS/3DSHelper.h b/code/AssetLib/3DS/3DSHelper.h index 3ccb1fd07..a2be07874 100644 --- a/code/AssetLib/3DS/3DSHelper.h +++ b/code/AssetLib/3DS/3DSHelper.h @@ -321,9 +321,10 @@ public: struct Face : public FaceWithSmoothingGroup { }; -#if _MSC_VER > 1920 +#ifdef _MSC_VER +#pragma warning(push) #pragma warning(disable : 4315) -#endif +#endif // _MSC_VER // --------------------------------------------------------------------------- /** Helper structure representing a texture */ @@ -412,6 +413,10 @@ struct Texture { #include +#ifdef _MSC_VER +#pragma warning(pop) +#endif // _MSC_VER + // --------------------------------------------------------------------------- /** Helper structure representing a 3ds material */ struct Material { diff --git a/code/AssetLib/IFC/IFCReaderGen_2x3.h b/code/AssetLib/IFC/IFCReaderGen_2x3.h index b3f71a7f1..f87f121b9 100644 --- a/code/AssetLib/IFC/IFCReaderGen_2x3.h +++ b/code/AssetLib/IFC/IFCReaderGen_2x3.h @@ -45,9 +45,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "AssetLib/Step/STEPFile.h" -#if _MSC_VER > 1920 +#ifdef _MSC_VER +# pragma warning(push) # pragma warning( disable : 4512 ) -#endif // _WIN32 +#endif // _MSC_VER namespace Assimp { namespace IFC { @@ -4372,4 +4373,8 @@ namespace STEP { } //! STEP } //! Assimp +#ifdef _MSC_VER +# pragma warning(pop) +#endif // _MSC_VER + #endif // INCLUDED_IFC_READER_GEN_H diff --git a/code/AssetLib/M3D/m3d.h b/code/AssetLib/M3D/m3d.h index 5ab3d16de..d8e13db4d 100644 --- a/code/AssetLib/M3D/m3d.h +++ b/code/AssetLib/M3D/m3d.h @@ -101,13 +101,13 @@ typedef uint16_t M3D_INDEX; #define _register #endif -#if _MSC_VER > 1920 +#ifdef _MSC_VER # pragma warning(push) # pragma warning(disable : 4100 4127 4189 4505 4244 4403 4701 4703) # if (_MSC_VER > 1800 ) # pragma warning(disable : 5573 5744) # endif -#endif // _WIN32 +#endif // _MSC_VER /*** File format structures ***/ @@ -821,7 +821,7 @@ static unsigned char *_m3dstbi__convert_format(unsigned char *data, int img_n, i break; STBI__CASE(4, 3) { dest[0] = src[0], dest[1] = src[1], dest[2] = src[2]; } break; - default: STBI_ASSERT(0); + default: STBI_ASSERT(0); } #undef STBI__CASE } @@ -881,7 +881,7 @@ static _m3dstbi__uint16 *_m3dstbi__convert_format16(_m3dstbi__uint16 *data, int break; STBI__CASE(4, 3) { dest[0] = src[0], dest[1] = src[1], dest[2] = src[2]; } break; - default: STBI_ASSERT(0); + default: STBI_ASSERT(0); } #undef STBI__CASE } @@ -1358,13 +1358,13 @@ static int _m3dstbi__create_png_image_raw(_m3dstbi__png *a, unsigned char *raw, for (k = 0; k < filter_bytes; ++k) { switch (filter) { - case STBI__F_none: cur[k] = raw[k]; break; - case STBI__F_sub: cur[k] = raw[k]; break; - case STBI__F_up: cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break; - case STBI__F_avg: cur[k] = STBI__BYTECAST(raw[k] + (prior[k] >> 1)); break; - case STBI__F_paeth: cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(0, prior[k], 0)); break; - case STBI__F_avg_first: cur[k] = raw[k]; break; - case STBI__F_paeth_first: cur[k] = raw[k]; break; + case STBI__F_none: cur[k] = raw[k]; break; + case STBI__F_sub: cur[k] = raw[k]; break; + case STBI__F_up: cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break; + case STBI__F_avg: cur[k] = STBI__BYTECAST(raw[k] + (prior[k] >> 1)); break; + case STBI__F_paeth: cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(0, prior[k], 0)); break; + case STBI__F_avg_first: cur[k] = raw[k]; break; + case STBI__F_paeth_first: cur[k] = raw[k]; break; } } @@ -1394,21 +1394,21 @@ static int _m3dstbi__create_png_image_raw(_m3dstbi__png *a, unsigned char *raw, case f: \ for (k = 0; k < nk; ++k) switch (filter) { - case STBI__F_none: - memcpy(cur, raw, nk); - break; - STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k - filter_bytes]); } - break; - STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } - break; - STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k - filter_bytes]) >> 1)); } - break; - STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - filter_bytes], prior[k], prior[k - filter_bytes])); } - break; - STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k - filter_bytes] >> 1)); } - break; - STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - filter_bytes], 0, 0)); } - break; + case STBI__F_none: + memcpy(cur, raw, nk); + break; + STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k - filter_bytes]); } + break; + STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } + break; + STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k - filter_bytes]) >> 1)); } + break; + STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - filter_bytes], prior[k], prior[k - filter_bytes])); } + break; + STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k - filter_bytes] >> 1)); } + break; + STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - filter_bytes], 0, 0)); } + break; } #undef STBI__CASE raw += nk; @@ -1658,155 +1658,155 @@ static int _m3dstbi__parse_png_file(_m3dstbi__png *z, int scan, int req_comp) { for (;;) { _m3dstbi__pngchunk c = _m3dstbi__get_chunk_header(s); switch (c.type) { - case STBI__PNG_TYPE('C', 'g', 'B', 'I'): - _m3dstbi__skip(s, c.length); - break; - case STBI__PNG_TYPE('I', 'H', 'D', 'R'): { - int comp, filter; - if (!first) return _m3dstbi__err("multiple IHDR", "Corrupt PNG"); - first = 0; - if (c.length != 13) return _m3dstbi__err("bad IHDR len", "Corrupt PNG"); - s->img_x = _m3dstbi__get32be(s); - if (s->img_x > (1 << 24)) return _m3dstbi__err("too large", "Very large image (corrupt?)"); - s->img_y = _m3dstbi__get32be(s); - if (s->img_y > (1 << 24)) return _m3dstbi__err("too large", "Very large image (corrupt?)"); - z->depth = _m3dstbi__get8(s); - if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return _m3dstbi__err("1/2/4/8/16-bit only", "PNG not supported: 1/2/4/8/16-bit only"); - color = _m3dstbi__get8(s); - if (color > 6) return _m3dstbi__err("bad ctype", "Corrupt PNG"); - if (color == 3 && z->depth == 16) return _m3dstbi__err("bad ctype", "Corrupt PNG"); - if (color == 3) - pal_img_n = 3; - else if (color & 1) - return _m3dstbi__err("bad ctype", "Corrupt PNG"); - comp = _m3dstbi__get8(s); - if (comp) return _m3dstbi__err("bad comp method", "Corrupt PNG"); - filter = _m3dstbi__get8(s); - if (filter) return _m3dstbi__err("bad filter method", "Corrupt PNG"); - interlace = _m3dstbi__get8(s); - if (interlace > 1) return _m3dstbi__err("bad interlace method", "Corrupt PNG"); - if (!s->img_x || !s->img_y) return _m3dstbi__err("0-pixel image", "Corrupt PNG"); - if (!pal_img_n) { - s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); - if ((1 << 30) / s->img_x / s->img_n < s->img_y) return _m3dstbi__err("too large", "Image too large to decode"); - if (scan == STBI__SCAN_header) return 1; - } else { - s->img_n = 1; - if ((1 << 30) / s->img_x / 4 < s->img_y) return _m3dstbi__err("too large", "Corrupt PNG"); - } - break; + case STBI__PNG_TYPE('C', 'g', 'B', 'I'): + _m3dstbi__skip(s, c.length); + break; + case STBI__PNG_TYPE('I', 'H', 'D', 'R'): { + int comp, filter; + if (!first) return _m3dstbi__err("multiple IHDR", "Corrupt PNG"); + first = 0; + if (c.length != 13) return _m3dstbi__err("bad IHDR len", "Corrupt PNG"); + s->img_x = _m3dstbi__get32be(s); + if (s->img_x > (1 << 24)) return _m3dstbi__err("too large", "Very large image (corrupt?)"); + s->img_y = _m3dstbi__get32be(s); + if (s->img_y > (1 << 24)) return _m3dstbi__err("too large", "Very large image (corrupt?)"); + z->depth = _m3dstbi__get8(s); + if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return _m3dstbi__err("1/2/4/8/16-bit only", "PNG not supported: 1/2/4/8/16-bit only"); + color = _m3dstbi__get8(s); + if (color > 6) return _m3dstbi__err("bad ctype", "Corrupt PNG"); + if (color == 3 && z->depth == 16) return _m3dstbi__err("bad ctype", "Corrupt PNG"); + if (color == 3) + pal_img_n = 3; + else if (color & 1) + return _m3dstbi__err("bad ctype", "Corrupt PNG"); + comp = _m3dstbi__get8(s); + if (comp) return _m3dstbi__err("bad comp method", "Corrupt PNG"); + filter = _m3dstbi__get8(s); + if (filter) return _m3dstbi__err("bad filter method", "Corrupt PNG"); + interlace = _m3dstbi__get8(s); + if (interlace > 1) return _m3dstbi__err("bad interlace method", "Corrupt PNG"); + if (!s->img_x || !s->img_y) return _m3dstbi__err("0-pixel image", "Corrupt PNG"); + if (!pal_img_n) { + s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); + if ((1 << 30) / s->img_x / s->img_n < s->img_y) return _m3dstbi__err("too large", "Image too large to decode"); + if (scan == STBI__SCAN_header) return 1; + } else { + s->img_n = 1; + if ((1 << 30) / s->img_x / 4 < s->img_y) return _m3dstbi__err("too large", "Corrupt PNG"); } + break; + } - case STBI__PNG_TYPE('P', 'L', 'T', 'E'): { - if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); - if (c.length > 256 * 3) return _m3dstbi__err("invalid PLTE", "Corrupt PNG"); - pal_len = c.length / 3; - if (pal_len * 3 != c.length) return _m3dstbi__err("invalid PLTE", "Corrupt PNG"); - for (i = 0; i < pal_len; ++i) { - palette[i * 4 + 0] = _m3dstbi__get8(s); - palette[i * 4 + 1] = _m3dstbi__get8(s); - palette[i * 4 + 2] = _m3dstbi__get8(s); - palette[i * 4 + 3] = 255; - } - break; + case STBI__PNG_TYPE('P', 'L', 'T', 'E'): { + if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); + if (c.length > 256 * 3) return _m3dstbi__err("invalid PLTE", "Corrupt PNG"); + pal_len = c.length / 3; + if (pal_len * 3 != c.length) return _m3dstbi__err("invalid PLTE", "Corrupt PNG"); + for (i = 0; i < pal_len; ++i) { + palette[i * 4 + 0] = _m3dstbi__get8(s); + palette[i * 4 + 1] = _m3dstbi__get8(s); + palette[i * 4 + 2] = _m3dstbi__get8(s); + palette[i * 4 + 3] = 255; } + break; + } - case STBI__PNG_TYPE('t', 'R', 'N', 'S'): { - if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); - if (z->idata) return _m3dstbi__err("tRNS after IDAT", "Corrupt PNG"); - if (pal_img_n) { - if (scan == STBI__SCAN_header) { - s->img_n = 4; - return 1; - } - if (pal_len == 0) return _m3dstbi__err("tRNS before PLTE", "Corrupt PNG"); - if (c.length > pal_len) return _m3dstbi__err("bad tRNS len", "Corrupt PNG"); - pal_img_n = 4; - for (i = 0; i < c.length; ++i) - palette[i * 4 + 3] = _m3dstbi__get8(s); - } else { - if (!(s->img_n & 1)) return _m3dstbi__err("tRNS with alpha", "Corrupt PNG"); - if (c.length != (_m3dstbi__uint32)s->img_n * 2) return _m3dstbi__err("bad tRNS len", "Corrupt PNG"); - has_trans = 1; - if (z->depth == 16) { - for (k = 0; k < s->img_n; ++k) - tc16[k] = (_m3dstbi__uint16)_m3dstbi__get16be(s); - } else { - for (k = 0; k < s->img_n; ++k) - tc[k] = (unsigned char)(_m3dstbi__get16be(s) & 255) * _m3dstbi__depth_scale_table[z->depth]; - } - } - break; - } - - case STBI__PNG_TYPE('I', 'D', 'A', 'T'): { - if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); - if (pal_img_n && !pal_len) return _m3dstbi__err("no PLTE", "Corrupt PNG"); + case STBI__PNG_TYPE('t', 'R', 'N', 'S'): { + if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); + if (z->idata) return _m3dstbi__err("tRNS after IDAT", "Corrupt PNG"); + if (pal_img_n) { if (scan == STBI__SCAN_header) { - s->img_n = pal_img_n; + s->img_n = 4; return 1; } - if ((int)(ioff + c.length) < (int)ioff) return 0; - if (ioff + c.length > idata_limit) { - _m3dstbi__uint32 idata_limit_old = idata_limit; - unsigned char *p; - if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; - while (ioff + c.length > idata_limit) - idata_limit *= 2; - STBI_NOTUSED(idata_limit_old); - p = (unsigned char *)STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); - if (p == NULL) return _m3dstbi__err("outofmem", "Out of memory"); - z->idata = p; + if (pal_len == 0) return _m3dstbi__err("tRNS before PLTE", "Corrupt PNG"); + if (c.length > pal_len) return _m3dstbi__err("bad tRNS len", "Corrupt PNG"); + pal_img_n = 4; + for (i = 0; i < c.length; ++i) + palette[i * 4 + 3] = _m3dstbi__get8(s); + } else { + if (!(s->img_n & 1)) return _m3dstbi__err("tRNS with alpha", "Corrupt PNG"); + if (c.length != (_m3dstbi__uint32)s->img_n * 2) return _m3dstbi__err("bad tRNS len", "Corrupt PNG"); + has_trans = 1; + if (z->depth == 16) { + for (k = 0; k < s->img_n; ++k) + tc16[k] = (_m3dstbi__uint16)_m3dstbi__get16be(s); + } else { + for (k = 0; k < s->img_n; ++k) + tc[k] = (unsigned char)(_m3dstbi__get16be(s) & 255) * _m3dstbi__depth_scale_table[z->depth]; } - if (!_m3dstbi__getn(s, z->idata + ioff, c.length)) return _m3dstbi__err("outofdata", "Corrupt PNG"); - ioff += c.length; - break; } + break; + } - case STBI__PNG_TYPE('I', 'E', 'N', 'D'): { - _m3dstbi__uint32 raw_len, bpl; - if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); - if (scan != STBI__SCAN_load) return 1; - if (z->idata == NULL) return _m3dstbi__err("no IDAT", "Corrupt PNG"); - bpl = (s->img_x * z->depth + 7) / 8; - raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; - z->expanded = (unsigned char *)_m3dstbi_zlib_decode_malloc_guesssize_headerflag((char *)z->idata, ioff, raw_len, (int *)&raw_len, 1); - if (z->expanded == NULL) return 0; - STBI_FREE(z->idata); - z->idata = NULL; - if ((req_comp == s->img_n + 1 && req_comp != 3 && !pal_img_n) || has_trans) - s->img_out_n = s->img_n + 1; - else - s->img_out_n = s->img_n; - if (!_m3dstbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; - if (has_trans) { - if (z->depth == 16) { - if (!_m3dstbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; - } else { - if (!_m3dstbi__compute_transparency(z, tc, s->img_out_n)) return 0; - } - } - if (pal_img_n) { - s->img_n = pal_img_n; - s->img_out_n = pal_img_n; - if (req_comp >= 3) s->img_out_n = req_comp; - if (!_m3dstbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) - return 0; - } else if (has_trans) { - ++s->img_n; - } - STBI_FREE(z->expanded); - z->expanded = NULL; + case STBI__PNG_TYPE('I', 'D', 'A', 'T'): { + if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); + if (pal_img_n && !pal_len) return _m3dstbi__err("no PLTE", "Corrupt PNG"); + if (scan == STBI__SCAN_header) { + s->img_n = pal_img_n; return 1; } + if ((int)(ioff + c.length) < (int)ioff) return 0; + if (ioff + c.length > idata_limit) { + _m3dstbi__uint32 idata_limit_old = idata_limit; + unsigned char *p; + if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; + while (ioff + c.length > idata_limit) + idata_limit *= 2; + STBI_NOTUSED(idata_limit_old); + p = (unsigned char *)STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); + if (p == NULL) return _m3dstbi__err("outofmem", "Out of memory"); + z->idata = p; + } + if (!_m3dstbi__getn(s, z->idata + ioff, c.length)) return _m3dstbi__err("outofdata", "Corrupt PNG"); + ioff += c.length; + break; + } - default: - if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); - if ((c.type & (1 << 29)) == 0) { - return _m3dstbi__err("invalid_chunk", "PNG not supported: unknown PNG chunk type"); + case STBI__PNG_TYPE('I', 'E', 'N', 'D'): { + _m3dstbi__uint32 raw_len, bpl; + if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); + if (scan != STBI__SCAN_load) return 1; + if (z->idata == NULL) return _m3dstbi__err("no IDAT", "Corrupt PNG"); + bpl = (s->img_x * z->depth + 7) / 8; + raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; + z->expanded = (unsigned char *)_m3dstbi_zlib_decode_malloc_guesssize_headerflag((char *)z->idata, ioff, raw_len, (int *)&raw_len, 1); + if (z->expanded == NULL) return 0; + STBI_FREE(z->idata); + z->idata = NULL; + if ((req_comp == s->img_n + 1 && req_comp != 3 && !pal_img_n) || has_trans) + s->img_out_n = s->img_n + 1; + else + s->img_out_n = s->img_n; + if (!_m3dstbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; + if (has_trans) { + if (z->depth == 16) { + if (!_m3dstbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; + } else { + if (!_m3dstbi__compute_transparency(z, tc, s->img_out_n)) return 0; } - _m3dstbi__skip(s, c.length); - break; + } + if (pal_img_n) { + s->img_n = pal_img_n; + s->img_out_n = pal_img_n; + if (req_comp >= 3) s->img_out_n = req_comp; + if (!_m3dstbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) + return 0; + } else if (has_trans) { + ++s->img_n; + } + STBI_FREE(z->expanded); + z->expanded = NULL; + return 1; + } + + default: + if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG"); + if ((c.type & (1 << 29)) == 0) { + return _m3dstbi__err("invalid_chunk", "PNG not supported: unknown PNG chunk type"); + } + _m3dstbi__skip(s, c.length); + break; } _m3dstbi__get32be(s); } @@ -2283,18 +2283,18 @@ void _m3d_getpr(m3d_t *model, _unused m3dread_t readfilecb, _unused m3dfree_t fr } while (0) _inline static unsigned char *_m3d_getidx(unsigned char *data, char type, M3D_INDEX *idx) { switch (type) { - case 1: - *idx = data[0] > 253 ? (int8_t)data[0] : data[0]; - data++; - break; - case 2: - *idx = *((uint16_t *)data) > 65533 ? *((int16_t *)data) : *((uint16_t *)data); - data += 2; - break; - case 4: - *idx = *((int32_t *)data); - data += 4; - break; + case 1: + *idx = data[0] > 253 ? (int8_t)data[0] : data[0]; + data++; + break; + case 2: + *idx = *((uint16_t *)data) > 65533 ? *((int16_t *)data) : *((uint16_t *)data); + data += 2; + break; + case 4: + *idx = *((int32_t *)data); + data += 4; + break; } return data; } @@ -2682,27 +2682,27 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d if (!m->prop) goto memerr; m->prop[j].type = n + (k == m3dpf_map && n < 128 ? 128 : 0); switch (k) { - case m3dpf_color: ptr = _m3d_gethex(ptr, &m->prop[j].value.color); break; - case m3dpf_uint8: - case m3dpf_uint16: - case m3dpf_uint32: ptr = _m3d_getint(ptr, &m->prop[j].value.num); break; - case m3dpf_float: ptr = _m3d_getfloat(ptr, &m->prop[j].value.fnum); break; - case m3dpf_map: - pe = _m3d_safestr(ptr, 0); - if (!pe || !*pe) goto asciiend; - m->prop[j].value.textureid = _m3d_gettx(model, readfilecb, freecb, pe); - if (model->errcode == M3D_ERR_ALLOC) { - M3D_FREE(pe); - goto memerr; - } - /* this error code only returned if readfilecb was specified */ - if (m->prop[j].value.textureid == M3D_UNDEF) { - M3D_LOG("Texture not found"); - M3D_LOG(pe); - m->numprop--; - } + case m3dpf_color: ptr = _m3d_gethex(ptr, &m->prop[j].value.color); break; + case m3dpf_uint8: + case m3dpf_uint16: + case m3dpf_uint32: ptr = _m3d_getint(ptr, &m->prop[j].value.num); break; + case m3dpf_float: ptr = _m3d_getfloat(ptr, &m->prop[j].value.fnum); break; + case m3dpf_map: + pe = _m3d_safestr(ptr, 0); + if (!pe || !*pe) goto asciiend; + m->prop[j].value.textureid = _m3d_gettx(model, readfilecb, freecb, pe); + if (model->errcode == M3D_ERR_ALLOC) { M3D_FREE(pe); - break; + goto memerr; + } + /* this error code only returned if readfilecb was specified */ + if (m->prop[j].value.textureid == M3D_UNDEF) { + M3D_LOG("Texture not found"); + M3D_LOG(pe); + m->numprop--; + } + M3D_FREE(pe); + break; } } else { M3D_LOG("Unknown material property in"); @@ -2835,48 +2835,48 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d } if (*ptr == ']' || *ptr == '\r' || *ptr == '\n') break; switch (cd->a[((k - n) % (cd->p - n)) + n]) { - case m3dcp_mi_t: - mi = M3D_UNDEF; - if (*ptr != '\r' && *ptr != '\n') { - pe = _m3d_safestr(ptr, 0); - if (!pe || !*pe) goto asciiend; - for (n = 0; n < model->nummaterial; n++) - if (!strcmp(pe, model->material[n].name)) { - mi = (M3D_INDEX)n; - break; - } - if (mi == M3D_UNDEF && !(model->flags & M3D_FLG_MTLLIB)) { - mi = model->nummaterial++; - model->material = (m3dm_t *)M3D_REALLOC(model->material, - model->nummaterial * sizeof(m3dm_t)); - if (!model->material) goto memerr; - model->material[mi].name = pe; - model->material[mi].numprop = 1; - model->material[mi].prop = NULL; - } else - M3D_FREE(pe); - } - h->cmd[j].arg[k] = mi; - break; - case m3dcp_vc_t: - _m3d_getfloat(ptr, &w); - h->cmd[j].arg[k] = *((uint32_t *)&w); - break; - case m3dcp_va_t: - ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]); - n = k + 1; - l += (h->cmd[j].arg[k] - 1) * (cd->p - k - 1); - h->cmd[j].arg = (uint32_t *)M3D_REALLOC(h->cmd[j].arg, l * sizeof(uint32_t)); - if (!h->cmd[j].arg) goto memerr; - memset(&h->cmd[j].arg[k + 1], 0, (l - k - 1) * sizeof(uint32_t)); - break; - case m3dcp_qi_t: - ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]); - model->vertex[h->cmd[i].arg[k]].skinid = M3D_INDEXMAX; - break; - default: - ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]); - break; + case m3dcp_mi_t: + mi = M3D_UNDEF; + if (*ptr != '\r' && *ptr != '\n') { + pe = _m3d_safestr(ptr, 0); + if (!pe || !*pe) goto asciiend; + for (n = 0; n < model->nummaterial; n++) + if (!strcmp(pe, model->material[n].name)) { + mi = (M3D_INDEX)n; + break; + } + if (mi == M3D_UNDEF && !(model->flags & M3D_FLG_MTLLIB)) { + mi = model->nummaterial++; + model->material = (m3dm_t *)M3D_REALLOC(model->material, + model->nummaterial * sizeof(m3dm_t)); + if (!model->material) goto memerr; + model->material[mi].name = pe; + model->material[mi].numprop = 1; + model->material[mi].prop = NULL; + } else + M3D_FREE(pe); + } + h->cmd[j].arg[k] = mi; + break; + case m3dcp_vc_t: + _m3d_getfloat(ptr, &w); + h->cmd[j].arg[k] = *((uint32_t *)&w); + break; + case m3dcp_va_t: + ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]); + n = k + 1; + l += (h->cmd[j].arg[k] - 1) * (cd->p - k - 1); + h->cmd[j].arg = (uint32_t *)M3D_REALLOC(h->cmd[j].arg, l * sizeof(uint32_t)); + if (!h->cmd[j].arg) goto memerr; + memset(&h->cmd[j].arg[k + 1], 0, (l - k - 1) * sizeof(uint32_t)); + break; + case m3dcp_qi_t: + ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]); + model->vertex[h->cmd[i].arg[k]].skinid = M3D_INDEXMAX; + break; + default: + ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]); + break; } } } else { @@ -3207,22 +3207,22 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d if (!model->tmap) goto memerr; for (i = 0, data += sizeof(m3dchunk_t); data < chunk; i++) { switch (model->vc_s) { - case 1: - model->tmap[i].u = (M3D_FLOAT)(data[0]) / (M3D_FLOAT)255.0; - model->tmap[i].v = (M3D_FLOAT)(data[1]) / (M3D_FLOAT)255.0; - break; - case 2: - model->tmap[i].u = (M3D_FLOAT)(*((int16_t *)(data + 0))) / (M3D_FLOAT)65535.0; - model->tmap[i].v = (M3D_FLOAT)(*((int16_t *)(data + 2))) / (M3D_FLOAT)65535.0; - break; - case 4: - model->tmap[i].u = (M3D_FLOAT)(*((float *)(data + 0))); - model->tmap[i].v = (M3D_FLOAT)(*((float *)(data + 4))); - break; - case 8: - model->tmap[i].u = (M3D_FLOAT)(*((double *)(data + 0))); - model->tmap[i].v = (M3D_FLOAT)(*((double *)(data + 8))); - break; + case 1: + model->tmap[i].u = (M3D_FLOAT)(data[0]) / (M3D_FLOAT)255.0; + model->tmap[i].v = (M3D_FLOAT)(data[1]) / (M3D_FLOAT)255.0; + break; + case 2: + model->tmap[i].u = (M3D_FLOAT)(*((int16_t *)(data + 0))) / (M3D_FLOAT)65535.0; + model->tmap[i].v = (M3D_FLOAT)(*((int16_t *)(data + 2))) / (M3D_FLOAT)65535.0; + break; + case 4: + model->tmap[i].u = (M3D_FLOAT)(*((float *)(data + 0))); + model->tmap[i].v = (M3D_FLOAT)(*((float *)(data + 4))); + break; + case 8: + model->tmap[i].u = (M3D_FLOAT)(*((double *)(data + 0))); + model->tmap[i].v = (M3D_FLOAT)(*((double *)(data + 8))); + break; } data += reclen; } @@ -3243,49 +3243,49 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d memset(model->vertex, 0, model->numvertex * sizeof(m3dv_t)); for (i = 0, data += sizeof(m3dchunk_t); data < chunk && i < model->numvertex; i++) { switch (model->vc_s) { - case 1: - model->vertex[i].x = (M3D_FLOAT)((int8_t)data[0]) / (M3D_FLOAT)127.0; - model->vertex[i].y = (M3D_FLOAT)((int8_t)data[1]) / (M3D_FLOAT)127.0; - model->vertex[i].z = (M3D_FLOAT)((int8_t)data[2]) / (M3D_FLOAT)127.0; - model->vertex[i].w = (M3D_FLOAT)((int8_t)data[3]) / (M3D_FLOAT)127.0; - data += 4; - break; - case 2: - model->vertex[i].x = (M3D_FLOAT)(*((int16_t *)(data + 0))) / (M3D_FLOAT)32767.0; - model->vertex[i].y = (M3D_FLOAT)(*((int16_t *)(data + 2))) / (M3D_FLOAT)32767.0; - model->vertex[i].z = (M3D_FLOAT)(*((int16_t *)(data + 4))) / (M3D_FLOAT)32767.0; - model->vertex[i].w = (M3D_FLOAT)(*((int16_t *)(data + 6))) / (M3D_FLOAT)32767.0; - data += 8; - break; - case 4: - model->vertex[i].x = (M3D_FLOAT)(*((float *)(data + 0))); - model->vertex[i].y = (M3D_FLOAT)(*((float *)(data + 4))); - model->vertex[i].z = (M3D_FLOAT)(*((float *)(data + 8))); - model->vertex[i].w = (M3D_FLOAT)(*((float *)(data + 12))); - data += 16; - break; - case 8: - model->vertex[i].x = (M3D_FLOAT)(*((double *)(data + 0))); - model->vertex[i].y = (M3D_FLOAT)(*((double *)(data + 8))); - model->vertex[i].z = (M3D_FLOAT)(*((double *)(data + 16))); - model->vertex[i].w = (M3D_FLOAT)(*((double *)(data + 24))); - data += 32; - break; + case 1: + model->vertex[i].x = (M3D_FLOAT)((int8_t)data[0]) / (M3D_FLOAT)127.0; + model->vertex[i].y = (M3D_FLOAT)((int8_t)data[1]) / (M3D_FLOAT)127.0; + model->vertex[i].z = (M3D_FLOAT)((int8_t)data[2]) / (M3D_FLOAT)127.0; + model->vertex[i].w = (M3D_FLOAT)((int8_t)data[3]) / (M3D_FLOAT)127.0; + data += 4; + break; + case 2: + model->vertex[i].x = (M3D_FLOAT)(*((int16_t *)(data + 0))) / (M3D_FLOAT)32767.0; + model->vertex[i].y = (M3D_FLOAT)(*((int16_t *)(data + 2))) / (M3D_FLOAT)32767.0; + model->vertex[i].z = (M3D_FLOAT)(*((int16_t *)(data + 4))) / (M3D_FLOAT)32767.0; + model->vertex[i].w = (M3D_FLOAT)(*((int16_t *)(data + 6))) / (M3D_FLOAT)32767.0; + data += 8; + break; + case 4: + model->vertex[i].x = (M3D_FLOAT)(*((float *)(data + 0))); + model->vertex[i].y = (M3D_FLOAT)(*((float *)(data + 4))); + model->vertex[i].z = (M3D_FLOAT)(*((float *)(data + 8))); + model->vertex[i].w = (M3D_FLOAT)(*((float *)(data + 12))); + data += 16; + break; + case 8: + model->vertex[i].x = (M3D_FLOAT)(*((double *)(data + 0))); + model->vertex[i].y = (M3D_FLOAT)(*((double *)(data + 8))); + model->vertex[i].z = (M3D_FLOAT)(*((double *)(data + 16))); + model->vertex[i].w = (M3D_FLOAT)(*((double *)(data + 24))); + data += 32; + break; } switch (model->ci_s) { - case 1: - model->vertex[i].color = model->cmap ? model->cmap[data[0]] : 0; - data++; - break; - case 2: - model->vertex[i].color = model->cmap ? model->cmap[*((uint16_t *)data)] : 0; - data += 2; - break; - case 4: - model->vertex[i].color = *((uint32_t *)data); - data += 4; - break; - /* case 8: break; */ + case 1: + model->vertex[i].color = model->cmap ? model->cmap[data[0]] : 0; + data++; + break; + case 2: + model->vertex[i].color = model->cmap ? model->cmap[*((uint16_t *)data)] : 0; + data += 2; + break; + case 4: + model->vertex[i].color = *((uint32_t *)data); + data += 4; + break; + /* case 8: break; */ } model->vertex[i].skinid = M3D_UNDEF; data = _m3d_getidx(data, model->sk_s, &model->vertex[i].skinid); @@ -3414,55 +3414,55 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d } } switch (k) { - case m3dpf_color: - switch (model->ci_s) { - case 1: - m->prop[i].value.color = model->cmap ? model->cmap[data[0]] : 0; - data++; - break; - case 2: - m->prop[i].value.color = model->cmap ? model->cmap[*((uint16_t *)data)] : 0; - data += 2; - break; - case 4: - m->prop[i].value.color = *((uint32_t *)data); - data += 4; - break; - } + case m3dpf_color: + switch (model->ci_s) { + case 1: + m->prop[i].value.color = model->cmap ? model->cmap[data[0]] : 0; + data++; break; - - case m3dpf_uint8: m->prop[i].value.num = *data++; break; - case m3dpf_uint16: - m->prop[i].value.num = *((uint16_t *)data); + case 2: + m->prop[i].value.color = model->cmap ? model->cmap[*((uint16_t *)data)] : 0; data += 2; break; - case m3dpf_uint32: - m->prop[i].value.num = *((uint32_t *)data); - data += 4; - break; - case m3dpf_float: - m->prop[i].value.fnum = *((float *)data); + case 4: + m->prop[i].value.color = *((uint32_t *)data); data += 4; break; + } + break; - case m3dpf_map: - M3D_GETSTR(name); - m->prop[i].value.textureid = _m3d_gettx(model, readfilecb, freecb, name); - if (model->errcode == M3D_ERR_ALLOC) goto memerr; - /* this error code only returned if readfilecb was specified */ - if (m->prop[i].value.textureid == M3D_UNDEF) { - M3D_LOG("Texture not found"); - M3D_LOG(m->name); - m->numprop--; - } - break; + case m3dpf_uint8: m->prop[i].value.num = *data++; break; + case m3dpf_uint16: + m->prop[i].value.num = *((uint16_t *)data); + data += 2; + break; + case m3dpf_uint32: + m->prop[i].value.num = *((uint32_t *)data); + data += 4; + break; + case m3dpf_float: + m->prop[i].value.fnum = *((float *)data); + data += 4; + break; - default: - M3D_LOG("Unknown material property in"); + case m3dpf_map: + M3D_GETSTR(name); + m->prop[i].value.textureid = _m3d_gettx(model, readfilecb, freecb, name); + if (model->errcode == M3D_ERR_ALLOC) goto memerr; + /* this error code only returned if readfilecb was specified */ + if (m->prop[i].value.textureid == M3D_UNDEF) { + M3D_LOG("Texture not found"); M3D_LOG(m->name); - model->errcode = M3D_ERR_UNKPROP; - data = chunk; - break; + m->numprop--; + } + break; + + default: + M3D_LOG("Unknown material property in"); + M3D_LOG(m->name); + model->errcode = M3D_ERR_UNKPROP; + data = chunk; + break; } } m->prop = (m3dp_t *)M3D_REALLOC(m->prop, m->numprop * sizeof(m3dp_t)); @@ -3570,45 +3570,45 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d memset(h->cmd[i].arg, 0, cd->p * sizeof(uint32_t)); for (k = n = 0, l = cd->p; k < l; k++) switch (cd->a[((k - n) % (cd->p - n)) + n]) { - case m3dcp_mi_t: - h->cmd[i].arg[k] = M3D_NOTDEFINED; - M3D_GETSTR(name); - if (name) { - for (n = 0; n < model->nummaterial; n++) - if (!strcmp(name, model->material[n].name)) { - h->cmd[i].arg[k] = n; - break; - } - if (h->cmd[i].arg[k] == M3D_NOTDEFINED) model->errcode = M3D_ERR_MTRL; - } - break; - case m3dcp_vc_t: - f = 0.0f; - switch (model->vc_s) { - case 1: f = (float)((int8_t)data[0]) / 127; break; - case 2: f = (float)(*((int16_t *)(data + 0))) / 32767; break; - case 4: f = (float)(*((float *)(data + 0))); break; - case 8: f = (float)(*((double *)(data + 0))); break; - } - h->cmd[i].arg[k] = *((uint32_t *)&f); - data += model->vc_s; - break; - case m3dcp_hi_t: data = _m3d_getidx(data, model->hi_s, &h->cmd[i].arg[k]); break; - case m3dcp_fi_t: data = _m3d_getidx(data, model->fi_s, &h->cmd[i].arg[k]); break; - case m3dcp_ti_t: data = _m3d_getidx(data, model->ti_s, &h->cmd[i].arg[k]); break; - case m3dcp_qi_t: - case m3dcp_vi_t: data = _m3d_getidx(data, model->vi_s, &h->cmd[i].arg[k]); break; - case m3dcp_i1_t: data = _m3d_getidx(data, 1, &h->cmd[i].arg[k]); break; - case m3dcp_i2_t: data = _m3d_getidx(data, 2, &h->cmd[i].arg[k]); break; - case m3dcp_i4_t: data = _m3d_getidx(data, 4, &h->cmd[i].arg[k]); break; - case m3dcp_va_t: - data = _m3d_getidx(data, 4, &h->cmd[i].arg[k]); - n = k + 1; - l += (h->cmd[i].arg[k] - 1) * (cd->p - k - 1); - h->cmd[i].arg = (uint32_t *)M3D_REALLOC(h->cmd[i].arg, l * sizeof(uint32_t)); - if (!h->cmd[i].arg) goto memerr; - memset(&h->cmd[i].arg[k + 1], 0, (l - k - 1) * sizeof(uint32_t)); - break; + case m3dcp_mi_t: + h->cmd[i].arg[k] = M3D_NOTDEFINED; + M3D_GETSTR(name); + if (name) { + for (n = 0; n < model->nummaterial; n++) + if (!strcmp(name, model->material[n].name)) { + h->cmd[i].arg[k] = n; + break; + } + if (h->cmd[i].arg[k] == M3D_NOTDEFINED) model->errcode = M3D_ERR_MTRL; + } + break; + case m3dcp_vc_t: + f = 0.0f; + switch (model->vc_s) { + case 1: f = (float)((int8_t)data[0]) / 127; break; + case 2: f = (float)(*((int16_t *)(data + 0))) / 32767; break; + case 4: f = (float)(*((float *)(data + 0))); break; + case 8: f = (float)(*((double *)(data + 0))); break; + } + h->cmd[i].arg[k] = *((uint32_t *)&f); + data += model->vc_s; + break; + case m3dcp_hi_t: data = _m3d_getidx(data, model->hi_s, &h->cmd[i].arg[k]); break; + case m3dcp_fi_t: data = _m3d_getidx(data, model->fi_s, &h->cmd[i].arg[k]); break; + case m3dcp_ti_t: data = _m3d_getidx(data, model->ti_s, &h->cmd[i].arg[k]); break; + case m3dcp_qi_t: + case m3dcp_vi_t: data = _m3d_getidx(data, model->vi_s, &h->cmd[i].arg[k]); break; + case m3dcp_i1_t: data = _m3d_getidx(data, 1, &h->cmd[i].arg[k]); break; + case m3dcp_i2_t: data = _m3d_getidx(data, 2, &h->cmd[i].arg[k]); break; + case m3dcp_i4_t: data = _m3d_getidx(data, 4, &h->cmd[i].arg[k]); break; + case m3dcp_va_t: + data = _m3d_getidx(data, 4, &h->cmd[i].arg[k]); + n = k + 1; + l += (h->cmd[i].arg[k] - 1) * (cd->p - k - 1); + h->cmd[i].arg = (uint32_t *)M3D_REALLOC(h->cmd[i].arg, l * sizeof(uint32_t)); + if (!h->cmd[i].arg) goto memerr; + memset(&h->cmd[i].arg[k + 1], 0, (l - k - 1) * sizeof(uint32_t)); + break; } } } else @@ -3627,19 +3627,19 @@ m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d if (model->ci_s && model->ci_s < 4 && !model->cmap) model->errcode = M3D_ERR_CMAP; k = 0; switch (model->ci_s) { - case 1: - k = model->cmap ? model->cmap[data[0]] : 0; - data++; - break; - case 2: - k = model->cmap ? model->cmap[*((uint16_t *)data)] : 0; - data += 2; - break; - case 4: - k = *((uint32_t *)data); - data += 4; - break; - /* case 8: break; */ + case 1: + k = model->cmap ? model->cmap[data[0]] : 0; + data++; + break; + case 2: + k = model->cmap ? model->cmap[*((uint16_t *)data)] : 0; + data += 2; + break; + case 4: + k = *((uint32_t *)data); + data += 4; + break; + /* case 8: break; */ } reclen = model->vi_s + model->si_s; i = model->numlabel; @@ -4267,16 +4267,16 @@ static uint32_t _m3d_cmapidx(uint32_t *cmap, uint32_t numcmap, uint32_t color) { /* add index to output */ static unsigned char *_m3d_addidx(unsigned char *out, char type, uint32_t idx) { switch (type) { - case 1: *out++ = (uint8_t)(idx); break; - case 2: - *((uint16_t *)out) = (uint16_t)(idx); - out += 2; - break; - case 4: - *((uint32_t *)out) = (uint32_t)(idx); - out += 4; - break; - /* case 0: case 8: break; */ + case 1: *out++ = (uint8_t)(idx); break; + case 2: + *((uint16_t *)out) = (uint16_t)(idx); + out += 2; + break; + case 4: + *((uint32_t *)out) = (uint32_t)(idx); + out += 4; + break; + /* case 0: case 8: break; */ } return out; } @@ -4288,26 +4288,26 @@ static void _m3d_round(int quality, m3dv_t *src, m3dv_t *dst) { if (src != dst) memcpy(dst, src, sizeof(m3dv_t)); /* round according to quality */ switch (quality) { - case M3D_EXP_INT8: - t = (int)(src->x * 127 + (src->x >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->x = (M3D_FLOAT)t / (M3D_FLOAT)127.0; - t = (int)(src->y * 127 + (src->y >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->y = (M3D_FLOAT)t / (M3D_FLOAT)127.0; - t = (int)(src->z * 127 + (src->z >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->z = (M3D_FLOAT)t / (M3D_FLOAT)127.0; - t = (int)(src->w * 127 + (src->w >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->w = (M3D_FLOAT)t / (M3D_FLOAT)127.0; - break; - case M3D_EXP_INT16: - t = (int)(src->x * 32767 + (src->x >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->x = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; - t = (int)(src->y * 32767 + (src->y >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->y = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; - t = (int)(src->z * 32767 + (src->z >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->z = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; - t = (int)(src->w * 32767 + (src->w >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); - dst->w = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; - break; + case M3D_EXP_INT8: + t = (int)(src->x * 127 + (src->x >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->x = (M3D_FLOAT)t / (M3D_FLOAT)127.0; + t = (int)(src->y * 127 + (src->y >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->y = (M3D_FLOAT)t / (M3D_FLOAT)127.0; + t = (int)(src->z * 127 + (src->z >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->z = (M3D_FLOAT)t / (M3D_FLOAT)127.0; + t = (int)(src->w * 127 + (src->w >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->w = (M3D_FLOAT)t / (M3D_FLOAT)127.0; + break; + case M3D_EXP_INT16: + t = (int)(src->x * 32767 + (src->x >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->x = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; + t = (int)(src->y * 32767 + (src->y >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->y = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; + t = (int)(src->z * 32767 + (src->z >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->z = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; + t = (int)(src->w * 32767 + (src->w >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5)); + dst->w = (M3D_FLOAT)t / (M3D_FLOAT)32767.0; + break; } if (dst->x == (M3D_FLOAT)-0.0) dst->x = (M3D_FLOAT)0.0; if (dst->y == (M3D_FLOAT)-0.0) dst->y = (M3D_FLOAT)0.0; @@ -4484,23 +4484,23 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size cd = &m3d_commandtypes[cmd->type]; for (k = n = 0, l = cd->p; k < l; k++) switch (cd->a[((k - n) % (cd->p - n)) + n]) { - case m3dcp_mi_t: - if (!(flags & M3D_EXP_NOMATERIAL) && cmd->arg[k] < model->nummaterial) - mtrlidx[cmd->arg[k]] = 0; - break; - case m3dcp_ti_t: - if (!(flags & M3D_EXP_NOTXTCRD) && cmd->arg[k] < model->numtmap) - tmapidx[cmd->arg[k]] = 0; - break; - case m3dcp_qi_t: - case m3dcp_vi_t: - if (cmd->arg[k] < model->numvertex) - vrtxidx[cmd->arg[k]] = 0; - break; - case m3dcp_va_t: - n = k + 1; - l += (cmd->arg[k] - 1) * (cd->p - k - 1); - break; + case m3dcp_mi_t: + if (!(flags & M3D_EXP_NOMATERIAL) && cmd->arg[k] < model->nummaterial) + mtrlidx[cmd->arg[k]] = 0; + break; + case m3dcp_ti_t: + if (!(flags & M3D_EXP_NOTXTCRD) && cmd->arg[k] < model->numtmap) + tmapidx[cmd->arg[k]] = 0; + break; + case m3dcp_qi_t: + case m3dcp_vi_t: + if (cmd->arg[k] < model->numvertex) + vrtxidx[cmd->arg[k]] = 0; + break; + case m3dcp_va_t: + n = k + 1; + l += (cmd->arg[k] - 1) * (cd->p - k - 1); + break; } } } @@ -4615,22 +4615,22 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size for (i = 0; i < model->numtmap; i++) { if (tmapidx[i] == M3D_UNDEF) continue; switch (quality) { - case M3D_EXP_INT8: - l = (unsigned int)(model->tmap[i].u * 255); - tcoord.data.u = (M3D_FLOAT)l / (M3D_FLOAT)255.0; - l = (unsigned int)(model->tmap[i].v * 255); - tcoord.data.v = (M3D_FLOAT)l / (M3D_FLOAT)255.0; - break; - case M3D_EXP_INT16: - l = (unsigned int)(model->tmap[i].u * 65535); - tcoord.data.u = (M3D_FLOAT)l / (M3D_FLOAT)65535.0; - l = (unsigned int)(model->tmap[i].v * 65535); - tcoord.data.v = (M3D_FLOAT)l / (M3D_FLOAT)65535.0; - break; - default: - tcoord.data.u = model->tmap[i].u; - tcoord.data.v = model->tmap[i].v; - break; + case M3D_EXP_INT8: + l = (unsigned int)(model->tmap[i].u * 255); + tcoord.data.u = (M3D_FLOAT)l / (M3D_FLOAT)255.0; + l = (unsigned int)(model->tmap[i].v * 255); + tcoord.data.v = (M3D_FLOAT)l / (M3D_FLOAT)255.0; + break; + case M3D_EXP_INT16: + l = (unsigned int)(model->tmap[i].u * 65535); + tcoord.data.u = (M3D_FLOAT)l / (M3D_FLOAT)65535.0; + l = (unsigned int)(model->tmap[i].v * 65535); + tcoord.data.v = (M3D_FLOAT)l / (M3D_FLOAT)65535.0; + break; + default: + tcoord.data.u = model->tmap[i].u; + tcoord.data.v = model->tmap[i].v; + break; } if (flags & M3D_EXP_FLIPTXTCRD) tcoord.data.v = (M3D_FLOAT)1.0 - tcoord.data.v; @@ -4959,26 +4959,26 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size } } switch (k) { - case m3dpf_color: ptr += sprintf(ptr, "%s #%08x\r\n", sn, m->prop[i].value.color); break; - case m3dpf_uint8: - case m3dpf_uint16: - case m3dpf_uint32: ptr += sprintf(ptr, "%s %d\r\n", sn, m->prop[i].value.num); break; - case m3dpf_float: ptr += sprintf(ptr, "%s %g\r\n", sn, m->prop[i].value.fnum); break; - case m3dpf_map: - if (m->prop[i].value.textureid < model->numtexture && - model->texture[m->prop[i].value.textureid].name) { - sl = _m3d_safestr(model->texture[m->prop[i].value.textureid].name, 0); - if (!sl) { - setlocale(LC_NUMERIC, ol); - goto memerr; - } - if (*sl) - ptr += sprintf(ptr, "map_%s %s\r\n", sn, sl); - M3D_FREE(sn); - M3D_FREE(sl); - sl = NULL; + case m3dpf_color: ptr += sprintf(ptr, "%s #%08x\r\n", sn, m->prop[i].value.color); break; + case m3dpf_uint8: + case m3dpf_uint16: + case m3dpf_uint32: ptr += sprintf(ptr, "%s %d\r\n", sn, m->prop[i].value.num); break; + case m3dpf_float: ptr += sprintf(ptr, "%s %g\r\n", sn, m->prop[i].value.fnum); break; + case m3dpf_map: + if (m->prop[i].value.textureid < model->numtexture && + model->texture[m->prop[i].value.textureid].name) { + sl = _m3d_safestr(model->texture[m->prop[i].value.textureid].name, 0); + if (!sl) { + setlocale(LC_NUMERIC, ol); + goto memerr; } - break; + if (*sl) + ptr += sprintf(ptr, "map_%s %s\r\n", sn, sl); + M3D_FREE(sn); + M3D_FREE(sl); + sl = NULL; + } + break; } sn = NULL; } @@ -5102,16 +5102,16 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size len = (unsigned int)((uintptr_t)ptr + (uintptr_t)strlen(cd->key) + (uintptr_t)3); for (k = 0; k < cd->p; k++) switch (cd->a[k]) { - case m3dcp_mi_t: - if (cmd->arg[k] != M3D_NOTDEFINED) { - len += (unsigned int)strlen(model->material[cmd->arg[k]].name) + 1; - } - break; - case m3dcp_va_t: - len += cmd->arg[k] * (cd->p - k - 1) * 16; - k = cd->p; - break; - default: len += 16; break; + case m3dcp_mi_t: + if (cmd->arg[k] != M3D_NOTDEFINED) { + len += (unsigned int)strlen(model->material[cmd->arg[k]].name) + 1; + } + break; + case m3dcp_va_t: + len += cmd->arg[k] * (cd->p - k - 1) * 16; + k = cd->p; + break; + default: len += 16; break; } out = (unsigned char *)M3D_REALLOC(out, len); ptr += (uintptr_t)out; @@ -5122,25 +5122,25 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size ptr += sprintf(ptr, "%s", cd->key); for (k = n = 0, l = cd->p; k < l; k++) { switch (cd->a[((k - n) % (cd->p - n)) + n]) { - case m3dcp_mi_t: - if (cmd->arg[k] != M3D_NOTDEFINED) { - sn = _m3d_safestr(model->material[cmd->arg[k]].name, 0); - if (!sn) { - setlocale(LC_NUMERIC, ol); - goto memerr; - } - ptr += sprintf(ptr, " %s", sn); - M3D_FREE(sn); - sn = NULL; + case m3dcp_mi_t: + if (cmd->arg[k] != M3D_NOTDEFINED) { + sn = _m3d_safestr(model->material[cmd->arg[k]].name, 0); + if (!sn) { + setlocale(LC_NUMERIC, ol); + goto memerr; } - break; - case m3dcp_vc_t: ptr += sprintf(ptr, " %g", *((float *)&cmd->arg[k])); break; - case m3dcp_va_t: - ptr += sprintf(ptr, " %d[", cmd->arg[k]); - n = k + 1; - l += (cmd->arg[k] - 1) * (cd->p - k - 1); - break; - default: ptr += sprintf(ptr, " %d", cmd->arg[k]); break; + ptr += sprintf(ptr, " %s", sn); + M3D_FREE(sn); + sn = NULL; + } + break; + case m3dcp_vc_t: ptr += sprintf(ptr, " %g", *((float *)&cmd->arg[k])); break; + case m3dcp_va_t: + ptr += sprintf(ptr, " %d[", cmd->arg[k]); + n = k + 1; + l += (cmd->arg[k] - 1) * (cd->p - k - 1); + break; + default: ptr += sprintf(ptr, " %d", cmd->arg[k]); break; } } ptr += sprintf(ptr, "%s\r\n", l > cd->p ? " ]" : ""); @@ -5384,28 +5384,28 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size if (tmap[i].newidx == last) continue; last = tmap[i].newidx; switch (vc_s) { - case 1: - *out++ = (uint8_t)(tmap[i].data.u * 255); - *out++ = (uint8_t)(tmap[i].data.v * 255); - break; - case 2: - *((uint16_t *)out) = (uint16_t)(tmap[i].data.u * 65535); - out += 2; - *((uint16_t *)out) = (uint16_t)(tmap[i].data.v * 65535); - out += 2; - break; - case 4: - *((float *)out) = tmap[i].data.u; - out += 4; - *((float *)out) = tmap[i].data.v; - out += 4; - break; - case 8: - *((double *)out) = tmap[i].data.u; - out += 8; - *((double *)out) = tmap[i].data.v; - out += 8; - break; + case 1: + *out++ = (uint8_t)(tmap[i].data.u * 255); + *out++ = (uint8_t)(tmap[i].data.v * 255); + break; + case 2: + *((uint16_t *)out) = (uint16_t)(tmap[i].data.u * 65535); + out += 2; + *((uint16_t *)out) = (uint16_t)(tmap[i].data.v * 65535); + out += 2; + break; + case 4: + *((float *)out) = tmap[i].data.u; + out += 4; + *((float *)out) = tmap[i].data.v; + out += 4; + break; + case 8: + *((double *)out) = tmap[i].data.u; + out += 8; + *((double *)out) = tmap[i].data.v; + out += 8; + break; } } *length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len)); @@ -5425,54 +5425,54 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size if (vrtx[i].newidx == last) continue; last = vrtx[i].newidx; switch (vc_s) { - case 1: - *out++ = (int8_t)(vrtx[i].data.x * 127); - *out++ = (int8_t)(vrtx[i].data.y * 127); - *out++ = (int8_t)(vrtx[i].data.z * 127); - *out++ = (int8_t)(vrtx[i].data.w * 127); - break; - case 2: - *((int16_t *)out) = (int16_t)(vrtx[i].data.x * 32767); - out += 2; - *((int16_t *)out) = (int16_t)(vrtx[i].data.y * 32767); - out += 2; - *((int16_t *)out) = (int16_t)(vrtx[i].data.z * 32767); - out += 2; - *((int16_t *)out) = (int16_t)(vrtx[i].data.w * 32767); - out += 2; - break; - case 4: - memcpy(out, &vrtx[i].data.x, sizeof(float)); - out += 4; - memcpy(out, &vrtx[i].data.y, sizeof(float)); - out += 4; - memcpy(out, &vrtx[i].data.z, sizeof(float)); - out += 4; - memcpy(out, &vrtx[i].data.w, sizeof(float)); - out += 4; - break; - case 8: - *((double *)out) = vrtx[i].data.x; - out += 8; - *((double *)out) = vrtx[i].data.y; - out += 8; - *((double *)out) = vrtx[i].data.z; - out += 8; - *((double *)out) = vrtx[i].data.w; - out += 8; - break; + case 1: + *out++ = (int8_t)(vrtx[i].data.x * 127); + *out++ = (int8_t)(vrtx[i].data.y * 127); + *out++ = (int8_t)(vrtx[i].data.z * 127); + *out++ = (int8_t)(vrtx[i].data.w * 127); + break; + case 2: + *((int16_t *)out) = (int16_t)(vrtx[i].data.x * 32767); + out += 2; + *((int16_t *)out) = (int16_t)(vrtx[i].data.y * 32767); + out += 2; + *((int16_t *)out) = (int16_t)(vrtx[i].data.z * 32767); + out += 2; + *((int16_t *)out) = (int16_t)(vrtx[i].data.w * 32767); + out += 2; + break; + case 4: + memcpy(out, &vrtx[i].data.x, sizeof(float)); + out += 4; + memcpy(out, &vrtx[i].data.y, sizeof(float)); + out += 4; + memcpy(out, &vrtx[i].data.z, sizeof(float)); + out += 4; + memcpy(out, &vrtx[i].data.w, sizeof(float)); + out += 4; + break; + case 8: + *((double *)out) = vrtx[i].data.x; + out += 8; + *((double *)out) = vrtx[i].data.y; + out += 8; + *((double *)out) = vrtx[i].data.z; + out += 8; + *((double *)out) = vrtx[i].data.w; + out += 8; + break; } idx = _m3d_cmapidx(cmap, numcmap, vrtx[i].data.color); switch (ci_s) { - case 1: *out++ = (uint8_t)(idx); break; - case 2: - *((uint16_t *)out) = (uint16_t)(idx); - out += 2; - break; - case 4: - *((uint32_t *)out) = vrtx[i].data.color; - out += 4; - break; + case 1: *out++ = (uint8_t)(idx); break; + case 2: + *((uint16_t *)out) = (uint16_t)(idx); + out += 2; + break; + case 4: + *((uint32_t *)out) = vrtx[i].data.color; + out += 4; + break; } out = _m3d_addidx(out, sk_s, vrtx[i].data.skinid); } @@ -5510,19 +5510,19 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size j++) weights[j] = (uint8_t)(skin[i].data.weight[j] * 255); switch (nb_s) { - case 1: weights[0] = 255; break; - case 2: - *((uint16_t *)out) = *((uint16_t *)&weights[0]); - out += 2; - break; - case 4: - *((uint32_t *)out) = *((uint32_t *)&weights[0]); - out += 4; - break; - case 8: - *((uint64_t *)out) = *((uint64_t *)&weights[0]); - out += 8; - break; + case 1: weights[0] = 255; break; + case 2: + *((uint16_t *)out) = *((uint16_t *)&weights[0]); + out += 2; + break; + case 4: + *((uint32_t *)out) = *((uint32_t *)&weights[0]); + out += 4; + break; + case 8: + *((uint64_t *)out) = *((uint64_t *)&weights[0]); + out += 8; + break; } for (j = 0; j < (uint32_t)nb_s && skin[i].data.boneid[j] != M3D_UNDEF && weights[j]; j++) { out = _m3d_addidx(out, bi_s, skin[i].data.boneid[j]); @@ -5561,41 +5561,41 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size if (k == 256) continue; *out++ = m->prop[i].type; switch (k) { - case m3dpf_color: - if (!(flags & M3D_EXP_NOCMAP)) { - idx = _m3d_cmapidx(cmap, numcmap, m->prop[i].value.color); - switch (ci_s) { - case 1: *out++ = (uint8_t)(idx); break; - case 2: - *((uint16_t *)out) = (uint16_t)(idx); - out += 2; - break; - case 4: - *((uint32_t *)out) = (uint32_t)(m->prop[i].value.color); - out += 4; - break; - } - } else - out--; - break; - case m3dpf_uint8: *out++ = m->prop[i].value.num; break; - case m3dpf_uint16: - *((uint16_t *)out) = m->prop[i].value.num; - out += 2; - break; - case m3dpf_uint32: - *((uint32_t *)out) = m->prop[i].value.num; - out += 4; - break; - case m3dpf_float: - *((float *)out) = m->prop[i].value.fnum; - out += 4; - break; + case m3dpf_color: + if (!(flags & M3D_EXP_NOCMAP)) { + idx = _m3d_cmapidx(cmap, numcmap, m->prop[i].value.color); + switch (ci_s) { + case 1: *out++ = (uint8_t)(idx); break; + case 2: + *((uint16_t *)out) = (uint16_t)(idx); + out += 2; + break; + case 4: + *((uint32_t *)out) = (uint32_t)(m->prop[i].value.color); + out += 4; + break; + } + } else + out--; + break; + case m3dpf_uint8: *out++ = m->prop[i].value.num; break; + case m3dpf_uint16: + *((uint16_t *)out) = m->prop[i].value.num; + out += 2; + break; + case m3dpf_uint32: + *((uint32_t *)out) = m->prop[i].value.num; + out += 4; + break; + case m3dpf_float: + *((float *)out) = m->prop[i].value.fnum; + out += 4; + break; - case m3dpf_map: - idx = _m3d_stridx(str, numstr, model->texture[m->prop[i].value.textureid].name); - out = _m3d_addidx(out, si_s, idx); - break; + case m3dpf_map: + idx = _m3d_stridx(str, numstr, model->texture[m->prop[i].value.textureid].name); + out = _m3d_addidx(out, si_s, idx); + break; } } *length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len)); @@ -5689,40 +5689,40 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size if (cmd->type > 127) *out++ = (cmd->type >> 7) & 0xff; for (k = n = 0, l = cd->p; k < l; k++) { switch (cd->a[((k - n) % (cd->p - n)) + n]) { - case m3dcp_mi_t: - out = _m3d_addidx(out, si_s, cmd->arg[k] < model->nummaterial ? _m3d_stridx(str, numstr, model->material[cmd->arg[k]].name) : 0); + case m3dcp_mi_t: + out = _m3d_addidx(out, si_s, cmd->arg[k] < model->nummaterial ? _m3d_stridx(str, numstr, model->material[cmd->arg[k]].name) : 0); + break; + case m3dcp_vc_t: + min_x = *((float *)&cmd->arg[k]); + switch (vc_s) { + case 1: *out++ = (int8_t)(min_x * 127); break; + case 2: + *((int16_t *)out) = (int16_t)(min_x * 32767); + out += 2; break; - case m3dcp_vc_t: - min_x = *((float *)&cmd->arg[k]); - switch (vc_s) { - case 1: *out++ = (int8_t)(min_x * 127); break; - case 2: - *((int16_t *)out) = (int16_t)(min_x * 32767); - out += 2; - break; - case 4: - *((float *)out) = min_x; - out += 4; - break; - case 8: - *((double *)out) = min_x; - out += 8; - break; - } + case 4: + *((float *)out) = min_x; + out += 4; break; - case m3dcp_hi_t: out = _m3d_addidx(out, hi_s, cmd->arg[k]); break; - case m3dcp_fi_t: out = _m3d_addidx(out, fi_s, cmd->arg[k]); break; - case m3dcp_ti_t: out = _m3d_addidx(out, ti_s, cmd->arg[k]); break; - case m3dcp_qi_t: - case m3dcp_vi_t: out = _m3d_addidx(out, vi_s, cmd->arg[k]); break; - case m3dcp_i1_t: out = _m3d_addidx(out, 1, cmd->arg[k]); break; - case m3dcp_i2_t: out = _m3d_addidx(out, 2, cmd->arg[k]); break; - case m3dcp_i4_t: out = _m3d_addidx(out, 4, cmd->arg[k]); break; - case m3dcp_va_t: - out = _m3d_addidx(out, 4, cmd->arg[k]); - n = k + 1; - l += (cmd->arg[k] - 1) * (cd->p - k - 1); + case 8: + *((double *)out) = min_x; + out += 8; break; + } + break; + case m3dcp_hi_t: out = _m3d_addidx(out, hi_s, cmd->arg[k]); break; + case m3dcp_fi_t: out = _m3d_addidx(out, fi_s, cmd->arg[k]); break; + case m3dcp_ti_t: out = _m3d_addidx(out, ti_s, cmd->arg[k]); break; + case m3dcp_qi_t: + case m3dcp_vi_t: out = _m3d_addidx(out, vi_s, cmd->arg[k]); break; + case m3dcp_i1_t: out = _m3d_addidx(out, 1, cmd->arg[k]); break; + case m3dcp_i2_t: out = _m3d_addidx(out, 2, cmd->arg[k]); break; + case m3dcp_i4_t: out = _m3d_addidx(out, 4, cmd->arg[k]); break; + case m3dcp_va_t: + out = _m3d_addidx(out, 4, cmd->arg[k]); + n = k + 1; + l += (cmd->arg[k] - 1) * (cd->p - k - 1); + break; } } } @@ -5756,15 +5756,15 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->label[l].lang)); idx = _m3d_cmapidx(cmap, numcmap, model->label[i].color); switch (ci_s) { - case 1: *out++ = (uint8_t)(idx); break; - case 2: - *((uint16_t *)out) = (uint16_t)(idx); - out += 2; - break; - case 4: - *((uint32_t *)out) = model->label[i].color; - out += 4; - break; + case 1: *out++ = (uint8_t)(idx); break; + case 2: + *((uint16_t *)out) = (uint16_t)(idx); + out += 2; + break; + case 4: + *((uint32_t *)out) = model->label[i].color; + out += 4; + break; } } out = _m3d_addidx(out, vi_s, vrtxidx[model->label[i].vertexid]); @@ -5889,7 +5889,7 @@ unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size } #endif -#endif +#endif /* M3D_IMPLEMENTATION */ #ifdef __cplusplus } @@ -6147,11 +6147,11 @@ public: #endif /* impl */ } // namespace M3D -#ifdef _WIN32 -# pragma warning(pop) -#endif // _WIN32 +#endif /* M3D_CPPWRAPPER */ -#endif +#ifdef _MSC_VER +# pragma warning(pop) +#endif /* _MSC_VER */ #endif /* __cplusplus */ diff --git a/code/AssetLib/MDL/HalfLife/HL1MDLLoader.cpp b/code/AssetLib/MDL/HalfLife/HL1MDLLoader.cpp index e9930dfed..c57b43c11 100644 --- a/code/AssetLib/MDL/HalfLife/HL1MDLLoader.cpp +++ b/code/AssetLib/MDL/HalfLife/HL1MDLLoader.cpp @@ -68,9 +68,9 @@ namespace Assimp { namespace MDL { namespace HalfLife { -#if _MSC_VER > 1920 +#ifdef _MSC_VER # pragma warning(disable : 4706) -#endif // _WIN32 +#endif // _MSC_VER // ------------------------------------------------------------------------------------------------ HL1MDLLoader::HL1MDLLoader( diff --git a/code/AssetLib/Step/STEPFile.h b/code/AssetLib/Step/STEPFile.h index 72648e462..2072ac2a6 100644 --- a/code/AssetLib/Step/STEPFile.h +++ b/code/AssetLib/Step/STEPFile.h @@ -54,10 +54,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#if _MSC_VER > 1920 +#ifdef _MSC_VER # pragma warning(push) # pragma warning(disable : 4127 4456 4245 4512 ) -#endif // _WIN32 +#endif // _MSC_VER // #if _MSC_VER > 1500 || (defined __GNUC___) @@ -960,9 +960,9 @@ private: const EXPRESS::ConversionSchema *schema; }; -#if _MSC_VER > 1920 +#ifdef _MSC_VER #pragma warning(pop) -#endif // _WIN32 +#endif // _MSC_VER } // namespace STEP diff --git a/code/Common/Exporter.cpp b/code/Common/Exporter.cpp index 031d5c24e..6b3a50346 100644 --- a/code/Common/Exporter.cpp +++ b/code/Common/Exporter.cpp @@ -74,9 +74,9 @@ Here we implement only the C++ interface (Assimp::Exporter). namespace Assimp { -#if _MSC_VER > 1920 +#ifdef _MSC_VER # pragma warning( disable : 4800 ) -#endif // _WIN32 +#endif // _MSC_VER // PostStepRegistry.cpp diff --git a/code/Common/Subdivision.cpp b/code/Common/Subdivision.cpp index 4f64a00c4..c60d2f0d3 100644 --- a/code/Common/Subdivision.cpp +++ b/code/Common/Subdivision.cpp @@ -53,9 +53,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using namespace Assimp; void mydummy() {} -#if _MSC_VER > 1920 +#ifdef _MSC_VER #pragma warning(disable : 4709) -#endif // _WIN32 +#endif // _MSC_VER // ------------------------------------------------------------------------------------------------ /** Subdivider stub class to implement the Catmull-Clarke subdivision algorithm. The * implementation is basing on recursive refinement. Directly evaluating the result is also From 13ee2306c34ae25a6376fc5de0811e39225306f5 Mon Sep 17 00:00:00 2001 From: MeyerFabian Date: Mon, 20 Jul 2020 17:04:11 +0200 Subject: [PATCH 17/24] build/clang-cl-windows --- CMakeLists.txt | 6 +++- code/AssetLib/M3D/m3d.h | 4 +-- code/AssetLib/Step/STEPFile.h | 8 ++--- code/CMakeLists.txt | 1 - .../gtest/include/gtest/internal/gtest-port.h | 14 +++++++++ contrib/gtest/src/gtest-port.cc | 3 +- .../rapidjson/include/rapidjson/document.h | 2 +- .../rapidjson/include/rapidjson/encodings.h | 4 +-- .../include/rapidjson/internal/meta.h | 4 +-- contrib/rapidjson/include/rapidjson/reader.h | 4 +-- contrib/rapidjson/include/rapidjson/writer.h | 6 ++-- contrib/unzip/ioapi.c | 15 +++++----- contrib/unzip/ioapi.h | 2 +- contrib/unzip/unzip.c | 1 - contrib/zip/src/miniz.h | 6 ++-- include/assimp/Compiler/poppack1.h | 2 +- include/assimp/Compiler/pushpack1.h | 2 +- test/unit/UnitTestFileGenerator.h | 30 +++++++++---------- test/unit/utColladaImportExport.cpp | 1 - 19 files changed, 65 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23725381d..ecf1de619 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -257,7 +257,11 @@ IF ((CMAKE_C_COMPILER_ID MATCHES "GNU") AND NOT CMAKE_COMPILER_IS_MINGW) SET(LIBSTDC++_LIBRARIES -lstdc++) ELSEIF(MSVC) # enable multi-core compilation with MSVC - ADD_COMPILE_OPTIONS(/MP /bigobj /W4 /WX ) + IF( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) # clang-cl + ADD_COMPILE_OPTIONS(/bigobj /W4 /WX ) + ELSE() # msvc + ADD_COMPILE_OPTIONS(/MP /bigobj /W4 /WX) + ENDIF() # disable "elements of array '' will be default initialized" warning on MSVC2013 IF(MSVC12) ADD_COMPILE_OPTIONS(/wd4351) diff --git a/code/AssetLib/M3D/m3d.h b/code/AssetLib/M3D/m3d.h index 5ab3d16de..e0d456c3c 100644 --- a/code/AssetLib/M3D/m3d.h +++ b/code/AssetLib/M3D/m3d.h @@ -84,7 +84,7 @@ typedef uint16_t M3D_INDEX; #ifndef M3D_BONEMAXLEVEL #define M3D_BONEMAXLEVEL 8 #endif -#ifndef _MSC_VER +#if !defined(_MSC_VER) || defined(__clang__) #ifndef _inline #define _inline __inline__ #endif @@ -101,7 +101,7 @@ typedef uint16_t M3D_INDEX; #define _register #endif -#if _MSC_VER > 1920 +#if _MSC_VER > 1920 && !defined(__clang__) # pragma warning(push) # pragma warning(disable : 4100 4127 4189 4505 4244 4403 4701 4703) # if (_MSC_VER > 1800 ) diff --git a/code/AssetLib/Step/STEPFile.h b/code/AssetLib/Step/STEPFile.h index 72648e462..1ef5d65c4 100644 --- a/code/AssetLib/Step/STEPFile.h +++ b/code/AssetLib/Step/STEPFile.h @@ -130,8 +130,8 @@ namespace STEP { * coupled with a line number. */ // ------------------------------------------------------------------------------- struct SyntaxError : DeadlyImportError { - enum { - LINE_NOT_SPECIFIED = 0xffffffffffffffffLL + enum : uint64_t { + LINE_NOT_SPECIFIED = 0xfffffffffffffffLL }; SyntaxError(const std::string &s, uint64_t line = LINE_NOT_SPECIFIED); @@ -143,8 +143,8 @@ struct SyntaxError : DeadlyImportError { * It is typically coupled with both an entity id and a line number.*/ // ------------------------------------------------------------------------------- struct TypeError : DeadlyImportError { - enum { - ENTITY_NOT_SPECIFIED = 0xffffffffffffffffLL, + enum : uint64_t { + ENTITY_NOT_SPECIFIED = 0xffffffffffffffffUL, ENTITY_NOT_SPECIFIED_32 = 0x00000000ffffffff }; diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index bb43449e1..2ba893281 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -45,7 +45,6 @@ # cmake_minimum_required( VERSION 3.0 ) SET( HEADER_PATH ../include/assimp ) - if(NOT ANDROID AND ASSIMP_ANDROID_JNIIOSYSTEM) message(WARNING "Requesting Android JNI I/O-System in non-Android toolchain. Resetting ASSIMP_ANDROID_JNIIOSYSTEM to OFF.") set(ASSIMP_ANDROID_JNIIOSYSTEM OFF) diff --git a/contrib/gtest/include/gtest/internal/gtest-port.h b/contrib/gtest/include/gtest/internal/gtest-port.h index 0094ed507..f66d9cd32 100644 --- a/contrib/gtest/include/gtest/internal/gtest-port.h +++ b/contrib/gtest/include/gtest/internal/gtest-port.h @@ -312,10 +312,22 @@ __pragma(warning(disable: warnings)) # define GTEST_DISABLE_MSC_WARNINGS_POP_() \ __pragma(warning(pop)) +# if defined(__clang__) +# define GTEST_DISABLE_CLANG_DEPRECATED_WARNINGS_PUSH_() \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") +# define GTEST_DISABLE_CLANG_WARNINGS_POP_() \ + _Pragma("clang diagnostic pop") +# else +# define GTEST_DISABLE_CLANG_DEPRECATED_WARNINGS_PUSH_() +# define GTEST_DISABLE_CLANG_WARNINGS_POP_() +# endif #else // Older versions of MSVC don't have __pragma. # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) # define GTEST_DISABLE_MSC_WARNINGS_POP_() +# define GTEST_DISABLE_CLANG_DEPRECATED_WARNINGS_PUSH_() +# define GTEST_DISABLE_CLANG_WARNINGS_POP_() #endif #ifndef GTEST_LANG_CXX11 @@ -2352,6 +2364,7 @@ inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } // Functions deprecated by MSVC 8.0. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 /* deprecated function */) +GTEST_DISABLE_CLANG_DEPRECATED_WARNINGS_PUSH_() inline const char* StrNCpy(char* dest, const char* src, size_t n) { return strncpy(dest, src, n); @@ -2399,6 +2412,7 @@ inline const char* GetEnv(const char* name) { #endif } +GTEST_DISABLE_CLANG_WARNINGS_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() #if GTEST_OS_WINDOWS_MOBILE diff --git a/contrib/gtest/src/gtest-port.cc b/contrib/gtest/src/gtest-port.cc index e5bf3dd2b..e04aa9a57 100644 --- a/contrib/gtest/src/gtest-port.cc +++ b/contrib/gtest/src/gtest-port.cc @@ -926,7 +926,7 @@ GTestLog::~GTestLog() { // Disable Microsoft deprecation warnings for POSIX functions called from // this class (creat, dup, dup2, and close) GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996) - +GTEST_DISABLE_CLANG_DEPRECATED_WARNINGS_PUSH_() #if GTEST_HAS_STREAM_REDIRECTION // Object that captures an output stream (stdout/stderr). @@ -1010,6 +1010,7 @@ class CapturedStream { }; GTEST_DISABLE_MSC_WARNINGS_POP_() +GTEST_DISABLE_CLANG_WARNINGS_POP_() static CapturedStream* g_captured_stderr = NULL; static CapturedStream* g_captured_stdout = NULL; diff --git a/contrib/rapidjson/include/rapidjson/document.h b/contrib/rapidjson/include/rapidjson/document.h index 473d846e3..17df30725 100644 --- a/contrib/rapidjson/include/rapidjson/document.h +++ b/contrib/rapidjson/include/rapidjson/document.h @@ -31,7 +31,7 @@ #include RAPIDJSON_DIAG_PUSH -#ifdef _MSC_VER +#if defined(_MSC_VER) && !(__clang__) RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data #endif diff --git a/contrib/rapidjson/include/rapidjson/encodings.h b/contrib/rapidjson/include/rapidjson/encodings.h index 0df1c3435..de4d9d216 100644 --- a/contrib/rapidjson/include/rapidjson/encodings.h +++ b/contrib/rapidjson/include/rapidjson/encodings.h @@ -17,7 +17,7 @@ #include "rapidjson.h" -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data RAPIDJSON_DIAG_OFF(4702) // unreachable code @@ -709,7 +709,7 @@ struct Transcoder { RAPIDJSON_NAMESPACE_END -#if defined(__GNUC__) || defined(_MSC_VER) +#if defined(__GNUC__) || (defined(_MSC_VER) && !defined(__clang__)) RAPIDJSON_DIAG_POP #endif diff --git a/contrib/rapidjson/include/rapidjson/internal/meta.h b/contrib/rapidjson/include/rapidjson/internal/meta.h index 5a9aaa428..bcbfc7709 100644 --- a/contrib/rapidjson/include/rapidjson/internal/meta.h +++ b/contrib/rapidjson/include/rapidjson/internal/meta.h @@ -21,7 +21,7 @@ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) #endif -#if defined(_MSC_VER) +#if defined(_MSC_VER) && !defined(__clang__) RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(6334) #endif @@ -174,7 +174,7 @@ template struct RemoveSfinaeTag { typedef T Type; RAPIDJSON_NAMESPACE_END //@endcond -#if defined(__GNUC__) || defined(_MSC_VER) +#if defined(__GNUC__) || (defined(_MSC_VER) && !defined(__clang__)) RAPIDJSON_DIAG_POP #endif diff --git a/contrib/rapidjson/include/rapidjson/reader.h b/contrib/rapidjson/include/rapidjson/reader.h index 120c31115..4b172e953 100644 --- a/contrib/rapidjson/include/rapidjson/reader.h +++ b/contrib/rapidjson/include/rapidjson/reader.h @@ -37,7 +37,7 @@ #include #endif -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_OFF(4702) // unreachable code @@ -2214,7 +2214,7 @@ RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP #endif -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) RAPIDJSON_DIAG_POP #endif diff --git a/contrib/rapidjson/include/rapidjson/writer.h b/contrib/rapidjson/include/rapidjson/writer.h index e610ebb60..53d1a5d7f 100644 --- a/contrib/rapidjson/include/rapidjson/writer.h +++ b/contrib/rapidjson/include/rapidjson/writer.h @@ -36,7 +36,7 @@ #include #endif -#ifdef _MSC_VER +#if defined (_MSC_VER) && !defined(__clang__) RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant #endif @@ -700,11 +700,11 @@ inline bool Writer::ScanWriteUnescapedString(StringStream& is, siz RAPIDJSON_NAMESPACE_END -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) RAPIDJSON_DIAG_POP #endif -#ifdef __clang__ +#if defined(__clang__) RAPIDJSON_DIAG_POP #endif diff --git a/contrib/unzip/ioapi.c b/contrib/unzip/ioapi.c index e1ef46088..0f1bf4f21 100644 --- a/contrib/unzip/ioapi.c +++ b/contrib/unzip/ioapi.c @@ -1,11 +1,3 @@ -/* ioapi.c -- IO base function header for compress/uncompress .zip - files using zlib + zip or unzip API - - Version 1.01e, February 12th, 2005 - - Copyright (C) 1998-2005 Gilles Vollant -*/ - #include #include #include @@ -16,6 +8,10 @@ #ifdef _WIN32 # pragma warning(push) # pragma warning(disable : 4131 4100) +# ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunused-parameter" +# endif #endif // _WIN32 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ @@ -181,4 +177,7 @@ void fill_fopen_filefunc (pzlib_filefunc_def) #ifdef _WIN32 # pragma warning(pop) +# ifdef __clang__ +# pragma clang diagnostic pop +# endif #endif // _WIN32 diff --git a/contrib/unzip/ioapi.h b/contrib/unzip/ioapi.h index 7d457baab..63ec641bf 100644 --- a/contrib/unzip/ioapi.h +++ b/contrib/unzip/ioapi.h @@ -24,7 +24,7 @@ #ifndef ZCALLBACK -#if (defined(WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK) +#if (defined(_WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK) #define ZCALLBACK CALLBACK #else #define ZCALLBACK diff --git a/contrib/unzip/unzip.c b/contrib/unzip/unzip.c index 3937f821e..7bac01df0 100644 --- a/contrib/unzip/unzip.c +++ b/contrib/unzip/unzip.c @@ -1554,7 +1554,6 @@ extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf) char *szComment; uLong uSizeBuf; { - int err=UNZ_OK; unz_s* s; uLong uReadThis ; if (file==NULL) diff --git a/contrib/zip/src/miniz.h b/contrib/zip/src/miniz.h index 07f7b2de2..7570ae929 100644 --- a/contrib/zip/src/miniz.h +++ b/contrib/zip/src/miniz.h @@ -5361,7 +5361,7 @@ mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, } else { // Temporarily allocate a read buffer. read_buf_size = MZ_MIN(file_stat.m_comp_size, MZ_ZIP_MAX_IO_BUF_SIZE); -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF)) #else @@ -5454,7 +5454,7 @@ void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) #else if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) @@ -5560,7 +5560,7 @@ mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) { // The file is stored or the caller has requested the compressed data. if (pZip->m_pState->m_pMem) { -#ifdef _MSC_VER +#if defined (_MSC_VER) && !defined(__clang__) if (((0, sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > 0xFFFFFFFF)) #else diff --git a/include/assimp/Compiler/poppack1.h b/include/assimp/Compiler/poppack1.h index e033bc147..a8e9a3c7e 100644 --- a/include/assimp/Compiler/poppack1.h +++ b/include/assimp/Compiler/poppack1.h @@ -14,7 +14,7 @@ #endif // reset packing to the original value -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) +#if (defined(_MSC_VER) && !defined(__clang__)) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) # pragma pack( pop ) #endif #undef PACK_STRUCT diff --git a/include/assimp/Compiler/pushpack1.h b/include/assimp/Compiler/pushpack1.h index 4c9fbb857..2a5e2dfe6 100644 --- a/include/assimp/Compiler/pushpack1.h +++ b/include/assimp/Compiler/pushpack1.h @@ -22,7 +22,7 @@ # error poppack1.h must be included after pushpack1.h #endif -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) +#if (defined(_MSC_VER) && !defined(__clang__)) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) # pragma pack(push,1) # define PACK_STRUCT #elif defined( __GNUC__ ) || defined(__clang__) diff --git a/test/unit/UnitTestFileGenerator.h b/test/unit/UnitTestFileGenerator.h index 91d69357f..2166c6939 100644 --- a/test/unit/UnitTestFileGenerator.h +++ b/test/unit/UnitTestFileGenerator.h @@ -44,21 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#if defined(__GNUC__) || defined(__clang__) -#define TMP_PATH "/tmp/" -inline FILE* MakeTmpFile(char* tmplate) -{ - auto fd = mkstemp(tmplate); - EXPECT_NE(-1, fd); - if(fd == -1) - { - return nullptr; - } - auto fs = fdopen(fd, "w+"); - EXPECT_NE(nullptr, fs); - return fs; -} -#elif defined(_MSC_VER) +#if defined(_MSC_VER) #include #define TMP_PATH "./" inline FILE* MakeTmpFile(char* tmplate) @@ -73,4 +59,18 @@ inline FILE* MakeTmpFile(char* tmplate) EXPECT_NE(fs, nullptr); return fs; } +#elif defined(__GNUC__) || defined(__clang__) +#define TMP_PATH "/tmp/" +inline FILE* MakeTmpFile(char* tmplate) +{ + auto fd = mkstemp(tmplate); + EXPECT_NE(-1, fd); + if(fd == -1) + { + return nullptr; + } + auto fs = fdopen(fd, "w+"); + EXPECT_NE(nullptr, fs); + return fs; +} #endif diff --git a/test/unit/utColladaImportExport.cpp b/test/unit/utColladaImportExport.cpp index 451c8e235..100ca548a 100644 --- a/test/unit/utColladaImportExport.cpp +++ b/test/unit/utColladaImportExport.cpp @@ -157,7 +157,6 @@ public: 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()); From a19e4e41129589c2ca1e09a476ae57874462d685 Mon Sep 17 00:00:00 2001 From: MeyerFabian Date: Mon, 20 Jul 2020 18:28:50 +0200 Subject: [PATCH 18/24] Make clang with msvc abi work. --- code/AssetLib/Blender/BlenderTessellator.cpp | 8 +++++++- code/CMakeLists.txt | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/code/AssetLib/Blender/BlenderTessellator.cpp b/code/AssetLib/Blender/BlenderTessellator.cpp index 3c1cd6ea8..3366d56c5 100644 --- a/code/AssetLib/Blender/BlenderTessellator.cpp +++ b/code/AssetLib/Blender/BlenderTessellator.cpp @@ -386,7 +386,14 @@ void BlenderTessellatorP2T::ReferencePoints( std::vector< Blender::PointP2T >& p // ------------------------------------------------------------------------------------------------ inline PointP2T& BlenderTessellatorP2T::GetActualPointStructure( p2t::Point& point ) const { +#if defined __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Winvalid-offsetof" +#endif // __clang__ unsigned int pointOffset = offsetof( PointP2T, point2D ); +#if defined __clang__ +# pragma clang diagnostic pop +#endif PointP2T& pointStruct = *reinterpret_cast< PointP2T* >( reinterpret_cast< char* >( &point ) - pointOffset ); if ( pointStruct.magic != static_cast( BLEND_TESS_MAGIC ) ) { @@ -394,7 +401,6 @@ inline PointP2T& BlenderTessellatorP2T::GetActualPointStructure( p2t::Point& poi } return pointStruct; } - // ------------------------------------------------------------------------------------------------ void BlenderTessellatorP2T::MakeFacesFromTriangles( std::vector< p2t::Triangle* >& triangles ) const { diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 2ba893281..23cfba4ff 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -1065,7 +1065,7 @@ endif() ADD_DEFINITIONS( -DASSIMP_BUILD_DLL_EXPORT ) -if ( MSVC ) +IF( MSVC OR "${CMAKE_CXX_SIMULATE_ID}" MATCHES "MSVC") # clang with MSVC ABI ADD_DEFINITIONS( -D_SCL_SECURE_NO_WARNINGS ) ADD_DEFINITIONS( -D_CRT_SECURE_NO_WARNINGS ) endif () From 51e592123add5da3359f37465b3e28b73852e678 Mon Sep 17 00:00:00 2001 From: MeyerFabian Date: Mon, 20 Jul 2020 18:42:57 +0200 Subject: [PATCH 19/24] Fix two deletions. --- code/CMakeLists.txt | 1 + contrib/unzip/ioapi.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 23cfba4ff..7ccc6439f 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -45,6 +45,7 @@ # cmake_minimum_required( VERSION 3.0 ) SET( HEADER_PATH ../include/assimp ) + if(NOT ANDROID AND ASSIMP_ANDROID_JNIIOSYSTEM) message(WARNING "Requesting Android JNI I/O-System in non-Android toolchain. Resetting ASSIMP_ANDROID_JNIIOSYSTEM to OFF.") set(ASSIMP_ANDROID_JNIIOSYSTEM OFF) diff --git a/contrib/unzip/ioapi.c b/contrib/unzip/ioapi.c index 0f1bf4f21..5e0497f9e 100644 --- a/contrib/unzip/ioapi.c +++ b/contrib/unzip/ioapi.c @@ -1,3 +1,11 @@ +/* ioapi.c -- IO base function header for compress/uncompress .zip + files using zlib + zip or unzip API + + Version 1.01e, February 12th, 2005 + + Copyright (C) 1998-2005 Gilles Vollant +*/ + #include #include #include From 5e0b9e0f32f93520b26187161ad1b23e5defad1b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 23 Jul 2020 11:35:36 +0200 Subject: [PATCH 20/24] Remove travix + assveyor. --- Readme.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/Readme.md b/Readme.md index dc54870a0..3f2edd402 100644 --- a/Readme.md +++ b/Readme.md @@ -4,8 +4,6 @@ A library to import and export various 3d-model-formats including scene-post-pro ### Current project status ### [![Financial Contributors on Open Collective](https://opencollective.com/assimp/all/badge.svg?label=financial+contributors)](https://opencollective.com/assimp) ![C/C++ CI](https://github.com/assimp/assimp/workflows/C/C++%20CI/badge.svg) -[![Linux Build Status](https://travis-ci.org/assimp/assimp.svg)](https://travis-ci.org/assimp/assimp) -[![Windows Build Status](https://ci.appveyor.com/api/projects/status/tmo433wax6u6cjp4?svg=true)](https://ci.appveyor.com/project/kimkulling/assimp) Coverity Scan Build Status From eb44eb13e6120d2bf74a0f4c8f6b479a0351e339 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 23 Jul 2020 19:15:46 +0200 Subject: [PATCH 21/24] fix namespace issue in fuzzer. --- fuzz/assimp_fuzzer.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fuzz/assimp_fuzzer.cc b/fuzz/assimp_fuzzer.cc index 86ffe18ed..933d783db 100644 --- a/fuzz/assimp_fuzzer.cc +++ b/fuzz/assimp_fuzzer.cc @@ -42,6 +42,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +using namespace Assimp; + extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize) { aiLogStream stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL); aiAttachLogStream(&stream); From 9a4b3fd9de1f91df5e4f8b1f26ce090024cc6107 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 23 Jul 2020 20:16:11 +0200 Subject: [PATCH 22/24] use correct include. --- fuzz/assimp_fuzzer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuzz/assimp_fuzzer.cc b/fuzz/assimp_fuzzer.cc index 933d783db..ca3f765e1 100644 --- a/fuzz/assimp_fuzzer.cc +++ b/fuzz/assimp_fuzzer.cc @@ -38,7 +38,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --------------------------------------------------------------------------- */ -#include +#include #include #include From dcc8419722258aaf577af1ff858233aa0d3fa552 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 23 Jul 2020 21:01:08 +0200 Subject: [PATCH 23/24] add missing include for logging. --- fuzz/assimp_fuzzer.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/fuzz/assimp_fuzzer.cc b/fuzz/assimp_fuzzer.cc index ca3f765e1..b65ee0236 100644 --- a/fuzz/assimp_fuzzer.cc +++ b/fuzz/assimp_fuzzer.cc @@ -38,6 +38,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --------------------------------------------------------------------------- */ +#include #include #include #include From 6886ea6c6571bfc03a9811cb38ee31f5f16b2d64 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Fri, 24 Jul 2020 10:57:24 +0200 Subject: [PATCH 24/24] Fix warning: comparison between unsigned and signed. --- test/unit/utglTF2ImportExport.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/unit/utglTF2ImportExport.cpp b/test/unit/utglTF2ImportExport.cpp index c3ad2d994..e991b04f3 100644 --- a/test/unit/utglTF2ImportExport.cpp +++ b/test/unit/utglTF2ImportExport.cpp @@ -548,11 +548,9 @@ TEST_F(utglTF2ImportExport, indexOutOfRange) { // The contents of an asset should not lead to an assert. Assimp::Importer importer; - struct LogObserver : Assimp::LogStream - { + struct LogObserver : Assimp::LogStream { bool m_observedWarning = false; - void write(const char *message) override - { + void write(const char *message) override { m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped"); } }; @@ -562,8 +560,8 @@ TEST_F(utglTF2ImportExport, indexOutOfRange) { const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure); ASSERT_NE(scene, nullptr); ASSERT_NE(scene->mRootNode, nullptr); - ASSERT_EQ(scene->mNumMeshes, 1); - EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11); + ASSERT_EQ(scene->mNumMeshes, 1u); + EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11u); DefaultLogger::get()->detachStream(&logObserver); EXPECT_TRUE(logObserver.m_observedWarning); }