OpenGEXImporter.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, 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_OPENGEX_IMPORTER
  34. #include "AssetLib/OpenGEX/OpenGEXImporter.h"
  35. #include "PostProcessing/MakeVerboseFormat.h"
  36. #include <assimp/DefaultIOSystem.h>
  37. #include <assimp/StringComparison.h>
  38. #include <assimp/StringUtils.h>
  39. #include <assimp/DefaultLogger.hpp>
  40. #include <assimp/ai_assert.h>
  41. #include <assimp/importerdesc.h>
  42. #include <assimp/scene.h>
  43. #include <openddlparser/OpenDDLParser.h>
  44. static const aiImporterDesc desc = {
  45. "Open Game Engine Exchange",
  46. "",
  47. "",
  48. "",
  49. aiImporterFlags_SupportTextFlavour,
  50. 0,
  51. 0,
  52. 0,
  53. 0,
  54. "ogex"
  55. };
  56. namespace Grammar {
  57. static const char* MetricType = "Metric";
  58. static const char *Metric_DistanceType = "distance";
  59. static const char *Metric_AngleType = "angle";
  60. static const char *Metric_TimeType = "time";
  61. static const char *Metric_UpType = "up";
  62. static const char *NameType = "Name";
  63. static const char *ObjectRefType = "ObjectRef";
  64. static const char *MaterialRefType = "MaterialRef";
  65. static const char *MetricKeyType = "key";
  66. static const char *GeometryNodeType = "GeometryNode";
  67. static const char *CameraNodeType = "CameraNode";
  68. static const char *LightNodeType = "LightNode";
  69. static const char *GeometryObjectType = "GeometryObject";
  70. static const char *CameraObjectType = "CameraObject";
  71. static const char *LightObjectType = "LightObject";
  72. static const char *TransformType = "Transform";
  73. static const char *MeshType = "Mesh";
  74. static const char *VertexArrayType = "VertexArray";
  75. static const char *IndexArrayType = "IndexArray";
  76. static const char *MaterialType = "Material";
  77. static const char *ColorType = "Color";
  78. static const char *ParamType = "Param";
  79. static const char *TextureType = "Texture";
  80. static const char *AttenType = "Atten";
  81. static const char *DiffuseColorToken = "diffuse";
  82. static const char *SpecularColorToken = "specular";
  83. static const char *EmissionColorToken = "emission";
  84. static const char *DiffuseTextureToken = "diffuse";
  85. static const char *DiffuseSpecularTextureToken = "specular";
  86. static const char *SpecularPowerTextureToken = "specular_power";
  87. static const char *EmissionTextureToken = "emission";
  88. static const char *OpacyTextureToken = "opacity";
  89. static const char *TransparencyTextureToken = "transparency";
  90. static const char *NormalTextureToken = "normal";
  91. enum TokenType {
  92. NoneType = -1,
  93. MetricToken,
  94. NameToken,
  95. ObjectRefToken,
  96. MaterialRefToken,
  97. MetricKeyToken,
  98. GeometryNodeToken,
  99. CameraNodeToken,
  100. LightNodeToken,
  101. GeometryObjectToken,
  102. CameraObjectToken,
  103. LightObjectToken,
  104. TransformToken,
  105. MeshToken,
  106. VertexArrayToken,
  107. IndexArrayToken,
  108. MaterialToken,
  109. ColorToken,
  110. ParamToken,
  111. TextureToken,
  112. AttenToken
  113. };
  114. static const std::string ValidMetricToken[4] = {
  115. Metric_DistanceType,
  116. Metric_AngleType,
  117. Metric_TimeType,
  118. Metric_UpType
  119. };
  120. static int isValidMetricType(const char *token) {
  121. if (nullptr == token) {
  122. return false;
  123. }
  124. int idx(-1);
  125. for (size_t i = 0; i < 4; i++) {
  126. if (ValidMetricToken[i] == token) {
  127. idx = (int)i;
  128. break;
  129. }
  130. }
  131. return idx;
  132. }
  133. static TokenType matchTokenType(const char *tokenType) {
  134. if (MetricType == tokenType) {
  135. return MetricToken;
  136. } else if (NameType == tokenType) {
  137. return NameToken;
  138. } else if (ObjectRefType == tokenType) {
  139. return ObjectRefToken;
  140. } else if (MaterialRefType == tokenType) {
  141. return MaterialRefToken;
  142. } else if (MetricKeyType == tokenType) {
  143. return MetricKeyToken;
  144. } else if (GeometryNodeType == tokenType) {
  145. return GeometryNodeToken;
  146. } else if (CameraNodeType == tokenType) {
  147. return CameraNodeToken;
  148. } else if (LightNodeType == tokenType) {
  149. return LightNodeToken;
  150. } else if (GeometryObjectType == tokenType) {
  151. return GeometryObjectToken;
  152. } else if (CameraObjectType == tokenType) {
  153. return CameraObjectToken;
  154. } else if (LightObjectType == tokenType) {
  155. return LightObjectToken;
  156. } else if (TransformType == tokenType) {
  157. return TransformToken;
  158. } else if (MeshType == tokenType) {
  159. return MeshToken;
  160. } else if (VertexArrayType == tokenType) {
  161. return VertexArrayToken;
  162. } else if (IndexArrayType == tokenType) {
  163. return IndexArrayToken;
  164. } else if (MaterialType == tokenType) {
  165. return MaterialToken;
  166. } else if (ColorType == tokenType) {
  167. return ColorToken;
  168. } else if (ParamType == tokenType) {
  169. return ParamToken;
  170. } else if (TextureType == tokenType) {
  171. return TextureToken;
  172. } else if (AttenType == tokenType) {
  173. return AttenToken;
  174. }
  175. return NoneType;
  176. }
  177. } // Namespace Grammar
  178. namespace Assimp {
  179. namespace OpenGEX {
  180. USE_ODDLPARSER_NS
  181. //------------------------------------------------------------------------------------------------
  182. static void propId2StdString(Property *prop, std::string &name, std::string &key) {
  183. name = key = std::string();
  184. if (nullptr == prop) {
  185. return;
  186. }
  187. if (nullptr != prop->m_key) {
  188. #ifdef ASSIMP_USE_HUNTER
  189. name = prop->m_key->m_text.m_buffer;
  190. #else
  191. name = prop->m_key->m_buffer;
  192. #endif
  193. if (Value::ValueType::ddl_string == prop->m_value->m_type) {
  194. key = prop->m_value->getString();
  195. }
  196. }
  197. }
  198. //------------------------------------------------------------------------------------------------
  199. static void logDDLParserMessage (LogSeverity severity, const std::string &rawmsg) {
  200. std::string msg = ai_str_toprintable(rawmsg);
  201. switch (severity) {
  202. case ddl_debug_msg: ASSIMP_LOG_DEBUG(msg); break;
  203. case ddl_info_msg: ASSIMP_LOG_INFO(msg); break;
  204. case ddl_warn_msg: ASSIMP_LOG_WARN(msg); break;
  205. case ddl_error_msg: ASSIMP_LOG_ERROR(msg); break;
  206. default: ASSIMP_LOG_VERBOSE_DEBUG(msg); break;
  207. }
  208. }
  209. //------------------------------------------------------------------------------------------------
  210. OpenGEXImporter::VertexContainer::VertexContainer() :
  211. m_numColors(0), m_colors(nullptr), m_numUVComps(), m_textureCoords() {
  212. // empty
  213. }
  214. //------------------------------------------------------------------------------------------------
  215. OpenGEXImporter::VertexContainer::~VertexContainer() {
  216. delete[] m_colors;
  217. for (auto &texcoords : m_textureCoords) {
  218. delete[] texcoords;
  219. }
  220. }
  221. //------------------------------------------------------------------------------------------------
  222. OpenGEXImporter::RefInfo::RefInfo(aiNode *node, Type type, std::vector<std::string> &names) :
  223. m_node(node),
  224. m_type(type),
  225. m_Names(names) {
  226. // empty
  227. }
  228. //------------------------------------------------------------------------------------------------
  229. OpenGEXImporter::RefInfo::~RefInfo() {
  230. // empty
  231. }
  232. //------------------------------------------------------------------------------------------------
  233. OpenGEXImporter::OpenGEXImporter() :
  234. m_root(nullptr),
  235. m_nodeChildMap(),
  236. m_meshCache(),
  237. m_mesh2refMap(),
  238. m_material2refMap(),
  239. m_ctx(nullptr),
  240. m_metrics(),
  241. m_currentNode(nullptr),
  242. m_currentVertices(),
  243. m_currentMesh(nullptr),
  244. m_currentMaterial(nullptr),
  245. m_currentLight(nullptr),
  246. m_currentCamera(nullptr),
  247. m_tokenType(Grammar::NoneType),
  248. m_materialCache(),
  249. m_cameraCache(),
  250. m_lightCache(),
  251. m_nodeStack(),
  252. m_unresolvedRefStack() {
  253. // empty
  254. }
  255. //------------------------------------------------------------------------------------------------
  256. OpenGEXImporter::~OpenGEXImporter() {
  257. }
  258. //------------------------------------------------------------------------------------------------
  259. bool OpenGEXImporter::CanRead(const std::string &file, IOSystem *pIOHandler, bool /*checkSig*/) const {
  260. static const char *tokens[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" };
  261. return SearchFileHeaderForToken(pIOHandler, file, tokens, AI_COUNT_OF(tokens));
  262. }
  263. //------------------------------------------------------------------------------------------------
  264. void OpenGEXImporter::InternReadFile(const std::string &filename, aiScene *pScene, IOSystem *pIOHandler) {
  265. // open source file
  266. IOStream *file = pIOHandler->Open(filename, "rb");
  267. if (!file) {
  268. throw DeadlyImportError("Failed to open file ", filename);
  269. }
  270. std::vector<char> buffer;
  271. TextFileToBuffer(file, buffer);
  272. pIOHandler->Close(file);
  273. OpenDDLParser myParser;
  274. myParser.setLogCallback(&logDDLParserMessage);
  275. myParser.setBuffer(&buffer[0], buffer.size());
  276. bool success(myParser.parse());
  277. if (success) {
  278. m_ctx = myParser.getContext();
  279. pScene->mRootNode = new aiNode;
  280. pScene->mRootNode->mName.Set(filename);
  281. handleNodes(m_ctx->m_root, pScene);
  282. }
  283. copyMeshes(pScene);
  284. copyCameras(pScene);
  285. copyLights(pScene);
  286. copyMaterials(pScene);
  287. resolveReferences();
  288. createNodeTree(pScene);
  289. }
  290. //------------------------------------------------------------------------------------------------
  291. const aiImporterDesc *OpenGEXImporter::GetInfo() const {
  292. return &desc;
  293. }
  294. //------------------------------------------------------------------------------------------------
  295. void OpenGEXImporter::SetupProperties(const Importer *pImp) {
  296. if (nullptr == pImp) {
  297. return;
  298. }
  299. }
  300. //------------------------------------------------------------------------------------------------
  301. void OpenGEXImporter::handleNodes(DDLNode *node, aiScene *pScene) {
  302. if (nullptr == node) {
  303. return;
  304. }
  305. DDLNode::DllNodeList children = node->getChildNodeList();
  306. for (DDLNode::DllNodeList::iterator it = children.begin(); it != children.end(); ++it) {
  307. Grammar::TokenType tokenType(Grammar::matchTokenType((*it)->getType().c_str()));
  308. switch (tokenType) {
  309. case Grammar::MetricToken:
  310. handleMetricNode(*it, pScene);
  311. break;
  312. case Grammar::NameToken:
  313. handleNameNode(*it, pScene);
  314. break;
  315. case Grammar::ObjectRefToken:
  316. handleObjectRefNode(*it, pScene);
  317. break;
  318. case Grammar::MaterialRefToken:
  319. handleMaterialRefNode(*it, pScene);
  320. break;
  321. case Grammar::MetricKeyToken:
  322. break;
  323. case Grammar::GeometryNodeToken:
  324. handleGeometryNode(*it, pScene);
  325. break;
  326. case Grammar::CameraNodeToken:
  327. handleCameraNode(*it, pScene);
  328. break;
  329. case Grammar::LightNodeToken:
  330. handleLightNode(*it, pScene);
  331. break;
  332. case Grammar::GeometryObjectToken:
  333. handleGeometryObject(*it, pScene);
  334. break;
  335. case Grammar::CameraObjectToken:
  336. handleCameraObject(*it, pScene);
  337. break;
  338. case Grammar::LightObjectToken:
  339. handleLightObject(*it, pScene);
  340. break;
  341. case Grammar::TransformToken:
  342. handleTransformNode(*it, pScene);
  343. break;
  344. case Grammar::MeshToken:
  345. handleMeshNode(*it, pScene);
  346. break;
  347. case Grammar::VertexArrayToken:
  348. handleVertexArrayNode(*it, pScene);
  349. break;
  350. case Grammar::IndexArrayToken:
  351. handleIndexArrayNode(*it, pScene);
  352. break;
  353. case Grammar::MaterialToken:
  354. handleMaterialNode(*it, pScene);
  355. break;
  356. case Grammar::ColorToken:
  357. handleColorNode(*it, pScene);
  358. break;
  359. case Grammar::ParamToken:
  360. handleParamNode(*it, pScene);
  361. break;
  362. case Grammar::TextureToken:
  363. handleTextureNode(*it, pScene);
  364. break;
  365. default:
  366. break;
  367. }
  368. }
  369. }
  370. //------------------------------------------------------------------------------------------------
  371. void OpenGEXImporter::handleMetricNode(DDLNode *node, aiScene * /*pScene*/) {
  372. if (nullptr == node || nullptr == m_ctx) {
  373. return;
  374. }
  375. if (m_ctx->m_root != node->getParent()) {
  376. return;
  377. }
  378. Property *prop(node->getProperties());
  379. while (nullptr != prop) {
  380. if (nullptr != prop->m_key) {
  381. if (Value::ValueType::ddl_string == prop->m_value->m_type) {
  382. std::string valName((char *)prop->m_value->m_data);
  383. int type(Grammar::isValidMetricType(valName.c_str()));
  384. if (Grammar::NoneType != type) {
  385. Value *val(node->getValue());
  386. if (nullptr != val) {
  387. if (Value::ValueType::ddl_float == val->m_type) {
  388. m_metrics[type].m_floatValue = val->getFloat();
  389. } else if (Value::ValueType::ddl_int32 == val->m_type) {
  390. m_metrics[type].m_intValue = val->getInt32();
  391. } else if (Value::ValueType::ddl_string == val->m_type) {
  392. m_metrics[type].m_stringValue = std::string(val->getString());
  393. } else {
  394. throw DeadlyImportError("OpenGEX: invalid data type for Metric node.");
  395. }
  396. }
  397. }
  398. }
  399. }
  400. prop = prop->m_next;
  401. }
  402. }
  403. //------------------------------------------------------------------------------------------------
  404. void OpenGEXImporter::handleNameNode(DDLNode *node, aiScene * /*pScene*/) {
  405. if (nullptr == m_currentNode) {
  406. throw DeadlyImportError("No current node for name.");
  407. return;
  408. }
  409. Value *val(node->getValue());
  410. if (nullptr != val) {
  411. if (Value::ValueType::ddl_string != val->m_type) {
  412. throw DeadlyImportError("OpenGEX: invalid data type for value in node name.");
  413. return;
  414. }
  415. const std::string name(val->getString());
  416. if (m_tokenType == Grammar::GeometryNodeToken || m_tokenType == Grammar::LightNodeToken || m_tokenType == Grammar::CameraNodeToken) {
  417. m_currentNode->mName.Set(name.c_str());
  418. } else if (m_tokenType == Grammar::MaterialToken) {
  419. aiString aiName;
  420. aiName.Set(name);
  421. m_currentMaterial->AddProperty(&aiName, AI_MATKEY_NAME);
  422. m_material2refMap[name] = m_materialCache.size() - 1;
  423. }
  424. }
  425. }
  426. //------------------------------------------------------------------------------------------------
  427. static void getRefNames(DDLNode *node, std::vector<std::string> &names) {
  428. ai_assert(nullptr != node);
  429. Reference *ref = node->getReferences();
  430. if (nullptr != ref) {
  431. for (size_t i = 0; i < ref->m_numRefs; i++) {
  432. Name *currentName(ref->m_referencedName[i]);
  433. if (nullptr != currentName && nullptr != currentName->m_id) {
  434. #ifdef ASSIMP_USE_HUNTER
  435. const std::string name(currentName->m_id->m_text.m_buffer);
  436. #else
  437. const std::string name(currentName->m_id->m_buffer);
  438. #endif
  439. if (!name.empty()) {
  440. names.push_back(name);
  441. }
  442. }
  443. }
  444. }
  445. }
  446. //------------------------------------------------------------------------------------------------
  447. void OpenGEXImporter::handleObjectRefNode(DDLNode *node, aiScene * /*pScene*/) {
  448. if (nullptr == m_currentNode) {
  449. throw DeadlyImportError("No parent node for name.");
  450. return;
  451. }
  452. std::vector<std::string> objRefNames;
  453. getRefNames(node, objRefNames);
  454. // when we are dealing with a geometry node prepare the mesh cache
  455. if (m_tokenType == Grammar::GeometryNodeToken) {
  456. m_currentNode->mNumMeshes = static_cast<unsigned int>(objRefNames.size());
  457. m_currentNode->mMeshes = new unsigned int[objRefNames.size()];
  458. if (!objRefNames.empty()) {
  459. m_unresolvedRefStack.push_back(std::unique_ptr<RefInfo>(new RefInfo(m_currentNode, RefInfo::MeshRef, objRefNames)));
  460. }
  461. } else if (m_tokenType == Grammar::LightNodeToken) {
  462. // TODO!
  463. } else if (m_tokenType == Grammar::CameraNodeToken) {
  464. // TODO!
  465. }
  466. }
  467. //------------------------------------------------------------------------------------------------
  468. void OpenGEXImporter::handleMaterialRefNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  469. if (nullptr == m_currentNode) {
  470. throw DeadlyImportError("No parent node for name.");
  471. return;
  472. }
  473. std::vector<std::string> matRefNames;
  474. getRefNames(node, matRefNames);
  475. if (!matRefNames.empty()) {
  476. m_unresolvedRefStack.push_back(std::unique_ptr<RefInfo>(new RefInfo(m_currentNode, RefInfo::MaterialRef, matRefNames)));
  477. }
  478. }
  479. //------------------------------------------------------------------------------------------------
  480. void OpenGEXImporter::handleGeometryNode(DDLNode *node, aiScene *pScene) {
  481. aiNode *newNode = new aiNode;
  482. pushNode(newNode, pScene);
  483. m_tokenType = Grammar::GeometryNodeToken;
  484. m_currentNode = newNode;
  485. handleNodes(node, pScene);
  486. popNode();
  487. }
  488. //------------------------------------------------------------------------------------------------
  489. void OpenGEXImporter::handleCameraNode(DDLNode *node, aiScene *pScene) {
  490. aiCamera *camera(new aiCamera);
  491. m_cameraCache.push_back(camera);
  492. m_currentCamera = camera;
  493. aiNode *newNode = new aiNode;
  494. pushNode(newNode, pScene);
  495. m_tokenType = Grammar::CameraNodeToken;
  496. m_currentNode = newNode;
  497. handleNodes(node, pScene);
  498. popNode();
  499. m_currentCamera->mName.Set(newNode->mName.C_Str());
  500. }
  501. //------------------------------------------------------------------------------------------------
  502. void OpenGEXImporter::handleLightNode(ODDLParser::DDLNode *node, aiScene *pScene) {
  503. aiLight *light(new aiLight);
  504. m_lightCache.push_back(light);
  505. m_currentLight = light;
  506. aiNode *newNode = new aiNode;
  507. m_tokenType = Grammar::LightNodeToken;
  508. m_currentNode = newNode;
  509. pushNode(newNode, pScene);
  510. handleNodes(node, pScene);
  511. popNode();
  512. m_currentLight->mName.Set(newNode->mName.C_Str());
  513. }
  514. //------------------------------------------------------------------------------------------------
  515. void OpenGEXImporter::handleGeometryObject(DDLNode *node, aiScene *pScene) {
  516. // parameters will be parsed normally in the tree, so just go for it
  517. handleNodes(node, pScene);
  518. }
  519. //------------------------------------------------------------------------------------------------
  520. void OpenGEXImporter::handleCameraObject(ODDLParser::DDLNode *node, aiScene *pScene) {
  521. // parameters will be parsed normally in the tree, so just go for it
  522. handleNodes(node, pScene);
  523. }
  524. //------------------------------------------------------------------------------------------------
  525. void OpenGEXImporter::handleLightObject(ODDLParser::DDLNode *node, aiScene *pScene) {
  526. aiLight *light(new aiLight);
  527. m_lightCache.push_back(light);
  528. std::string objName = node->getName();
  529. if (!objName.empty()) {
  530. light->mName.Set(objName);
  531. }
  532. m_currentLight = light;
  533. Property *prop(node->findPropertyByName("type"));
  534. if (nullptr != prop) {
  535. if (nullptr != prop->m_value) {
  536. std::string typeStr(prop->m_value->getString());
  537. if ("point" == typeStr) {
  538. m_currentLight->mType = aiLightSource_POINT;
  539. } else if ("spot" == typeStr) {
  540. m_currentLight->mType = aiLightSource_SPOT;
  541. } else if ("infinite" == typeStr) {
  542. m_currentLight->mType = aiLightSource_DIRECTIONAL;
  543. }
  544. }
  545. }
  546. // parameters will be parsed normally in the tree, so just go for it
  547. handleNodes(node, pScene);
  548. }
  549. //------------------------------------------------------------------------------------------------
  550. static void setMatrix(aiNode *node, DataArrayList *transformData) {
  551. ai_assert(nullptr != node);
  552. ai_assert(nullptr != transformData);
  553. float m[16];
  554. size_t i(1);
  555. Value *next(transformData->m_dataList->m_next);
  556. m[0] = transformData->m_dataList->getFloat();
  557. while (next != nullptr) {
  558. m[i] = next->getFloat();
  559. next = next->m_next;
  560. i++;
  561. }
  562. ai_assert(i == 16);
  563. node->mTransformation.a1 = m[0];
  564. node->mTransformation.a2 = m[4];
  565. node->mTransformation.a3 = m[8];
  566. node->mTransformation.a4 = m[12];
  567. node->mTransformation.b1 = m[1];
  568. node->mTransformation.b2 = m[5];
  569. node->mTransformation.b3 = m[9];
  570. node->mTransformation.b4 = m[13];
  571. node->mTransformation.c1 = m[2];
  572. node->mTransformation.c2 = m[6];
  573. node->mTransformation.c3 = m[10];
  574. node->mTransformation.c4 = m[14];
  575. node->mTransformation.d1 = m[3];
  576. node->mTransformation.d2 = m[7];
  577. node->mTransformation.d3 = m[11];
  578. node->mTransformation.d4 = m[15];
  579. }
  580. //------------------------------------------------------------------------------------------------
  581. void OpenGEXImporter::handleTransformNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  582. if (nullptr == m_currentNode) {
  583. throw DeadlyImportError("No parent node for name.");
  584. return;
  585. }
  586. DataArrayList *transformData(node->getDataArrayList());
  587. if (nullptr != transformData) {
  588. if (transformData->m_numItems != 16) {
  589. throw DeadlyImportError("Invalid number of data for transform matrix.");
  590. return;
  591. }
  592. setMatrix(m_currentNode, transformData);
  593. }
  594. }
  595. //------------------------------------------------------------------------------------------------
  596. void OpenGEXImporter::handleMeshNode(ODDLParser::DDLNode *node, aiScene *pScene) {
  597. m_currentMesh = new aiMesh;
  598. const size_t meshidx(m_meshCache.size());
  599. // ownership is transferred but a reference remains in m_currentMesh
  600. m_meshCache.emplace_back(m_currentMesh);
  601. Property *prop = node->getProperties();
  602. if (nullptr != prop) {
  603. std::string propName, propKey;
  604. propId2StdString(prop, propName, propKey);
  605. if ("primitive" == propName) {
  606. if ("points" == propKey) {
  607. m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  608. } else if ("lines" == propKey) {
  609. m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  610. } else if ("triangles" == propKey) {
  611. m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  612. } else if ("quads" == propKey) {
  613. m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  614. } else {
  615. ASSIMP_LOG_WARN(propKey, " is not supported primitive type.");
  616. }
  617. }
  618. }
  619. handleNodes(node, pScene);
  620. DDLNode *parent(node->getParent());
  621. if (nullptr != parent) {
  622. const std::string &name = parent->getName();
  623. m_mesh2refMap[name] = meshidx;
  624. }
  625. }
  626. //------------------------------------------------------------------------------------------------
  627. enum MeshAttribute {
  628. None,
  629. Position,
  630. Color,
  631. Normal,
  632. TexCoord
  633. };
  634. constexpr auto PosToken = "position";
  635. constexpr auto ColToken = "color";
  636. constexpr auto NormalToken = "normal";
  637. constexpr auto TexCoordToken = "texcoord";
  638. //------------------------------------------------------------------------------------------------
  639. static MeshAttribute getAttributeByName(const char *attribName) {
  640. ai_assert(nullptr != attribName);
  641. if (0 == strcmp(PosToken, attribName)) {
  642. return Position;
  643. } else if (0 == strcmp(ColToken, attribName)) {
  644. return Color;
  645. } else if (0 == strcmp(NormalToken, attribName)) {
  646. return Normal;
  647. } else if (0 == strcmp(TexCoordToken, attribName)) {
  648. return TexCoord;
  649. }
  650. return None;
  651. }
  652. //------------------------------------------------------------------------------------------------
  653. static void fillVector3(aiVector3D *vec3, Value *vals) {
  654. ai_assert(nullptr != vec3);
  655. ai_assert(nullptr != vals);
  656. float x(0.0f), y(0.0f), z(0.0f);
  657. Value *next(vals);
  658. x = next->getFloat();
  659. next = next->m_next;
  660. y = next->getFloat();
  661. next = next->m_next;
  662. if (nullptr != next) {
  663. z = next->getFloat();
  664. }
  665. vec3->Set(x, y, z);
  666. }
  667. //------------------------------------------------------------------------------------------------
  668. static void fillColor4(aiColor4D *col4, Value *vals) {
  669. ai_assert(nullptr != col4);
  670. ai_assert(nullptr != vals);
  671. Value *next(vals);
  672. col4->r = next->getFloat();
  673. next = next->m_next;
  674. if (!next) {
  675. throw DeadlyImportError("OpenGEX: Not enough values to fill 4-element color, only 1");
  676. }
  677. col4->g = next->getFloat();
  678. next = next->m_next;
  679. if (!next) {
  680. throw DeadlyImportError("OpenGEX: Not enough values to fill 4-element color, only 2");
  681. }
  682. col4->b = next->getFloat();
  683. next = next->m_next;
  684. if (!next) {
  685. throw DeadlyImportError("OpenGEX: Not enough values to fill 4-element color, only 3");
  686. }
  687. col4->a = next->getFloat();
  688. }
  689. //------------------------------------------------------------------------------------------------
  690. static size_t countDataArrayListItems(DataArrayList *vaList) {
  691. size_t numItems(0);
  692. if (nullptr == vaList) {
  693. return numItems;
  694. }
  695. DataArrayList *next(vaList);
  696. while (nullptr != next) {
  697. if (nullptr != vaList->m_dataList) {
  698. numItems++;
  699. }
  700. next = next->m_next;
  701. }
  702. return numItems;
  703. }
  704. //------------------------------------------------------------------------------------------------
  705. static void copyVectorArray(size_t numItems, DataArrayList *vaList, aiVector3D *vectorArray) {
  706. for (size_t i = 0; i < numItems; i++) {
  707. Value *next(vaList->m_dataList);
  708. fillVector3(&vectorArray[i], next);
  709. vaList = vaList->m_next;
  710. }
  711. }
  712. //------------------------------------------------------------------------------------------------
  713. static void copyColor4DArray(size_t numItems, DataArrayList *vaList, aiColor4D *colArray) {
  714. for (size_t i = 0; i < numItems; i++) {
  715. Value *next(vaList->m_dataList);
  716. fillColor4(&colArray[i], next);
  717. }
  718. }
  719. //------------------------------------------------------------------------------------------------
  720. void OpenGEXImporter::handleVertexArrayNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  721. if (nullptr == node) {
  722. throw DeadlyImportError("No parent node for name.");
  723. return;
  724. }
  725. Property *prop = node->getProperties();
  726. if (nullptr != prop) {
  727. std::string propName, propKey;
  728. propId2StdString(prop, propName, propKey);
  729. MeshAttribute attribType(getAttributeByName(propKey.c_str()));
  730. if (None == attribType) {
  731. return;
  732. }
  733. DataArrayList *vaList = node->getDataArrayList();
  734. if (nullptr == vaList) {
  735. return;
  736. }
  737. const size_t numItems(countDataArrayListItems(vaList));
  738. if (Position == attribType) {
  739. m_currentVertices.m_vertices.resize(numItems);
  740. copyVectorArray(numItems, vaList, m_currentVertices.m_vertices.data());
  741. } else if (Color == attribType) {
  742. m_currentVertices.m_numColors = numItems;
  743. m_currentVertices.m_colors = new aiColor4D[numItems];
  744. copyColor4DArray(numItems, vaList, m_currentVertices.m_colors);
  745. } else if (Normal == attribType) {
  746. m_currentVertices.m_normals.resize(numItems);
  747. copyVectorArray(numItems, vaList, m_currentVertices.m_normals.data());
  748. } else if (TexCoord == attribType) {
  749. m_currentVertices.m_numUVComps[0] = numItems;
  750. m_currentVertices.m_textureCoords[0] = new aiVector3D[numItems];
  751. copyVectorArray(numItems, vaList, m_currentVertices.m_textureCoords[0]);
  752. }
  753. }
  754. }
  755. //------------------------------------------------------------------------------------------------
  756. void OpenGEXImporter::handleIndexArrayNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  757. if (nullptr == node) {
  758. throw DeadlyImportError("No parent node for name.");
  759. return;
  760. }
  761. if (nullptr == m_currentMesh) {
  762. throw DeadlyImportError("No current mesh for index data found.");
  763. return;
  764. }
  765. DataArrayList *vaList = node->getDataArrayList();
  766. if (nullptr == vaList) {
  767. return;
  768. }
  769. const size_t numItems(countDataArrayListItems(vaList));
  770. m_currentMesh->mNumFaces = static_cast<unsigned int>(numItems);
  771. m_currentMesh->mFaces = new aiFace[numItems];
  772. m_currentMesh->mNumVertices = static_cast<unsigned int>(numItems * 3);
  773. m_currentMesh->mVertices = new aiVector3D[m_currentMesh->mNumVertices];
  774. bool hasColors(false);
  775. if (m_currentVertices.m_numColors > 0) {
  776. m_currentMesh->mColors[0] = new aiColor4D[m_currentVertices.m_numColors];
  777. hasColors = true;
  778. }
  779. bool hasNormalCoords(false);
  780. if (!m_currentVertices.m_normals.empty()) {
  781. m_currentMesh->mNormals = new aiVector3D[m_currentMesh->mNumVertices];
  782. hasNormalCoords = true;
  783. }
  784. bool hasTexCoords(false);
  785. if (m_currentVertices.m_numUVComps[0] > 0) {
  786. m_currentMesh->mTextureCoords[0] = new aiVector3D[m_currentMesh->mNumVertices];
  787. hasTexCoords = true;
  788. }
  789. unsigned int index(0);
  790. for (size_t i = 0; i < m_currentMesh->mNumFaces; i++) {
  791. aiFace &current(m_currentMesh->mFaces[i]);
  792. current.mNumIndices = 3;
  793. current.mIndices = new unsigned int[current.mNumIndices];
  794. Value *next(vaList->m_dataList);
  795. for (size_t indices = 0; indices < current.mNumIndices; indices++) {
  796. const int idx(next->getUnsignedInt32());
  797. ai_assert(static_cast<size_t>(idx) <= m_currentVertices.m_vertices.size());
  798. ai_assert(index < m_currentMesh->mNumVertices);
  799. aiVector3D &pos = (m_currentVertices.m_vertices[idx]);
  800. m_currentMesh->mVertices[index].Set(pos.x, pos.y, pos.z);
  801. if (hasColors) {
  802. aiColor4D &col = m_currentVertices.m_colors[idx];
  803. m_currentMesh->mColors[0][index] = col;
  804. }
  805. if (hasNormalCoords) {
  806. aiVector3D &normal = (m_currentVertices.m_normals[idx]);
  807. m_currentMesh->mNormals[index].Set(normal.x, normal.y, normal.z);
  808. }
  809. if (hasTexCoords) {
  810. aiVector3D &tex = (m_currentVertices.m_textureCoords[0][idx]);
  811. m_currentMesh->mTextureCoords[0][index].Set(tex.x, tex.y, tex.z);
  812. }
  813. current.mIndices[indices] = index;
  814. index++;
  815. next = next->m_next;
  816. }
  817. vaList = vaList->m_next;
  818. }
  819. }
  820. //------------------------------------------------------------------------------------------------
  821. static void getColorRGB3(aiColor3D *pColor, DataArrayList *colList) {
  822. if (nullptr == pColor || nullptr == colList) {
  823. return;
  824. }
  825. ai_assert(3 == colList->m_numItems);
  826. Value *val(colList->m_dataList);
  827. pColor->r = val->getFloat();
  828. val = val->getNext();
  829. pColor->g = val->getFloat();
  830. val = val->getNext();
  831. pColor->b = val->getFloat();
  832. }
  833. //------------------------------------------------------------------------------------------------
  834. static void getColorRGB4(aiColor4D *pColor, DataArrayList *colList) {
  835. if (nullptr == pColor || nullptr == colList) {
  836. return;
  837. }
  838. ai_assert(4 == colList->m_numItems);
  839. Value *val(colList->m_dataList);
  840. pColor->r = val->getFloat();
  841. val = val->getNext();
  842. pColor->g = val->getFloat();
  843. val = val->getNext();
  844. pColor->b = val->getFloat();
  845. val = val->getNext();
  846. pColor->a = val->getFloat();
  847. }
  848. //------------------------------------------------------------------------------------------------
  849. enum ColorType {
  850. NoneColor = 0,
  851. DiffuseColor,
  852. SpecularColor,
  853. EmissionColor,
  854. LightColor
  855. };
  856. //------------------------------------------------------------------------------------------------
  857. static ColorType getColorType(Text *id) {
  858. if (nullptr == id) {
  859. return NoneColor;
  860. }
  861. if (*id == Grammar::DiffuseColorToken) {
  862. return DiffuseColor;
  863. } else if (*id == Grammar::SpecularColorToken) {
  864. return SpecularColor;
  865. } else if (*id == Grammar::EmissionColorToken) {
  866. return EmissionColor;
  867. } else if (*id == "light") {
  868. return LightColor;
  869. }
  870. return NoneColor;
  871. }
  872. //------------------------------------------------------------------------------------------------
  873. void OpenGEXImporter::handleMaterialNode(ODDLParser::DDLNode *node, aiScene *pScene) {
  874. m_currentMaterial = new aiMaterial;
  875. m_materialCache.push_back(m_currentMaterial);
  876. m_tokenType = Grammar::MaterialToken;
  877. handleNodes(node, pScene);
  878. }
  879. //------------------------------------------------------------------------------------------------
  880. void OpenGEXImporter::handleColorNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  881. if (nullptr == node) {
  882. return;
  883. }
  884. Property *prop = node->findPropertyByName("attrib");
  885. if (nullptr != prop) {
  886. if (nullptr != prop->m_value) {
  887. DataArrayList *colList(node->getDataArrayList());
  888. if (nullptr == colList) {
  889. return;
  890. }
  891. aiColor3D col;
  892. if (3 == colList->m_numItems) {
  893. aiColor3D col3;
  894. getColorRGB3(&col3, colList);
  895. col = col3;
  896. } else {
  897. aiColor4D col4;
  898. getColorRGB4(&col4, colList);
  899. col.r = col4.r;
  900. col.g = col4.g;
  901. col.b = col4.b;
  902. }
  903. #ifdef ASSIMP_USE_HUNTER
  904. const ColorType colType(getColorType(&prop->m_key->m_text));
  905. #else
  906. const ColorType colType(getColorType(prop->m_key));
  907. #endif
  908. if (DiffuseColor == colType) {
  909. m_currentMaterial->AddProperty(&col, 1, AI_MATKEY_COLOR_DIFFUSE);
  910. } else if (SpecularColor == colType) {
  911. m_currentMaterial->AddProperty(&col, 1, AI_MATKEY_COLOR_SPECULAR);
  912. } else if (EmissionColor == colType) {
  913. m_currentMaterial->AddProperty(&col, 1, AI_MATKEY_COLOR_EMISSIVE);
  914. } else if (LightColor == colType) {
  915. m_currentLight->mColorDiffuse = col;
  916. }
  917. }
  918. }
  919. }
  920. //------------------------------------------------------------------------------------------------
  921. void OpenGEXImporter::handleTextureNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  922. if (nullptr == node) {
  923. return;
  924. }
  925. Property *prop = node->findPropertyByName("attrib");
  926. if (nullptr != prop) {
  927. if (nullptr != prop->m_value) {
  928. Value *val(node->getValue());
  929. if (nullptr != val) {
  930. aiString tex;
  931. tex.Set(val->getString());
  932. if (prop->m_value->getString() == Grammar::DiffuseTextureToken) {
  933. m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  934. } else if (prop->m_value->getString() == Grammar::DiffuseSpecularTextureToken) {
  935. m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  936. } else if (prop->m_value->getString() == Grammar::SpecularPowerTextureToken) {
  937. m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  938. } else if (prop->m_value->getString() == Grammar::EmissionTextureToken) {
  939. m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  940. } else if (prop->m_value->getString() == Grammar::OpacyTextureToken) {
  941. m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_OPACITY(0));
  942. } else if (prop->m_value->getString() == Grammar::TransparencyTextureToken) {
  943. // ToDo!
  944. // m_currentMaterial->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) );
  945. } else if (prop->m_value->getString() == Grammar::NormalTextureToken) {
  946. m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_NORMALS(0));
  947. } else {
  948. ai_assert(false);
  949. }
  950. }
  951. }
  952. }
  953. }
  954. //------------------------------------------------------------------------------------------------
  955. void OpenGEXImporter::handleParamNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  956. if (nullptr == node) {
  957. return;
  958. }
  959. Property *prop = node->findPropertyByName("attrib");
  960. if (nullptr == prop) {
  961. return;
  962. }
  963. if (nullptr != prop->m_value) {
  964. Value *val(node->getValue());
  965. if (nullptr == val) {
  966. return;
  967. }
  968. const float floatVal(val->getFloat());
  969. if (0 == ASSIMP_strincmp("fov", prop->m_value->getString(), 3)) {
  970. m_currentCamera->mHorizontalFOV = floatVal;
  971. } else if (0 == ASSIMP_strincmp("near", prop->m_value->getString(), 4)) {
  972. m_currentCamera->mClipPlaneNear = floatVal;
  973. } else if (0 == ASSIMP_strincmp("far", prop->m_value->getString(), 3)) {
  974. m_currentCamera->mClipPlaneFar = floatVal;
  975. }
  976. }
  977. }
  978. //------------------------------------------------------------------------------------------------
  979. void OpenGEXImporter::handleAttenNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) {
  980. if (nullptr == node) {
  981. return;
  982. }
  983. Property *prop = node->findPropertyByName("curve");
  984. if (nullptr != prop) {
  985. if (nullptr != prop->m_value) {
  986. Value *val(node->getValue());
  987. const float floatVal(val->getFloat());
  988. if (0 == strncmp("scale", prop->m_value->getString(), strlen("scale"))) {
  989. m_currentLight->mAttenuationQuadratic = floatVal;
  990. }
  991. }
  992. }
  993. }
  994. //------------------------------------------------------------------------------------------------
  995. void OpenGEXImporter::copyMeshes(aiScene *pScene) {
  996. ai_assert(nullptr != pScene);
  997. if (m_meshCache.empty()) {
  998. return;
  999. }
  1000. pScene->mNumMeshes = static_cast<unsigned int>(m_meshCache.size());
  1001. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
  1002. for (unsigned int i = 0; i < pScene->mNumMeshes; i++) {
  1003. pScene->mMeshes[i] = m_meshCache[i].release();
  1004. }
  1005. }
  1006. //------------------------------------------------------------------------------------------------
  1007. void OpenGEXImporter::copyCameras(aiScene *pScene) {
  1008. ai_assert(nullptr != pScene);
  1009. if (m_cameraCache.empty()) {
  1010. return;
  1011. }
  1012. pScene->mNumCameras = static_cast<unsigned int>(m_cameraCache.size());
  1013. pScene->mCameras = new aiCamera *[pScene->mNumCameras];
  1014. std::copy(m_cameraCache.begin(), m_cameraCache.end(), pScene->mCameras);
  1015. }
  1016. //------------------------------------------------------------------------------------------------
  1017. void OpenGEXImporter::copyLights(aiScene *pScene) {
  1018. ai_assert(nullptr != pScene);
  1019. if (m_lightCache.empty()) {
  1020. return;
  1021. }
  1022. pScene->mNumLights = static_cast<unsigned int>(m_lightCache.size());
  1023. pScene->mLights = new aiLight *[pScene->mNumLights];
  1024. std::copy(m_lightCache.begin(), m_lightCache.end(), pScene->mLights);
  1025. }
  1026. //------------------------------------------------------------------------------------------------
  1027. void OpenGEXImporter::copyMaterials(aiScene *pScene) {
  1028. ai_assert(nullptr != pScene);
  1029. if (m_materialCache.empty()) {
  1030. return;
  1031. }
  1032. pScene->mNumMaterials = static_cast<unsigned int>(m_materialCache.size());
  1033. pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
  1034. std::copy(m_materialCache.begin(), m_materialCache.end(), pScene->mMaterials);
  1035. }
  1036. //------------------------------------------------------------------------------------------------
  1037. void OpenGEXImporter::resolveReferences() {
  1038. if (m_unresolvedRefStack.empty()) {
  1039. return;
  1040. }
  1041. RefInfo *currentRefInfo(nullptr);
  1042. for (auto it = m_unresolvedRefStack.begin(); it != m_unresolvedRefStack.end(); ++it) {
  1043. currentRefInfo = it->get();
  1044. if (nullptr != currentRefInfo) {
  1045. aiNode *node(currentRefInfo->m_node);
  1046. if (RefInfo::MeshRef == currentRefInfo->m_type) {
  1047. for (size_t i = 0; i < currentRefInfo->m_Names.size(); ++i) {
  1048. const std::string &name(currentRefInfo->m_Names[i]);
  1049. ReferenceMap::const_iterator curIt(m_mesh2refMap.find(name));
  1050. if (m_mesh2refMap.end() != curIt) {
  1051. unsigned int meshIdx = static_cast<unsigned int>(m_mesh2refMap[name]);
  1052. node->mMeshes[i] = meshIdx;
  1053. }
  1054. }
  1055. } else if (RefInfo::MaterialRef == currentRefInfo->m_type) {
  1056. for (size_t i = 0; i < currentRefInfo->m_Names.size(); ++i) {
  1057. const std::string name(currentRefInfo->m_Names[i]);
  1058. ReferenceMap::const_iterator curIt(m_material2refMap.find(name));
  1059. if (m_material2refMap.end() != curIt) {
  1060. if (nullptr != m_currentMesh) {
  1061. unsigned int matIdx = static_cast<unsigned int>(m_material2refMap[name]);
  1062. if (m_currentMesh->mMaterialIndex != 0) {
  1063. ASSIMP_LOG_WARN("Override of material reference in current mesh by material reference.");
  1064. }
  1065. m_currentMesh->mMaterialIndex = matIdx;
  1066. } else {
  1067. ASSIMP_LOG_WARN("Cannot resolve material reference, because no current mesh is there.");
  1068. }
  1069. }
  1070. }
  1071. } else {
  1072. throw DeadlyImportError("Unknown reference info to resolve.");
  1073. }
  1074. }
  1075. }
  1076. }
  1077. //------------------------------------------------------------------------------------------------
  1078. void OpenGEXImporter::createNodeTree(aiScene *pScene) {
  1079. if (nullptr == m_root) {
  1080. return;
  1081. }
  1082. if (m_root->m_children.empty()) {
  1083. return;
  1084. }
  1085. pScene->mRootNode->mNumChildren = static_cast<unsigned int>(m_root->m_children.size());
  1086. pScene->mRootNode->mChildren = new aiNode *[pScene->mRootNode->mNumChildren];
  1087. std::copy(m_root->m_children.begin(), m_root->m_children.end(), pScene->mRootNode->mChildren);
  1088. }
  1089. //------------------------------------------------------------------------------------------------
  1090. void OpenGEXImporter::pushNode(aiNode *node, aiScene *pScene) {
  1091. ai_assert(nullptr != pScene);
  1092. if (nullptr == node) {
  1093. return;
  1094. }
  1095. ChildInfo *info(nullptr);
  1096. if (m_nodeStack.empty()) {
  1097. node->mParent = pScene->mRootNode;
  1098. NodeChildMap::iterator it(m_nodeChildMap.find(node->mParent));
  1099. if (m_nodeChildMap.end() == it) {
  1100. info = new ChildInfo;
  1101. m_root = info;
  1102. m_nodeChildMap[node->mParent] = std::unique_ptr<ChildInfo>(info);
  1103. } else {
  1104. info = it->second.get();
  1105. }
  1106. info->m_children.push_back(node);
  1107. } else {
  1108. aiNode *parent(m_nodeStack.back());
  1109. ai_assert(nullptr != parent);
  1110. node->mParent = parent;
  1111. NodeChildMap::iterator it(m_nodeChildMap.find(node->mParent));
  1112. if (m_nodeChildMap.end() == it) {
  1113. info = new ChildInfo;
  1114. m_nodeChildMap[node->mParent] = std::unique_ptr<ChildInfo>(info);
  1115. } else {
  1116. info = it->second.get();
  1117. }
  1118. info->m_children.push_back(node);
  1119. }
  1120. m_nodeStack.push_back(node);
  1121. }
  1122. //------------------------------------------------------------------------------------------------
  1123. aiNode *OpenGEXImporter::popNode() {
  1124. if (m_nodeStack.empty()) {
  1125. return nullptr;
  1126. }
  1127. aiNode *node(top());
  1128. m_nodeStack.pop_back();
  1129. return node;
  1130. }
  1131. //------------------------------------------------------------------------------------------------
  1132. aiNode *OpenGEXImporter::top() const {
  1133. if (m_nodeStack.empty()) {
  1134. return nullptr;
  1135. }
  1136. return m_nodeStack.back();
  1137. }
  1138. //------------------------------------------------------------------------------------------------
  1139. void OpenGEXImporter::clearNodeStack() {
  1140. m_nodeStack.clear();
  1141. }
  1142. //------------------------------------------------------------------------------------------------
  1143. } // Namespace OpenGEX
  1144. } // Namespace Assimp
  1145. #endif // ASSIMP_BUILD_NO_OPENGEX_IMPORTER