utglTF2ImportExport.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #include "AbstractImportExportBase.h"
  35. #include "UnitTestPCH.h"
  36. #include <assimp/commonMetaData.h>
  37. #include <assimp/postprocess.h>
  38. #include <assimp/scene.h>
  39. #include <assimp/Exporter.hpp>
  40. #include <assimp/Importer.hpp>
  41. #include <assimp/LogStream.hpp>
  42. #include <assimp/DefaultLogger.hpp>
  43. #include <array>
  44. #include <assimp/pbrmaterial.h>
  45. using namespace Assimp;
  46. class utglTF2ImportExport : public AbstractImportExportBase {
  47. public:
  48. virtual bool importerTest() {
  49. Assimp::Importer importer;
  50. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  51. aiProcess_ValidateDataStructure);
  52. EXPECT_NE(scene, nullptr);
  53. if (!scene) {
  54. return false;
  55. }
  56. EXPECT_TRUE(scene->HasMaterials());
  57. if (!scene->HasMaterials()) {
  58. return false;
  59. }
  60. const aiMaterial *material = scene->mMaterials[0];
  61. aiString path;
  62. aiTextureMapMode modes[2];
  63. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_DIFFUSE, 0, &path, nullptr, nullptr,
  64. nullptr, nullptr, modes));
  65. EXPECT_STREQ(path.C_Str(), "CesiumLogoFlat.png");
  66. EXPECT_EQ(modes[0], aiTextureMapMode_Mirror);
  67. EXPECT_EQ(modes[1], aiTextureMapMode_Clamp);
  68. return true;
  69. }
  70. virtual bool binaryImporterTest() {
  71. Assimp::Importer importer;
  72. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/2CylinderEngine-glTF-Binary/2CylinderEngine.glb",
  73. aiProcess_ValidateDataStructure);
  74. return nullptr != scene;
  75. }
  76. #ifndef ASSIMP_BUILD_NO_EXPORT
  77. virtual bool exporterTest() {
  78. Assimp::Importer importer;
  79. Assimp::Exporter exporter;
  80. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  81. aiProcess_ValidateDataStructure);
  82. EXPECT_NE(nullptr, scene);
  83. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.gltf"));
  84. return true;
  85. }
  86. #endif // ASSIMP_BUILD_NO_EXPORT
  87. };
  88. TEST_F(utglTF2ImportExport, importglTF2FromFileTest) {
  89. EXPECT_TRUE(importerTest());
  90. }
  91. TEST_F(utglTF2ImportExport, importBinaryglTF2FromFileTest) {
  92. EXPECT_TRUE(binaryImporterTest());
  93. }
  94. #ifndef ASSIMP_BUILD_NO_EXPORT
  95. TEST_F(utglTF2ImportExport, importglTF2AndExportToOBJ) {
  96. Assimp::Importer importer;
  97. Assimp::Exporter exporter;
  98. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  99. aiProcess_ValidateDataStructure);
  100. EXPECT_NE(nullptr, scene);
  101. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.obj"));
  102. }
  103. TEST_F(utglTF2ImportExport, importglTF2EmbeddedAndExportToOBJ) {
  104. Assimp::Importer importer;
  105. Assimp::Exporter exporter;
  106. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured.gltf",
  107. aiProcess_ValidateDataStructure);
  108. EXPECT_NE(nullptr, scene);
  109. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured_out.obj"));
  110. }
  111. #endif // ASSIMP_BUILD_NO_EXPORT
  112. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) {
  113. Assimp::Importer importer;
  114. //Points without indices
  115. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_00.gltf", aiProcess_ValidateDataStructure);
  116. EXPECT_NE(nullptr, scene);
  117. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  118. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  119. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  120. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  121. }
  122. }
  123. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesWithoutIndices) {
  124. Assimp::Importer importer;
  125. //Lines without indices
  126. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_01.gltf", aiProcess_ValidateDataStructure);
  127. EXPECT_NE(nullptr, scene);
  128. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 8u);
  129. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  130. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  131. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i * 2u);
  132. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i * 2u + 1u);
  133. }
  134. }
  135. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesLoopWithoutIndices) {
  136. Assimp::Importer importer;
  137. //Lines loop without indices
  138. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_02.gltf", aiProcess_ValidateDataStructure);
  139. EXPECT_NE(nullptr, scene);
  140. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  141. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  142. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  143. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  144. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  145. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  146. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1u]);
  147. }
  148. }
  149. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesStripWithoutIndices) {
  150. Assimp::Importer importer;
  151. //Lines strip without indices
  152. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_03.gltf", aiProcess_ValidateDataStructure);
  153. EXPECT_NE(nullptr, scene);
  154. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 5u);
  155. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  156. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  157. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  158. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  159. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i + 1u);
  160. }
  161. }
  162. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStripWithoutIndices) {
  163. Assimp::Importer importer;
  164. //Triangles strip without indices
  165. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_04.gltf", aiProcess_ValidateDataStructure);
  166. EXPECT_NE(nullptr, scene);
  167. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  168. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  169. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  170. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  171. for (unsigned int i = 0; i < 3; ++i) {
  172. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  173. }
  174. std::array<unsigned int, 3> f2 = { { 2u, 1u, 3u } };
  175. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  176. for (size_t i = 0; i < 3; ++i) {
  177. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  178. }
  179. }
  180. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFanWithoutIndices) {
  181. Assimp::Importer importer;
  182. //Triangles fan without indices
  183. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_05.gltf", aiProcess_ValidateDataStructure);
  184. EXPECT_NE(nullptr, scene);
  185. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  186. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  187. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  188. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  189. for (size_t i = 0; i < 3; ++i) {
  190. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  191. }
  192. std::array<unsigned int, 3> f2 = { { 0u, 2u, 3u } };
  193. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  194. for (size_t i = 0; i < 3; ++i) {
  195. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  196. }
  197. }
  198. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesWithoutIndices) {
  199. Assimp::Importer importer;
  200. //Triangles without indices
  201. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_06.gltf", aiProcess_ValidateDataStructure);
  202. EXPECT_NE(nullptr, scene);
  203. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  204. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 6u);
  205. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  206. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  207. for (size_t i = 0; i < 3; ++i) {
  208. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  209. }
  210. std::array<unsigned int, 3> f2 = { { 3u, 4u, 5u } };
  211. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  212. for (size_t i = 0; i < 3; ++i) {
  213. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  214. }
  215. }
  216. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePoints) {
  217. Assimp::Importer importer;
  218. //Line loop
  219. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_07.gltf", aiProcess_ValidateDataStructure);
  220. EXPECT_NE(nullptr, scene);
  221. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  222. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  223. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  224. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  225. }
  226. }
  227. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLines) {
  228. Assimp::Importer importer;
  229. //Lines
  230. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_08.gltf", aiProcess_ValidateDataStructure);
  231. EXPECT_NE(nullptr, scene);
  232. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  233. std::array<unsigned int, 5> l1 = { { 0u, 3u, 2u, 1u, 0u } };
  234. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  235. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  236. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  237. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  238. }
  239. }
  240. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineLoop) {
  241. Assimp::Importer importer;
  242. //Line loop
  243. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_09.gltf", aiProcess_ValidateDataStructure);
  244. EXPECT_NE(nullptr, scene);
  245. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  246. std::array<unsigned int, 5> l1 = { { 0, 3u, 2u, 1u, 0u } };
  247. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  248. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  249. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  250. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  251. }
  252. }
  253. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineStrip) {
  254. Assimp::Importer importer;
  255. //Lines Strip
  256. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_10.gltf", aiProcess_ValidateDataStructure);
  257. EXPECT_NE(nullptr, scene);
  258. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  259. std::array<unsigned int, 5> l1 = { { 0u, 3u, 2u, 1u, 0u } };
  260. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  261. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  262. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  263. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  264. }
  265. }
  266. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStrip) {
  267. Assimp::Importer importer;
  268. //Triangles strip
  269. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_11.gltf", aiProcess_ValidateDataStructure);
  270. EXPECT_NE(nullptr, scene);
  271. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  272. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  273. std::array<unsigned int, 3> f1 = { { 0u, 3u, 1u } };
  274. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  275. for (size_t i = 0; i < 3; ++i) {
  276. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  277. }
  278. std::array<unsigned int, 3> f2 = { { 1u, 3u, 2u } };
  279. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  280. for (size_t i = 0; i < 3; ++i) {
  281. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  282. }
  283. }
  284. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFan) {
  285. Assimp::Importer importer;
  286. //Triangles fan
  287. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_12.gltf", aiProcess_ValidateDataStructure);
  288. EXPECT_NE(nullptr, scene);
  289. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  290. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  291. std::array<unsigned int, 3> f1 = { { 0u, 3u, 2u } };
  292. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  293. for (size_t i = 0; i < 3; ++i) {
  294. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  295. }
  296. std::array<unsigned int, 3> f2 = { { 0u, 2u, 1u } };
  297. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  298. for (size_t i = 0; i < 3; ++i) {
  299. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  300. }
  301. }
  302. std::vector<char> ReadFile(const char *name) {
  303. std::vector<char> ret;
  304. FILE *p = ::fopen(name, "r");
  305. if (nullptr == p) {
  306. return ret;
  307. }
  308. ::fseek(p, 0, SEEK_END);
  309. const size_t size = ::ftell(p);
  310. ::fseek(p, 0, SEEK_SET);
  311. ret.resize(size);
  312. const size_t readSize = ::fread(&ret[0], 1, size, p);
  313. EXPECT_EQ(readSize, size);
  314. ::fclose(p);
  315. return ret;
  316. }
  317. TEST_F(utglTF2ImportExport, importglTF2FromMemory) {
  318. /*const auto flags = aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_RemoveComponent |
  319. aiProcess_GenSmoothNormals | aiProcess_PreTransformVertices | aiProcess_FixInfacingNormals |
  320. aiProcess_FindDegenerates | aiProcess_GenUVCoords | aiProcess_SortByPType;
  321. const auto& buff = ReadFile("C:\\Users\\kimkulling\\Downloads\\camel\\camel\\scene.gltf");*/
  322. /*const aiScene* Scene = ::aiImportFileFromMemory(&buff[0], buff.size(), flags, ".gltf");
  323. EXPECT_EQ( nullptr, Scene );*/
  324. }
  325. TEST_F(utglTF2ImportExport, bug_import_simple_skin) {
  326. Assimp::Importer importer;
  327. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/simple_skin.gltf",
  328. aiProcess_ValidateDataStructure);
  329. EXPECT_NE(nullptr, scene);
  330. }
  331. TEST_F(utglTF2ImportExport, import_cameras) {
  332. Assimp::Importer importer;
  333. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/cameras/Cameras.gltf",
  334. aiProcess_ValidateDataStructure);
  335. EXPECT_NE(nullptr, scene);
  336. }
  337. TEST_F(utglTF2ImportExport, incorrect_vertex_arrays) {
  338. Assimp::Importer importer;
  339. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IncorrectVertexArrays/Cube.gltf",
  340. aiProcess_ValidateDataStructure);
  341. EXPECT_NE(nullptr, scene);
  342. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 36u);
  343. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 12u);
  344. EXPECT_EQ(scene->mMeshes[1]->mNumVertices, 35u);
  345. EXPECT_EQ(scene->mMeshes[1]->mNumFaces, 11u);
  346. EXPECT_EQ(scene->mMeshes[2]->mNumVertices, 36u);
  347. EXPECT_EQ(scene->mMeshes[2]->mNumFaces, 18u);
  348. EXPECT_EQ(scene->mMeshes[3]->mNumVertices, 35u);
  349. EXPECT_EQ(scene->mMeshes[3]->mNumFaces, 17u);
  350. EXPECT_EQ(scene->mMeshes[4]->mNumVertices, 36u);
  351. EXPECT_EQ(scene->mMeshes[4]->mNumFaces, 12u);
  352. EXPECT_EQ(scene->mMeshes[5]->mNumVertices, 35u);
  353. EXPECT_EQ(scene->mMeshes[5]->mNumFaces, 11u);
  354. EXPECT_EQ(scene->mMeshes[6]->mNumVertices, 36u);
  355. EXPECT_EQ(scene->mMeshes[6]->mNumFaces, 18u);
  356. EXPECT_EQ(scene->mMeshes[7]->mNumVertices, 35u);
  357. EXPECT_EQ(scene->mMeshes[7]->mNumFaces, 17u);
  358. }
  359. TEST_F(utglTF2ImportExport, texture_transform_test) {
  360. Assimp::Importer importer;
  361. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/textureTransform/TextureTransformTest.gltf",
  362. aiProcess_ValidateDataStructure);
  363. EXPECT_NE(nullptr, scene);
  364. }
  365. #ifndef ASSIMP_BUILD_NO_EXPORT
  366. TEST_F(utglTF2ImportExport, exportglTF2FromFileTest) {
  367. EXPECT_TRUE(exporterTest());
  368. }
  369. TEST_F(utglTF2ImportExport, crash_in_anim_mesh_destructor) {
  370. Assimp::Importer importer;
  371. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube.gltf",
  372. aiProcess_ValidateDataStructure);
  373. ASSERT_NE(nullptr, scene);
  374. Assimp::Exporter exporter;
  375. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube_out.glTF"));
  376. }
  377. TEST_F(utglTF2ImportExport, error_string_preserved) {
  378. Assimp::Importer importer;
  379. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/MissingBin/BoxTextured.gltf",
  380. aiProcess_ValidateDataStructure);
  381. ASSERT_EQ(nullptr, scene);
  382. std::string error = importer.GetErrorString();
  383. ASSERT_NE(error.find("BoxTextured0.bin"), std::string::npos) << "Error string should contain an error about missing .bin file";
  384. }
  385. TEST_F(utglTF2ImportExport, export_bad_accessor_bounds) {
  386. Assimp::Importer importer;
  387. Assimp::Exporter exporter;
  388. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb", aiProcess_ValidateDataStructure);
  389. ASSERT_NE(scene, nullptr);
  390. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.glb"));
  391. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.gltf"));
  392. }
  393. TEST_F(utglTF2ImportExport, export_normalized_normals) {
  394. Assimp::Importer importer;
  395. Assimp::Exporter exporter;
  396. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb", aiProcess_ValidateDataStructure);
  397. ASSERT_NE(scene, nullptr);
  398. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb"));
  399. // load in again and ensure normal-length normals but no Nan's or Inf's introduced
  400. scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb", aiProcess_ValidateDataStructure);
  401. for ( auto i = 0u; i < scene->mMeshes[0]->mNumVertices; ++i ) {
  402. const auto length = scene->mMeshes[0]->mNormals[i].Length();
  403. EXPECT_TRUE(abs(length) < 1e-6 || abs(length - 1) < 1e-6);
  404. }
  405. }
  406. #endif // ASSIMP_BUILD_NO_EXPORT
  407. TEST_F(utglTF2ImportExport, sceneMetadata) {
  408. Assimp::Importer importer;
  409. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  410. aiProcess_ValidateDataStructure);
  411. ASSERT_NE(scene, nullptr);
  412. ASSERT_NE(scene->mMetaData, nullptr);
  413. {
  414. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  415. aiString format;
  416. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  417. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  418. }
  419. {
  420. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  421. aiString version;
  422. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  423. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  424. }
  425. {
  426. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  427. aiString generator;
  428. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  429. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  430. }
  431. }
  432. TEST_F(utglTF2ImportExport, texcoords) {
  433. Assimp::Importer importer;
  434. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf",
  435. aiProcess_ValidateDataStructure);
  436. ASSERT_NE(scene, nullptr);
  437. ASSERT_TRUE(scene->HasMaterials());
  438. const aiMaterial *material = scene->mMaterials[0];
  439. aiString path;
  440. aiTextureMapMode modes[2];
  441. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_DIFFUSE, 0, &path, nullptr, nullptr,
  442. nullptr, nullptr, modes));
  443. EXPECT_STREQ(path.C_Str(), "texture.png");
  444. int uvIndex = -1;
  445. EXPECT_EQ(aiGetMaterialInteger(material, AI_MATKEY_GLTF_TEXTURE_TEXCOORD(aiTextureType_DIFFUSE, 0), &uvIndex), aiReturn_SUCCESS);
  446. EXPECT_EQ(uvIndex, 0);
  447. // Using manual macro expansion of AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE here.
  448. // The following works with some but not all compilers:
  449. // #define APPLY(X, Y) X(Y)
  450. // ..., APPLY(AI_MATKEY_GLTF_TEXTURE_TEXCOORD, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE), ...
  451. EXPECT_EQ(aiGetMaterialInteger(material, AI_MATKEY_GLTF_TEXTURE_TEXCOORD(aiTextureType_UNKNOWN, 0), &uvIndex), aiReturn_SUCCESS);
  452. EXPECT_EQ(uvIndex, 1);
  453. }
  454. TEST_F(utglTF2ImportExport, recursive_nodes) {
  455. Assimp::Importer importer;
  456. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/RecursiveNodes/RecursiveNodes.gltf", aiProcess_ValidateDataStructure);
  457. EXPECT_EQ(nullptr, scene);
  458. }
  459. TEST_F(utglTF2ImportExport, norootnode_noscene) {
  460. Assimp::Importer importer;
  461. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/NoScene.gltf", aiProcess_ValidateDataStructure);
  462. ASSERT_EQ(scene, nullptr);
  463. }
  464. TEST_F(utglTF2ImportExport, norootnode_scenewithoutnodes) {
  465. Assimp::Importer importer;
  466. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/SceneWithoutNodes.gltf", aiProcess_ValidateDataStructure);
  467. ASSERT_NE(scene, nullptr);
  468. ASSERT_NE(scene->mRootNode, nullptr);
  469. }
  470. // Shall not crash!
  471. TEST_F(utglTF2ImportExport, norootnode_issue_3269) {
  472. Assimp::Importer importer;
  473. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/issue_3269/texcoord_crash.gltf", aiProcess_ValidateDataStructure);
  474. ASSERT_EQ(scene, nullptr);
  475. }
  476. TEST_F(utglTF2ImportExport, indexOutOfRange) {
  477. // The contents of an asset should not lead to an assert.
  478. Assimp::Importer importer;
  479. struct LogObserver : Assimp::LogStream {
  480. bool m_observedWarning = false;
  481. void write(const char *message) override {
  482. m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped");
  483. }
  484. };
  485. LogObserver logObserver;
  486. DefaultLogger::get()->attachStream(&logObserver);
  487. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure);
  488. ASSERT_NE(scene, nullptr);
  489. ASSERT_NE(scene->mRootNode, nullptr);
  490. ASSERT_EQ(scene->mNumMeshes, 1u);
  491. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11u);
  492. DefaultLogger::get()->detachStream(&logObserver);
  493. EXPECT_TRUE(logObserver.m_observedWarning);
  494. }
  495. TEST_F(utglTF2ImportExport, allIndicesOutOfRange) {
  496. // The contents of an asset should not lead to an assert.
  497. Assimp::Importer importer;
  498. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf", aiProcess_ValidateDataStructure);
  499. ASSERT_EQ(scene, nullptr);
  500. std::string error = importer.GetErrorString();
  501. ASSERT_NE(error.find("Mesh \"Mesh\" has no faces"), std::string::npos);
  502. }
  503. /////////////////////////////////
  504. // Draco decoding
  505. TEST_F(utglTF2ImportExport, import_dracoEncoded) {
  506. Assimp::Importer importer;
  507. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/draco/2CylinderEngine.gltf",
  508. aiProcess_ValidateDataStructure);
  509. #ifndef ASSIMP_ENABLE_DRACO
  510. // No draco support, scene should not load
  511. ASSERT_EQ(scene, nullptr);
  512. #else
  513. ASSERT_NE(scene, nullptr);
  514. ASSERT_NE(scene->mMetaData, nullptr);
  515. {
  516. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  517. aiString format;
  518. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  519. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  520. }
  521. {
  522. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  523. aiString version;
  524. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  525. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  526. }
  527. {
  528. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  529. aiString generator;
  530. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  531. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  532. }
  533. #endif
  534. }
  535. TEST_F(utglTF2ImportExport, wrongTypes) {
  536. // Deliberately broken version of the BoxTextured.gltf asset.
  537. std::vector<std::tuple<std::string, std::string, std::string, std::string>> wrongTypes = {
  538. { "/glTF2/wrongTypes/badArray.gltf", "array", "primitives", "meshes[0]" },
  539. { "/glTF2/wrongTypes/badString.gltf", "string", "name", "scenes[0]" },
  540. { "/glTF2/wrongTypes/badUint.gltf", "uint", "index", "materials[0]" },
  541. { "/glTF2/wrongTypes/badNumber.gltf", "number", "scale", "materials[0]" },
  542. { "/glTF2/wrongTypes/badObject.gltf", "object", "pbrMetallicRoughness", "materials[0]" },
  543. { "/glTF2/wrongTypes/badExtension.gltf", "object", "KHR_texture_transform", "materials[0]" }
  544. };
  545. for (const auto& tuple : wrongTypes)
  546. {
  547. const auto& file = std::get<0>(tuple);
  548. const auto& type = std::get<1>(tuple);
  549. const auto& member = std::get<2>(tuple);
  550. const auto& context = std::get<3>(tuple);
  551. Assimp::Importer importer;
  552. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR + file , aiProcess_ValidateDataStructure);
  553. ASSERT_EQ(scene, nullptr);
  554. const std::string error = importer.GetErrorString();
  555. EXPECT_FALSE(error.empty());
  556. EXPECT_NE(error.find(member + "\" was not of type \"" + type + "\" when reading " + context), std::string::npos);
  557. }
  558. }