utglTF2ImportExport.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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 with specular glossiness disabled
  185. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb"));
  186. // Export with specular glossiness enabled
  187. ExportProperties props;
  188. props.SetPropertyBool(AI_CONFIG_USE_GLTF_PBR_SPECULAR_GLOSSINESS, true);
  189. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb", 0, &props));
  190. // And re-import
  191. EXPECT_TRUE(importerMatTest(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-pbrSpecularGlossiness/BoxTextured_out.glb", true));
  192. }
  193. TEST_F(utglTF2ImportExport, importglTF2AndExportToOBJ) {
  194. Assimp::Importer importer;
  195. Assimp::Exporter exporter;
  196. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  197. aiProcess_ValidateDataStructure);
  198. EXPECT_NE(nullptr, scene);
  199. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.obj"));
  200. }
  201. TEST_F(utglTF2ImportExport, importglTF2EmbeddedAndExportToOBJ) {
  202. Assimp::Importer importer;
  203. Assimp::Exporter exporter;
  204. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured.gltf",
  205. aiProcess_ValidateDataStructure);
  206. EXPECT_NE(nullptr, scene);
  207. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured_out.obj"));
  208. }
  209. #endif // ASSIMP_BUILD_NO_EXPORT
  210. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) {
  211. Assimp::Importer importer;
  212. //Points without indices
  213. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_00.gltf", aiProcess_ValidateDataStructure);
  214. EXPECT_NE(nullptr, scene);
  215. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  216. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  217. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  218. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  219. }
  220. }
  221. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesWithoutIndices) {
  222. Assimp::Importer importer;
  223. //Lines without indices
  224. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_01.gltf", aiProcess_ValidateDataStructure);
  225. EXPECT_NE(nullptr, scene);
  226. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 8u);
  227. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  228. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  229. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i * 2u);
  230. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i * 2u + 1u);
  231. }
  232. }
  233. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesLoopWithoutIndices) {
  234. Assimp::Importer importer;
  235. //Lines loop without indices
  236. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_02.gltf", aiProcess_ValidateDataStructure);
  237. EXPECT_NE(nullptr, scene);
  238. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  239. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  240. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  241. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  242. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  243. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  244. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1u]);
  245. }
  246. }
  247. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLinesStripWithoutIndices) {
  248. Assimp::Importer importer;
  249. //Lines strip without indices
  250. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_03.gltf", aiProcess_ValidateDataStructure);
  251. EXPECT_NE(nullptr, scene);
  252. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 5u);
  253. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  254. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  255. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 2u);
  256. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  257. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], i + 1u);
  258. }
  259. }
  260. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStripWithoutIndices) {
  261. Assimp::Importer importer;
  262. //Triangles strip without indices
  263. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_04.gltf", aiProcess_ValidateDataStructure);
  264. EXPECT_NE(nullptr, scene);
  265. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  266. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  267. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  268. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  269. for (unsigned int i = 0; i < 3; ++i) {
  270. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  271. }
  272. std::array<unsigned int, 3> f2 = { { 2u, 1u, 3u } };
  273. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  274. for (size_t i = 0; i < 3; ++i) {
  275. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  276. }
  277. }
  278. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFanWithoutIndices) {
  279. Assimp::Importer importer;
  280. //Triangles fan without indices
  281. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_05.gltf", aiProcess_ValidateDataStructure);
  282. EXPECT_NE(nullptr, scene);
  283. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  284. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  285. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  286. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  287. for (size_t i = 0; i < 3; ++i) {
  288. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  289. }
  290. std::array<unsigned int, 3> f2 = { { 0u, 2u, 3u } };
  291. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  292. for (size_t i = 0; i < 3; ++i) {
  293. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  294. }
  295. }
  296. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesWithoutIndices) {
  297. Assimp::Importer importer;
  298. //Triangles without indices
  299. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_06.gltf", aiProcess_ValidateDataStructure);
  300. EXPECT_NE(nullptr, scene);
  301. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  302. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 6u);
  303. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  304. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  305. for (size_t i = 0; i < 3; ++i) {
  306. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  307. }
  308. std::array<unsigned int, 3> f2 = { { 3u, 4u, 5u } };
  309. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  310. for (size_t i = 0; i < 3; ++i) {
  311. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  312. }
  313. }
  314. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePoints) {
  315. Assimp::Importer importer;
  316. //Line loop
  317. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_07.gltf", aiProcess_ValidateDataStructure);
  318. EXPECT_NE(nullptr, scene);
  319. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 1024u);
  320. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  321. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mNumIndices, 1u);
  322. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], i);
  323. }
  324. }
  325. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLines) {
  326. Assimp::Importer importer;
  327. //Lines
  328. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_08.gltf", aiProcess_ValidateDataStructure);
  329. EXPECT_NE(nullptr, scene);
  330. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  331. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  332. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  333. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  334. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  335. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  336. }
  337. }
  338. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineLoop) {
  339. Assimp::Importer importer;
  340. //Line loop
  341. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_09.gltf", aiProcess_ValidateDataStructure);
  342. EXPECT_NE(nullptr, scene);
  343. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  344. std::array<unsigned int, 5> l1 = { { 0, 1u, 2u, 3u, 0u } };
  345. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  346. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  347. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  348. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  349. }
  350. }
  351. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeLineStrip) {
  352. Assimp::Importer importer;
  353. //Lines Strip
  354. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_10.gltf", aiProcess_ValidateDataStructure);
  355. EXPECT_NE(nullptr, scene);
  356. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  357. std::array<unsigned int, 5> l1 = { { 0u, 1u, 2u, 3u, 0u } };
  358. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 2u);
  359. for (unsigned int i = 0; i < scene->mMeshes[0]->mNumFaces; ++i) {
  360. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[0], l1[i]);
  361. EXPECT_EQ(scene->mMeshes[0]->mFaces[i].mIndices[1], l1[i + 1]);
  362. }
  363. }
  364. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesStrip) {
  365. Assimp::Importer importer;
  366. //Triangles strip
  367. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_11.gltf", aiProcess_ValidateDataStructure);
  368. EXPECT_NE(nullptr, scene);
  369. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  370. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  371. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  372. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  373. for (size_t i = 0; i < 3; ++i) {
  374. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  375. }
  376. std::array<unsigned int, 3> f2 = { { 2u, 1u, 3u } };
  377. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  378. for (size_t i = 0; i < 3; ++i) {
  379. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  380. }
  381. }
  382. TEST_F(utglTF2ImportExport, importglTF2PrimitiveModeTrianglesFan) {
  383. Assimp::Importer importer;
  384. //Triangles fan
  385. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Asset-Generator/Mesh_PrimitiveMode/Mesh_PrimitiveMode_12.gltf", aiProcess_ValidateDataStructure);
  386. EXPECT_NE(nullptr, scene);
  387. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 4u);
  388. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 2u);
  389. std::array<unsigned int, 3> f1 = { { 0u, 1u, 2u } };
  390. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mNumIndices, 3u);
  391. for (size_t i = 0; i < 3; ++i) {
  392. EXPECT_EQ(scene->mMeshes[0]->mFaces[0].mIndices[i], f1[i]);
  393. }
  394. std::array<unsigned int, 3> f2 = { { 0u, 2u, 3u } };
  395. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mNumIndices, 3u);
  396. for (size_t i = 0; i < 3; ++i) {
  397. EXPECT_EQ(scene->mMeshes[0]->mFaces[1].mIndices[i], f2[i]);
  398. }
  399. }
  400. std::vector<char> ReadFile(const char *name) {
  401. std::vector<char> ret;
  402. FILE *p = ::fopen(name, "r");
  403. if (nullptr == p) {
  404. return ret;
  405. }
  406. ::fseek(p, 0, SEEK_END);
  407. const size_t size = ::ftell(p);
  408. ::fseek(p, 0, SEEK_SET);
  409. ret.resize(size);
  410. const size_t readSize = ::fread(&ret[0], 1, size, p);
  411. EXPECT_EQ(readSize, size);
  412. ::fclose(p);
  413. return ret;
  414. }
  415. TEST_F(utglTF2ImportExport, importglTF2FromMemory) {
  416. /*const auto flags = aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_RemoveComponent |
  417. aiProcess_GenSmoothNormals | aiProcess_PreTransformVertices | aiProcess_FixInfacingNormals |
  418. aiProcess_FindDegenerates | aiProcess_GenUVCoords | aiProcess_SortByPType;
  419. const auto& buff = ReadFile("C:\\Users\\kimkulling\\Downloads\\camel\\camel\\scene.gltf");*/
  420. /*const aiScene* Scene = ::aiImportFileFromMemory(&buff[0], buff.size(), flags, ".gltf");
  421. EXPECT_EQ( nullptr, Scene );*/
  422. }
  423. TEST_F(utglTF2ImportExport, bug_import_simple_skin) {
  424. Assimp::Importer importer;
  425. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/simple_skin/simple_skin.gltf",
  426. aiProcess_ValidateDataStructure);
  427. EXPECT_NE(nullptr, scene);
  428. }
  429. TEST_F(utglTF2ImportExport, import_cameras) {
  430. Assimp::Importer importer;
  431. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/cameras/Cameras.gltf",
  432. aiProcess_ValidateDataStructure);
  433. EXPECT_NE(nullptr, scene);
  434. }
  435. TEST_F(utglTF2ImportExport, incorrect_vertex_arrays) {
  436. Assimp::Importer importer;
  437. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IncorrectVertexArrays/Cube.gltf",
  438. aiProcess_ValidateDataStructure);
  439. EXPECT_NE(nullptr, scene);
  440. EXPECT_EQ(scene->mMeshes[0]->mNumVertices, 36u);
  441. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 12u);
  442. EXPECT_EQ(scene->mMeshes[1]->mNumVertices, 35u);
  443. EXPECT_EQ(scene->mMeshes[1]->mNumFaces, 11u);
  444. EXPECT_EQ(scene->mMeshes[2]->mNumVertices, 36u);
  445. EXPECT_EQ(scene->mMeshes[2]->mNumFaces, 18u);
  446. EXPECT_EQ(scene->mMeshes[3]->mNumVertices, 35u);
  447. EXPECT_EQ(scene->mMeshes[3]->mNumFaces, 17u);
  448. EXPECT_EQ(scene->mMeshes[4]->mNumVertices, 36u);
  449. EXPECT_EQ(scene->mMeshes[4]->mNumFaces, 12u);
  450. EXPECT_EQ(scene->mMeshes[5]->mNumVertices, 35u);
  451. EXPECT_EQ(scene->mMeshes[5]->mNumFaces, 11u);
  452. EXPECT_EQ(scene->mMeshes[6]->mNumVertices, 36u);
  453. EXPECT_EQ(scene->mMeshes[6]->mNumFaces, 18u);
  454. EXPECT_EQ(scene->mMeshes[7]->mNumVertices, 35u);
  455. EXPECT_EQ(scene->mMeshes[7]->mNumFaces, 17u);
  456. }
  457. TEST_F(utglTF2ImportExport, texture_transform_test) {
  458. Assimp::Importer importer;
  459. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/textureTransform/TextureTransformTest.gltf",
  460. aiProcess_ValidateDataStructure);
  461. EXPECT_NE(nullptr, scene);
  462. }
  463. #ifndef ASSIMP_BUILD_NO_EXPORT
  464. TEST_F(utglTF2ImportExport, exportglTF2FromFileTest) {
  465. EXPECT_TRUE(exporterTest());
  466. }
  467. TEST_F(utglTF2ImportExport, crash_in_anim_mesh_destructor) {
  468. Assimp::Importer importer;
  469. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube.gltf",
  470. aiProcess_ValidateDataStructure);
  471. ASSERT_NE(nullptr, scene);
  472. Assimp::Exporter exporter;
  473. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/glTF-Sample-Models/AnimatedMorphCube-glTF/AnimatedMorphCube_out.glTF"));
  474. }
  475. TEST_F(utglTF2ImportExport, error_string_preserved) {
  476. Assimp::Importer importer;
  477. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/MissingBin/BoxTextured.gltf",
  478. aiProcess_ValidateDataStructure);
  479. ASSERT_EQ(nullptr, scene);
  480. std::string error = importer.GetErrorString();
  481. ASSERT_NE(error.find("BoxTextured0.bin"), std::string::npos) << "Error string should contain an error about missing .bin file";
  482. }
  483. TEST_F(utglTF2ImportExport, export_bad_accessor_bounds) {
  484. Assimp::Importer importer;
  485. Assimp::Exporter exporter;
  486. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites.glb", aiProcess_ValidateDataStructure);
  487. ASSERT_NE(scene, nullptr);
  488. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.glb"));
  489. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "gltf2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxWithInfinites-glTF-Binary/BoxWithInfinites_out.gltf"));
  490. }
  491. TEST_F(utglTF2ImportExport, export_normalized_normals) {
  492. Assimp::Importer importer;
  493. Assimp::Exporter exporter;
  494. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals.glb", aiProcess_ValidateDataStructure);
  495. ASSERT_NE(scene, nullptr);
  496. EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb"));
  497. // load in again and ensure normal-length normals but no Nan's or Inf's introduced
  498. scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxBadNormals-glTF-Binary/BoxBadNormals_out.glb", aiProcess_ValidateDataStructure);
  499. for ( auto i = 0u; i < scene->mMeshes[0]->mNumVertices; ++i ) {
  500. const auto length = scene->mMeshes[0]->mNormals[i].Length();
  501. EXPECT_TRUE(abs(length) < 1e-6 || abs(length - 1) < ai_epsilon);
  502. }
  503. }
  504. #endif // ASSIMP_BUILD_NO_EXPORT
  505. TEST_F(utglTF2ImportExport, sceneMetadata) {
  506. Assimp::Importer importer;
  507. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf",
  508. aiProcess_ValidateDataStructure);
  509. ASSERT_NE(scene, nullptr);
  510. ASSERT_NE(scene->mMetaData, nullptr);
  511. {
  512. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  513. aiString format;
  514. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  515. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  516. }
  517. {
  518. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  519. aiString version;
  520. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  521. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  522. }
  523. {
  524. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  525. aiString generator;
  526. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  527. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  528. }
  529. }
  530. TEST_F(utglTF2ImportExport, texcoords) {
  531. Assimp::Importer importer;
  532. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  533. ASSERT_NE(scene, nullptr);
  534. ASSERT_TRUE(scene->HasMaterials());
  535. const aiMaterial *material = scene->mMaterials[0];
  536. aiString path;
  537. unsigned int uvIndex = 255;
  538. aiTextureMapMode modes[2];
  539. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  540. EXPECT_STREQ(path.C_Str(), "texture.png");
  541. EXPECT_EQ(uvIndex, 0u);
  542. uvIndex = 255;
  543. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  544. EXPECT_STREQ(path.C_Str(), "texture.png");
  545. EXPECT_EQ(uvIndex, 1u);
  546. }
  547. #ifndef ASSIMP_BUILD_NO_EXPORT
  548. TEST_F(utglTF2ImportExport, texcoords_export) {
  549. {
  550. Assimp::Importer importer;
  551. Assimp::Exporter exporter;
  552. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  553. ASSERT_NE(scene, nullptr);
  554. ASSERT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "glb2", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf_out.glb"));
  555. }
  556. Assimp::Importer importer;
  557. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf", aiProcess_ValidateDataStructure);
  558. ASSERT_NE(scene, nullptr);
  559. ASSERT_TRUE(scene->HasMaterials());
  560. const aiMaterial *material = scene->mMaterials[0];
  561. aiString path;
  562. unsigned int uvIndex = 255;
  563. aiTextureMapMode modes[2];
  564. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_BASE_COLOR_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  565. EXPECT_STREQ(path.C_Str(), "texture.png");
  566. EXPECT_EQ(uvIndex, 0u);
  567. uvIndex = 255;
  568. EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE, &path, nullptr, &uvIndex, nullptr, nullptr, modes));
  569. EXPECT_STREQ(path.C_Str(), "texture.png");
  570. EXPECT_EQ(uvIndex, 1u);
  571. }
  572. #endif // ASSIMP_BUILD_NO_EXPORT
  573. TEST_F(utglTF2ImportExport, recursive_nodes) {
  574. Assimp::Importer importer;
  575. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/RecursiveNodes/RecursiveNodes.gltf", aiProcess_ValidateDataStructure);
  576. EXPECT_EQ(nullptr, scene);
  577. }
  578. TEST_F(utglTF2ImportExport, norootnode_noscene) {
  579. Assimp::Importer importer;
  580. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/NoScene.gltf", aiProcess_ValidateDataStructure);
  581. ASSERT_EQ(scene, nullptr);
  582. }
  583. TEST_F(utglTF2ImportExport, norootnode_scenewithoutnodes) {
  584. Assimp::Importer importer;
  585. const aiScene* scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/TestNoRootNode/SceneWithoutNodes.gltf", aiProcess_ValidateDataStructure);
  586. ASSERT_NE(scene, nullptr);
  587. ASSERT_NE(scene->mRootNode, nullptr);
  588. }
  589. // Shall not crash!
  590. TEST_F(utglTF2ImportExport, norootnode_issue_3269) {
  591. Assimp::Importer importer;
  592. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/issue_3269/texcoord_crash.gltf", aiProcess_ValidateDataStructure);
  593. ASSERT_EQ(scene, nullptr);
  594. }
  595. TEST_F(utglTF2ImportExport, indexOutOfRange) {
  596. // The contents of an asset should not lead to an assert.
  597. Assimp::Importer importer;
  598. struct LogObserver : Assimp::LogStream {
  599. bool m_observedWarning = false;
  600. void write(const char *message) override {
  601. m_observedWarning = m_observedWarning || std::strstr(message, "faces were dropped");
  602. }
  603. };
  604. LogObserver logObserver;
  605. DefaultLogger::get()->attachStream(&logObserver);
  606. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/IndexOutOfRange.gltf", aiProcess_ValidateDataStructure);
  607. ASSERT_NE(scene, nullptr);
  608. ASSERT_NE(scene->mRootNode, nullptr);
  609. ASSERT_EQ(scene->mNumMeshes, 1u);
  610. EXPECT_EQ(scene->mMeshes[0]->mNumFaces, 11u);
  611. DefaultLogger::get()->detachStream(&logObserver);
  612. EXPECT_TRUE(logObserver.m_observedWarning);
  613. }
  614. TEST_F(utglTF2ImportExport, allIndicesOutOfRange) {
  615. // The contents of an asset should not lead to an assert.
  616. Assimp::Importer importer;
  617. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/IndexOutOfRange/AllIndicesOutOfRange.gltf", aiProcess_ValidateDataStructure);
  618. ASSERT_EQ(scene, nullptr);
  619. std::string error = importer.GetErrorString();
  620. ASSERT_NE(error.find("Mesh \"Mesh\" has no faces"), std::string::npos);
  621. }
  622. /////////////////////////////////
  623. // Draco decoding
  624. TEST_F(utglTF2ImportExport, import_dracoEncoded) {
  625. Assimp::Importer importer;
  626. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/draco/2CylinderEngine.gltf",
  627. aiProcess_ValidateDataStructure);
  628. #ifndef ASSIMP_ENABLE_DRACO
  629. // No draco support, scene should not load
  630. ASSERT_EQ(scene, nullptr);
  631. #else
  632. ASSERT_NE(scene, nullptr);
  633. ASSERT_NE(scene->mMetaData, nullptr);
  634. {
  635. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT));
  636. aiString format;
  637. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT, format));
  638. ASSERT_EQ(strcmp(format.C_Str(), "glTF2 Importer"), 0);
  639. }
  640. {
  641. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT_VERSION));
  642. aiString version;
  643. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_FORMAT_VERSION, version));
  644. ASSERT_EQ(strcmp(version.C_Str(), "2.0"), 0);
  645. }
  646. {
  647. ASSERT_TRUE(scene->mMetaData->HasKey(AI_METADATA_SOURCE_GENERATOR));
  648. aiString generator;
  649. ASSERT_TRUE(scene->mMetaData->Get(AI_METADATA_SOURCE_GENERATOR, generator));
  650. ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
  651. }
  652. #endif
  653. }
  654. TEST_F(utglTF2ImportExport, wrongTypes) {
  655. // Deliberately broken version of the BoxTextured.gltf asset.
  656. using tup_T = std::tuple<std::string, std::string, std::string, std::string>;
  657. std::vector<tup_T> wrongTypes = {
  658. #ifdef __cpp_lib_constexpr_tuple
  659. #define TUPLE(x, y, z, w) {x, y, z, w}
  660. #else
  661. #define TUPLE(x, y, z, w) tup_T(x, y, z, w)
  662. #endif
  663. TUPLE("/glTF2/wrongTypes/badArray.gltf", "array", "primitives", "meshes[0]"),
  664. TUPLE("/glTF2/wrongTypes/badString.gltf", "string", "name", "scenes[0]"),
  665. TUPLE("/glTF2/wrongTypes/badUint.gltf", "uint", "index", "materials[0]"),
  666. TUPLE("/glTF2/wrongTypes/badNumber.gltf", "number", "scale", "materials[0]"),
  667. TUPLE("/glTF2/wrongTypes/badObject.gltf", "object", "pbrMetallicRoughness", "materials[0]"),
  668. TUPLE("/glTF2/wrongTypes/badExtension.gltf", "object", "KHR_texture_transform", "materials[0]")
  669. #undef TUPLE
  670. };
  671. for (const auto& tuple : wrongTypes)
  672. {
  673. const auto& file = std::get<0>(tuple);
  674. const auto& type = std::get<1>(tuple);
  675. const auto& member = std::get<2>(tuple);
  676. const auto& context = std::get<3>(tuple);
  677. Assimp::Importer importer;
  678. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR + file , aiProcess_ValidateDataStructure);
  679. ASSERT_EQ(scene, nullptr);
  680. const std::string error = importer.GetErrorString();
  681. EXPECT_FALSE(error.empty());
  682. EXPECT_NE(error.find(member + "\" was not of type \"" + type + "\" when reading " + context), std::string::npos);
  683. }
  684. }
  685. namespace {
  686. /// This class provides a fake schema to the GLTF importer.
  687. /// It just checks that the file has a top-level "scene" property which is an integer.
  688. class FakeSchemaProvider : public rapidjson::IRemoteSchemaDocumentProvider
  689. {
  690. public:
  691. FakeSchemaProvider(const char* schemaName) :
  692. m_schemaName(schemaName)
  693. {
  694. rapidjson::Document schemaDoc;
  695. schemaDoc.Parse(R"==({"properties":{"scene" : { "type" : "integer" }}, "required": [ "scene" ]})==");
  696. EXPECT_FALSE(schemaDoc.HasParseError());
  697. m_schema.reset(new rapidjson::SchemaDocument(schemaDoc, m_schemaName.c_str(), static_cast<rapidjson::SizeType>(m_schemaName.size()), this));
  698. }
  699. const rapidjson::SchemaDocument* GetRemoteDocument(const char* uri, rapidjson::SizeType) override {
  700. if (m_schemaName == uri) {
  701. return m_schema.get();
  702. }
  703. return nullptr;
  704. }
  705. private:
  706. std::string m_schemaName;
  707. std::unique_ptr<const rapidjson::SchemaDocument> m_schema;
  708. };
  709. }
  710. TEST_F(utglTF2ImportExport, schemaCheckPass) {
  711. FakeSchemaProvider schemaProvider("glTF.schema.json");
  712. Assimp::Importer importer;
  713. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  714. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
  715. EXPECT_NE(scene, nullptr);
  716. EXPECT_STREQ(importer.GetErrorString(), "");
  717. }
  718. TEST_F(utglTF2ImportExport, schemaCheckFail) {
  719. FakeSchemaProvider schemaProvider("glTF.schema.json");
  720. Assimp::Importer importer;
  721. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  722. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/SchemaFailures/sceneWrongType.gltf", aiProcess_ValidateDataStructure);
  723. EXPECT_EQ(scene, nullptr);
  724. const std::string errorString = importer.GetErrorString();
  725. EXPECT_NE(errorString.find("The JSON document did not satisfy the glTF2 schema"), std::string::npos);
  726. }
  727. TEST_F(utglTF2ImportExport, noSchemaFound) {
  728. // More than one importer might make use the provider, but not all schemas might be present.
  729. // Check that the glTF importer handles the case when an non-null provider returns null when asked for schemas.
  730. FakeSchemaProvider schemaProvider("missingSchema.json");
  731. Assimp::Importer importer;
  732. importer.SetPropertyPointer(AI_CONFIG_IMPORT_SCHEMA_DOCUMENT_PROVIDER, &schemaProvider);
  733. const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure);
  734. EXPECT_NE(scene, nullptr);
  735. EXPECT_STREQ(importer.GetErrorString(), "");
  736. }