utglTF2ImportExport.cpp 34 KB

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