OpenGEXImporter.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2014, 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 "OpenGEXImporter.h"
  35. #include "DefaultIOSystem.h"
  36. #include "MakeVerboseFormat.h"
  37. #include <openddlparser/OpenDDLParser.h>
  38. #include "../include/assimp/scene.h"
  39. #include <vector>
  40. static const aiImporterDesc desc = {
  41. "Open Game Engine Exchange",
  42. "",
  43. "",
  44. "",
  45. aiImporterFlags_SupportTextFlavour,
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. "ogex"
  51. };
  52. namespace Grammar {
  53. static const char *MetricType = "Metric";
  54. static const char *Metric_DistanceType = "distance";
  55. static const char *Metric_AngleType = "angle";
  56. static const char *Metric_TimeType = "time";
  57. static const char *Metric_UpType = "up";
  58. static const char *NameType = "Name";
  59. static const char *ObjectRefType = "ObjectRef";
  60. static const char *MaterialRefType = "MaterialRef";
  61. static const char *MetricKeyType = "key";
  62. static const char *GeometryNodeType = "GeometryNode";
  63. static const char *GeometryObjectType = "GeometryObject";
  64. static const char *TransformType = "Transform";
  65. static const char *MeshType = "Mesh";
  66. static const char *VertexArrayType = "VertexArray";
  67. static const char *IndexArrayType = "IndexArray";
  68. static const char *MaterialType = "Material";
  69. static const char *ColorType = "Color";
  70. static const char *TextureType = "Texture";
  71. enum TokenType {
  72. NoneType = -1,
  73. MetricToken,
  74. NameToken,
  75. ObjectRefToken,
  76. MaterialRefToken,
  77. MetricKeyToken,
  78. GeometryNodeToken,
  79. GeometryObjectToken,
  80. TransformToken,
  81. MeshToken,
  82. VertexArrayToken,
  83. IndexArrayToken,
  84. MaterialToken,
  85. ColorToken,
  86. TextureToken
  87. };
  88. static const char *ValidMetricToken[ 4 ] = {
  89. Metric_DistanceType,
  90. Metric_AngleType,
  91. Metric_TimeType,
  92. Metric_UpType
  93. };
  94. static int isValidMetricType( const char *token ) {
  95. if( NULL == token ) {
  96. return false;
  97. }
  98. int idx( -1 );
  99. for( size_t i = 0; i < 4; i++ ) {
  100. if( 0 == strncmp( ValidMetricToken[ i ], token, strlen( token ) ) ) {
  101. idx = (int) i;
  102. break;
  103. }
  104. }
  105. return idx;
  106. }
  107. static TokenType matchTokenType( const char *tokenType ) {
  108. if( 0 == strncmp( MetricType, tokenType, strlen( MetricType ) ) ) {
  109. return MetricToken;
  110. } else if( 0 == strncmp( NameType, tokenType, strlen( NameType ) ) ) {
  111. return NameToken;
  112. }
  113. else if( 0 == strncmp( ObjectRefType, tokenType, strlen( ObjectRefType ) ) ) {
  114. return ObjectRefToken;
  115. }
  116. else if( 0 == strncmp( MaterialRefType, tokenType, strlen( MaterialRefType ) ) ) {
  117. return MaterialRefToken;
  118. }
  119. else if( 0 == strncmp( MetricKeyType, tokenType, strlen( MetricKeyType ) ) ) {
  120. return MetricKeyToken;
  121. }
  122. else if( 0 == strncmp( GeometryNodeType, tokenType, strlen( GeometryNodeType ) ) ) {
  123. return GeometryNodeToken;
  124. }
  125. else if( 0 == strncmp( GeometryObjectType, tokenType, strlen( GeometryObjectType ) ) ) {
  126. return GeometryObjectToken;
  127. }
  128. else if( 0 == strncmp( TransformType, tokenType, strlen( TransformType ) ) ) {
  129. return TransformToken;
  130. }
  131. else if( 0 == strncmp( MeshType, tokenType, strlen( MeshType ) ) ) {
  132. return MeshToken;
  133. }
  134. else if( 0 == strncmp( VertexArrayType, tokenType, strlen( VertexArrayType ) ) ) {
  135. return VertexArrayToken;
  136. }
  137. else if( 0 == strncmp( IndexArrayType, tokenType, strlen( IndexArrayType ) ) ) {
  138. return IndexArrayToken;
  139. }
  140. else if( 0 == strncmp( MaterialType, tokenType, strlen( MaterialType ) ) ) {
  141. return MaterialToken;
  142. }
  143. else if( 0 == strncmp( ColorType, tokenType, strlen( ColorType ) ) ) {
  144. return ColorToken;
  145. }
  146. else if( 0 == strncmp( TextureType, tokenType, strlen( TextureType ) ) ) {
  147. return TextureToken;
  148. }
  149. return NoneType;
  150. }
  151. } // Namespace Grammar
  152. namespace Assimp {
  153. namespace OpenGEX {
  154. USE_ODDLPARSER_NS
  155. //------------------------------------------------------------------------------------------------
  156. OpenGEXImporter::VertexContainer::VertexContainer()
  157. : m_numVerts( 0 )
  158. , m_vertices()
  159. , m_numNormals( 0 )
  160. , m_normals()
  161. , m_textureCoords()
  162. , m_numUVComps() {
  163. // empty
  164. }
  165. //------------------------------------------------------------------------------------------------
  166. OpenGEXImporter::VertexContainer::~VertexContainer() {
  167. delete[] m_vertices;
  168. delete[] m_normals;
  169. for( size_t i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++ ) {
  170. delete [] m_textureCoords[ i ];
  171. }
  172. }
  173. //------------------------------------------------------------------------------------------------
  174. OpenGEXImporter::RefInfo::RefInfo( aiNode *node, Type type, std::vector<std::string> &names )
  175. : m_node( node )
  176. , m_type( type )
  177. , m_Names( names ) {
  178. // empty
  179. }
  180. //------------------------------------------------------------------------------------------------
  181. OpenGEXImporter::RefInfo::~RefInfo() {
  182. // empty
  183. }
  184. //------------------------------------------------------------------------------------------------
  185. OpenGEXImporter::OpenGEXImporter()
  186. : m_meshCache()
  187. , m_root( NULL )
  188. , m_nodeChildMap()
  189. , m_mesh2refMap()
  190. , m_ctx( NULL )
  191. , m_currentNode( NULL )
  192. , m_currentMesh( NULL )
  193. , m_currentMaterial( NULL )
  194. , m_nodeStack()
  195. , m_unresolvedRefStack() {
  196. // empty
  197. }
  198. //------------------------------------------------------------------------------------------------
  199. OpenGEXImporter::~OpenGEXImporter() {
  200. m_ctx = NULL;
  201. }
  202. //------------------------------------------------------------------------------------------------
  203. bool OpenGEXImporter::CanRead( const std::string &file, IOSystem *pIOHandler, bool checkSig ) const {
  204. bool canRead( false );
  205. if( !checkSig ) {
  206. canRead = SimpleExtensionCheck( file, "ogex" );
  207. } else {
  208. static const char *token[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" };
  209. canRead = BaseImporter::SearchFileHeaderForToken( pIOHandler, file, token, 4 );
  210. }
  211. return canRead;
  212. }
  213. //------------------------------------------------------------------------------------------------
  214. void OpenGEXImporter::InternReadFile( const std::string &filename, aiScene *pScene, IOSystem *pIOHandler ) {
  215. // open source file
  216. IOStream *file = pIOHandler->Open( filename, "rb" );
  217. if( !file ) {
  218. throw DeadlyImportError( "Failed to open file " + filename );
  219. }
  220. std::vector<char> buffer;
  221. TextFileToBuffer( file, buffer );
  222. OpenDDLParser myParser;
  223. myParser.setBuffer( &buffer[ 0 ], buffer.size() );
  224. bool success( myParser.parse() );
  225. if( success ) {
  226. m_ctx = myParser.getContext();
  227. pScene->mRootNode = new aiNode;
  228. pScene->mRootNode->mName.Set( filename );
  229. handleNodes( m_ctx->m_root, pScene );
  230. }
  231. copyMeshes( pScene );
  232. resolveReferences();
  233. createNodeTree( pScene );
  234. }
  235. //------------------------------------------------------------------------------------------------
  236. const aiImporterDesc *OpenGEXImporter::GetInfo() const {
  237. return &desc;
  238. }
  239. //------------------------------------------------------------------------------------------------
  240. void OpenGEXImporter::SetupProperties( const Importer *pImp ) {
  241. if( NULL == pImp ) {
  242. return;
  243. }
  244. }
  245. //------------------------------------------------------------------------------------------------
  246. void OpenGEXImporter::handleNodes( DDLNode *node, aiScene *pScene ) {
  247. if( NULL == node ) {
  248. return;
  249. }
  250. DDLNode::DllNodeList childs = node->getChildNodeList();
  251. for( DDLNode::DllNodeList::iterator it = childs.begin(); it != childs.end(); it++ ) {
  252. Grammar::TokenType tokenType( Grammar::matchTokenType( ( *it )->getType().c_str() ) );
  253. switch( tokenType ) {
  254. case Grammar::MetricToken:
  255. handleMetricNode( *it, pScene );
  256. break;
  257. case Grammar::NameToken:
  258. handleNameNode( *it, pScene );
  259. break;
  260. case Grammar::ObjectRefToken:
  261. handleObjectRefNode( *it, pScene );
  262. break;
  263. case Grammar::MaterialRefToken:
  264. handleMaterialRefNode( *it, pScene );
  265. break;
  266. case Grammar::MetricKeyToken:
  267. break;
  268. case Grammar::GeometryNodeToken:
  269. handleGeometryNode( *it, pScene );
  270. break;
  271. case Grammar::GeometryObjectToken:
  272. handleGeometryObject( *it, pScene );
  273. break;
  274. case Grammar::TransformToken:
  275. handleTransformNode( *it, pScene );
  276. break;
  277. case Grammar::MeshToken:
  278. handleMeshNode( *it, pScene );
  279. break;
  280. case Grammar::VertexArrayToken:
  281. handleVertexArrayNode( *it, pScene );
  282. break;
  283. case Grammar::IndexArrayToken:
  284. handleIndexArrayNode( *it, pScene );
  285. break;
  286. case Grammar::MaterialToken:
  287. handleMaterialNode( *it, pScene );
  288. break;
  289. case Grammar::ColorToken:
  290. handleColorNode( *it, pScene );
  291. break;
  292. case Grammar::TextureToken:
  293. handleTextureNode( *it, pScene );
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. }
  300. //------------------------------------------------------------------------------------------------
  301. void OpenGEXImporter::handleMetricNode( DDLNode *node, aiScene *pScene ) {
  302. if( NULL == node || NULL == m_ctx ) {
  303. return;
  304. }
  305. if( m_ctx->m_root != node->getParent() ) {
  306. return;
  307. }
  308. Property *prop( node->getProperties() );
  309. while( NULL != prop ) {
  310. if( NULL != prop->m_id ) {
  311. if( Value::ddl_string == prop->m_primData->m_type ) {
  312. std::string valName( (char*) prop->m_primData->m_data );
  313. int type( Grammar::isValidMetricType( valName.c_str() ) );
  314. if( Grammar::NoneType != type ) {
  315. Value *val( node->getValue() );
  316. if( NULL != val ) {
  317. if( Value::ddl_float == val->m_type ) {
  318. m_metrics[ type ].m_floatValue = val->getFloat();
  319. } else if( Value::ddl_int32 == val->m_type ) {
  320. m_metrics[ type ].m_intValue = val->getInt32();
  321. } else if( Value::ddl_string == val->m_type ) {
  322. m_metrics[type].m_stringValue = std::string( val->getString() );
  323. } else {
  324. throw DeadlyImportError( "OpenGEX: invalid data type for Metric node." );
  325. }
  326. }
  327. }
  328. }
  329. }
  330. prop = prop->m_next;
  331. }
  332. }
  333. //------------------------------------------------------------------------------------------------
  334. void OpenGEXImporter::handleNameNode( DDLNode *node, aiScene *pScene ) {
  335. if( NULL == m_currentNode ) {
  336. throw DeadlyImportError( "No parent node for name." );
  337. return;
  338. }
  339. Value *val( node->getValue() );
  340. if( NULL != val ) {
  341. if( Value::ddl_string != val->m_type ) {
  342. throw DeadlyImportError( "OpenGEX: invalid data type for value in node name." );
  343. return;
  344. }
  345. const std::string name( val->getString() );
  346. m_currentNode->mName.Set( name.c_str() );
  347. }
  348. }
  349. //------------------------------------------------------------------------------------------------
  350. static void getRefNames( DDLNode *node, std::vector<std::string> &names ) {
  351. ai_assert( NULL != node );
  352. Reference *ref = node->getReferences();
  353. if( NULL != ref ) {
  354. for( size_t i = 0; i < ref->m_numRefs; i++ ) {
  355. Name *currentName( ref->m_referencedName[ i ] );
  356. if( NULL != currentName && NULL != currentName->m_id ) {
  357. const std::string name( currentName->m_id->m_buffer );
  358. if( !name.empty() ) {
  359. names.push_back( name );
  360. }
  361. }
  362. }
  363. }
  364. }
  365. //------------------------------------------------------------------------------------------------
  366. void OpenGEXImporter::handleObjectRefNode( DDLNode *node, aiScene *pScene ) {
  367. if( NULL == m_currentNode ) {
  368. throw DeadlyImportError( "No parent node for name." );
  369. return;
  370. }
  371. std::vector<std::string> objRefNames;
  372. getRefNames( node, objRefNames );
  373. m_currentNode->mNumMeshes = objRefNames.size();
  374. m_currentNode->mMeshes = new unsigned int[ objRefNames.size() ];
  375. if( !objRefNames.empty() ) {
  376. m_unresolvedRefStack.push_back( new RefInfo( m_currentNode, RefInfo::MeshRef, objRefNames ) );
  377. }
  378. }
  379. //------------------------------------------------------------------------------------------------
  380. void OpenGEXImporter::handleMaterialRefNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  381. if( NULL == m_currentNode ) {
  382. throw DeadlyImportError( "No parent node for name." );
  383. return;
  384. }
  385. std::vector<std::string> matRefNames;
  386. getRefNames( node, matRefNames );
  387. if( !matRefNames.empty() ) {
  388. m_unresolvedRefStack.push_back( new RefInfo( m_currentNode, RefInfo::MaterialRef, matRefNames ) );
  389. }
  390. }
  391. //------------------------------------------------------------------------------------------------
  392. void OpenGEXImporter::handleGeometryNode( DDLNode *node, aiScene *pScene ) {
  393. aiNode *newNode = new aiNode;
  394. pushNode( newNode, pScene );
  395. m_currentNode = newNode;
  396. handleNodes( node, pScene );
  397. popNode();
  398. }
  399. //------------------------------------------------------------------------------------------------
  400. void OpenGEXImporter::handleGeometryObject( DDLNode *node, aiScene *pScene ) {
  401. handleNodes( node, pScene );
  402. }
  403. //------------------------------------------------------------------------------------------------
  404. static void setMatrix( aiNode *node, DataArrayList *transformData ) {
  405. ai_assert( NULL != node );
  406. ai_assert( NULL != transformData );
  407. float m[ 16 ];
  408. size_t i( 1 );
  409. Value *next( transformData->m_dataList->m_next );
  410. m[ 0 ] = transformData->m_dataList->getFloat();
  411. while( next != NULL ) {
  412. m[ i ] = next->getFloat();
  413. next = next->m_next;
  414. i++;
  415. }
  416. node->mTransformation.a1 = m[ 0 ];
  417. node->mTransformation.a2 = m[ 1 ];
  418. node->mTransformation.a3 = m[ 2 ];
  419. node->mTransformation.a4 = m[ 3 ];
  420. node->mTransformation.b1 = m[ 4 ];
  421. node->mTransformation.b2 = m[ 5 ];
  422. node->mTransformation.b3 = m[ 6 ];
  423. node->mTransformation.b4 = m[ 7 ];
  424. node->mTransformation.c1 = m[ 8 ];
  425. node->mTransformation.c2 = m[ 9 ];
  426. node->mTransformation.c3 = m[ 10 ];
  427. node->mTransformation.c4 = m[ 11 ];
  428. node->mTransformation.d1 = m[ 12 ];
  429. node->mTransformation.d2 = m[ 13 ];
  430. node->mTransformation.d3 = m[ 14 ];
  431. node->mTransformation.d4 = m[ 15 ];
  432. }
  433. //------------------------------------------------------------------------------------------------
  434. void OpenGEXImporter::handleTransformNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  435. if( NULL == m_currentNode ) {
  436. throw DeadlyImportError( "No parent node for name." );
  437. return;
  438. }
  439. DataArrayList *transformData( node->getDataArrayList() );
  440. if( NULL != transformData ) {
  441. if( transformData->m_numItems != 16 ) {
  442. throw DeadlyImportError( "Invalid number of data for transform matrix." );
  443. return;
  444. }
  445. setMatrix( m_currentNode, transformData );
  446. }
  447. }
  448. //------------------------------------------------------------------------------------------------
  449. static void propId2StdString( Property *prop, std::string &name, std::string &key ) {
  450. name = key = "";
  451. if( NULL == prop ) {
  452. return;
  453. }
  454. if( NULL != prop->m_id ) {
  455. name = prop->m_id->m_buffer;
  456. if( Value::ddl_string == prop->m_primData->m_type ) {
  457. key = prop->m_primData->getString();
  458. }
  459. }
  460. }
  461. //------------------------------------------------------------------------------------------------
  462. void OpenGEXImporter::handleMeshNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  463. m_currentMesh = new aiMesh;
  464. const size_t meshidx( m_meshCache.size() );
  465. m_meshCache.push_back( m_currentMesh );
  466. Property *prop = node->getProperties();
  467. if( NULL != prop ) {
  468. std::string propName, propKey;
  469. propId2StdString( prop, propName, propKey );
  470. if( "primitive" == propName ) {
  471. if( "triangles" == propKey ) {
  472. m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  473. }
  474. }
  475. }
  476. handleNodes( node, pScene );
  477. DDLNode *parent( node->getParent() );
  478. if( NULL != parent ) {
  479. const std::string &name = parent->getName();
  480. m_mesh2refMap[ name ] = meshidx;
  481. }
  482. }
  483. //------------------------------------------------------------------------------------------------
  484. enum MeshAttribute {
  485. None,
  486. Position,
  487. Normal,
  488. TexCoord
  489. };
  490. //------------------------------------------------------------------------------------------------
  491. static MeshAttribute getAttributeByName( const char *attribName ) {
  492. ai_assert( NULL != attribName );
  493. if( 0 == strncmp( "position", attribName, strlen( "position" ) ) ) {
  494. return Position;
  495. } else if( 0 == strncmp( "normal", attribName, strlen( "normal" ) ) ) {
  496. return Normal;
  497. } else if( 0 == strncmp( "texcoord", attribName, strlen( "texcoord" ) ) ) {
  498. return TexCoord;
  499. }
  500. return None;
  501. }
  502. //------------------------------------------------------------------------------------------------
  503. static void fillVector3( aiVector3D *vec3, Value *vals ) {
  504. ai_assert( NULL != vec3 );
  505. ai_assert( NULL != vals );
  506. float x( 0.0f ), y( 0.0f ), z( 0.0f );
  507. Value *next( vals );
  508. x = next->getFloat();
  509. next = next->m_next;
  510. y = next->getFloat();
  511. next = next->m_next;
  512. if( NULL != next ) {
  513. z = next->getFloat();
  514. }
  515. vec3->Set( x, y, z );
  516. }
  517. //------------------------------------------------------------------------------------------------
  518. static size_t countDataArrayListItems( DataArrayList *vaList ) {
  519. size_t numItems( 0 );
  520. if( NULL == vaList ) {
  521. return numItems;
  522. }
  523. DataArrayList *next( vaList );
  524. while( NULL != next ) {
  525. if( NULL != vaList->m_dataList ) {
  526. numItems++;
  527. }
  528. next = next->m_next;
  529. }
  530. return numItems;
  531. }
  532. //------------------------------------------------------------------------------------------------
  533. static void copyVectorArray( size_t numItems, DataArrayList *vaList, aiVector3D *vectorArray ) {
  534. for( size_t i = 0; i < numItems; i++ ) {
  535. Value *next( vaList->m_dataList );
  536. fillVector3( &vectorArray[ i ], next );
  537. vaList = vaList->m_next;
  538. }
  539. }
  540. //------------------------------------------------------------------------------------------------
  541. void OpenGEXImporter::handleVertexArrayNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  542. if( NULL == node ) {
  543. throw DeadlyImportError( "No parent node for name." );
  544. return;
  545. }
  546. Property *prop( node->getProperties() );
  547. if( NULL != prop ) {
  548. std::string propName, propKey;
  549. propId2StdString( prop, propName, propKey );
  550. MeshAttribute attribType( getAttributeByName( propKey.c_str() ) );
  551. if( None == attribType ) {
  552. return;
  553. }
  554. DataArrayList *vaList = node->getDataArrayList();
  555. if( NULL == vaList ) {
  556. return;
  557. }
  558. const size_t numItems( countDataArrayListItems( vaList ) );
  559. Value *next( vaList->m_dataList );
  560. if( Position == attribType ) {
  561. m_currentVertices.m_numVerts = numItems;
  562. m_currentVertices.m_vertices = new aiVector3D[ numItems ];
  563. copyVectorArray( numItems, vaList, m_currentVertices.m_vertices );
  564. } else if( Normal == attribType ) {
  565. m_currentVertices.m_numNormals = numItems;
  566. m_currentVertices.m_normals = new aiVector3D[ numItems ];
  567. copyVectorArray( numItems, vaList, m_currentVertices.m_normals );
  568. } else if( TexCoord == attribType ) {
  569. m_currentVertices.m_numUVComps[ 0 ] = numItems;
  570. m_currentVertices.m_textureCoords[ 0 ] = new aiVector3D[ numItems ];
  571. copyVectorArray( numItems, vaList, m_currentVertices.m_textureCoords[ 0 ] );
  572. }
  573. }
  574. }
  575. //------------------------------------------------------------------------------------------------
  576. void OpenGEXImporter::handleIndexArrayNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  577. if( NULL == node ) {
  578. throw DeadlyImportError( "No parent node for name." );
  579. return;
  580. }
  581. if( NULL == m_currentMesh ) {
  582. throw DeadlyImportError( "No current mesh for index data found." );
  583. return;
  584. }
  585. DataArrayList *vaList = node->getDataArrayList();
  586. if( NULL == vaList ) {
  587. return;
  588. }
  589. const size_t numItems( countDataArrayListItems( vaList ) );
  590. m_currentMesh->mNumFaces = numItems;
  591. m_currentMesh->mFaces = new aiFace[ numItems ];
  592. m_currentMesh->mNumVertices = numItems * 3;
  593. m_currentMesh->mVertices = new aiVector3D[ m_currentMesh->mNumVertices ];
  594. m_currentMesh->mNormals = new aiVector3D[ m_currentMesh->mNumVertices ];
  595. m_currentMesh->mNumUVComponents[ 0 ] = numItems * 3;
  596. m_currentMesh->mTextureCoords[ 0 ] = new aiVector3D[ m_currentMesh->mNumUVComponents[ 0 ] ];
  597. unsigned int index( 0 );
  598. for( size_t i = 0; i < m_currentMesh->mNumFaces; i++ ) {
  599. aiFace &current( m_currentMesh->mFaces[ i ] );
  600. current.mNumIndices = 3;
  601. current.mIndices = new unsigned int[ current.mNumIndices ];
  602. Value *next( vaList->m_dataList );
  603. for( size_t indices = 0; indices < current.mNumIndices; indices++ ) {
  604. const int idx = next->getInt32();
  605. ai_assert( idx <= m_currentVertices.m_numVerts );
  606. aiVector3D &pos = ( m_currentVertices.m_vertices[ idx ] );
  607. aiVector3D &normal = ( m_currentVertices.m_normals[ idx ] );
  608. aiVector3D &tex = ( m_currentVertices.m_textureCoords[ 0 ][ idx ] );
  609. ai_assert( index < m_currentMesh->mNumVertices );
  610. m_currentMesh->mVertices[ index ].Set( pos.x, pos.y, pos.z );
  611. m_currentMesh->mNormals[ index ].Set( normal.x, normal.y, normal.z );
  612. m_currentMesh->mTextureCoords[0][ index ].Set( tex.x, tex.y, tex.z );
  613. current.mIndices[ indices ] = index;
  614. index++;
  615. next = next->m_next;
  616. }
  617. vaList = vaList->m_next;
  618. }
  619. }
  620. //------------------------------------------------------------------------------------------------
  621. static void getColorRGBA( aiColor3D *pColor, Value *data ) {
  622. if( NULL == pColor || NULL == data ) {
  623. return;
  624. }
  625. pColor->r = data->getFloat();
  626. data = data->getNext();
  627. pColor->g = data->getFloat();
  628. data = data->getNext();
  629. pColor->b = data->getFloat();
  630. data = data->getNext();
  631. }
  632. //------------------------------------------------------------------------------------------------
  633. enum ColorType {
  634. NoneColor = 0,
  635. DiffuseColor
  636. };
  637. //------------------------------------------------------------------------------------------------
  638. static ColorType getColorType( Identifier *id ) {
  639. const int res(strncmp("diffuse", id->m_buffer, id->m_len ) );
  640. if( 0 == res ) {
  641. return DiffuseColor;
  642. }
  643. return NoneColor;
  644. }
  645. //------------------------------------------------------------------------------------------------
  646. void OpenGEXImporter::handleMaterialNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  647. m_currentMaterial = new aiMaterial;
  648. m_materialCache.push_back( m_currentMaterial );
  649. handleNodes( node, pScene );
  650. }
  651. //------------------------------------------------------------------------------------------------
  652. void OpenGEXImporter::handleColorNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  653. if( NULL == node ) {
  654. return;
  655. }
  656. Property *colorProp = node->getProperties();
  657. if( NULL != colorProp ) {
  658. if( NULL != colorProp->m_id ) {
  659. }
  660. }
  661. }
  662. //------------------------------------------------------------------------------------------------
  663. void OpenGEXImporter::handleTextureNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  664. }
  665. //------------------------------------------------------------------------------------------------
  666. void OpenGEXImporter::copyMeshes( aiScene *pScene ) {
  667. if( m_meshCache.empty() ) {
  668. return;
  669. }
  670. pScene->mNumMeshes = m_meshCache.size();
  671. pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];
  672. size_t i( 0 );
  673. for( std::vector<aiMesh*>::iterator it = m_meshCache.begin(); it != m_meshCache.end(); it++ ) {
  674. pScene->mMeshes[ i ] = *it;
  675. i++;
  676. }
  677. }
  678. //------------------------------------------------------------------------------------------------
  679. void OpenGEXImporter::resolveReferences() {
  680. if( m_unresolvedRefStack.empty() ) {
  681. return;
  682. }
  683. RefInfo *currentRefInfo( NULL );
  684. for( std::vector<RefInfo*>::iterator it = m_unresolvedRefStack.begin(); it != m_unresolvedRefStack.end(); ++it ) {
  685. currentRefInfo = *it;
  686. if( NULL != currentRefInfo ) {
  687. aiNode *node( currentRefInfo->m_node );
  688. if( RefInfo::MeshRef == currentRefInfo->m_type ) {
  689. for( size_t i = 0; i < currentRefInfo->m_Names.size(); i++ ) {
  690. const std::string &name(currentRefInfo->m_Names[ i ] );
  691. ReferenceMap::const_iterator it( m_mesh2refMap.find( name ) );
  692. if( m_mesh2refMap.end() != it ) {
  693. unsigned int meshIdx = m_mesh2refMap[ name ];
  694. node->mMeshes[ i ] = meshIdx;
  695. }
  696. }
  697. } else if( RefInfo::MaterialRef == currentRefInfo->m_type ) {
  698. // ToDo!
  699. } else {
  700. throw DeadlyImportError( "Unknown reference info to resolve." );
  701. }
  702. }
  703. }
  704. }
  705. //------------------------------------------------------------------------------------------------
  706. void OpenGEXImporter::createNodeTree( aiScene *pScene ) {
  707. if( NULL == m_root ) {
  708. return;
  709. }
  710. if( m_root->m_children.empty() ) {
  711. return;
  712. }
  713. size_t i( 0 );
  714. pScene->mRootNode->mNumChildren = m_root->m_children.size();
  715. pScene->mRootNode->mChildren = new C_STRUCT aiNode*[ pScene->mRootNode->mNumChildren ];
  716. for( ChildInfo::NodeList::iterator it = m_root->m_children.begin(); it != m_root->m_children.end(); it++ ) {
  717. pScene->mRootNode->mChildren[ i ] = *it;
  718. i++;
  719. }
  720. }
  721. //------------------------------------------------------------------------------------------------
  722. void OpenGEXImporter::pushNode( aiNode *node, aiScene *pScene ) {
  723. ai_assert( NULL != pScene );
  724. if( NULL != node ) {
  725. ChildInfo *info( NULL );
  726. if( m_nodeStack.empty() ) {
  727. node->mParent = pScene->mRootNode;
  728. NodeChildMap::iterator it( m_nodeChildMap.find( node->mParent ) );
  729. if( m_nodeChildMap.end() == it ) {
  730. info = new ChildInfo;
  731. m_root = info;
  732. m_nodeChildMap[ node->mParent ] = info;
  733. } else {
  734. info = it->second;
  735. }
  736. info->m_children.push_back( node );
  737. } else {
  738. aiNode *parent( m_nodeStack.back() );
  739. ai_assert( NULL != parent );
  740. node->mParent = parent;
  741. NodeChildMap::iterator it( m_nodeChildMap.find( node->mParent ) );
  742. if( m_nodeChildMap.end() == it ) {
  743. info = new ChildInfo;
  744. m_nodeChildMap[ node->mParent ] = info;
  745. } else {
  746. info = it->second;
  747. }
  748. info->m_children.push_back( node );
  749. }
  750. m_nodeStack.push_back( node );
  751. }
  752. }
  753. //------------------------------------------------------------------------------------------------
  754. aiNode *OpenGEXImporter::popNode() {
  755. if( m_nodeStack.empty() ) {
  756. return NULL;
  757. }
  758. aiNode *node( top() );
  759. m_nodeStack.pop_back();
  760. return node;
  761. }
  762. //------------------------------------------------------------------------------------------------
  763. aiNode *OpenGEXImporter::top() const {
  764. if( m_nodeStack.empty() ) {
  765. return NULL;
  766. }
  767. return m_nodeStack.back();
  768. }
  769. //------------------------------------------------------------------------------------------------
  770. void OpenGEXImporter::clearNodeStack() {
  771. m_nodeStack.clear();
  772. }
  773. //------------------------------------------------------------------------------------------------
  774. } // Namespace OpenGEX
  775. } // Namespace Assimp
  776. #endif // ASSIMP_BUILD_NO_OPENGEX_IMPORTER