glTFImporter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef ASSIMP_BUILD_NO_GLTF_IMPORTER
  34. #include "glTFImporter.h"
  35. #include "StringComparison.h"
  36. #include <assimp/Importer.hpp>
  37. #include <assimp/scene.h>
  38. #include <assimp/ai_assert.h>
  39. #include <assimp/DefaultLogger.hpp>
  40. #include <memory>
  41. #include "MakeVerboseFormat.h"
  42. #include "glTFAsset.h"
  43. // This is included here so WriteLazyDict<T>'s definition is found.
  44. #include "glTFAssetWriter.h"
  45. using namespace Assimp;
  46. using namespace glTF;
  47. //
  48. // glTFImporter
  49. //
  50. static const aiImporterDesc desc = {
  51. "glTF Importer",
  52. "",
  53. "",
  54. "",
  55. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour
  56. | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
  57. 0,
  58. 0,
  59. 0,
  60. 0,
  61. "gltf glb"
  62. };
  63. glTFImporter::glTFImporter()
  64. : BaseImporter()
  65. , meshOffsets()
  66. , embeddedTexIdxs()
  67. , mScene( NULL ) {
  68. // empty
  69. }
  70. glTFImporter::~glTFImporter() {
  71. // empty
  72. }
  73. const aiImporterDesc* glTFImporter::GetInfo() const
  74. {
  75. return &desc;
  76. }
  77. bool glTFImporter::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  78. {
  79. const std::string& extension = GetExtension(pFile);
  80. if (extension == "gltf" || extension == "glb")
  81. return true;
  82. if ((checkSig || !extension.length()) && pIOHandler) {
  83. char buffer[4];
  84. std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile));
  85. if (pStream && pStream->Read(buffer, sizeof(buffer), 1) == 1) {
  86. if (memcmp(buffer, AI_GLB_MAGIC_NUMBER, sizeof(buffer)) == 0) {
  87. return true; // Has GLB header
  88. }
  89. else if (memcmp(buffer, "{\r\n ", sizeof(buffer)) == 0
  90. || memcmp(buffer, "{\n ", sizeof(buffer)) == 0) {
  91. // seems a JSON file, and we're the only format that can read them
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. //static void CopyValue(const glTF::vec3& v, aiColor3D& out)
  99. //{
  100. // out.r = v[0]; out.g = v[1]; out.b = v[2];
  101. //}
  102. static void CopyValue(const glTF::vec4& v, aiColor4D& out)
  103. {
  104. out.r = v[0]; out.g = v[1]; out.b = v[2]; out.a = v[3];
  105. }
  106. static void CopyValue(const glTF::vec4& v, aiColor3D& out)
  107. {
  108. out.r = v[0]; out.g = v[1]; out.b = v[2];
  109. }
  110. static void CopyValue(const glTF::vec3& v, aiVector3D& out)
  111. {
  112. out.x = v[0]; out.y = v[1]; out.z = v[2];
  113. }
  114. static void CopyValue(const glTF::vec4& v, aiQuaternion& out)
  115. {
  116. out.x = v[0]; out.y = v[1]; out.z = v[2]; out.w = v[3];
  117. }
  118. static void CopyValue(const glTF::mat4& v, aiMatrix4x4& o)
  119. {
  120. o.a1 = v[ 0]; o.b1 = v[ 1]; o.c1 = v[ 2]; o.d1 = v[ 3];
  121. o.a2 = v[ 4]; o.b2 = v[ 5]; o.c2 = v[ 6]; o.d2 = v[ 7];
  122. o.a3 = v[ 8]; o.b3 = v[ 9]; o.c3 = v[10]; o.d3 = v[11];
  123. o.a4 = v[12]; o.b4 = v[13]; o.c4 = v[14]; o.d4 = v[15];
  124. }
  125. inline void SetMaterialColorProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF::TexProperty prop, aiMaterial* mat,
  126. aiTextureType texType, const char* pKey, unsigned int type, unsigned int idx)
  127. {
  128. if (prop.texture) {
  129. if (prop.texture->source) {
  130. aiString uri(prop.texture->source->uri);
  131. int texIdx = embeddedTexIdxs[prop.texture->source.GetIndex()];
  132. if (texIdx != -1) { // embedded
  133. // setup texture reference string (copied from ColladaLoader::FindFilenameForEffectTexture)
  134. uri.data[0] = '*';
  135. uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx);
  136. }
  137. mat->AddProperty(&uri, _AI_MATKEY_TEXTURE_BASE, texType, 0);
  138. }
  139. }
  140. else {
  141. aiColor4D col;
  142. CopyValue(prop.color, col);
  143. if (col.r != 1.f || col.g != 1.f || col.b != 1.f || col.a != 1.f) {
  144. mat->AddProperty(&col, 1, pKey, type, idx);
  145. }
  146. }
  147. }
  148. void glTFImporter::ImportMaterials(glTF::Asset& r)
  149. {
  150. mScene->mNumMaterials = unsigned(r.materials.Size());
  151. mScene->mMaterials = new aiMaterial*[mScene->mNumMaterials];
  152. for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
  153. aiMaterial* aimat = mScene->mMaterials[i] = new aiMaterial();
  154. Material& mat = r.materials[i];
  155. /*if (!mat.name.empty())*/ {
  156. aiString str(mat.id /*mat.name*/);
  157. aimat->AddProperty(&str, AI_MATKEY_NAME);
  158. }
  159. SetMaterialColorProperty(embeddedTexIdxs, r, mat.diffuse, aimat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE);
  160. SetMaterialColorProperty(embeddedTexIdxs, r, mat.specular, aimat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR);
  161. SetMaterialColorProperty(embeddedTexIdxs, r, mat.ambient, aimat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT);
  162. if (mat.shininess > 0.f) {
  163. aimat->AddProperty(&mat.shininess, 1, AI_MATKEY_SHININESS);
  164. }
  165. }
  166. if (mScene->mNumMaterials == 0) {
  167. mScene->mNumMaterials = 1;
  168. mScene->mMaterials = new aiMaterial*[1];
  169. mScene->mMaterials[0] = new aiMaterial();
  170. }
  171. }
  172. static inline void SetFace(aiFace& face, int a)
  173. {
  174. face.mNumIndices = 1;
  175. face.mIndices = new unsigned int[1];
  176. face.mIndices[0] = a;
  177. }
  178. static inline void SetFace(aiFace& face, int a, int b)
  179. {
  180. face.mNumIndices = 2;
  181. face.mIndices = new unsigned int[2];
  182. face.mIndices[0] = a;
  183. face.mIndices[1] = b;
  184. }
  185. static inline void SetFace(aiFace& face, int a, int b, int c)
  186. {
  187. face.mNumIndices = 3;
  188. face.mIndices = new unsigned int[3];
  189. face.mIndices[0] = a;
  190. face.mIndices[1] = b;
  191. face.mIndices[2] = c;
  192. }
  193. static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsigned nVerts)
  194. {
  195. for (unsigned i = 0; i < nFaces; ++i) {
  196. for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
  197. unsigned idx = faces[i].mIndices[j];
  198. if (idx >= nVerts)
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. void glTFImporter::ImportMeshes(glTF::Asset& r)
  205. {
  206. std::vector<aiMesh*> meshes;
  207. unsigned int k = 0;
  208. for (unsigned int m = 0; m < r.meshes.Size(); ++m) {
  209. Mesh& mesh = r.meshes[m];
  210. meshOffsets.push_back(k);
  211. k += unsigned(mesh.primitives.size());
  212. for (unsigned int p = 0; p < mesh.primitives.size(); ++p) {
  213. Mesh::Primitive& prim = mesh.primitives[p];
  214. aiMesh* aim = new aiMesh();
  215. meshes.push_back(aim);
  216. aim->mName = mesh.id;
  217. if (mesh.primitives.size() > 1) {
  218. size_t& len = aim->mName.length;
  219. aim->mName.data[len] = '-';
  220. len += 1 + ASSIMP_itoa10(aim->mName.data + len + 1, unsigned(MAXLEN - len - 1), p);
  221. }
  222. switch (prim.mode) {
  223. case PrimitiveMode_POINTS:
  224. aim->mPrimitiveTypes |= aiPrimitiveType_POINT;
  225. break;
  226. case PrimitiveMode_LINES:
  227. case PrimitiveMode_LINE_LOOP:
  228. case PrimitiveMode_LINE_STRIP:
  229. aim->mPrimitiveTypes |= aiPrimitiveType_LINE;
  230. break;
  231. case PrimitiveMode_TRIANGLES:
  232. case PrimitiveMode_TRIANGLE_STRIP:
  233. case PrimitiveMode_TRIANGLE_FAN:
  234. aim->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  235. break;
  236. }
  237. Mesh::Primitive::Attributes& attr = prim.attributes;
  238. if (attr.position.size() > 0 && attr.position[0]) {
  239. aim->mNumVertices = attr.position[0]->count;
  240. attr.position[0]->ExtractData(aim->mVertices);
  241. }
  242. if (attr.normal.size() > 0 && attr.normal[0]) {
  243. attr.normal[0]->ExtractData(aim->mNormals);
  244. }
  245. for (size_t tc = 0; tc < attr.texcoord.size() && tc <= AI_MAX_NUMBER_OF_TEXTURECOORDS; ++tc) {
  246. attr.texcoord[tc]->ExtractData(aim->mTextureCoords[tc]);
  247. aim->mNumUVComponents[tc] = attr.texcoord[tc]->GetNumComponents();
  248. aiVector3D* values = aim->mTextureCoords[tc];
  249. for (unsigned int i = 0; i < aim->mNumVertices; ++i) {
  250. values[i].y = 1 - values[i].y; // Flip Y coords
  251. }
  252. }
  253. if (prim.indices) {
  254. aiFace* faces = 0;
  255. unsigned int nFaces = 0;
  256. unsigned int count = prim.indices->count;
  257. Accessor::Indexer data = prim.indices->GetIndexer();
  258. ai_assert(data.IsValid());
  259. switch (prim.mode) {
  260. case PrimitiveMode_POINTS: {
  261. nFaces = count;
  262. faces = new aiFace[nFaces];
  263. for (unsigned int i = 0; i < count; ++i) {
  264. SetFace(faces[i], data.GetUInt(i));
  265. }
  266. break;
  267. }
  268. case PrimitiveMode_LINES: {
  269. nFaces = count / 2;
  270. faces = new aiFace[nFaces];
  271. for (unsigned int i = 0; i < count; i += 2) {
  272. SetFace(faces[i / 2], data.GetUInt(i), data.GetUInt(i + 1));
  273. }
  274. break;
  275. }
  276. case PrimitiveMode_LINE_LOOP:
  277. case PrimitiveMode_LINE_STRIP: {
  278. nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0);
  279. faces = new aiFace[nFaces];
  280. SetFace(faces[0], data.GetUInt(0), data.GetUInt(1));
  281. for (unsigned int i = 2; i < count; ++i) {
  282. SetFace(faces[i - 1], faces[i - 2].mIndices[1], data.GetUInt(i));
  283. }
  284. if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop
  285. SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]);
  286. }
  287. break;
  288. }
  289. case PrimitiveMode_TRIANGLES: {
  290. nFaces = count / 3;
  291. faces = new aiFace[nFaces];
  292. for (unsigned int i = 0; i < count; i += 3) {
  293. SetFace(faces[i / 3], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
  294. }
  295. break;
  296. }
  297. case PrimitiveMode_TRIANGLE_STRIP: {
  298. nFaces = count - 2;
  299. faces = new aiFace[nFaces];
  300. SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2));
  301. for (unsigned int i = 3; i < count; ++i) {
  302. SetFace(faces[i - 2], faces[i - 1].mIndices[1], faces[i - 1].mIndices[2], data.GetUInt(i));
  303. }
  304. break;
  305. }
  306. case PrimitiveMode_TRIANGLE_FAN:
  307. nFaces = count - 2;
  308. faces = new aiFace[nFaces];
  309. SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2));
  310. for (unsigned int i = 3; i < count; ++i) {
  311. SetFace(faces[i - 2], faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i));
  312. }
  313. break;
  314. }
  315. if (faces) {
  316. aim->mFaces = faces;
  317. aim->mNumFaces = nFaces;
  318. ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices));
  319. }
  320. }
  321. if (prim.material) {
  322. aim->mMaterialIndex = prim.material.GetIndex();
  323. }
  324. }
  325. }
  326. meshOffsets.push_back(k);
  327. CopyVector(meshes, mScene->mMeshes, mScene->mNumMeshes);
  328. }
  329. void glTFImporter::ImportCameras(glTF::Asset& r)
  330. {
  331. if (!r.cameras.Size()) return;
  332. mScene->mNumCameras = r.cameras.Size();
  333. mScene->mCameras = new aiCamera*[r.cameras.Size()];
  334. for (size_t i = 0; i < r.cameras.Size(); ++i) {
  335. Camera& cam = r.cameras[i];
  336. aiCamera* aicam = mScene->mCameras[i] = new aiCamera();
  337. if (cam.type == Camera::Perspective) {
  338. aicam->mAspect = cam.perspective.aspectRatio;
  339. aicam->mHorizontalFOV = cam.perspective.yfov * aicam->mAspect;
  340. aicam->mClipPlaneFar = cam.perspective.zfar;
  341. aicam->mClipPlaneNear = cam.perspective.znear;
  342. }
  343. else {
  344. // assimp does not support orthographic cameras
  345. }
  346. }
  347. }
  348. void glTFImporter::ImportLights(glTF::Asset& r)
  349. {
  350. if (!r.lights.Size()) return;
  351. mScene->mNumLights = r.lights.Size();
  352. mScene->mLights = new aiLight*[r.lights.Size()];
  353. for (size_t i = 0; i < r.lights.Size(); ++i) {
  354. Light& l = r.lights[i];
  355. aiLight* ail = mScene->mLights[i] = new aiLight();
  356. switch (l.type) {
  357. case Light::Type_directional:
  358. ail->mType = aiLightSource_DIRECTIONAL; break;
  359. case Light::Type_spot:
  360. ail->mType = aiLightSource_SPOT; break;
  361. case Light::Type_ambient:
  362. ail->mType = aiLightSource_AMBIENT; break;
  363. default: // Light::Type_point
  364. ail->mType = aiLightSource_POINT; break;
  365. }
  366. CopyValue(l.color, ail->mColorAmbient);
  367. CopyValue(l.color, ail->mColorDiffuse);
  368. CopyValue(l.color, ail->mColorSpecular);
  369. ail->mAngleOuterCone = l.falloffAngle;
  370. ail->mAngleInnerCone = l.falloffExponent; // TODO fix this, it does not look right at all
  371. ail->mAttenuationConstant = l.constantAttenuation;
  372. ail->mAttenuationLinear = l.linearAttenuation;
  373. ail->mAttenuationQuadratic = l.quadraticAttenuation;
  374. }
  375. }
  376. aiNode* ImportNode(aiScene* pScene, glTF::Asset& r, std::vector<unsigned int>& meshOffsets, glTF::Ref<glTF::Node>& ptr)
  377. {
  378. Node& node = *ptr;
  379. aiNode* ainode = new aiNode(node.id);
  380. if (!node.children.empty()) {
  381. ainode->mNumChildren = unsigned(node.children.size());
  382. ainode->mChildren = new aiNode*[ainode->mNumChildren];
  383. for (unsigned int i = 0; i < ainode->mNumChildren; ++i) {
  384. aiNode* child = ImportNode(pScene, r, meshOffsets, node.children[i]);
  385. child->mParent = ainode;
  386. ainode->mChildren[i] = child;
  387. }
  388. }
  389. aiMatrix4x4& matrix = ainode->mTransformation;
  390. if (node.matrix.isPresent) {
  391. CopyValue(node.matrix.value, matrix);
  392. }
  393. else {
  394. if (node.translation.isPresent) {
  395. aiVector3D trans;
  396. CopyValue(node.translation.value, trans);
  397. aiMatrix4x4 t;
  398. aiMatrix4x4::Translation(trans, t);
  399. matrix = t * matrix;
  400. }
  401. if (node.scale.isPresent) {
  402. aiVector3D scal(1.f);
  403. CopyValue(node.scale.value, scal);
  404. aiMatrix4x4 s;
  405. aiMatrix4x4::Scaling(scal, s);
  406. matrix = s * matrix;
  407. }
  408. if (node.rotation.isPresent) {
  409. aiQuaternion rot;
  410. CopyValue(node.rotation.value, rot);
  411. matrix = aiMatrix4x4(rot.GetMatrix()) * matrix;
  412. }
  413. }
  414. if (!node.meshes.empty()) {
  415. int count = 0;
  416. for (size_t i = 0; i < node.meshes.size(); ++i) {
  417. int idx = node.meshes[i].GetIndex();
  418. count += meshOffsets[idx + 1] - meshOffsets[idx];
  419. }
  420. ainode->mNumMeshes = count;
  421. ainode->mMeshes = new unsigned int[count];
  422. int k = 0;
  423. for (size_t i = 0; i < node.meshes.size(); ++i) {
  424. int idx = node.meshes[i].GetIndex();
  425. for (unsigned int j = meshOffsets[idx]; j < meshOffsets[idx + 1]; ++j, ++k) {
  426. ainode->mMeshes[k] = j;
  427. }
  428. }
  429. }
  430. if (node.camera) {
  431. pScene->mCameras[node.camera.GetIndex()]->mName = ainode->mName;
  432. }
  433. if (node.light) {
  434. pScene->mLights[node.light.GetIndex()]->mName = ainode->mName;
  435. }
  436. return ainode;
  437. }
  438. void glTFImporter::ImportNodes(glTF::Asset& r)
  439. {
  440. if (!r.scene) return;
  441. std::vector< Ref<Node> > rootNodes = r.scene->nodes;
  442. // The root nodes
  443. unsigned int numRootNodes = unsigned(rootNodes.size());
  444. if (numRootNodes == 1) { // a single root node: use it
  445. mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]);
  446. }
  447. else if (numRootNodes > 1) { // more than one root node: create a fake root
  448. aiNode* root = new aiNode("ROOT");
  449. root->mChildren = new aiNode*[numRootNodes];
  450. for (unsigned int i = 0; i < numRootNodes; ++i) {
  451. aiNode* node = ImportNode(mScene, r, meshOffsets, rootNodes[i]);
  452. node->mParent = root;
  453. root->mChildren[root->mNumChildren++] = node;
  454. }
  455. mScene->mRootNode = root;
  456. }
  457. //if (!mScene->mRootNode) {
  458. // mScene->mRootNode = new aiNode("EMPTY");
  459. //}
  460. }
  461. void glTFImporter::ImportEmbeddedTextures(glTF::Asset& r)
  462. {
  463. embeddedTexIdxs.resize(r.images.Size(), -1);
  464. int numEmbeddedTexs = 0;
  465. for (size_t i = 0; i < r.images.Size(); ++i) {
  466. if (r.images[i].HasData())
  467. numEmbeddedTexs += 1;
  468. }
  469. if (numEmbeddedTexs == 0)
  470. return;
  471. mScene->mTextures = new aiTexture*[numEmbeddedTexs];
  472. // Add the embedded textures
  473. for (size_t i = 0; i < r.images.Size(); ++i) {
  474. Image img = r.images[i];
  475. if (!img.HasData()) continue;
  476. int idx = mScene->mNumTextures++;
  477. embeddedTexIdxs[i] = idx;
  478. aiTexture* tex = mScene->mTextures[idx] = new aiTexture();
  479. size_t length = img.GetDataLength();
  480. void* data = img.StealData();
  481. tex->mWidth = static_cast<unsigned int>(length);
  482. tex->mHeight = 0;
  483. tex->pcData = reinterpret_cast<aiTexel*>(data);
  484. if (!img.mimeType.empty()) {
  485. const char* ext = strchr(img.mimeType.c_str(), '/') + 1;
  486. if (ext) {
  487. if (strcmp(ext, "jpeg") == 0) ext = "jpg";
  488. size_t len = strlen(ext);
  489. if (len <= 3) {
  490. strcpy(tex->achFormatHint, ext);
  491. }
  492. }
  493. }
  494. }
  495. }
  496. void glTFImporter::InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) {
  497. this->mScene = pScene;
  498. // read the asset file
  499. glTF::Asset asset(pIOHandler);
  500. asset.Load(pFile, GetExtension(pFile) == "glb");
  501. //
  502. // Copy the data out
  503. //
  504. ImportEmbeddedTextures(asset);
  505. ImportMaterials(asset);
  506. ImportMeshes(asset);
  507. ImportCameras(asset);
  508. ImportLights(asset);
  509. ImportNodes(asset);
  510. // TODO: it does not split the loaded vertices, should it?
  511. //pScene->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
  512. Assimp::MakeVerboseFormatProcess process;
  513. process.Execute(pScene);
  514. if (pScene->mNumMeshes == 0) {
  515. pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  516. }
  517. }
  518. #endif // ASSIMP_BUILD_NO_GLTF_IMPORTER