utglTF2ImportExport.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 <rapidjson/schema.h>
  44. #include <array>
  45. #include <assimp/material.h>
  46. #include <assimp/GltfMaterial.h>
  47. using namespace Assimp;
  48. class utglTF2ImportExport : public AbstractImportExportBase {
  49. public:
  50. virtual bool importerMatTest(const char *file, bool spec_gloss, std::array<aiTextureMapMode, 2> exp_modes = { aiTextureMapMode_Wrap, aiTextureMapMode_Wrap }) {
  51. Assimp::Importer importer;
  52. const aiScene *scene = importer.ReadFile(file, aiProcess_ValidateDataStructure);
  53. EXPECT_NE(scene, nullptr);
  54. if (!scene) {
  55. return false;
  56. }
  57. EXPECT_TRUE(scene->HasMaterials());
  58. if (!scene->HasMaterials()) {
  59. return false;
  60. }
  61. const aiMaterial *material = scene->mMaterials[0];
  62. // This Material should be a PBR
  63. aiShadingMode shadingMode;
  64. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_SHADING_MODEL, shadingMode));
  65. EXPECT_EQ(aiShadingMode_PBR_BRDF, shadingMode);
  66. // Should import the texture as diffuse and as base color
  67. aiString path;
  68. std::array<aiTextureMapMode,2> modes;
  69. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_DIFFUSE, 0, &path, nullptr, nullptr,
  70. nullptr, nullptr, modes.data()));
  71. EXPECT_STREQ(path.C_Str(), "CesiumLogoFlat.png");
  72. EXPECT_EQ(exp_modes, modes);
  73. // Also as Base Color
  74. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_BASE_COLOR, 0, &path, nullptr, nullptr,
  75. nullptr, nullptr, modes.data()));
  76. EXPECT_STREQ(path.C_Str(), "CesiumLogoFlat.png");
  77. EXPECT_EQ(exp_modes, modes);
  78. // Should have a MetallicFactor (default is 1.0)
  79. ai_real metal_factor = ai_real(0.5);
  80. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_METALLIC_FACTOR, metal_factor));
  81. EXPECT_EQ(ai_real(0.0), metal_factor);
  82. // And a roughness factor (default is 1.0)
  83. ai_real roughness_factor = ai_real(0.5);
  84. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_ROUGHNESS_FACTOR, roughness_factor));
  85. EXPECT_EQ(ai_real(1.0), roughness_factor);
  86. aiColor3D spec_color = { 0, 0, 0 };
  87. ai_real glossiness = ai_real(0.5);
  88. if (spec_gloss) {
  89. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_COLOR_SPECULAR, spec_color));
  90. constexpr ai_real spec_val(0.20000000298023225); // From the file
  91. EXPECT_EQ(spec_val, spec_color.r);
  92. EXPECT_EQ(spec_val, spec_color.g);
  93. EXPECT_EQ(spec_val, spec_color.b);
  94. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_GLOSSINESS_FACTOR, glossiness));
  95. EXPECT_EQ(ai_real(1.0), glossiness);
  96. } else {
  97. EXPECT_EQ(aiReturn_FAILURE, material->Get(AI_MATKEY_COLOR_SPECULAR, spec_color));
  98. EXPECT_EQ(aiReturn_FAILURE, material->Get(AI_MATKEY_GLOSSINESS_FACTOR, glossiness));
  99. }
  100. return true;
  101. }
  102. virtual bool binaryImporterTest() {
  103. Assimp::Importer importer;
  104. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/2CylinderEngine-glTF-Binary/2CylinderEngine.glb",
  105. aiProcess_ValidateDataStructure);
  106. return nullptr != scene;
  107. }
  108. #ifndef ASSIMP_BUILD_NO_EXPORT
  109. virtual bool exporterTest() {
  110. Assimp::Importer importer;
  111. Assimp::Exporter exporter;
  112. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  113. aiProcess_ValidateDataStructure);
  114. EXPECT_NE(nullptr, scene);
  115. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.gltf"));
  116. return true;
  117. }
  118. #endif // ASSIMP_BUILD_NO_EXPORT
  119. };
  120. TEST_F(utglTF2ImportExport, importglTF2FromFileTest) {
  121. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", false, {aiTextureMapMode_Mirror, aiTextureMapMode_Clamp}));
  122. }
  123. TEST_F(utglTF2ImportExport, importBinaryglTF2FromFileTest) {
  124. EXPECT_TRUE(binaryImporterTest());
  125. }
  126. TEST_F(utglTF2ImportExport, importglTF2_KHR_materials_pbrSpecularGlossiness) {
  127. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured.gltf", true));
  128. }
  129. void VerifyClearCoatScene(const aiScene *scene) {
  130. ASSERT_NE(nullptr, scene);
  131. ASSERT_TRUE(scene->HasMaterials());
  132. // Find a specific Clearcoat material and check the values
  133. const aiString partial_coated("Partial_Coated");
  134. bool found_partial_coat = false;
  135. for (size_t i = 0; i < scene->mNumMaterials; ++i) {
  136. const aiMaterial *material = scene->mMaterials[i];
  137. ASSERT_NE(nullptr, material);
  138. if (material->GetName() == partial_coated) {
  139. found_partial_coat = true;
  140. ai_real clearcoat_factor(0.0f);
  141. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_CLEARCOAT_FACTOR, clearcoat_factor));
  142. EXPECT_EQ(ai_real(1.0f), clearcoat_factor);
  143. ai_real clearcoat_rough_factor(0.0f);
  144. EXPECT_EQ(aiReturn_SUCCESS, material->Get(AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR, clearcoat_rough_factor));
  145. EXPECT_EQ(ai_real(0.03f), clearcoat_rough_factor);
  146. // Should import the texture as diffuse and as base color
  147. aiString path;
  148. std::array<aiTextureMapMode, 2> modes;
  149. static const std::array<aiTextureMapMode, 2> exp_modes = { aiTextureMapMode_Wrap, aiTextureMapMode_Wrap };
  150. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_CLEARCOAT_TEXTURE, &path, nullptr, nullptr,
  151. nullptr, nullptr, modes.data()));
  152. EXPECT_STREQ(path.C_Str(), "PartialCoating.png");
  153. EXPECT_EQ(exp_modes, modes);
  154. }
  155. }
  156. EXPECT_TRUE(found_partial_coat);
  157. }
  158. TEST_F(utglTF2ImportExport, importglTF2_KHR_materials_clearcoat) {
  159. Assimp::Importer importer;
  160. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest.gltf", aiProcess_ValidateDataStructure);
  161. VerifyClearCoatScene(scene);
  162. }
  163. #ifndef ASSIMP_BUILD_NO_EXPORT
  164. TEST_F(utglTF2ImportExport, importglTF2AndExport_KHR_materials_clearcoat) {
  165. {
  166. Assimp::Importer importer;
  167. Assimp::Exporter exporter;
  168. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest.gltf", aiProcess_ValidateDataStructure);
  169. ASSERT_NE(nullptr, scene);
  170. // Export
  171. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest_out.glb"));
  172. }
  173. // And re-import
  174. Assimp::Importer importer;
  175. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/ClearCoat-glTF/ClearCoatTest_out.glb", aiProcess_ValidateDataStructure);
  176. VerifyClearCoatScene(scene);
  177. }
  178. TEST_F(utglTF2ImportExport, importglTF2AndExport_KHR_materials_pbrSpecularGlossiness) {
  179. Assimp::Importer importer;
  180. Assimp::Exporter exporter;
  181. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured.gltf",
  182. aiProcess_ValidateDataStructure);
  183. EXPECT_NE(nullptr, scene);
  184. // Export
  185. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb"));
  186. // And re-import
  187. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb", true));
  188. }
  189. TEST_F(utglTF2ImportExport, importglTF2AndExportToOBJ) {
  190. Assimp::Importer importer;
  191. Assimp::Exporter exporter;
  192. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  193. aiProcess_ValidateDataStructure);
  194. EXPECT_NE(nullptr, scene);
  195. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.obj"));
  196. }
  197. TEST_F(utglTF2ImportExport, importglTF2EmbeddedAndExportToOBJ) {
  198. Assimp::Importer importer;
  199. Assimp::Exporter exporter;
  200. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured.gltf",
  201. aiProcess_ValidateDataStructure);
  202. EXPECT_NE(nullptr, scene);
  203. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured_out.obj"));
  204. }
  205. #endif // ASSIMP_BUILD_NO_EXPORT
  206. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) {
  207. Assimp::Importer importer;
  208. //Points without indices
  209. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_00.gltf", aiProcess_ValidateDataStructure);
  210. EXPECT_NE(nullptr, scene);
  211. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  212. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  213. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  214. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  215. }
  216. }
  217. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesWithoutIndices) {
  218. Assimp::Importer importer;
  219. //Lines without indices
  220. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_01.gltf", aiProcess_ValidateDataStructure);
  221. EXPECT_NE(nullptr, scene);
  222. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 8u);
  223. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  224. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  225. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i * 2u);
  226. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i * 2u + 1u);
  227. }
  228. }
  229. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesLoopWithoutIndices) {
  230. Assimp::Importer importer;
  231. //Lines loop without indices
  232. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_02.gltf", aiProcess_ValidateDataStructure);
  233. EXPECT_NE(nullptr, scene);
  234. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  235. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  236. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  237. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  238. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  239. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  240. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1u]);
  241. }
  242. }
  243. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesStripWithoutIndices) {
  244. Assimp::Importer importer;
  245. //Lines strip without indices
  246. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_03.gltf", aiProcess_ValidateDataStructure);
  247. EXPECT_NE(nullptr, scene);
  248. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 5u);
  249. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  250. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  251. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  252. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  253. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i + 1u);
  254. }
  255. }
  256. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStripWithoutIndices) {
  257. Assimp::Importer importer;
  258. //Triangles strip without indices
  259. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_04.gltf", aiProcess_ValidateDataStructure);
  260. EXPECT_NE(nullptr, scene);
  261. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  262. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  263. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  264. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  265. for (unsigned int i = 0; i < 3; ++i) {
  266. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  267. }
  268. std::array<unsigned int, 3> f2 = { { 2u, 1u, 3u } };
  269. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  270. for (size_t i = 0; i < 3; ++i) {
  271. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  272. }
  273. }
  274. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFanWithoutIndices) {
  275. Assimp::Importer importer;
  276. //Triangles fan without indices
  277. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_05.gltf", aiProcess_ValidateDataStructure);
  278. EXPECT_NE(nullptr, scene);
  279. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  280. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  281. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  282. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  283. for (size_t i = 0; i < 3; ++i) {
  284. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  285. }
  286. std::array<unsigned int, 3> f2 = { { 0u, 2u, 3u } };
  287. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  288. for (size_t i = 0; i < 3; ++i) {
  289. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  290. }
  291. }
  292. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesWithoutIndices) {
  293. Assimp::Importer importer;
  294. //Triangles without indices
  295. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_06.gltf", aiProcess_ValidateDataStructure);
  296. EXPECT_NE(nullptr, scene);
  297. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  298. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 6u);
  299. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  300. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  301. for (size_t i = 0; i < 3; ++i) {
  302. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  303. }
  304. std::array<unsigned int, 3> f2 = { { 3u, 4u, 5u } };
  305. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  306. for (size_t i = 0; i < 3; ++i) {
  307. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  308. }
  309. }
  310. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePoints) {
  311. Assimp::Importer importer;
  312. //Line loop
  313. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_07.gltf", aiProcess_ValidateDataStructure);
  314. EXPECT_NE(nullptr, scene);
  315. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  316. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  317. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  318. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  319. }
  320. }
  321. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLines) {
  322. Assimp::Importer importer;
  323. //Lines
  324. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_08.gltf", aiProcess_ValidateDataStructure);
  325. EXPECT_NE(nullptr, scene);
  326. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  327. std::array<unsigned int, 5> l1 = { { 0u, 3u, 2u, 1u, 0u } };
  328. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  329. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  330. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  331. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  332. }
  333. }
  334. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineLoop) {
  335. Assimp::Importer importer;
  336. //Line loop
  337. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_09.gltf", aiProcess_ValidateDataStructure);
  338. EXPECT_NE(nullptr, scene);
  339. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  340. std::array<unsigned int, 5> l1 = { { 0, 3u, 2u, 1u, 0u } };
  341. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  342. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  343. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  344. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  345. }
  346. }
  347. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineStrip) {
  348. Assimp::Importer importer;
  349. //Lines Strip
  350. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_10.gltf", aiProcess_ValidateDataStructure);
  351. EXPECT_NE(nullptr, scene);
  352. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  353. std::array<unsigned int, 5> l1 = { { 0u, 3u, 2u, 1u, 0u } };
  354. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  355. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  356. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  357. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  358. }
  359. }
  360. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStrip) {
  361. Assimp::Importer importer;
  362. //Triangles strip
  363. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_11.gltf", aiProcess_ValidateDataStructure);
  364. EXPECT_NE(nullptr, scene);
  365. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  366. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  367. std::array<unsigned int, 3> f1 = { { 0u, 3u, 1u } };
  368. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  369. for (size_t i = 0; i < 3; ++i) {
  370. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  371. }
  372. std::array<unsigned int, 3> f2 = { { 1u, 3u, 2u } };
  373. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  374. for (size_t i = 0; i < 3; ++i) {
  375. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  376. }
  377. }
  378. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFan) {
  379. Assimp::Importer importer;
  380. //Triangles fan
  381. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_12.gltf", aiProcess_ValidateDataStructure);
  382. EXPECT_NE(nullptr, scene);
  383. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  384. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  385. std::array<unsigned int, 3> f1 = { { 0u, 3u, 2u } };
  386. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  387. for (size_t i = 0; i < 3; ++i) {
  388. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  389. }
  390. std::array<unsigned int, 3> f2 = { { 0u, 2u, 1u } };
  391. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  392. for (size_t i = 0; i < 3; ++i) {
  393. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  394. }
  395. }
  396. std::vector<char> ReadFile(const char *name) {
  397. std::vector<char> ret;
  398. FILE *p = ::fopen(name, "r");
  399. if (nullptr == p) {
  400. return ret;
  401. }
  402. ::fseek(p, 0, SEEK_END);
  403. const size_t size = ::ftell(p);
  404. ::fseek(p, 0, SEEK_SET);
  405. ret.resize(size);
  406. const size_t readSize = ::fread(&ret[0], 1, size, p);
  407. EXPECT_EQ(readSize, size);
  408. ::fclose(p);
  409. return ret;
  410. }
  411. TEST_F(utglTF2ImportExport, importglTF2FromMemory) {
  412. /*const auto flags = aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_RemoveComponent |
  413. aiProcess_GenSmoothNormals | aiProcess_PreTransformVertices | aiProcess_FixInfacingNormals |
  414. aiProcess_FindDegenerates | aiProcess_GenUVCoords | aiProcess_SortByPType;
  415. const auto& buff = ReadFile("C:\\Users\\kimkulling\\Downloads\\camel\\camel\\scene.gltf");*/
  416. /*const aiScene* Scene = ::aiImportFileFromMemory(&buff[0], buff.size(), flags, ".gltf");
  417. EXPECT_EQ( nullptr, Scene );*/
  418. }
  419. TEST_F(utglTF2ImportExport, bug_import_simple_skin) {
  420. Assimp::Importer importer;
  421. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/simple_skin.gltf",
  422. aiProcess_ValidateDataStructure);
  423. EXPECT_NE(nullptr, scene);
  424. }
  425. TEST_F(utglTF2ImportExport, import_cameras) {
  426. Assimp::Importer importer;
  427. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/cameras/Cameras.gltf",
  428. aiProcess_ValidateDataStructure);
  429. EXPECT_NE(nullptr, scene);
  430. }
  431. TEST_F(utglTF2ImportExport, incorrect_vertex_arrays) {
  432. Assimp::Importer importer;
  433. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IncorrectVertexArrays/Cube.gltf",
  434. aiProcess_ValidateDataStructure);
  435. EXPECT_NE(nullptr, scene);
  436. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 36u);
  437. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 12u);
  438. EXPECT_EQ(scene->mMeshes[1]->mNumVertices, 35u);
  439. EXPECT_EQ(scene->mMeshes[1]->mNumFaces, 11u);
  440. EXPECT_EQ(scene->mMeshes[2]->mNumVertices, 36u);
  441. EXPECT_EQ(scene->mMeshes[2]->mNumFaces, 18u);
  442. EXPECT_EQ(scene->mMeshes[3]->mNumVertices, 35u);
  443. EXPECT_EQ(scene->mMeshes[3]->mNumFaces, 17u);
  444. EXPECT_EQ(scene->mMeshes[4]->mNumVertices, 36u);
  445. EXPECT_EQ(scene->mMeshes[4]->mNumFaces, 12u);
  446. EXPECT_EQ(scene->mMeshes[5]->mNumVertices, 35u);
  447. EXPECT_EQ(scene->mMeshes[5]->mNumFaces, 11u);
  448. EXPECT_EQ(scene->mMeshes[6]->mNumVertices, 36u);
  449. EXPECT_EQ(scene->mMeshes[6]->mNumFaces, 18u);
  450. EXPECT_EQ(scene->mMeshes[7]->mNumVertices, 35u);
  451. EXPECT_EQ(scene->mMeshes[7]->mNumFaces, 17u);
  452. }
  453. TEST_F(utglTF2ImportExport, texture_transform_test) {
  454. Assimp::Importer importer;
  455. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/textureTransform/TextureTransformTest.gltf",
  456. aiProcess_ValidateDataStructure);
  457. EXPECT_NE(nullptr, scene);
  458. }
  459. #ifndef ASSIMP_BUILD_NO_EXPORT
  460. TEST_F(utglTF2ImportExport, exportglTF2FromFileTest) {
  461. EXPECT_TRUE(exporterTest());
  462. }
  463. TEST_F(utglTF2ImportExport, crash_in_anim_mesh_destructor) {
  464. Assimp::Importer importer;
  465. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube.gltf",
  466. aiProcess_ValidateDataStructure);
  467. ASSERT_NE(nullptr, scene);
  468. Assimp::Exporter exporter;
  469. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube_out.glTF"));
  470. }
  471. TEST_F(utglTF2ImportExport, error_string_preserved) {
  472. Assimp::Importer importer;
  473. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/MissingBin/BoxTextured.gltf",
  474. aiProcess_ValidateDataStructure);
  475. ASSERT_EQ(nullptr, scene);
  476. std::string error = importer.GetErrorString();
  477. ASSERT_NE(error.find("BoxTextured0.bin"), std::string::npos) << "Error string should contain an error about missing .bin file";
  478. }
  479. TEST_F(utglTF2ImportExport, export_bad_accessor_bounds) {
  480. Assimp::Importer importer;
  481. Assimp::Exporter exporter;
  482. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb", aiProcess_ValidateDataStructure);
  483. ASSERT_NE(scene, nullptr);
  484. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.glb"));
  485. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.gltf"));
  486. }
  487. TEST_F(utglTF2ImportExport, export_normalized_normals) {
  488. Assimp::Importer importer;
  489. Assimp::Exporter exporter;
  490. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb", aiProcess_ValidateDataStructure);
  491. ASSERT_NE(scene, nullptr);
  492. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb"));
  493. // load in again and ensure normal-length normals but no Nan's or Inf's introduced
  494. scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb", aiProcess_ValidateDataStructure);
  495. for ( auto i = 0u; i < scene->mMeshes[0]->mNumVertices; ++i ) {
  496. const auto length = scene->mMeshes[0]->mNormals[i].Length();
  497. EXPECT_TRUE(abs(length) < 1e-6 || abs(length - 1) < 1e-6);
  498. }
  499. }
  500. #endif // ASSIMP_BUILD_NO_EXPORT
  501. TEST_F(utglTF2ImportExport, sceneMetadata) {
  502. Assimp::Importer importer;
  503. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  504. aiProcess_ValidateDataStructure);
  505. ASSERT_NE(scene, nullptr);
  506. ASSERT_NE(scene->mMetaData, nullptr);
  507. {
  508. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  509. aiString format;
  510. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  511. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  512. }
  513. {
  514. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  515. aiString version;
  516. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  517. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  518. }
  519. {
  520. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  521. aiString generator;
  522. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  523. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  524. }
  525. }
  526. TEST_F(utglTF2ImportExport, texcoords) {
  527. Assimp::Importer importer;
  528. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  529. ASSERT_NE(scene, nullptr);
  530. ASSERT_TRUE(scene->HasMaterials());
  531. const aiMaterial *material = scene->mMaterials[0];
  532. aiString path;
  533. unsigned int uvIndex = 255;
  534. aiTextureMapMode modes[2];
  535. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  536. EXPECT_STREQ(path.C_Str(), "texture.png");
  537. EXPECT_EQ(uvIndex, 0u);
  538. uvIndex = 255;
  539. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  540. EXPECT_STREQ(path.C_Str(), "texture.png");
  541. EXPECT_EQ(uvIndex, 1u);
  542. }
  543. #ifndef ASSIMP_BUILD_NO_EXPORT
  544. TEST_F(utglTF2ImportExport, texcoords_export) {
  545. {
  546. Assimp::Importer importer;
  547. Assimp::Exporter exporter;
  548. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  549. ASSERT_NE(scene, nullptr);
  550. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf_out.glb"));
  551. }
  552. Assimp::Importer importer;
  553. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  554. ASSERT_NE(scene, nullptr);
  555. ASSERT_TRUE(scene->HasMaterials());
  556. const aiMaterial *material = scene->mMaterials[0];
  557. aiString path;
  558. unsigned int uvIndex = 255;
  559. aiTextureMapMode modes[2];
  560. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  561. EXPECT_STREQ(path.C_Str(), "texture.png");
  562. EXPECT_EQ(uvIndex, 0u);
  563. uvIndex = 255;
  564. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  565. EXPECT_STREQ(path.C_Str(), "texture.png");
  566. EXPECT_EQ(uvIndex, 1u);
  567. }
  568. #endif // ASSIMP_BUILD_NO_EXPORT
  569. TEST_F(utglTF2ImportExport, recursive_nodes) {
  570. Assimp::Importer importer;
  571. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/RecursiveNodes/RecursiveNodes.gltf", aiProcess_ValidateDataStructure);
  572. EXPECT_EQ(nullptr, scene);
  573. }
  574. TEST_F(utglTF2ImportExport, norootnode_noscene) {
  575. Assimp::Importer importer;
  576. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/NoScene.gltf", aiProcess_ValidateDataStructure);
  577. ASSERT_EQ(scene, nullptr);
  578. }
  579. TEST_F(utglTF2ImportExport, norootnode_scenewithoutnodes) {
  580. Assimp::Importer importer;
  581. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/SceneWithoutNodes.gltf", aiProcess_ValidateDataStructure);
  582. ASSERT_NE(scene, nullptr);
  583. ASSERT_NE(scene->mRootNode, nullptr);
  584. }
  585. // Shall not crash!
  586. TEST_F(utglTF2ImportExport, norootnode_issue_3269) {
  587. Assimp::Importer importer;
  588. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/issue_3269/texcoord_crash.gltf", aiProcess_ValidateDataStructure);
  589. ASSERT_EQ(scene, nullptr);
  590. }
  591. TEST_F(utglTF2ImportExport, indexOutOfRange) {
  592. // The contents of an asset should not lead to an assert.
  593. Assimp::Importer importer;
  594. struct LogObserver : Assimp::LogStream {
  595. bool m_observedWarning = false;
  596. void write(const char *message) override {
  597. m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped");
  598. }
  599. };
  600. LogObserver logObserver;
  601. DefaultLogger::get()->attachStream(&logObserver);
  602. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure);
  603. ASSERT_NE(scene, nullptr);
  604. ASSERT_NE(scene->mRootNode, nullptr);
  605. ASSERT_EQ(scene->mNumMeshes, 1u);
  606. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11u);
  607. DefaultLogger::get()->detachStream(&logObserver);
  608. EXPECT_TRUE(logObserver.m_observedWarning);
  609. }
  610. TEST_F(utglTF2ImportExport, allIndicesOutOfRange) {
  611. // The contents of an asset should not lead to an assert.
  612. Assimp::Importer importer;
  613. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf", aiProcess_ValidateDataStructure);
  614. ASSERT_EQ(scene, nullptr);
  615. std::string error = importer.GetErrorString();
  616. ASSERT_NE(error.find("Mesh \"Mesh\" has no faces"), std::string::npos);
  617. }
  618. /////////////////////////////////
  619. // Draco decoding
  620. TEST_F(utglTF2ImportExport, import_dracoEncoded) {
  621. Assimp::Importer importer;
  622. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/draco/2CylinderEngine.gltf",
  623. aiProcess_ValidateDataStructure);
  624. #ifndef ASSIMP_ENABLE_DRACO
  625. // No draco support, scene should not load
  626. ASSERT_EQ(scene, nullptr);
  627. #else
  628. ASSERT_NE(scene, nullptr);
  629. ASSERT_NE(scene->mMetaData, nullptr);
  630. {
  631. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  632. aiString format;
  633. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  634. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  635. }
  636. {
  637. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  638. aiString version;
  639. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  640. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  641. }
  642. {
  643. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  644. aiString generator;
  645. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  646. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  647. }
  648. #endif
  649. }
  650. TEST_F(utglTF2ImportExport, wrongTypes) {
  651. // Deliberately broken version of the BoxTextured.gltf asset.
  652. using tup_T = std::tuple<std::string, std::string, std::string, std::string>;
  653. std::vector<tup_T> wrongTypes = {
  654. { "/glTF2/wrongTypes/badArray.gltf", "array", "primitives", "meshes[0]" },
  655. { "/glTF2/wrongTypes/badString.gltf", "string", "name", "scenes[0]" },
  656. { "/glTF2/wrongTypes/badUint.gltf", "uint", "index", "materials[0]" },
  657. { "/glTF2/wrongTypes/badNumber.gltf", "number", "scale", "materials[0]" },
  658. { "/glTF2/wrongTypes/badObject.gltf", "object", "pbrMetallicRoughness", "materials[0]" },
  659. { "/glTF2/wrongTypes/badExtension.gltf", "object", "KHR_texture_transform", "materials[0]" }
  660. };
  661. for (const auto& tuple : wrongTypes)
  662. {
  663. const auto& file = std::get<0>(tuple);
  664. const auto& type = std::get<1>(tuple);
  665. const auto& member = std::get<2>(tuple);
  666. const auto& context = std::get<3>(tuple);
  667. Assimp::Importer importer;
  668. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR + file , aiProcess_ValidateDataStructure);
  669. ASSERT_EQ(scene, nullptr);
  670. const std::string error = importer.GetErrorString();
  671. EXPECT_FALSE(error.empty());
  672. EXPECT_NE(error.find(member + "\" was not of type \"" + type + "\" when reading " + context), std::string::npos);
  673. }
  674. }
  675. namespace {
  676. /// This class provides a fake schema to the GLTF importer.
  677. /// It just checks that the file has a top-level "scene" property which is an integer.
  678. class FakeSchemaProvider : public rapidjson::IRemoteSchemaDocumentProvider
  679. {
  680. public:
  681. FakeSchemaProvider(const char* schemaName) :
  682. m_schemaName(schemaName)
  683. {
  684. rapidjson::Document schemaDoc;
  685. schemaDoc.Parse(R"==({"properties":{"scene" : { "type" : "integer" }}, "required": [ "scene" ]})==");
  686. EXPECT_FALSE(schemaDoc.HasParseError());
  687. m_schema.reset(new rapidjson::SchemaDocument(schemaDoc, m_schemaName.c_str(), static_cast<rapidjson::SizeType>(m_schemaName.size()), this));
  688. }
  689. const rapidjson::SchemaDocument* GetRemoteDocument(const char* uri, rapidjson::SizeType) override {
  690. if (m_schemaName == uri) {
  691. return m_schema.get();
  692. }
  693. return nullptr;
  694. }
  695. private:
  696. std::string m_schemaName;
  697. std::unique_ptr<const rapidjson::SchemaDocument> m_schema;
  698. };
  699. }
  700. TEST_F(utglTF2ImportExport, schemaCheckPass) {
  701. FakeSchemaProvider schemaProvider("glTF.schema.json");
  702. Assimp::Importer importer;
  703. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  704. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
  705. EXPECT_NE(scene, nullptr);
  706. EXPECT_STREQ(importer.GetErrorString(), "");
  707. }
  708. TEST_F(utglTF2ImportExport, schemaCheckFail) {
  709. FakeSchemaProvider schemaProvider("glTF.schema.json");
  710. Assimp::Importer importer;
  711. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  712. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/SchemaFailures/sceneWrongType.gltf", aiProcess_ValidateDataStructure);
  713. EXPECT_EQ(scene, nullptr);
  714. const std::string errorString = importer.GetErrorString();
  715. EXPECT_NE(errorString.find("The JSON document did not satisfy the glTF2 schema"), std::string::npos);
  716. }
  717. TEST_F(utglTF2ImportExport, noSchemaFound) {
  718. // More than one importer might make use the provider, but not all schemas might be present.
  719. // Check that the glTF importer handles the case when an non-null provider returns null when asked for schemas.
  720. FakeSchemaProvider schemaProvider("missingSchema.json");
  721. Assimp::Importer importer;
  722. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  723. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
  724. EXPECT_NE(scene, nullptr);
  725. EXPECT_STREQ(importer.GetErrorString(), "");
  726. }