OpenGEXImporter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_OPEMGEX_IMPORTER
  34. #include "AssimpPCH.h"
  35. #include "OpenGEXImporter.h"
  36. #include "DefaultIOSystem.h"
  37. #include <openddlparser/OpenDDLParser.h>
  38. #include <vector>
  39. static const aiImporterDesc desc = {
  40. "Open Game Engine Exchange",
  41. "",
  42. "",
  43. "",
  44. aiImporterFlags_SupportTextFlavour,
  45. 0,
  46. 0,
  47. 0,
  48. 0,
  49. "ogex"
  50. };
  51. namespace Grammar {
  52. static const char *MetricType = "Metric";
  53. static const char *Metric_DistanceType = "distance";
  54. static const char *Metric_AngleType = "angle";
  55. static const char *Metric_TimeType = "time";
  56. static const char *Metric_UpType = "up";
  57. static const char *NameType = "Name";
  58. static const char *ObjectRefType = "ObjectRef";
  59. static const char *MaterialRefType = "MaterialRef";
  60. static const char *MetricKeyType = "key";
  61. static const char *GeometryNodeType = "GeometryNode";
  62. static const char *GeometryObjectType = "GeometryObject";
  63. static const char *TransformType = "Transform";
  64. static const char *MeshType = "Mesh";
  65. static const char *VertexArrayType = "VertexArray";
  66. static const char *IndexArrayType = "IndexArray";
  67. static const char *MaterialType = "Material";
  68. static const char *ColorType = "Color";
  69. static const char *TextureType = "Texture";
  70. enum TokenType {
  71. NoneType = -1,
  72. MetricToken,
  73. NameToken,
  74. ObjectRefToken,
  75. MaterialRefToken,
  76. MetricKeyToken,
  77. GeometryNodeToken,
  78. GeometryObjectToken,
  79. TransformToken,
  80. MeshToken,
  81. VertexArrayToken,
  82. IndexArrayToken,
  83. MaterialToken,
  84. ColorToken,
  85. TextureToken
  86. };
  87. static const char *ValidMetricToken[ 4 ] = {
  88. Metric_DistanceType,
  89. Metric_AngleType,
  90. Metric_TimeType,
  91. Metric_UpType
  92. };
  93. static int isValidMetricType( const char *token ) {
  94. if( NULL == token ) {
  95. return false;
  96. }
  97. int idx( -1 );
  98. for( size_t i = 0; i < 4; i++ ) {
  99. if( 0 == strncmp( ValidMetricToken[ i ], token, strlen( token ) ) ) {
  100. idx = (int) i;
  101. break;
  102. }
  103. }
  104. return idx;
  105. }
  106. static TokenType matchTokenType( const char *tokenType ) {
  107. if( 0 == strncmp( MetricType, tokenType, strlen( tokenType ) ) ) {
  108. return MetricToken;
  109. } else if( 0 == strncmp( NameType, tokenType, strlen( tokenType ) ) ) {
  110. return NameToken;
  111. } else if( 0 == strncmp( ObjectRefType, tokenType, strlen( tokenType ) ) ) {
  112. return ObjectRefToken;
  113. } else if( 0 == strncmp( MaterialRefType, tokenType, strlen( tokenType ) ) ) {
  114. return MaterialRefToken;
  115. } else if( 0 == strncmp( MetricKeyType, tokenType, strlen( tokenType ) ) ) {
  116. return MetricKeyToken;
  117. } else if( 0 == strncmp( GeometryNodeType, tokenType, strlen( tokenType ) ) ) {
  118. return GeometryNodeToken;
  119. } else if( 0 == strncmp( GeometryObjectType, tokenType, strlen( tokenType ) ) ) {
  120. return GeometryObjectToken;
  121. } else if( 0 == strncmp( TransformType, tokenType, strlen( tokenType ) ) ) {
  122. return TransformToken;
  123. } else if( 0 == strncmp( MeshType, tokenType, strlen( tokenType ) ) ) {
  124. return MeshToken;
  125. } else if( 0 == strncmp( VertexArrayType, tokenType, strlen( tokenType ) ) ) {
  126. return VertexArrayToken;
  127. } else if( 0 == strncmp( IndexArrayType, tokenType, strlen( tokenType ) ) ) {
  128. return IndexArrayToken;
  129. } else if( 0 == strncmp( MaterialType, tokenType, strlen( tokenType ) ) ) {
  130. return MaterialToken;
  131. } else if( 0 == strncmp( ColorType, tokenType, strlen( tokenType ) ) ) {
  132. return ColorToken;
  133. } else if( 0 == strncmp( TextureType, tokenType, strlen( tokenType ) ) ) {
  134. return TextureToken;
  135. }
  136. return NoneType;
  137. }
  138. } // Namespace Grammar
  139. namespace Assimp {
  140. namespace OpenGEX {
  141. USE_ODDLPARSER_NS
  142. //------------------------------------------------------------------------------------------------
  143. OpenGEXImporter::RefInfo::RefInfo( aiNode *node, Type type, std::vector<std::string> &names )
  144. : m_node( node )
  145. , m_type( type )
  146. , m_Names( names ) {
  147. // empty
  148. }
  149. //------------------------------------------------------------------------------------------------
  150. OpenGEXImporter::OpenGEXImporter()
  151. : m_meshCache()
  152. , m_mesh2refMap()
  153. , m_ctx( NULL )
  154. , m_currentNode( NULL )
  155. , m_unresolvedRefStack() {
  156. // empty
  157. }
  158. //------------------------------------------------------------------------------------------------
  159. OpenGEXImporter::~OpenGEXImporter() {
  160. m_ctx = NULL;
  161. }
  162. //------------------------------------------------------------------------------------------------
  163. bool OpenGEXImporter::CanRead( const std::string &file, IOSystem *pIOHandler, bool checkSig ) const {
  164. bool canRead( false );
  165. if( !checkSig ) {
  166. canRead = SimpleExtensionCheck( file, "ogex" );
  167. } else {
  168. static const char *token[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" };
  169. canRead = BaseImporter::SearchFileHeaderForToken( pIOHandler, file, token, 4 );
  170. }
  171. return canRead;
  172. }
  173. //------------------------------------------------------------------------------------------------
  174. void OpenGEXImporter::InternReadFile( const std::string &filename, aiScene *pScene, IOSystem *pIOHandler ) {
  175. // open source file
  176. IOStream *file = pIOHandler->Open( filename, "rb" );
  177. if( !file ) {
  178. throw DeadlyImportError( "Failed to open file " + filename );
  179. }
  180. std::vector<char> buffer;
  181. TextFileToBuffer( file, buffer );
  182. OpenDDLParser myParser;
  183. myParser.setBuffer( &buffer[ 0 ], buffer.size() );
  184. bool success( myParser.parse() );
  185. if( success ) {
  186. m_ctx = myParser.getContext();
  187. handleNodes( m_ctx->m_root, pScene );
  188. }
  189. resolveReferences();
  190. }
  191. //------------------------------------------------------------------------------------------------
  192. const aiImporterDesc *OpenGEXImporter::GetInfo() const {
  193. return &desc;
  194. }
  195. //------------------------------------------------------------------------------------------------
  196. void OpenGEXImporter::SetupProperties( const Importer *pImp ) {
  197. if( NULL == pImp ) {
  198. return;
  199. }
  200. }
  201. //------------------------------------------------------------------------------------------------
  202. void OpenGEXImporter::handleNodes( DDLNode *node, aiScene *pScene ) {
  203. if( NULL == node ) {
  204. return;
  205. }
  206. DDLNode::DllNodeList childs = node->getChildNodeList();
  207. for( DDLNode::DllNodeList::iterator it = childs.begin(); it != childs.end(); it++ ) {
  208. Grammar::TokenType tokenType( Grammar::matchTokenType( ( *it )->getType().c_str() ) );
  209. switch( tokenType ) {
  210. case Grammar::MetricToken:
  211. handleMetricNode( *it, pScene );
  212. break;
  213. case Grammar::NameToken:
  214. handleNameNode( *it, pScene );
  215. break;
  216. case Grammar::ObjectRefToken:
  217. handleObjectRefNode( *it, pScene );
  218. break;
  219. case Grammar::MaterialRefToken:
  220. handleMaterialRefNode( *it, pScene );
  221. break;
  222. case Grammar::MetricKeyToken:
  223. break;
  224. case Grammar::GeometryNodeToken:
  225. handleGeometryNode( *it, pScene );
  226. break;
  227. case Grammar::GeometryObjectToken:
  228. handleGeometryObject( *it, pScene );
  229. break;
  230. case Grammar::TransformToken:
  231. break;
  232. case Grammar::MeshToken:
  233. break;
  234. case Grammar::VertexArrayToken:
  235. break;
  236. case Grammar::IndexArrayToken:
  237. break;
  238. case Grammar::MaterialToken:
  239. handleMaterial( *it, pScene );
  240. break;
  241. case Grammar::ColorToken:
  242. break;
  243. case Grammar::TextureToken:
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249. }
  250. //------------------------------------------------------------------------------------------------
  251. void OpenGEXImporter::handleMetricNode( DDLNode *node, aiScene *pScene ) {
  252. if( NULL == node || NULL == m_ctx ) {
  253. return;
  254. }
  255. if( m_ctx->m_root != node->getParent() ) {
  256. return;
  257. }
  258. Property *prop( node->getProperties() );
  259. while( NULL != prop ) {
  260. if( NULL != prop->m_id ) {
  261. if( Value::ddl_string == prop->m_primData->m_type ) {
  262. std::string valName( (char*) prop->m_primData->m_data );
  263. int type( Grammar::isValidMetricType( valName.c_str() ) );
  264. if( Grammar::NoneType != type ) {
  265. Value *val( node->getValue() );
  266. if( NULL != val ) {
  267. if( Value::ddl_float == val->m_type ) {
  268. m_metrics[ type ].m_floatValue = val->getFloat();
  269. } else if( Value::ddl_int32 == val->m_type ) {
  270. m_metrics[ type ].m_intValue = val->getInt32();
  271. } else if( Value::ddl_string == val->m_type ) {
  272. m_metrics[type].m_stringValue = std::string( val->getString() );
  273. } else {
  274. throw DeadlyImportError( "OpenGEX: invalid data type for Metric node." );
  275. }
  276. }
  277. }
  278. }
  279. }
  280. prop = prop->m_next;
  281. }
  282. }
  283. //------------------------------------------------------------------------------------------------
  284. void OpenGEXImporter::handleNameNode( DDLNode *node, aiScene *pScene ) {
  285. if( NULL == m_currentNode ) {
  286. throw DeadlyImportError( "No parent node for name." );
  287. return;
  288. }
  289. Value *val( node->getValue() );
  290. if( NULL != val ) {
  291. if( Value::ddl_string != val->m_type ) {
  292. throw DeadlyImportError( "OpenGEX: invalid data type for value in node name." );
  293. }
  294. std::string name( val->getString() );
  295. m_currentNode->mName.Set( name.c_str() );
  296. }
  297. }
  298. //------------------------------------------------------------------------------------------------
  299. static void getRefNames( DDLNode *node, std::vector<std::string> &names ) {
  300. ai_assert( NULL != node );
  301. Reference *ref = node->getReferences();
  302. if( NULL != ref ) {
  303. for( size_t i = 0; i < ref->m_numRefs; i++ ) {
  304. Name *currentName( ref->m_referencedName[ i ] );
  305. if( NULL != currentName && NULL != currentName->m_id ) {
  306. const std::string name( currentName->m_id->m_buffer );
  307. if( !name.empty() ) {
  308. names.push_back( name );
  309. }
  310. }
  311. }
  312. }
  313. }
  314. //------------------------------------------------------------------------------------------------
  315. void OpenGEXImporter::handleObjectRefNode( DDLNode *node, aiScene *pScene ) {
  316. if( NULL == m_currentNode ) {
  317. throw DeadlyImportError( "No parent node for name." );
  318. return;
  319. }
  320. std::vector<std::string> objRefNames;
  321. getRefNames( node, objRefNames );
  322. m_currentNode->mNumMeshes = objRefNames.size();
  323. m_currentNode->mMeshes = new unsigned int[ objRefNames.size() ];
  324. if( !objRefNames.empty() ) {
  325. m_unresolvedRefStack.push_back( new RefInfo( m_currentNode, RefInfo::MeshRef, objRefNames ) );
  326. }
  327. }
  328. //------------------------------------------------------------------------------------------------
  329. void OpenGEXImporter::handleMaterialRefNode( ODDLParser::DDLNode *node, aiScene *pScene ) {
  330. if( NULL == m_currentNode ) {
  331. throw DeadlyImportError( "No parent node for name." );
  332. return;
  333. }
  334. std::vector<std::string> matRefNames;
  335. getRefNames( node, matRefNames );
  336. if( !matRefNames.empty() ) {
  337. m_unresolvedRefStack.push_back( new RefInfo( m_currentNode, RefInfo::MaterialRef, matRefNames ) );
  338. }
  339. }
  340. //------------------------------------------------------------------------------------------------
  341. void OpenGEXImporter::handleGeometryNode( DDLNode *node, aiScene *pScene ) {
  342. m_currentNode = new aiNode;
  343. handleNodes( node, pScene );
  344. }
  345. //------------------------------------------------------------------------------------------------
  346. void OpenGEXImporter::handleGeometryObject( DDLNode *node, aiScene *pScene ) {
  347. aiMesh *currentMesh( new aiMesh );
  348. const size_t idx( m_meshCache.size() );
  349. m_meshCache.push_back( currentMesh );
  350. // store name to reference relation
  351. m_mesh2refMap[ node->getName() ] = idx;
  352. }
  353. //------------------------------------------------------------------------------------------------
  354. void OpenGEXImporter::handleMaterial( ODDLParser::DDLNode *node, aiScene *pScene ) {
  355. }
  356. //------------------------------------------------------------------------------------------------
  357. void OpenGEXImporter::resolveReferences() {
  358. if( m_unresolvedRefStack.empty() ) {
  359. return;
  360. }
  361. RefInfo *currentRefInfo( NULL );
  362. for( std::vector<RefInfo*>::iterator it = m_unresolvedRefStack.begin(); it != m_unresolvedRefStack.end(); ++it ) {
  363. currentRefInfo = *it;
  364. if( NULL != currentRefInfo ) {
  365. aiNode *node( currentRefInfo->m_node );
  366. if( RefInfo::MeshRef == currentRefInfo->m_type ) {
  367. for( size_t i = 0; i < currentRefInfo->m_Names.size(); i++ ) {
  368. const std::string &name(currentRefInfo->m_Names[ i ] );
  369. unsigned int meshIdx = m_mesh2refMap[ name ];
  370. node->mMeshes[ i ] = meshIdx;
  371. }
  372. } else if( RefInfo::MaterialRef == currentRefInfo->m_type ) {
  373. // ToDo
  374. }
  375. }
  376. }
  377. }
  378. //------------------------------------------------------------------------------------------------
  379. } // Namespace OpenGEX
  380. } // Namespace Assimp
  381. #endif // ASSIMP_BUILD_NO_OPEMGEX_IMPORTER