XGLLoader.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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. /** @file Implementation of the XGL/ZGL importer class */
  35. #ifndef ASSIMP_BUILD_NO_XGL_IMPORTER
  36. #include "XGLLoader.h"
  37. #include <assimp/ParsingUtils.h>
  38. #include <assimp/fast_atof.h>
  39. #include <assimp/MemoryIOWrapper.h>
  40. #include <assimp/StreamReader.h>
  41. #include <assimp/importerdesc.h>
  42. #include <assimp/mesh.h>
  43. #include <assimp/scene.h>
  44. #include <cctype>
  45. #include <memory>
  46. using namespace Assimp;
  47. //using namespace irr;
  48. //using namespace irr::io;
  49. // zlib is needed for compressed XGL files
  50. #ifndef ASSIMP_BUILD_NO_COMPRESSED_XGL
  51. #ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  52. #include <zlib.h>
  53. #else
  54. #include <contrib/zlib/zlib.h>
  55. #endif
  56. #endif
  57. namespace Assimp { // this has to be in here because LogFunctions is in ::Assimp
  58. template <>
  59. const char *LogFunctions<XGLImporter>::Prefix() {
  60. <<<<<<< HEAD
  61. static auto prefix = "XGL: ";
  62. return prefix;
  63. =======
  64. static auto prefix = "XGL: ";
  65. return prefix;
  66. >>>>>>> master
  67. }
  68. } // namespace Assimp
  69. static const aiImporterDesc desc = {
  70. "XGL Importer",
  71. "",
  72. "",
  73. "",
  74. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportCompressedFlavour,
  75. 0,
  76. 0,
  77. 0,
  78. 0,
  79. "xgl zgl"
  80. };
  81. // ------------------------------------------------------------------------------------------------
  82. // Constructor to be privately used by Importer
  83. XGLImporter::XGLImporter() :
  84. <<<<<<< HEAD
  85. m_xmlParser(nullptr), m_scene(nullptr) {
  86. // empty
  87. =======
  88. m_reader(nullptr), m_scene(nullptr) {
  89. // empty
  90. >>>>>>> master
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // Destructor, private as well
  94. XGLImporter::~XGLImporter() {
  95. // empty
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // Returns whether the class can handle the format of the given file.
  99. bool XGLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
  100. <<<<<<< HEAD
  101. /* NOTE: A simple check for the file extension is not enough
  102. =======
  103. /* NOTE: A simple check for the file extension is not enough
  104. >>>>>>> master
  105. * here. XGL and ZGL are ok, but xml is too generic
  106. * and might be collada as well. So open the file and
  107. * look for typical signal tokens.
  108. */
  109. const std::string extension = GetExtension(pFile);
  110. <<<<<<< HEAD
  111. if (extension == "xgl" || extension == "zgl") {
  112. return true;
  113. } else if (extension == "xml" || checkSig) {
  114. ai_assert(pIOHandler != NULL);
  115. const char *tokens[] = { "<world>", "<World>", "<WORLD>" };
  116. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 3);
  117. }
  118. return false;
  119. =======
  120. if (extension == "xgl" || extension == "zgl") {
  121. return true;
  122. } else if (extension == "xml" || checkSig) {
  123. ai_assert(pIOHandler != nullptr);
  124. const char *tokens[] = { "<world>", "<World>", "<WORLD>" };
  125. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 3);
  126. }
  127. return false;
  128. >>>>>>> master
  129. }
  130. // ------------------------------------------------------------------------------------------------
  131. // Get a list of all file extensions which are handled by this class
  132. const aiImporterDesc *XGLImporter::GetInfo() const {
  133. <<<<<<< HEAD
  134. return &desc;
  135. =======
  136. return &desc;
  137. >>>>>>> master
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. // Imports the given file into the given scene structure.
  141. void XGLImporter::InternReadFile(const std::string &pFile,
  142. <<<<<<< HEAD
  143. aiScene *pScene, IOSystem *pIOHandler) {
  144. =======
  145. aiScene *pScene, IOSystem *pIOHandler) {
  146. >>>>>>> master
  147. #ifndef ASSIMP_BUILD_NO_COMPRESSED_XGL
  148. std::vector<Bytef> uncompressed;
  149. #endif
  150. <<<<<<< HEAD
  151. m_scene = pScene;
  152. std::shared_ptr<IOStream> stream(pIOHandler->Open(pFile, "rb"));
  153. // check whether we can read from the file
  154. if (stream.get() == NULL) {
  155. throw DeadlyImportError("Failed to open XGL/ZGL file " + pFile + "");
  156. }
  157. =======
  158. m_scene = pScene;
  159. std::shared_ptr<IOStream> stream(pIOHandler->Open(pFile, "rb"));
  160. // check whether we can read from the file
  161. if (stream.get() == nullptr) {
  162. throw DeadlyImportError("Failed to open XGL/ZGL file " + pFile + "");
  163. }
  164. >>>>>>> master
  165. // see if its compressed, if so uncompress it
  166. if (GetExtension(pFile) == "zgl") {
  167. #ifdef ASSIMP_BUILD_NO_COMPRESSED_XGL
  168. ThrowException("Cannot read ZGL file since Assimp was built without compression support");
  169. #else
  170. <<<<<<< HEAD
  171. std::unique_ptr<StreamReaderLE> raw_reader(new StreamReaderLE(stream));
  172. // build a zlib stream
  173. z_stream zstream;
  174. zstream.opaque = Z_NULL;
  175. zstream.zalloc = Z_NULL;
  176. zstream.zfree = Z_NULL;
  177. zstream.data_type = Z_BINARY;
  178. // raw decompression without a zlib or gzip header
  179. inflateInit2(&zstream, -MAX_WBITS);
  180. // skip two extra bytes, zgl files do carry a crc16 upfront (I think)
  181. raw_reader->IncPtr(2);
  182. zstream.next_in = reinterpret_cast<Bytef *>(raw_reader->GetPtr());
  183. zstream.avail_in = raw_reader->GetRemainingSize();
  184. size_t total = 0l;
  185. // TODO: be smarter about this, decompress directly into heap buffer
  186. // and decompress the data .... do 1k chunks in the hope that we won't kill the stack
  187. #define MYBLOCK 1024
  188. Bytef block[MYBLOCK];
  189. int ret;
  190. do {
  191. zstream.avail_out = MYBLOCK;
  192. zstream.next_out = block;
  193. ret = inflate(&zstream, Z_NO_FLUSH);
  194. if (ret != Z_STREAM_END && ret != Z_OK) {
  195. ThrowException("Failure decompressing this file using gzip, seemingly it is NOT a compressed .XGL file");
  196. }
  197. const size_t have = MYBLOCK - zstream.avail_out;
  198. total += have;
  199. uncompressed.resize(total);
  200. memcpy(uncompressed.data() + total - have, block, have);
  201. } while (ret != Z_STREAM_END);
  202. // terminate zlib
  203. inflateEnd(&zstream);
  204. // replace the input stream with a memory stream
  205. stream.reset(new MemoryIOStream(reinterpret_cast<uint8_t *>(uncompressed.data()), total));
  206. #endif
  207. }
  208. // construct the irrXML parser
  209. /*CIrrXML_IOStreamReader st(stream.get());
  210. m_reader.reset( createIrrXMLReader( ( IFileReadCallBack* ) &st ) );*/
  211. m_xmlParser = new XmlParser;
  212. XmlNode *root = m_xmlParser->parse(stream.get());
  213. if (nullptr == root) {
  214. return;
  215. }
  216. // parse the XML file
  217. TempScope scope;
  218. if (!ASSIMP_stricmp(root->name(), "world")) {
  219. ReadWorld(scope);
  220. }
  221. /* while (ReadElement()) {
  222. if (!ASSIMP_stricmp(m_reader->getNodeName(),"world")) {
  223. =======
  224. std::unique_ptr<StreamReaderLE> raw_reader(new StreamReaderLE(stream));
  225. // build a zlib stream
  226. z_stream zstream;
  227. zstream.opaque = Z_NULL;
  228. zstream.zalloc = Z_NULL;
  229. zstream.zfree = Z_NULL;
  230. zstream.data_type = Z_BINARY;
  231. // raw decompression without a zlib or gzip header
  232. inflateInit2(&zstream, -MAX_WBITS);
  233. // skip two extra bytes, zgl files do carry a crc16 upfront (I think)
  234. raw_reader->IncPtr(2);
  235. zstream.next_in = reinterpret_cast<Bytef *>(raw_reader->GetPtr());
  236. zstream.avail_in = (uInt)raw_reader->GetRemainingSize();
  237. size_t total = 0l;
  238. // TODO: be smarter about this, decompress directly into heap buffer
  239. // and decompress the data .... do 1k chunks in the hope that we won't kill the stack
  240. #define MYBLOCK 1024
  241. Bytef block[MYBLOCK];
  242. int ret;
  243. do {
  244. zstream.avail_out = MYBLOCK;
  245. zstream.next_out = block;
  246. ret = inflate(&zstream, Z_NO_FLUSH);
  247. if (ret != Z_STREAM_END && ret != Z_OK) {
  248. ThrowException("Failure decompressing this file using gzip, seemingly it is NOT a compressed .XGL file");
  249. }
  250. const size_t have = MYBLOCK - zstream.avail_out;
  251. total += have;
  252. uncompressed.resize(total);
  253. memcpy(uncompressed.data() + total - have, block, have);
  254. } while (ret != Z_STREAM_END);
  255. // terminate zlib
  256. inflateEnd(&zstream);
  257. // replace the input stream with a memory stream
  258. stream.reset(new MemoryIOStream(reinterpret_cast<uint8_t *>(uncompressed.data()), total));
  259. #endif
  260. }
  261. // construct the irrXML parser
  262. CIrrXML_IOStreamReader st(stream.get());
  263. m_reader.reset(createIrrXMLReader((IFileReadCallBack *)&st));
  264. // parse the XML file
  265. TempScope scope;
  266. while (ReadElement()) {
  267. if (!ASSIMP_stricmp(m_reader->getNodeName(), "world")) {
  268. >>>>>>> master
  269. ReadWorld(scope);
  270. }
  271. }*/
  272. <<<<<<< HEAD
  273. std::vector<aiMesh *> &meshes = scope.meshes_linear;
  274. std::vector<aiMaterial *> &materials = scope.materials_linear;
  275. if (!meshes.size() || !materials.size()) {
  276. ThrowException("failed to extract data from XGL file, no meshes loaded");
  277. }
  278. // copy meshes
  279. m_scene->mNumMeshes = static_cast<unsigned int>(meshes.size());
  280. m_scene->mMeshes = new aiMesh *[m_scene->mNumMeshes]();
  281. std::copy(meshes.begin(), meshes.end(), m_scene->mMeshes);
  282. // copy materials
  283. m_scene->mNumMaterials = static_cast<unsigned int>(materials.size());
  284. m_scene->mMaterials = new aiMaterial *[m_scene->mNumMaterials]();
  285. std::copy(materials.begin(), materials.end(), m_scene->mMaterials);
  286. if (scope.light) {
  287. m_scene->mNumLights = 1;
  288. m_scene->mLights = new aiLight *[1];
  289. m_scene->mLights[0] = scope.light;
  290. =======
  291. std::vector<aiMesh *> &meshes = scope.meshes_linear;
  292. std::vector<aiMaterial *> &materials = scope.materials_linear;
  293. if (!meshes.size() || !materials.size()) {
  294. ThrowException("failed to extract data from XGL file, no meshes loaded");
  295. }
  296. // copy meshes
  297. m_scene->mNumMeshes = static_cast<unsigned int>(meshes.size());
  298. m_scene->mMeshes = new aiMesh *[m_scene->mNumMeshes]();
  299. std::copy(meshes.begin(), meshes.end(), m_scene->mMeshes);
  300. // copy materials
  301. m_scene->mNumMaterials = static_cast<unsigned int>(materials.size());
  302. m_scene->mMaterials = new aiMaterial *[m_scene->mNumMaterials]();
  303. std::copy(materials.begin(), materials.end(), m_scene->mMaterials);
  304. if (scope.light) {
  305. m_scene->mNumLights = 1;
  306. m_scene->mLights = new aiLight *[1];
  307. m_scene->mLights[0] = scope.light;
  308. >>>>>>> master
  309. scope.light->mName = m_scene->mRootNode->mName;
  310. }
  311. scope.dismiss();
  312. }
  313. // ------------------------------------------------------------------------------------------------
  314. <<<<<<< HEAD
  315. void XGLImporter::ReadWorld(TempScope &scope) {
  316. XmlNode *root = m_xmlParser->getRootNode();
  317. for (XmlNode &node : root->children()) {
  318. const std::string &s = node.name();
  319. // XXX right now we'd skip <lighting> if it comes after
  320. // <object> or <mesh>
  321. if (s == "lighting") {
  322. ReadLighting(node, scope);
  323. } else if (s == "object" || s == "mesh" || s == "mat") {
  324. break;
  325. }
  326. }
  327. aiNode *const nd = ReadObject(*root, scope, true, "world");
  328. if (!nd) {
  329. ThrowException("failure reading <world>");
  330. }
  331. if (!nd->mName.length) {
  332. nd->mName.Set("WORLD");
  333. }
  334. m_scene->mRootNode = nd;
  335. }
  336. // ------------------------------------------------------------------------------------------------
  337. void XGLImporter::ReadLighting(XmlNode &node, TempScope &scope) {
  338. const std::string &s = node.name();
  339. if (s == "directionallight") {
  340. scope.light = ReadDirectionalLight(node);
  341. } else if (s == "ambient") {
  342. LogWarn("ignoring <ambient> tag");
  343. } else if (s == "spheremap") {
  344. LogWarn("ignoring <spheremap> tag");
  345. }
  346. }
  347. // ------------------------------------------------------------------------------------------------
  348. aiLight *XGLImporter::ReadDirectionalLight(XmlNode &node) {
  349. std::unique_ptr<aiLight> l(new aiLight());
  350. l->mType = aiLightSource_DIRECTIONAL;
  351. find_node_by_name_predicate predicate("directionallight");
  352. XmlNode child = node.find_child(predicate);
  353. if (child.empty()) {
  354. return nullptr;
  355. }
  356. const std::string &s = child.name();
  357. if (s == "direction") {
  358. l->mDirection = ReadVec3(child);
  359. } else if (s == "diffuse") {
  360. l->mColorDiffuse = ReadCol3(child);
  361. } else if (s == "specular") {
  362. l->mColorSpecular = ReadCol3(child);
  363. }
  364. return l.release();
  365. }
  366. // ------------------------------------------------------------------------------------------------
  367. aiNode *XGLImporter::ReadObject(XmlNode &node, TempScope &scope, bool skipFirst, const char *closetag) {
  368. aiNode *nd = new aiNode;
  369. std::vector<aiNode *> children;
  370. std::vector<unsigned int> meshes;
  371. try {
  372. for (XmlNode &child : node.children()) {
  373. skipFirst = false;
  374. const std::string &s = child.name();
  375. if (s == "mesh") {
  376. const size_t prev = scope.meshes_linear.size();
  377. if (ReadMesh(child, scope)) {
  378. const size_t newc = scope.meshes_linear.size();
  379. for (size_t i = 0; i < newc - prev; ++i) {
  380. meshes.push_back(static_cast<unsigned int>(i + prev));
  381. }
  382. }
  383. } else if (s == "mat") {
  384. ReadMaterial(child, scope);
  385. } else if (s == "object") {
  386. children.push_back(ReadObject(child, scope));
  387. } else if (s == "objectref") {
  388. // XXX
  389. } else if (s == "meshref") {
  390. const unsigned int id = static_cast<unsigned int>(ReadIndexFromText(child));
  391. std::multimap<unsigned int, aiMesh *>::iterator it = scope.meshes.find(id), end = scope.meshes.end();
  392. if (it == end) {
  393. ThrowException("<meshref> index out of range");
  394. }
  395. for (; it != end && (*it).first == id; ++it) {
  396. // ok, this is n^2 and should get optimized one day
  397. aiMesh *const m = it->second;
  398. unsigned int i = 0, mcount = static_cast<unsigned int>(scope.meshes_linear.size());
  399. for (; i < mcount; ++i) {
  400. if (scope.meshes_linear[i] == m) {
  401. meshes.push_back(i);
  402. break;
  403. }
  404. }
  405. ai_assert(i < mcount);
  406. }
  407. } else if (s == "transform") {
  408. nd->mTransformation = ReadTrafo(child);
  409. }
  410. }
  411. } catch (...) {
  412. for (aiNode *ch : children) {
  413. delete ch;
  414. }
  415. throw;
  416. }
  417. // FIX: since we used std::multimap<> to keep meshes by id, mesh order now depends on the behaviour
  418. // of the multimap implementation with respect to the ordering of entries with same values.
  419. // C++11 gives the guarantee that it uses insertion order, before it is implementation-specific.
  420. // Sort by material id to always guarantee a deterministic result.
  421. std::sort(meshes.begin(), meshes.end(), SortMeshByMaterialId(scope));
  422. // link meshes to node
  423. nd->mNumMeshes = static_cast<unsigned int>(meshes.size());
  424. if (0 != nd->mNumMeshes) {
  425. nd->mMeshes = new unsigned int[nd->mNumMeshes]();
  426. for (unsigned int i = 0; i < nd->mNumMeshes; ++i) {
  427. nd->mMeshes[i] = meshes[i];
  428. }
  429. }
  430. // link children to parent
  431. nd->mNumChildren = static_cast<unsigned int>(children.size());
  432. if (nd->mNumChildren) {
  433. nd->mChildren = new aiNode *[nd->mNumChildren]();
  434. for (unsigned int i = 0; i < nd->mNumChildren; ++i) {
  435. nd->mChildren[i] = children[i];
  436. children[i]->mParent = nd;
  437. }
  438. }
  439. return nd;
  440. }
  441. // ------------------------------------------------------------------------------------------------
  442. aiMatrix4x4 XGLImporter::ReadTrafo(XmlNode &node) {
  443. aiVector3D forward, up, right, position;
  444. float scale = 1.0f;
  445. aiMatrix4x4 m;
  446. XmlNode child = node.child("transform");
  447. if (child.empty()) {
  448. return m;
  449. }
  450. for (XmlNode &sub_child : child.children()) {
  451. const std::string &s = sub_child.name();
  452. if (s == "forward") {
  453. forward = ReadVec3(sub_child);
  454. } else if (s == "up") {
  455. up = ReadVec3(sub_child);
  456. } else if (s == "position") {
  457. position = ReadVec3(sub_child);
  458. }
  459. if (s == "scale") {
  460. scale = ReadFloat(sub_child);
  461. if (scale < 0.f) {
  462. // this is wrong, but we can leave the value and pass it to the caller
  463. LogError("found negative scaling in <transform>, ignoring");
  464. }
  465. }
  466. }
  467. if (forward.SquareLength() < 1e-4 || up.SquareLength() < 1e-4) {
  468. LogError("A direction vector in <transform> is zero, ignoring trafo");
  469. return m;
  470. =======
  471. bool XGLImporter::ReadElement() {
  472. while (m_reader->read()) {
  473. if (m_reader->getNodeType() == EXN_ELEMENT) {
  474. return true;
  475. }
  476. }
  477. return false;
  478. }
  479. // ------------------------------------------------------------------------------------------------
  480. bool XGLImporter::ReadElementUpToClosing(const char *closetag) {
  481. while (m_reader->read()) {
  482. if (m_reader->getNodeType() == EXN_ELEMENT) {
  483. return true;
  484. } else if (m_reader->getNodeType() == EXN_ELEMENT_END && !ASSIMP_stricmp(m_reader->getNodeName(), closetag)) {
  485. return false;
  486. }
  487. }
  488. LogError("unexpected EOF, expected closing <" + std::string(closetag) + "> tag");
  489. return false;
  490. }
  491. // ------------------------------------------------------------------------------------------------
  492. bool XGLImporter::SkipToText() {
  493. while (m_reader->read()) {
  494. if (m_reader->getNodeType() == EXN_TEXT) {
  495. return true;
  496. } else if (m_reader->getNodeType() == EXN_ELEMENT || m_reader->getNodeType() == EXN_ELEMENT_END) {
  497. ThrowException("expected text contents but found another element (or element end)");
  498. }
  499. }
  500. return false;
  501. }
  502. // ------------------------------------------------------------------------------------------------
  503. std::string XGLImporter::GetElementName() {
  504. const char *s = m_reader->getNodeName();
  505. size_t len = strlen(s);
  506. std::string ret;
  507. ret.resize(len);
  508. std::transform(s, s + len, ret.begin(), ::ToLower<char>);
  509. return ret;
  510. }
  511. // ------------------------------------------------------------------------------------------------
  512. void XGLImporter::ReadWorld(TempScope &scope) {
  513. while (ReadElementUpToClosing("world")) {
  514. const std::string &s = GetElementName();
  515. // XXX right now we'd skip <lighting> if it comes after
  516. // <object> or <mesh>
  517. if (s == "lighting") {
  518. ReadLighting(scope);
  519. } else if (s == "object" || s == "mesh" || s == "mat") {
  520. break;
  521. }
  522. }
  523. aiNode *const nd = ReadObject(scope, true, "world");
  524. if (!nd) {
  525. ThrowException("failure reading <world>");
  526. }
  527. if (!nd->mName.length) {
  528. nd->mName.Set("WORLD");
  529. }
  530. m_scene->mRootNode = nd;
  531. }
  532. // ------------------------------------------------------------------------------------------------
  533. void XGLImporter::ReadLighting(TempScope &scope) {
  534. while (ReadElementUpToClosing("lighting")) {
  535. const std::string &s = GetElementName();
  536. if (s == "directionallight") {
  537. scope.light = ReadDirectionalLight();
  538. } else if (s == "ambient") {
  539. LogWarn("ignoring <ambient> tag");
  540. } else if (s == "spheremap") {
  541. LogWarn("ignoring <spheremap> tag");
  542. }
  543. }
  544. }
  545. // ------------------------------------------------------------------------------------------------
  546. aiLight *XGLImporter::ReadDirectionalLight() {
  547. std::unique_ptr<aiLight> l(new aiLight());
  548. l->mType = aiLightSource_DIRECTIONAL;
  549. while (ReadElementUpToClosing("directionallight")) {
  550. const std::string &s = GetElementName();
  551. if (s == "direction") {
  552. l->mDirection = ReadVec3();
  553. } else if (s == "diffuse") {
  554. l->mColorDiffuse = ReadCol3();
  555. } else if (s == "specular") {
  556. l->mColorSpecular = ReadCol3();
  557. }
  558. }
  559. return l.release();
  560. }
  561. // ------------------------------------------------------------------------------------------------
  562. aiNode *XGLImporter::ReadObject(TempScope &scope, bool skipFirst, const char *closetag) {
  563. aiNode *nd = new aiNode;
  564. std::vector<aiNode *> children;
  565. std::vector<unsigned int> meshes;
  566. try {
  567. while (skipFirst || ReadElementUpToClosing(closetag)) {
  568. skipFirst = false;
  569. const std::string &s = GetElementName();
  570. if (s == "mesh") {
  571. const size_t prev = scope.meshes_linear.size();
  572. if (ReadMesh(scope)) {
  573. const size_t newc = scope.meshes_linear.size();
  574. for (size_t i = 0; i < newc - prev; ++i) {
  575. meshes.push_back(static_cast<unsigned int>(i + prev));
  576. }
  577. }
  578. } else if (s == "mat") {
  579. ReadMaterial(scope);
  580. } else if (s == "object") {
  581. children.push_back(ReadObject(scope));
  582. } else if (s == "objectref") {
  583. // XXX
  584. } else if (s == "meshref") {
  585. const unsigned int id = static_cast<unsigned int>(ReadIndexFromText());
  586. std::multimap<unsigned int, aiMesh *>::iterator it = scope.meshes.find(id), end = scope.meshes.end();
  587. if (it == end) {
  588. ThrowException("<meshref> index out of range");
  589. }
  590. for (; it != end && (*it).first == id; ++it) {
  591. // ok, this is n^2 and should get optimized one day
  592. aiMesh *const m = (*it).second;
  593. unsigned int i = 0, mcount = static_cast<unsigned int>(scope.meshes_linear.size());
  594. for (; i < mcount; ++i) {
  595. if (scope.meshes_linear[i] == m) {
  596. meshes.push_back(i);
  597. break;
  598. }
  599. }
  600. ai_assert(i < mcount);
  601. }
  602. } else if (s == "transform") {
  603. nd->mTransformation = ReadTrafo();
  604. }
  605. }
  606. } catch (...) {
  607. for (aiNode *ch : children) {
  608. delete ch;
  609. }
  610. throw;
  611. }
  612. // FIX: since we used std::multimap<> to keep meshes by id, mesh order now depends on the behaviour
  613. // of the multimap implementation with respect to the ordering of entries with same values.
  614. // C++11 gives the guarantee that it uses insertion order, before it is implementation-specific.
  615. // Sort by material id to always guarantee a deterministic result.
  616. std::sort(meshes.begin(), meshes.end(), SortMeshByMaterialId(scope));
  617. // link meshes to node
  618. nd->mNumMeshes = static_cast<unsigned int>(meshes.size());
  619. if (nd->mNumMeshes) {
  620. nd->mMeshes = new unsigned int[nd->mNumMeshes]();
  621. for (unsigned int i = 0; i < nd->mNumMeshes; ++i) {
  622. nd->mMeshes[i] = meshes[i];
  623. }
  624. }
  625. // link children to parent
  626. nd->mNumChildren = static_cast<unsigned int>(children.size());
  627. if (nd->mNumChildren) {
  628. nd->mChildren = new aiNode *[nd->mNumChildren]();
  629. for (unsigned int i = 0; i < nd->mNumChildren; ++i) {
  630. nd->mChildren[i] = children[i];
  631. children[i]->mParent = nd;
  632. }
  633. }
  634. return nd;
  635. }
  636. // ------------------------------------------------------------------------------------------------
  637. aiMatrix4x4 XGLImporter::ReadTrafo() {
  638. aiVector3D forward, up, right, position;
  639. float scale = 1.0f;
  640. while (ReadElementUpToClosing("transform")) {
  641. const std::string &s = GetElementName();
  642. if (s == "forward") {
  643. forward = ReadVec3();
  644. } else if (s == "up") {
  645. up = ReadVec3();
  646. } else if (s == "position") {
  647. position = ReadVec3();
  648. }
  649. if (s == "scale") {
  650. scale = ReadFloat();
  651. if (scale < 0.f) {
  652. // this is wrong, but we can leave the value and pass it to the caller
  653. LogError("found negative scaling in <transform>, ignoring");
  654. }
  655. }
  656. }
  657. aiMatrix4x4 m;
  658. if (forward.SquareLength() < 1e-4 || up.SquareLength() < 1e-4) {
  659. LogError("A direction vector in <transform> is zero, ignoring trafo");
  660. return m;
  661. >>>>>>> master
  662. }
  663. forward.Normalize();
  664. up.Normalize();
  665. right = forward ^ up;
  666. if (std::fabs(up * forward) > 1e-4) {
  667. // this is definitely wrong - a degenerate coordinate space ruins everything
  668. // so substitute identity transform.
  669. LogError("<forward> and <up> vectors in <transform> are skewing, ignoring trafo");
  670. return m;
  671. }
  672. right *= scale;
  673. up *= scale;
  674. forward *= scale;
  675. m.a1 = right.x;
  676. m.b1 = right.y;
  677. m.c1 = right.z;
  678. m.a2 = up.x;
  679. m.b2 = up.y;
  680. m.c2 = up.z;
  681. m.a3 = forward.x;
  682. m.b3 = forward.y;
  683. m.c3 = forward.z;
  684. m.a4 = position.x;
  685. m.b4 = position.y;
  686. m.c4 = position.z;
  687. return m;
  688. }
  689. // ------------------------------------------------------------------------------------------------
  690. aiMesh *XGLImporter::ToOutputMesh(const TempMaterialMesh &m) {
  691. <<<<<<< HEAD
  692. std::unique_ptr<aiMesh> mesh(new aiMesh());
  693. mesh->mNumVertices = static_cast<unsigned int>(m.positions.size());
  694. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  695. std::copy(m.positions.begin(), m.positions.end(), mesh->mVertices);
  696. if (m.normals.size()) {
  697. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  698. std::copy(m.normals.begin(), m.normals.end(), mesh->mNormals);
  699. }
  700. if (m.uvs.size()) {
  701. mesh->mNumUVComponents[0] = 2;
  702. mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  703. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  704. mesh->mTextureCoords[0][i] = aiVector3D(m.uvs[i].x, m.uvs[i].y, 0.f);
  705. }
  706. }
  707. mesh->mNumFaces = static_cast<unsigned int>(m.vcounts.size());
  708. mesh->mFaces = new aiFace[m.vcounts.size()];
  709. unsigned int idx = 0;
  710. for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
  711. aiFace &f = mesh->mFaces[i];
  712. f.mNumIndices = m.vcounts[i];
  713. f.mIndices = new unsigned int[f.mNumIndices];
  714. for (unsigned int c = 0; c < f.mNumIndices; ++c) {
  715. f.mIndices[c] = idx++;
  716. }
  717. }
  718. ai_assert(idx == mesh->mNumVertices);
  719. =======
  720. std::unique_ptr<aiMesh> mesh(new aiMesh());
  721. mesh->mNumVertices = static_cast<unsigned int>(m.positions.size());
  722. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  723. std::copy(m.positions.begin(), m.positions.end(), mesh->mVertices);
  724. if (m.normals.size()) {
  725. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  726. std::copy(m.normals.begin(), m.normals.end(), mesh->mNormals);
  727. }
  728. if (m.uvs.size()) {
  729. mesh->mNumUVComponents[0] = 2;
  730. mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  731. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  732. mesh->mTextureCoords[0][i] = aiVector3D(m.uvs[i].x, m.uvs[i].y, 0.f);
  733. }
  734. }
  735. mesh->mNumFaces = static_cast<unsigned int>(m.vcounts.size());
  736. mesh->mFaces = new aiFace[m.vcounts.size()];
  737. unsigned int idx = 0;
  738. for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
  739. aiFace &f = mesh->mFaces[i];
  740. f.mNumIndices = m.vcounts[i];
  741. f.mIndices = new unsigned int[f.mNumIndices];
  742. for (unsigned int c = 0; c < f.mNumIndices; ++c) {
  743. f.mIndices[c] = idx++;
  744. }
  745. }
  746. >>>>>>> master
  747. mesh->mPrimitiveTypes = m.pflags;
  748. mesh->mMaterialIndex = m.matid;
  749. return mesh.release();
  750. }
  751. // ------------------------------------------------------------------------------------------------
  752. <<<<<<< HEAD
  753. bool XGLImporter::ReadMesh(XmlNode &node, TempScope &scope) {
  754. TempMesh t;
  755. std::map<unsigned int, TempMaterialMesh> bymat;
  756. const unsigned int mesh_id = ReadIDAttr(node);
  757. for (XmlNode &child : node.children()) {
  758. const std::string &s = child.name();
  759. if (s == "mat") {
  760. ReadMaterial(child, scope);
  761. } else if (s == "p") {
  762. pugi::xml_attribute attr = child.attribute("ID");
  763. if (attr.empty()) {
  764. LogWarn("no ID attribute on <p>, ignoring");
  765. } else {
  766. int id = attr.as_int();
  767. t.points[id] = ReadVec3(child);
  768. }
  769. } else if (s == "n") {
  770. pugi::xml_attribute attr = child.attribute("ID");
  771. if (attr.empty()) {
  772. LogWarn("no ID attribute on <n>, ignoring");
  773. } else {
  774. int id = attr.as_int();
  775. t.normals[id] = ReadVec3(child);
  776. }
  777. } else if (s == "tc") {
  778. pugi::xml_attribute attr = child.attribute("ID");
  779. if (attr.empty()) {
  780. LogWarn("no ID attribute on <tc>, ignoring");
  781. } else {
  782. int id = attr.as_int();
  783. t.uvs[id] = ReadVec2(child);
  784. }
  785. } else if (s == "f" || s == "l" || s == "p") {
  786. const unsigned int vcount = s == "f" ? 3 : (s == "l" ? 2 : 1);
  787. unsigned int mid = ~0u;
  788. TempFace tf[3];
  789. bool has[3] = { false };
  790. for (XmlNode &sub_child : child.children()) {
  791. const std::string &s = sub_child.name();
  792. if (s == "fv1" || s == "lv1" || s == "pv1") {
  793. ReadFaceVertex(sub_child, t, tf[0]);
  794. has[0] = true;
  795. } else if (s == "fv2" || s == "lv2") {
  796. ReadFaceVertex(sub_child, t, tf[1]);
  797. has[1] = true;
  798. } else if (s == "fv3") {
  799. ReadFaceVertex(sub_child, t, tf[2]);
  800. has[2] = true;
  801. } else if (s == "mat") {
  802. if (mid != ~0u) {
  803. LogWarn("only one material tag allowed per <f>");
  804. }
  805. mid = ResolveMaterialRef(sub_child, scope);
  806. } else if (s == "matref") {
  807. if (mid != ~0u) {
  808. LogWarn("only one material tag allowed per <f>");
  809. }
  810. mid = ResolveMaterialRef(sub_child, scope);
  811. }
  812. }
  813. if (mid == ~0u) {
  814. ThrowException("missing material index");
  815. }
  816. bool nor = false;
  817. bool uv = false;
  818. for (unsigned int i = 0; i < vcount; ++i) {
  819. if (!has[i]) {
  820. ThrowException("missing face vertex data");
  821. }
  822. nor = nor || tf[i].has_normal;
  823. uv = uv || tf[i].has_uv;
  824. }
  825. if (mid >= (1 << 30)) {
  826. LogWarn("material indices exhausted, this may cause errors in the output");
  827. }
  828. unsigned int meshId = mid | ((nor ? 1 : 0) << 31) | ((uv ? 1 : 0) << 30);
  829. TempMaterialMesh &mesh = bymat[meshId];
  830. mesh.matid = mid;
  831. for (unsigned int i = 0; i < vcount; ++i) {
  832. mesh.positions.push_back(tf[i].pos);
  833. if (nor) {
  834. mesh.normals.push_back(tf[i].normal);
  835. }
  836. if (uv) {
  837. mesh.uvs.push_back(tf[i].uv);
  838. }
  839. mesh.pflags |= 1 << (vcount - 1);
  840. }
  841. mesh.vcounts.push_back(vcount);
  842. }
  843. }
  844. // finally extract output meshes and add them to the scope
  845. typedef std::pair<unsigned int, TempMaterialMesh> pairt;
  846. for (const pairt &p : bymat) {
  847. aiMesh *const m = ToOutputMesh(p.second);
  848. scope.meshes_linear.push_back(m);
  849. // if this is a definition, keep it on the stack
  850. if (mesh_id != ~0u) {
  851. scope.meshes.insert(std::pair<unsigned int, aiMesh *>(mesh_id, m));
  852. }
  853. }
  854. // no id == not a reference, insert this mesh right *here*
  855. return mesh_id == ~0u;
  856. }
  857. // ----------------------------------------------------------------------------------------------
  858. unsigned int XGLImporter::ResolveMaterialRef(XmlNode &node, TempScope &scope) {
  859. const std::string &s = node.name();
  860. if (s == "mat") {
  861. ReadMaterial(node, scope);
  862. return static_cast<unsigned int>(scope.materials_linear.size() - 1);
  863. }
  864. =======
  865. bool XGLImporter::ReadMesh(TempScope &scope) {
  866. TempMesh t;
  867. std::map<unsigned int, TempMaterialMesh> bymat;
  868. const unsigned int mesh_id = ReadIDAttr();
  869. while (ReadElementUpToClosing("mesh")) {
  870. const std::string &s = GetElementName();
  871. if (s == "mat") {
  872. ReadMaterial(scope);
  873. } else if (s == "p") {
  874. if (!m_reader->getAttributeValue("ID")) {
  875. LogWarn("no ID attribute on <p>, ignoring");
  876. } else {
  877. int id = m_reader->getAttributeValueAsInt("ID");
  878. t.points[id] = ReadVec3();
  879. }
  880. } else if (s == "n") {
  881. if (!m_reader->getAttributeValue("ID")) {
  882. LogWarn("no ID attribute on <n>, ignoring");
  883. } else {
  884. int id = m_reader->getAttributeValueAsInt("ID");
  885. t.normals[id] = ReadVec3();
  886. }
  887. } else if (s == "tc") {
  888. if (!m_reader->getAttributeValue("ID")) {
  889. LogWarn("no ID attribute on <tc>, ignoring");
  890. } else {
  891. int id = m_reader->getAttributeValueAsInt("ID");
  892. t.uvs[id] = ReadVec2();
  893. }
  894. } else if (s == "f" || s == "l" || s == "p") {
  895. const unsigned int vcount = s == "f" ? 3 : (s == "l" ? 2 : 1);
  896. unsigned int mid = ~0u;
  897. TempFace tf[3];
  898. bool has[3] = { 0 };
  899. while (ReadElementUpToClosing(s.c_str())) {
  900. const std::string &elemName = GetElementName();
  901. if (elemName == "fv1" || elemName == "lv1" || elemName == "pv1") {
  902. ReadFaceVertex(t, tf[0]);
  903. has[0] = true;
  904. } else if (elemName == "fv2" || elemName == "lv2") {
  905. ReadFaceVertex(t, tf[1]);
  906. has[1] = true;
  907. } else if (elemName == "fv3") {
  908. ReadFaceVertex(t, tf[2]);
  909. has[2] = true;
  910. } else if (elemName == "mat") {
  911. if (mid != ~0u) {
  912. LogWarn("only one material tag allowed per <f>");
  913. }
  914. mid = ResolveMaterialRef(scope);
  915. } else if (elemName == "matref") {
  916. if (mid != ~0u) {
  917. LogWarn("only one material tag allowed per <f>");
  918. }
  919. mid = ResolveMaterialRef(scope);
  920. }
  921. }
  922. if (mid == ~0u) {
  923. ThrowException("missing material index");
  924. }
  925. bool nor = false;
  926. bool uv = false;
  927. for (unsigned int i = 0; i < vcount; ++i) {
  928. if (!has[i]) {
  929. ThrowException("missing face vertex data");
  930. }
  931. nor = nor || tf[i].has_normal;
  932. uv = uv || tf[i].has_uv;
  933. }
  934. if (mid >= (1 << 30)) {
  935. LogWarn("material indices exhausted, this may cause errors in the output");
  936. }
  937. unsigned int meshId = mid | ((nor ? 1 : 0) << 31) | ((uv ? 1 : 0) << 30);
  938. TempMaterialMesh &mesh = bymat[meshId];
  939. mesh.matid = mid;
  940. for (unsigned int i = 0; i < vcount; ++i) {
  941. mesh.positions.push_back(tf[i].pos);
  942. if (nor) {
  943. mesh.normals.push_back(tf[i].normal);
  944. }
  945. if (uv) {
  946. mesh.uvs.push_back(tf[i].uv);
  947. }
  948. mesh.pflags |= 1 << (vcount - 1);
  949. }
  950. mesh.vcounts.push_back(vcount);
  951. }
  952. }
  953. // finally extract output meshes and add them to the scope
  954. typedef std::pair<const unsigned int, TempMaterialMesh> pairt;
  955. for (const pairt &p : bymat) {
  956. aiMesh *const m = ToOutputMesh(p.second);
  957. scope.meshes_linear.push_back(m);
  958. // if this is a definition, keep it on the stack
  959. if (mesh_id != ~0u) {
  960. scope.meshes.insert(std::pair<unsigned int, aiMesh *>(mesh_id, m));
  961. }
  962. }
  963. // no id == not a reference, insert this mesh right *here*
  964. return mesh_id == ~0u;
  965. }
  966. // ----------------------------------------------------------------------------------------------
  967. unsigned int XGLImporter::ResolveMaterialRef(TempScope &scope) {
  968. const std::string &s = GetElementName();
  969. if (s == "mat") {
  970. ReadMaterial(scope);
  971. return static_cast<unsigned int>(scope.materials_linear.size() - 1);
  972. }
  973. >>>>>>> master
  974. const int id = ReadIndexFromText(node);
  975. <<<<<<< HEAD
  976. std::map<unsigned int, aiMaterial *>::iterator it = scope.materials.find(id), end = scope.materials.end();
  977. if (it == end) {
  978. ThrowException("<matref> index out of range");
  979. }
  980. // ok, this is n^2 and should get optimized one day
  981. aiMaterial *const m = (*it).second;
  982. unsigned int i = 0, mcount = static_cast<unsigned int>(scope.materials_linear.size());
  983. for (; i < mcount; ++i) {
  984. if (scope.materials_linear[i] == m) {
  985. return i;
  986. }
  987. }
  988. ai_assert(false);
  989. =======
  990. std::map<unsigned int, aiMaterial *>::iterator it = scope.materials.find(id), end = scope.materials.end();
  991. if (it == end) {
  992. ThrowException("<matref> index out of range");
  993. }
  994. // ok, this is n^2 and should get optimized one day
  995. aiMaterial *const m = (*it).second;
  996. unsigned int i = 0, mcount = static_cast<unsigned int>(scope.materials_linear.size());
  997. for (; i < mcount; ++i) {
  998. if (scope.materials_linear[i] == m) {
  999. return i;
  1000. }
  1001. }
  1002. >>>>>>> master
  1003. return 0;
  1004. }
  1005. // ------------------------------------------------------------------------------------------------
  1006. <<<<<<< HEAD
  1007. void XGLImporter::ReadMaterial(XmlNode &node, TempScope &scope) {
  1008. const unsigned int mat_id = ReadIDAttr(node);
  1009. aiMaterial *mat(new aiMaterial);
  1010. for (XmlNode &child : node.children()) {
  1011. const std::string &s = child.name();
  1012. if (s == "amb") {
  1013. const aiColor3D c = ReadCol3(child);
  1014. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_AMBIENT);
  1015. } else if (s == "diff") {
  1016. const aiColor3D c = ReadCol3(child);
  1017. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_DIFFUSE);
  1018. } else if (s == "spec") {
  1019. const aiColor3D c = ReadCol3(child);
  1020. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_SPECULAR);
  1021. } else if (s == "emiss") {
  1022. const aiColor3D c = ReadCol3(child);
  1023. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_EMISSIVE);
  1024. } else if (s == "alpha") {
  1025. const float f = ReadFloat(child);
  1026. mat->AddProperty(&f, 1, AI_MATKEY_OPACITY);
  1027. } else if (s == "shine") {
  1028. const float f = ReadFloat(child);
  1029. mat->AddProperty(&f, 1, AI_MATKEY_SHININESS);
  1030. }
  1031. }
  1032. scope.materials[mat_id] = mat;
  1033. scope.materials_linear.push_back(mat);
  1034. }
  1035. // ----------------------------------------------------------------------------------------------
  1036. void XGLImporter::ReadFaceVertex(XmlNode &node, const TempMesh &t, TempFace &out) {
  1037. const std::string &end = node.name();
  1038. bool havep = false;
  1039. //while (ReadElementUpToClosing(end.c_str())) {
  1040. for (XmlNode &child : node.children()) {
  1041. const std::string &s = child.name();
  1042. if (s == "pref") {
  1043. const unsigned int id = ReadIndexFromText(child);
  1044. std::map<unsigned int, aiVector3D>::const_iterator it = t.points.find(id);
  1045. if (it == t.points.end()) {
  1046. ThrowException("point index out of range");
  1047. }
  1048. out.pos = (*it).second;
  1049. havep = true;
  1050. } else if (s == "nref") {
  1051. const unsigned int id = ReadIndexFromText(child);
  1052. std::map<unsigned int, aiVector3D>::const_iterator it = t.normals.find(id);
  1053. if (it == t.normals.end()) {
  1054. ThrowException("normal index out of range");
  1055. }
  1056. out.normal = (*it).second;
  1057. out.has_normal = true;
  1058. } else if (s == "tcref") {
  1059. const unsigned int id = ReadIndexFromText(child);
  1060. std::map<unsigned int, aiVector2D>::const_iterator it = t.uvs.find(id);
  1061. if (it == t.uvs.end()) {
  1062. ThrowException("uv index out of range");
  1063. }
  1064. out.uv = (*it).second;
  1065. out.has_uv = true;
  1066. } else if (s == "p") {
  1067. out.pos = ReadVec3(child);
  1068. } else if (s == "n") {
  1069. out.normal = ReadVec3(child);
  1070. } else if (s == "tc") {
  1071. out.uv = ReadVec2(child);
  1072. }
  1073. }
  1074. if (!havep) {
  1075. ThrowException("missing <pref> in <fvN> element");
  1076. }
  1077. }
  1078. // ------------------------------------------------------------------------------------------------
  1079. unsigned int XGLImporter::ReadIDAttr(XmlNode &node) {
  1080. for (pugi::xml_attribute attr : node.attributes()) {
  1081. if (!ASSIMP_stricmp(attr.name(), "id")) {
  1082. return attr.as_int();
  1083. }
  1084. }
  1085. return ~0u;
  1086. }
  1087. // ------------------------------------------------------------------------------------------------
  1088. float XGLImporter::ReadFloat(XmlNode &node) {
  1089. const char *s = node.value(), *se;
  1090. if (!SkipSpaces(&s)) {
  1091. LogError("unexpected EOL, failed to parse index element");
  1092. return 0.f;
  1093. }
  1094. float t;
  1095. se = fast_atoreal_move(s, t);
  1096. if (se == s) {
  1097. LogError("failed to read float text");
  1098. return 0.f;
  1099. }
  1100. return t;
  1101. }
  1102. // ------------------------------------------------------------------------------------------------
  1103. unsigned int XGLImporter::ReadIndexFromText(XmlNode &node) {
  1104. const char *s = node.value();
  1105. if (!SkipSpaces(&s)) {
  1106. LogError("unexpected EOL, failed to parse index element");
  1107. return ~0u;
  1108. }
  1109. const char *se;
  1110. const unsigned int t = strtoul10(s, &se);
  1111. if (se == s) {
  1112. LogError("failed to read index");
  1113. return ~0u;
  1114. }
  1115. return t;
  1116. }
  1117. // ------------------------------------------------------------------------------------------------
  1118. aiVector2D XGLImporter::ReadVec2(XmlNode &node) {
  1119. aiVector2D vec;
  1120. const char *s = node.value();
  1121. ai_real v[2];
  1122. for (int i = 0; i < 2; ++i) {
  1123. if (!SkipSpaces(&s)) {
  1124. LogError("unexpected EOL, failed to parse vec2");
  1125. return vec;
  1126. }
  1127. v[i] = fast_atof(&s);
  1128. SkipSpaces(&s);
  1129. if (i != 1 && *s != ',') {
  1130. LogError("expected comma, failed to parse vec2");
  1131. return vec;
  1132. }
  1133. ++s;
  1134. }
  1135. vec.x = v[0];
  1136. vec.y = v[1];
  1137. =======
  1138. void XGLImporter::ReadMaterial(TempScope &scope) {
  1139. const unsigned int mat_id = ReadIDAttr();
  1140. aiMaterial *mat(new aiMaterial);
  1141. while (ReadElementUpToClosing("mat")) {
  1142. const std::string &s = GetElementName();
  1143. if (s == "amb") {
  1144. const aiColor3D c = ReadCol3();
  1145. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_AMBIENT);
  1146. } else if (s == "diff") {
  1147. const aiColor3D c = ReadCol3();
  1148. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_DIFFUSE);
  1149. } else if (s == "spec") {
  1150. const aiColor3D c = ReadCol3();
  1151. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_SPECULAR);
  1152. } else if (s == "emiss") {
  1153. const aiColor3D c = ReadCol3();
  1154. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_EMISSIVE);
  1155. } else if (s == "alpha") {
  1156. const float f = ReadFloat();
  1157. mat->AddProperty(&f, 1, AI_MATKEY_OPACITY);
  1158. } else if (s == "shine") {
  1159. const float f = ReadFloat();
  1160. mat->AddProperty(&f, 1, AI_MATKEY_SHININESS);
  1161. }
  1162. }
  1163. scope.materials[mat_id] = mat;
  1164. scope.materials_linear.push_back(mat);
  1165. }
  1166. // ----------------------------------------------------------------------------------------------
  1167. void XGLImporter::ReadFaceVertex(const TempMesh &t, TempFace &out) {
  1168. const std::string &end = GetElementName();
  1169. bool havep = false;
  1170. while (ReadElementUpToClosing(end.c_str())) {
  1171. const std::string &s = GetElementName();
  1172. if (s == "pref") {
  1173. const unsigned int id = ReadIndexFromText();
  1174. std::map<unsigned int, aiVector3D>::const_iterator it = t.points.find(id);
  1175. if (it == t.points.end()) {
  1176. ThrowException("point index out of range");
  1177. }
  1178. out.pos = (*it).second;
  1179. havep = true;
  1180. } else if (s == "nref") {
  1181. const unsigned int id = ReadIndexFromText();
  1182. std::map<unsigned int, aiVector3D>::const_iterator it = t.normals.find(id);
  1183. if (it == t.normals.end()) {
  1184. ThrowException("normal index out of range");
  1185. }
  1186. out.normal = (*it).second;
  1187. out.has_normal = true;
  1188. } else if (s == "tcref") {
  1189. const unsigned int id = ReadIndexFromText();
  1190. std::map<unsigned int, aiVector2D>::const_iterator it = t.uvs.find(id);
  1191. if (it == t.uvs.end()) {
  1192. ThrowException("uv index out of range");
  1193. }
  1194. out.uv = (*it).second;
  1195. out.has_uv = true;
  1196. } else if (s == "p") {
  1197. out.pos = ReadVec3();
  1198. } else if (s == "n") {
  1199. out.normal = ReadVec3();
  1200. } else if (s == "tc") {
  1201. out.uv = ReadVec2();
  1202. }
  1203. }
  1204. if (!havep) {
  1205. ThrowException("missing <pref> in <fvN> element");
  1206. }
  1207. }
  1208. // ------------------------------------------------------------------------------------------------
  1209. unsigned int XGLImporter::ReadIDAttr() {
  1210. for (int i = 0, e = m_reader->getAttributeCount(); i < e; ++i) {
  1211. if (!ASSIMP_stricmp(m_reader->getAttributeName(i), "id")) {
  1212. return m_reader->getAttributeValueAsInt(i);
  1213. }
  1214. }
  1215. return ~0u;
  1216. }
  1217. // ------------------------------------------------------------------------------------------------
  1218. float XGLImporter::ReadFloat() {
  1219. if (!SkipToText()) {
  1220. LogError("unexpected EOF reading float element contents");
  1221. return 0.f;
  1222. }
  1223. const char *s = m_reader->getNodeData(), *se;
  1224. if (!SkipSpaces(&s)) {
  1225. LogError("unexpected EOL, failed to parse float");
  1226. return 0.f;
  1227. }
  1228. float t;
  1229. se = fast_atoreal_move(s, t);
  1230. if (se == s) {
  1231. LogError("failed to read float text");
  1232. return 0.f;
  1233. }
  1234. return t;
  1235. }
  1236. // ------------------------------------------------------------------------------------------------
  1237. unsigned int XGLImporter::ReadIndexFromText() {
  1238. if (!SkipToText()) {
  1239. LogError("unexpected EOF reading index element contents");
  1240. return ~0u;
  1241. }
  1242. const char *s = m_reader->getNodeData(), *se;
  1243. if (!SkipSpaces(&s)) {
  1244. LogError("unexpected EOL, failed to parse index element");
  1245. return ~0u;
  1246. }
  1247. const unsigned int t = strtoul10(s, &se);
  1248. if (se == s) {
  1249. LogError("failed to read index");
  1250. return ~0u;
  1251. }
  1252. return t;
  1253. }
  1254. // ------------------------------------------------------------------------------------------------
  1255. aiVector2D XGLImporter::ReadVec2() {
  1256. aiVector2D vec;
  1257. if (!SkipToText()) {
  1258. LogError("unexpected EOF reading vec2 contents");
  1259. return vec;
  1260. }
  1261. const char *s = m_reader->getNodeData();
  1262. ai_real v[2];
  1263. for (int i = 0; i < 2; ++i) {
  1264. if (!SkipSpaces(&s)) {
  1265. LogError("unexpected EOL, failed to parse vec2");
  1266. return vec;
  1267. }
  1268. v[i] = fast_atof(&s);
  1269. SkipSpaces(&s);
  1270. if (i != 1 && *s != ',') {
  1271. LogError("expected comma, failed to parse vec2");
  1272. return vec;
  1273. }
  1274. ++s;
  1275. }
  1276. vec.x = v[0];
  1277. vec.y = v[1];
  1278. >>>>>>> master
  1279. return vec;
  1280. }
  1281. // ------------------------------------------------------------------------------------------------
  1282. <<<<<<< HEAD
  1283. aiVector3D XGLImporter::ReadVec3(XmlNode &node) {
  1284. aiVector3D vec;
  1285. const char *s = node.value();
  1286. for (int i = 0; i < 3; ++i) {
  1287. if (!SkipSpaces(&s)) {
  1288. LogError("unexpected EOL, failed to parse vec3");
  1289. return vec;
  1290. }
  1291. vec[i] = fast_atof(&s);
  1292. SkipSpaces(&s);
  1293. if (i != 2 && *s != ',') {
  1294. LogError("expected comma, failed to parse vec3");
  1295. return vec;
  1296. }
  1297. ++s;
  1298. }
  1299. return vec;
  1300. }
  1301. // ------------------------------------------------------------------------------------------------
  1302. aiColor3D XGLImporter::ReadCol3(XmlNode &node) {
  1303. const aiVector3D &v = ReadVec3(node);
  1304. if (v.x < 0.f || v.x > 1.0f || v.y < 0.f || v.y > 1.0f || v.z < 0.f || v.z > 1.0f) {
  1305. LogWarn("color values out of range, ignoring");
  1306. }
  1307. return aiColor3D(v.x, v.y, v.z);
  1308. =======
  1309. aiVector3D XGLImporter::ReadVec3() {
  1310. aiVector3D vec;
  1311. if (!SkipToText()) {
  1312. LogError("unexpected EOF reading vec3 contents");
  1313. return vec;
  1314. }
  1315. const char *s = m_reader->getNodeData();
  1316. for (int i = 0; i < 3; ++i) {
  1317. if (!SkipSpaces(&s)) {
  1318. LogError("unexpected EOL, failed to parse vec3");
  1319. return vec;
  1320. }
  1321. vec[i] = fast_atof(&s);
  1322. SkipSpaces(&s);
  1323. if (i != 2 && *s != ',') {
  1324. LogError("expected comma, failed to parse vec3");
  1325. return vec;
  1326. }
  1327. ++s;
  1328. }
  1329. return vec;
  1330. }
  1331. // ------------------------------------------------------------------------------------------------
  1332. aiColor3D XGLImporter::ReadCol3() {
  1333. const aiVector3D &v = ReadVec3();
  1334. if (v.x < 0.f || v.x > 1.0f || v.y < 0.f || v.y > 1.0f || v.z < 0.f || v.z > 1.0f) {
  1335. LogWarn("color values out of range, ignoring");
  1336. }
  1337. return aiColor3D(v.x, v.y, v.z);
  1338. >>>>>>> master
  1339. }
  1340. #endif