utglTF2ImportExport.cpp 47 KB

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