2
0

XFileParser.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the XFile parser helper class */
  35. #include "AssimpPCH.h"
  36. #ifndef ASSIMP_BUILD_NO_X_IMPORTER
  37. #include "XFileParser.h"
  38. #include "XFileHelper.h"
  39. #include "fast_atof.h"
  40. using namespace Assimp;
  41. using namespace Assimp::XFile;
  42. #ifndef ASSIMP_BUILD_NO_COMPRESSED_X
  43. #include "../contrib/zlib/zlib.h"
  44. // Magic identifier for MSZIP compressed data
  45. #define MSZIP_MAGIC 0x4B43
  46. #define MSZIP_BLOCK 32786
  47. // ------------------------------------------------------------------------------------------------
  48. // Dummy memory wrappers for use with zlib
  49. void* dummy_alloc (void* opaque, unsigned int items, unsigned int size) {
  50. // we're using calloc to make it easier to debug the whole stuff
  51. return ::calloc(items,size);
  52. }
  53. void dummy_free (void* opaque, void* address) {
  54. return ::free(address);
  55. }
  56. #endif // !! ASSIMP_BUILD_NO_COMPRESSED_X
  57. // ------------------------------------------------------------------------------------------------
  58. // Constructor. Creates a data structure out of the XFile given in the memory block.
  59. XFileParser::XFileParser( const std::vector<char>& pBuffer)
  60. {
  61. mMajorVersion = mMinorVersion = 0;
  62. mIsBinaryFormat = false;
  63. mBinaryNumCount = 0;
  64. P = End = NULL;
  65. mLineNumber = 0;
  66. mScene = NULL;
  67. // vector to store uncompressed file for INFLATE'd X files
  68. std::vector<char> uncompressed;
  69. // set up memory pointers
  70. P = &pBuffer.front();
  71. End = P + pBuffer.size();
  72. // check header
  73. if( strncmp( P, "xof ", 4) != 0)
  74. throw new ImportErrorException( "Header mismatch, file is not an XFile.");
  75. // read version. It comes in a four byte format such as "0302"
  76. mMajorVersion = (unsigned int)(P[4] - 48) * 10 + (unsigned int)(P[5] - 48);
  77. mMinorVersion = (unsigned int)(P[6] - 48) * 10 + (unsigned int)(P[7] - 48);
  78. bool compressed = false;
  79. // txt - pure ASCII text format
  80. if( strncmp( P + 8, "txt ", 4) == 0)
  81. mIsBinaryFormat = false;
  82. // bin - Binary format
  83. else if( strncmp( P + 8, "bin ", 4) == 0)
  84. mIsBinaryFormat = true;
  85. // tzip - Inflate compressed text format
  86. else if( strncmp( P + 8, "tzip", 4) == 0)
  87. {
  88. mIsBinaryFormat = false;
  89. compressed = true;
  90. }
  91. // bzip - Inflate compressed binary format
  92. else if( strncmp( P + 8, "bzip", 4) == 0)
  93. {
  94. mIsBinaryFormat = true;
  95. compressed = true;
  96. }
  97. else ThrowException( boost::str(boost::format("Unsupported xfile format '%c%c%c%c'")
  98. % P[8] % P[9] % P[10] % P[11]));
  99. // float size
  100. mBinaryFloatSize = (unsigned int)(P[12] - 48) * 1000
  101. + (unsigned int)(P[13] - 48) * 100
  102. + (unsigned int)(P[14] - 48) * 10
  103. + (unsigned int)(P[15] - 48);
  104. if( mBinaryFloatSize != 32 && mBinaryFloatSize != 64)
  105. ThrowException( boost::str( boost::format( "Unknown float size %1% specified in xfile header.")
  106. % mBinaryFloatSize));
  107. P += 16;
  108. // If this is a compressed X file, apply the inflate algorithm to it
  109. if (compressed)
  110. {
  111. #ifdef ASSIMP_BUILD_NO_COMPRESSED_X
  112. throw new ImportErrorException("Assimp was built without compressed X support");
  113. #else
  114. /* ///////////////////////////////////////////////////////////////////////
  115. * COMPRESSED X FILE FORMAT
  116. * ///////////////////////////////////////////////////////////////////////
  117. * [xhead]
  118. * 2 major
  119. * 2 minor
  120. * 4 type // bzip,tzip
  121. * [mszip_master_head]
  122. * 4 unkn // checksum?
  123. * 2 unkn // flags? (seems to be constant)
  124. * [mszip_head]
  125. * 2 ofs // offset to next section
  126. * 2 magic // 'CK'
  127. * ... ofs bytes of data
  128. * ... next mszip_head
  129. *
  130. * http://www.kdedevelopers.org/node/3181 has been very helpful.
  131. * ///////////////////////////////////////////////////////////////////////
  132. */
  133. // build a zlib stream
  134. z_stream stream;
  135. stream.opaque = NULL;
  136. stream.zalloc = &dummy_alloc;
  137. stream.zfree = &dummy_free;
  138. stream.data_type = (mIsBinaryFormat ? Z_BINARY : Z_ASCII);
  139. // initialize the inflation algorithm
  140. ::inflateInit2(&stream, -MAX_WBITS);
  141. // skip unknown data (checksum, flags?)
  142. P += 6;
  143. // First find out how much storage we'll need. Count sections.
  144. const char* P1 = P;
  145. unsigned int est_out = 0;
  146. while (P1 < End)
  147. {
  148. // read next offset
  149. uint16_t ofs = *((uint16_t*)P1);
  150. AI_SWAP2(ofs); P1 += 2;
  151. if (ofs >= MSZIP_BLOCK)
  152. throw new ImportErrorException("X: Invalid offset to next MSZIP compressed block");
  153. // check magic word
  154. uint16_t magic = *((uint16_t*)P1);
  155. AI_SWAP2(magic); P1 += 2;
  156. if (magic != MSZIP_MAGIC)
  157. throw new ImportErrorException("X: Unsupported compressed format, expected MSZIP header");
  158. // and advance to the next offset
  159. P1 += ofs;
  160. est_out += MSZIP_BLOCK; // one decompressed block is 32786 in size
  161. }
  162. // Allocate storage and do the actual uncompressing
  163. uncompressed.resize(est_out);
  164. char* out = &uncompressed.front();
  165. while (P < End)
  166. {
  167. uint16_t ofs = *((uint16_t*)P);
  168. AI_SWAP2(ofs);
  169. P += 4;
  170. // push data to the stream
  171. stream.next_in = (Bytef*)P;
  172. stream.avail_in = ofs;
  173. stream.next_out = (Bytef*)out;
  174. stream.avail_out = MSZIP_BLOCK;
  175. // and decompress the data ....
  176. int ret = ::inflate( &stream, Z_SYNC_FLUSH );
  177. if (ret != Z_OK && ret != Z_STREAM_END)
  178. throw new ImportErrorException("X: Failed to decompress MSZIP-compressed data");
  179. ::inflateReset( &stream );
  180. ::inflateSetDictionary( &stream, (const Bytef*)out , MSZIP_BLOCK - stream.avail_out );
  181. // and advance to the next offset
  182. out += MSZIP_BLOCK - stream.avail_out;
  183. P += ofs;
  184. }
  185. // terminate zlib
  186. ::inflateEnd(&stream);
  187. // ok, update pointers to point to the uncompressed file data
  188. P = &uncompressed[0];
  189. End = out;
  190. // FIXME: we don't need the compressed data anymore, could release
  191. // it already for better memory usage. Consider breaking const-co.
  192. DefaultLogger::get()->info("Successfully decompressed MSZIP-compressed file");
  193. #endif // !! ASSIMP_BUILD_NO_COMPRESSED_X
  194. }
  195. else
  196. {
  197. // start reading here
  198. ReadUntilEndOfLine();
  199. }
  200. mScene = new Scene;
  201. ParseFile();
  202. // filter the imported hierarchy for some degenerated cases
  203. if( mScene->mRootNode) {
  204. FilterHierarchy( mScene->mRootNode);
  205. }
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. // Destructor. Destroys all imported data along with it
  209. XFileParser::~XFileParser()
  210. {
  211. // kill everything we created
  212. delete mScene;
  213. }
  214. // ------------------------------------------------------------------------------------------------
  215. void XFileParser::ParseFile()
  216. {
  217. while( 1)
  218. {
  219. // read name of next object
  220. std::string objectName = GetNextToken();
  221. if (objectName.length() == 0)
  222. break;
  223. // parse specific object
  224. if( objectName == "template")
  225. ParseDataObjectTemplate();
  226. else
  227. if( objectName == "Frame")
  228. ParseDataObjectFrame( NULL);
  229. else
  230. if( objectName == "Mesh")
  231. {
  232. // some meshes have no frames at all
  233. Mesh* mesh = new Mesh;
  234. ParseDataObjectMesh( mesh);
  235. mScene->mGlobalMeshes.push_back( mesh);
  236. } else
  237. if( objectName == "AnimTicksPerSecond")
  238. ParseDataObjectAnimTicksPerSecond();
  239. else
  240. if( objectName == "AnimationSet")
  241. ParseDataObjectAnimationSet();
  242. else
  243. if( objectName == "Material")
  244. {
  245. // Material outside of a mesh or node
  246. Material material;
  247. ParseDataObjectMaterial( &material);
  248. mScene->mGlobalMaterials.push_back( material);
  249. } else
  250. if( objectName == "}")
  251. {
  252. // whatever?
  253. DefaultLogger::get()->warn("} found in dataObject");
  254. } else
  255. {
  256. // unknown format
  257. DefaultLogger::get()->warn("Unknown data object in animation of .x file");
  258. ParseUnknownDataObject();
  259. }
  260. }
  261. }
  262. // ------------------------------------------------------------------------------------------------
  263. void XFileParser::ParseDataObjectTemplate()
  264. {
  265. // parse a template data object. Currently not stored.
  266. std::string name;
  267. readHeadOfDataObject( &name);
  268. // read GUID
  269. std::string guid = GetNextToken();
  270. // read and ignore data members
  271. while(true)
  272. {
  273. std::string s = GetNextToken();
  274. if( s == "}")
  275. break;
  276. if( s.length() == 0)
  277. ThrowException( "Unexpected end of file reached while parsing template definition");
  278. }
  279. }
  280. // ------------------------------------------------------------------------------------------------
  281. void XFileParser::ParseDataObjectFrame( Node* pParent)
  282. {
  283. // A coordinate frame, or "frame of reference." The Frame template
  284. // is open and can contain any object. The Direct3D extensions (D3DX)
  285. // mesh-loading functions recognize Mesh, FrameTransformMatrix, and
  286. // Frame template instances as child objects when loading a Frame
  287. // instance.
  288. std::string name;
  289. readHeadOfDataObject(&name);
  290. // create a named node and place it at its parent, if given
  291. Node* node = new Node( pParent);
  292. node->mName = name;
  293. if( pParent)
  294. {
  295. pParent->mChildren.push_back( node);
  296. } else
  297. {
  298. // there might be multiple root nodes
  299. if( mScene->mRootNode != NULL)
  300. {
  301. // place a dummy root if not there
  302. if( mScene->mRootNode->mName != "$dummy_root")
  303. {
  304. Node* exroot = mScene->mRootNode;
  305. mScene->mRootNode = new Node( NULL);
  306. mScene->mRootNode->mName = "$dummy_root";
  307. mScene->mRootNode->mChildren.push_back( exroot);
  308. exroot->mParent = mScene->mRootNode;
  309. }
  310. // put the new node as its child instead
  311. mScene->mRootNode->mChildren.push_back( node);
  312. node->mParent = mScene->mRootNode;
  313. } else
  314. {
  315. // it's the first node imported. place it as root
  316. mScene->mRootNode = node;
  317. }
  318. }
  319. // Now inside a frame.
  320. // read tokens until closing brace is reached.
  321. while(true)
  322. {
  323. std::string objectName = GetNextToken();
  324. if (objectName.size() == 0)
  325. ThrowException( "Unexpected end of file reached while parsing frame");
  326. if( objectName == "}")
  327. break; // frame finished
  328. else
  329. if( objectName == "Frame")
  330. ParseDataObjectFrame( node); // child frame
  331. else
  332. if( objectName == "FrameTransformMatrix")
  333. ParseDataObjectTransformationMatrix( node->mTrafoMatrix);
  334. else
  335. if( objectName == "Mesh")
  336. {
  337. Mesh* mesh = new Mesh;
  338. node->mMeshes.push_back( mesh);
  339. ParseDataObjectMesh( mesh);
  340. } else
  341. {
  342. DefaultLogger::get()->warn("Unknown data object in frame in x file");
  343. ParseUnknownDataObject();
  344. }
  345. }
  346. }
  347. // ------------------------------------------------------------------------------------------------
  348. void XFileParser::ParseDataObjectTransformationMatrix( aiMatrix4x4& pMatrix)
  349. {
  350. // read header, we're not interested if it has a name
  351. readHeadOfDataObject();
  352. // read its components
  353. pMatrix.a1 = ReadFloat(); pMatrix.b1 = ReadFloat();
  354. pMatrix.c1 = ReadFloat(); pMatrix.d1 = ReadFloat();
  355. pMatrix.a2 = ReadFloat(); pMatrix.b2 = ReadFloat();
  356. pMatrix.c2 = ReadFloat(); pMatrix.d2 = ReadFloat();
  357. pMatrix.a3 = ReadFloat(); pMatrix.b3 = ReadFloat();
  358. pMatrix.c3 = ReadFloat(); pMatrix.d3 = ReadFloat();
  359. pMatrix.a4 = ReadFloat(); pMatrix.b4 = ReadFloat();
  360. pMatrix.c4 = ReadFloat(); pMatrix.d4 = ReadFloat();
  361. // trailing symbols
  362. CheckForSemicolon();
  363. CheckForClosingBrace();
  364. }
  365. // ------------------------------------------------------------------------------------------------
  366. void XFileParser::ParseDataObjectMesh( Mesh* pMesh)
  367. {
  368. std::string name;
  369. readHeadOfDataObject( &name);
  370. // read vertex count
  371. unsigned int numVertices = ReadInt();
  372. pMesh->mPositions.resize( numVertices);
  373. // read vertices
  374. for( unsigned int a = 0; a < numVertices; a++)
  375. pMesh->mPositions[a] = ReadVector3();
  376. // read position faces
  377. unsigned int numPosFaces = ReadInt();
  378. pMesh->mPosFaces.resize( numPosFaces);
  379. for( unsigned int a = 0; a < numPosFaces; a++)
  380. {
  381. unsigned int numIndices = ReadInt();
  382. if( numIndices < 3)
  383. ThrowException( boost::str( boost::format( "Invalid index count %1% for face %2%.") % numIndices % a));
  384. // read indices
  385. Face& face = pMesh->mPosFaces[a];
  386. for( unsigned int b = 0; b < numIndices; b++)
  387. face.mIndices.push_back( ReadInt());
  388. CheckForSeparator();
  389. }
  390. // here, other data objects may follow
  391. while(true)
  392. {
  393. std::string objectName = GetNextToken();
  394. if( objectName.size() == 0)
  395. ThrowException( "Unexpected end of file while parsing mesh structure");
  396. else
  397. if( objectName == "}")
  398. break; // mesh finished
  399. else
  400. if( objectName == "MeshNormals")
  401. ParseDataObjectMeshNormals( pMesh);
  402. else
  403. if( objectName == "MeshTextureCoords")
  404. ParseDataObjectMeshTextureCoords( pMesh);
  405. else
  406. if( objectName == "MeshVertexColors")
  407. ParseDataObjectMeshVertexColors( pMesh);
  408. else
  409. if( objectName == "MeshMaterialList")
  410. ParseDataObjectMeshMaterialList( pMesh);
  411. else
  412. if( objectName == "VertexDuplicationIndices")
  413. ParseUnknownDataObject(); // we'll ignore vertex duplication indices
  414. else
  415. if( objectName == "XSkinMeshHeader")
  416. ParseDataObjectSkinMeshHeader( pMesh);
  417. else
  418. if( objectName == "SkinWeights")
  419. ParseDataObjectSkinWeights( pMesh);
  420. else
  421. {
  422. DefaultLogger::get()->warn("Unknown data object in mesh in x file");
  423. ParseUnknownDataObject();
  424. }
  425. }
  426. }
  427. // ------------------------------------------------------------------------------------------------
  428. void XFileParser::ParseDataObjectSkinWeights( Mesh *pMesh)
  429. {
  430. readHeadOfDataObject();
  431. std::string transformNodeName;
  432. GetNextTokenAsString( transformNodeName);
  433. pMesh->mBones.push_back( Bone());
  434. Bone& bone = pMesh->mBones.back();
  435. bone.mName = transformNodeName;
  436. // read vertex weights
  437. unsigned int numWeights = ReadInt();
  438. bone.mWeights.reserve( numWeights);
  439. for( unsigned int a = 0; a < numWeights; a++)
  440. {
  441. BoneWeight weight;
  442. weight.mVertex = ReadInt();
  443. bone.mWeights.push_back( weight);
  444. }
  445. // read vertex weights
  446. for( unsigned int a = 0; a < numWeights; a++)
  447. bone.mWeights[a].mWeight = ReadFloat();
  448. // read matrix offset
  449. bone.mOffsetMatrix.a1 = ReadFloat(); bone.mOffsetMatrix.b1 = ReadFloat();
  450. bone.mOffsetMatrix.c1 = ReadFloat(); bone.mOffsetMatrix.d1 = ReadFloat();
  451. bone.mOffsetMatrix.a2 = ReadFloat(); bone.mOffsetMatrix.b2 = ReadFloat();
  452. bone.mOffsetMatrix.c2 = ReadFloat(); bone.mOffsetMatrix.d2 = ReadFloat();
  453. bone.mOffsetMatrix.a3 = ReadFloat(); bone.mOffsetMatrix.b3 = ReadFloat();
  454. bone.mOffsetMatrix.c3 = ReadFloat(); bone.mOffsetMatrix.d3 = ReadFloat();
  455. bone.mOffsetMatrix.a4 = ReadFloat(); bone.mOffsetMatrix.b4 = ReadFloat();
  456. bone.mOffsetMatrix.c4 = ReadFloat(); bone.mOffsetMatrix.d4 = ReadFloat();
  457. CheckForSemicolon();
  458. CheckForClosingBrace();
  459. }
  460. // ------------------------------------------------------------------------------------------------
  461. void XFileParser::ParseDataObjectSkinMeshHeader( Mesh* pMesh)
  462. {
  463. readHeadOfDataObject();
  464. /*unsigned int maxSkinWeightsPerVertex =*/ ReadInt();
  465. /*unsigned int maxSkinWeightsPerFace =*/ ReadInt();
  466. /*unsigned int numBonesInMesh = */ReadInt();
  467. CheckForClosingBrace();
  468. }
  469. // ------------------------------------------------------------------------------------------------
  470. void XFileParser::ParseDataObjectMeshNormals( Mesh* pMesh)
  471. {
  472. readHeadOfDataObject();
  473. // read count
  474. unsigned int numNormals = ReadInt();
  475. pMesh->mNormals.resize( numNormals);
  476. // read normal vectors
  477. for( unsigned int a = 0; a < numNormals; a++)
  478. pMesh->mNormals[a] = ReadVector3();
  479. // read normal indices
  480. unsigned int numFaces = ReadInt();
  481. if( numFaces != pMesh->mPosFaces.size())
  482. ThrowException( "Normal face count does not match vertex face count.");
  483. for( unsigned int a = 0; a < numFaces; a++)
  484. {
  485. unsigned int numIndices = ReadInt();
  486. pMesh->mNormFaces.push_back( Face());
  487. Face& face = pMesh->mNormFaces.back();
  488. for( unsigned int b = 0; b < numIndices; b++)
  489. face.mIndices.push_back( ReadInt());
  490. CheckForSeparator();
  491. }
  492. CheckForClosingBrace();
  493. }
  494. // ------------------------------------------------------------------------------------------------
  495. void XFileParser::ParseDataObjectMeshTextureCoords( Mesh* pMesh)
  496. {
  497. readHeadOfDataObject();
  498. std::vector<aiVector2D>& coords = pMesh->mTexCoords[pMesh->mNumTextures++];
  499. unsigned int numCoords = ReadInt();
  500. if( numCoords != pMesh->mPositions.size())
  501. ThrowException( "Texture coord count does not match vertex count");
  502. coords.resize( numCoords);
  503. for( unsigned int a = 0; a < numCoords; a++)
  504. coords[a] = ReadVector2();
  505. CheckForClosingBrace();
  506. }
  507. // ------------------------------------------------------------------------------------------------
  508. void XFileParser::ParseDataObjectMeshVertexColors( Mesh* pMesh)
  509. {
  510. readHeadOfDataObject();
  511. std::vector<aiColor4D>& colors = pMesh->mColors[pMesh->mNumColorSets++];
  512. unsigned int numColors = ReadInt();
  513. if( numColors != pMesh->mPositions.size())
  514. ThrowException( "Vertex color count does not match vertex count");
  515. colors.resize( numColors, aiColor4D( 0, 0, 0, 1));
  516. for( unsigned int a = 0; a < numColors; a++)
  517. {
  518. unsigned int index = ReadInt();
  519. if( index >= pMesh->mPositions.size())
  520. ThrowException( "Vertex color index out of bounds");
  521. colors[index] = ReadRGBA();
  522. // HACK: (thom) Maxon Cinema XPort plugin puts a third separator here, kwxPort puts a comma.
  523. // Ignore gracefully.
  524. if( !mIsBinaryFormat)
  525. {
  526. FindNextNoneWhiteSpace();
  527. if( *P == ';' || *P == ',')
  528. P++;
  529. }
  530. }
  531. CheckForClosingBrace();
  532. }
  533. // ------------------------------------------------------------------------------------------------
  534. void XFileParser::ParseDataObjectMeshMaterialList( Mesh* pMesh)
  535. {
  536. readHeadOfDataObject();
  537. // read material count
  538. /*unsigned int numMaterials =*/ ReadInt();
  539. // read non triangulated face material index count
  540. unsigned int numMatIndices = ReadInt();
  541. // some models have a material index count of 1... to be able to read them we
  542. // replicate this single material index on every face
  543. if( numMatIndices != pMesh->mPosFaces.size() && numMatIndices != 1)
  544. ThrowException( "Per-Face material index count does not match face count.");
  545. // read per-face material indices
  546. for( unsigned int a = 0; a < numMatIndices; a++)
  547. pMesh->mFaceMaterials.push_back( ReadInt());
  548. // in version 03.02, the face indices end with two semicolons.
  549. // commented out version check, as version 03.03 exported from blender also has 2 semicolons
  550. if( !mIsBinaryFormat) // && MajorVersion == 3 && MinorVersion <= 2)
  551. {
  552. if( *P == ';')
  553. ++P;
  554. }
  555. // if there was only a single material index, replicate it on all faces
  556. while( pMesh->mFaceMaterials.size() < pMesh->mPosFaces.size())
  557. pMesh->mFaceMaterials.push_back( pMesh->mFaceMaterials.front());
  558. // read following data objects
  559. while(true)
  560. {
  561. std::string objectName = GetNextToken();
  562. if( objectName.size() == 0)
  563. ThrowException( "Unexpected end of file while parsing mesh material list.");
  564. else
  565. if( objectName == "}")
  566. break; // material list finished
  567. else
  568. if( objectName == "{")
  569. {
  570. // template materials
  571. std::string matName = GetNextToken();
  572. Material material;
  573. material.mIsReference = true;
  574. material.mName = matName;
  575. pMesh->mMaterials.push_back( material);
  576. CheckForClosingBrace(); // skip }
  577. } else
  578. if( objectName == "Material")
  579. {
  580. pMesh->mMaterials.push_back( Material());
  581. ParseDataObjectMaterial( &pMesh->mMaterials.back());
  582. } else
  583. if( objectName == ";")
  584. {
  585. // ignore
  586. } else
  587. {
  588. DefaultLogger::get()->warn("Unknown data object in material list in x file");
  589. ParseUnknownDataObject();
  590. }
  591. }
  592. }
  593. // ------------------------------------------------------------------------------------------------
  594. void XFileParser::ParseDataObjectMaterial( Material* pMaterial)
  595. {
  596. std::string matName;
  597. readHeadOfDataObject( &matName);
  598. pMaterial->mName = matName;
  599. pMaterial->mIsReference = false;
  600. // read material values
  601. pMaterial->mDiffuse = ReadRGBA();
  602. pMaterial->mSpecularExponent = ReadFloat();
  603. pMaterial->mSpecular = ReadRGB();
  604. pMaterial->mEmissive = ReadRGB();
  605. // read other data objects
  606. while(true)
  607. {
  608. std::string objectName = GetNextToken();
  609. if( objectName.size() == 0)
  610. ThrowException( "Unexpected end of file while parsing mesh material");
  611. else
  612. if( objectName == "}")
  613. break; // material finished
  614. else
  615. if( objectName == "TextureFilename" || objectName == "TextureFileName")
  616. {
  617. // some exporters write "TextureFileName" instead.
  618. std::string texname;
  619. ParseDataObjectTextureFilename( texname);
  620. pMaterial->mTextures.push_back( TexEntry( texname));
  621. } else
  622. if( objectName == "NormalmapFilename" || objectName == "NormalmapFileName")
  623. {
  624. // one exporter writes out the normal map in a separate filename tag
  625. std::string texname;
  626. ParseDataObjectTextureFilename( texname);
  627. pMaterial->mTextures.push_back( TexEntry( texname, true));
  628. } else
  629. {
  630. DefaultLogger::get()->warn("Unknown data object in material in x file");
  631. ParseUnknownDataObject();
  632. }
  633. }
  634. }
  635. // ------------------------------------------------------------------------------------------------
  636. void XFileParser::ParseDataObjectAnimTicksPerSecond()
  637. {
  638. readHeadOfDataObject();
  639. mScene->mAnimTicksPerSecond = ReadInt();
  640. CheckForClosingBrace();
  641. }
  642. // ------------------------------------------------------------------------------------------------
  643. void XFileParser::ParseDataObjectAnimationSet()
  644. {
  645. std::string animName;
  646. readHeadOfDataObject( &animName);
  647. Animation* anim = new Animation;
  648. mScene->mAnims.push_back( anim);
  649. anim->mName = animName;
  650. while(true)
  651. {
  652. std::string objectName = GetNextToken();
  653. if( objectName.length() == 0)
  654. ThrowException( "Unexpected end of file while parsing animation set.");
  655. else
  656. if( objectName == "}")
  657. break; // animation set finished
  658. else
  659. if( objectName == "Animation")
  660. ParseDataObjectAnimation( anim);
  661. else
  662. {
  663. DefaultLogger::get()->warn("Unknown data object in animation set in x file");
  664. ParseUnknownDataObject();
  665. }
  666. }
  667. }
  668. // ------------------------------------------------------------------------------------------------
  669. void XFileParser::ParseDataObjectAnimation( Animation* pAnim)
  670. {
  671. readHeadOfDataObject();
  672. AnimBone* banim = new AnimBone;
  673. pAnim->mAnims.push_back( banim);
  674. while(true)
  675. {
  676. std::string objectName = GetNextToken();
  677. if( objectName.length() == 0)
  678. ThrowException( "Unexpected end of file while parsing animation.");
  679. else
  680. if( objectName == "}")
  681. break; // animation finished
  682. else
  683. if( objectName == "AnimationKey")
  684. ParseDataObjectAnimationKey( banim);
  685. else
  686. if( objectName == "AnimationOptions")
  687. ParseUnknownDataObject(); // not interested
  688. else
  689. if( objectName == "{")
  690. {
  691. // read frame name
  692. banim->mBoneName = GetNextToken();
  693. CheckForClosingBrace();
  694. } else
  695. {
  696. DefaultLogger::get()->warn("Unknown data object in animation in x file");
  697. ParseUnknownDataObject();
  698. }
  699. }
  700. }
  701. // ------------------------------------------------------------------------------------------------
  702. void XFileParser::ParseDataObjectAnimationKey( AnimBone* pAnimBone)
  703. {
  704. readHeadOfDataObject();
  705. // read key type
  706. unsigned int keyType = ReadInt();
  707. // read number of keys
  708. unsigned int numKeys = ReadInt();
  709. for( unsigned int a = 0; a < numKeys; a++)
  710. {
  711. // read time
  712. unsigned int time = ReadInt();
  713. // read keys
  714. switch( keyType)
  715. {
  716. case 0: // rotation quaternion
  717. {
  718. // read count
  719. if( ReadInt() != 4)
  720. ThrowException( "Invalid number of arguments for quaternion key in animation");
  721. aiQuatKey key;
  722. key.mTime = double( time);
  723. key.mValue.w = ReadFloat();
  724. key.mValue.x = ReadFloat();
  725. key.mValue.y = ReadFloat();
  726. key.mValue.z = ReadFloat();
  727. pAnimBone->mRotKeys.push_back( key);
  728. CheckForSemicolon();
  729. break;
  730. }
  731. case 1: // scale vector
  732. case 2: // position vector
  733. {
  734. // read count
  735. if( ReadInt() != 3)
  736. ThrowException( "Invalid number of arguments for vector key in animation");
  737. aiVectorKey key;
  738. key.mTime = double( time);
  739. key.mValue = ReadVector3();
  740. if( keyType == 2)
  741. pAnimBone->mPosKeys.push_back( key);
  742. else
  743. pAnimBone->mScaleKeys.push_back( key);
  744. break;
  745. }
  746. case 3: // combined transformation matrix
  747. case 4: // denoted both as 3 or as 4
  748. {
  749. // read count
  750. if( ReadInt() != 16)
  751. ThrowException( "Invalid number of arguments for matrix key in animation");
  752. // read matrix
  753. MatrixKey key;
  754. key.mTime = double( time);
  755. key.mMatrix.a1 = ReadFloat(); key.mMatrix.b1 = ReadFloat();
  756. key.mMatrix.c1 = ReadFloat(); key.mMatrix.d1 = ReadFloat();
  757. key.mMatrix.a2 = ReadFloat(); key.mMatrix.b2 = ReadFloat();
  758. key.mMatrix.c2 = ReadFloat(); key.mMatrix.d2 = ReadFloat();
  759. key.mMatrix.a3 = ReadFloat(); key.mMatrix.b3 = ReadFloat();
  760. key.mMatrix.c3 = ReadFloat(); key.mMatrix.d3 = ReadFloat();
  761. key.mMatrix.a4 = ReadFloat(); key.mMatrix.b4 = ReadFloat();
  762. key.mMatrix.c4 = ReadFloat(); key.mMatrix.d4 = ReadFloat();
  763. pAnimBone->mTrafoKeys.push_back( key);
  764. CheckForSemicolon();
  765. break;
  766. }
  767. default:
  768. ThrowException( boost::str( boost::format( "Unknown key type %1% in animation.") % keyType));
  769. break;
  770. } // end switch
  771. // key separator
  772. CheckForSeparator();
  773. }
  774. CheckForClosingBrace();
  775. }
  776. // ------------------------------------------------------------------------------------------------
  777. void XFileParser::ParseDataObjectTextureFilename( std::string& pName)
  778. {
  779. readHeadOfDataObject();
  780. GetNextTokenAsString( pName);
  781. CheckForClosingBrace();
  782. // FIX: some files (e.g. AnimationTest.x) have "" as texture file name
  783. if (!pName.length())
  784. {
  785. DefaultLogger::get()->warn("Length of texture file name is zero. Skipping this texture.");
  786. }
  787. // some exporters write double backslash paths out. We simply replace them if we find them
  788. while( pName.find( "\\\\") != std::string::npos)
  789. pName.replace( pName.find( "\\\\"), 2, "\\");
  790. }
  791. // ------------------------------------------------------------------------------------------------
  792. void XFileParser::ParseUnknownDataObject()
  793. {
  794. // find opening delimiter
  795. while( true)
  796. {
  797. std::string t = GetNextToken();
  798. if( t.length() == 0)
  799. ThrowException( "Unexpected end of file while parsing unknown segment.");
  800. if( t == "{")
  801. break;
  802. }
  803. unsigned int counter = 1;
  804. // parse until closing delimiter
  805. while( counter > 0)
  806. {
  807. std::string t = GetNextToken();
  808. if( t.length() == 0)
  809. ThrowException( "Unexpected end of file while parsing unknown segment.");
  810. if( t == "{")
  811. ++counter;
  812. else
  813. if( t == "}")
  814. --counter;
  815. }
  816. }
  817. // ------------------------------------------------------------------------------------------------
  818. //! checks for closing curly brace
  819. void XFileParser::CheckForClosingBrace()
  820. {
  821. if( GetNextToken() != "}")
  822. ThrowException( "Closing brace expected.");
  823. }
  824. // ------------------------------------------------------------------------------------------------
  825. //! checks for one following semicolon
  826. void XFileParser::CheckForSemicolon()
  827. {
  828. if( mIsBinaryFormat)
  829. return;
  830. if( GetNextToken() != ";")
  831. ThrowException( "Semicolon expected.");
  832. }
  833. // ------------------------------------------------------------------------------------------------
  834. //! checks for a separator char, either a ',' or a ';'
  835. void XFileParser::CheckForSeparator()
  836. {
  837. if( mIsBinaryFormat)
  838. return;
  839. std::string token = GetNextToken();
  840. if( token != "," && token != ";")
  841. ThrowException( "Separator character (';' or ',') expected.");
  842. }
  843. // ------------------------------------------------------------------------------------------------
  844. void XFileParser::readHeadOfDataObject( std::string* poName)
  845. {
  846. std::string nameOrBrace = GetNextToken();
  847. if( nameOrBrace != "{")
  848. {
  849. if( poName)
  850. *poName = nameOrBrace;
  851. if( GetNextToken() != "{")
  852. ThrowException( "Opening brace expected.");
  853. }
  854. }
  855. // ------------------------------------------------------------------------------------------------
  856. std::string XFileParser::GetNextToken()
  857. {
  858. std::string s;
  859. // process binary-formatted file
  860. if( mIsBinaryFormat)
  861. {
  862. // in binary mode it will only return NAME and STRING token
  863. // and (correctly) skip over other tokens.
  864. unsigned int tok = ReadBinWord();
  865. unsigned int len;
  866. // standalone tokens
  867. switch( tok)
  868. {
  869. case 1:
  870. // name token
  871. len = ReadBinDWord();
  872. s = std::string(P, len);
  873. P += len;
  874. return s;
  875. case 2:
  876. // string token
  877. len = ReadBinDWord();
  878. s = std::string(P, len);
  879. P += (len + 2);
  880. return s;
  881. case 3:
  882. // integer token
  883. P += 4;
  884. return "<integer>";
  885. case 5:
  886. // GUID token
  887. P += 16;
  888. return "<guid>";
  889. case 6:
  890. len = ReadBinDWord();
  891. P += (len * 4);
  892. return "<int_list>";
  893. case 7:
  894. len = ReadBinDWord();
  895. P += (len * mBinaryFloatSize);
  896. return "<flt_list>";
  897. case 0x0a:
  898. return "{";
  899. case 0x0b:
  900. return "}";
  901. case 0x0c:
  902. return "(";
  903. case 0x0d:
  904. return ")";
  905. case 0x0e:
  906. return "[";
  907. case 0x0f:
  908. return "]";
  909. case 0x10:
  910. return "<";
  911. case 0x11:
  912. return ">";
  913. case 0x12:
  914. return ".";
  915. case 0x13:
  916. return ",";
  917. case 0x14:
  918. return ";";
  919. case 0x1f:
  920. return "template";
  921. case 0x28:
  922. return "WORD";
  923. case 0x29:
  924. return "DWORD";
  925. case 0x2a:
  926. return "FLOAT";
  927. case 0x2b:
  928. return "DOUBLE";
  929. case 0x2c:
  930. return "CHAR";
  931. case 0x2d:
  932. return "UCHAR";
  933. case 0x2e:
  934. return "SWORD";
  935. case 0x2f:
  936. return "SDWORD";
  937. case 0x30:
  938. return "void";
  939. case 0x31:
  940. return "string";
  941. case 0x32:
  942. return "unicode";
  943. case 0x33:
  944. return "cstring";
  945. case 0x34:
  946. return "array";
  947. }
  948. }
  949. // process text-formatted file
  950. else
  951. {
  952. FindNextNoneWhiteSpace();
  953. if( P >= End)
  954. return s;
  955. while( (P < End) && !isspace( (unsigned char) *P))
  956. {
  957. // either keep token delimiters when already holding a token, or return if first valid char
  958. if( *P == ';' || *P == '}' || *P == '{' || *P == ',')
  959. {
  960. if( !s.size())
  961. s.append( P++, 1);
  962. break; // stop for delimiter
  963. }
  964. s.append( P++, 1);
  965. }
  966. }
  967. return s;
  968. }
  969. // ------------------------------------------------------------------------------------------------
  970. void XFileParser::FindNextNoneWhiteSpace()
  971. {
  972. if( mIsBinaryFormat)
  973. return;
  974. while( true)
  975. {
  976. while( P < End && isspace( (unsigned char) *P))
  977. {
  978. if( *P == '\n')
  979. mLineNumber++;
  980. ++P;
  981. }
  982. if( P >= End)
  983. return;
  984. // check if this is a comment
  985. if( (P[0] == '/' && P[1] == '/') || P[0] == '#')
  986. ReadUntilEndOfLine();
  987. else
  988. break;
  989. }
  990. }
  991. // ------------------------------------------------------------------------------------------------
  992. void XFileParser::GetNextTokenAsString( std::string& poString)
  993. {
  994. if( mIsBinaryFormat)
  995. {
  996. poString = GetNextToken();
  997. return;
  998. }
  999. FindNextNoneWhiteSpace();
  1000. if( P >= End)
  1001. ThrowException( "Unexpected end of file while parsing string");
  1002. if( *P != '"')
  1003. ThrowException( "Expected quotation mark.");
  1004. ++P;
  1005. while( P < End && *P != '"')
  1006. poString.append( P++, 1);
  1007. if( P >= End-1)
  1008. ThrowException( "Unexpected end of file while parsing string");
  1009. if( P[1] != ';' || P[0] != '"')
  1010. ThrowException( "Expected quotation mark and semicolon at the end of a string.");
  1011. P+=2;
  1012. }
  1013. // ------------------------------------------------------------------------------------------------
  1014. void XFileParser::ReadUntilEndOfLine()
  1015. {
  1016. if( mIsBinaryFormat)
  1017. return;
  1018. while( P < End)
  1019. {
  1020. if( *P == '\n' || *P == '\r')
  1021. {
  1022. ++P; mLineNumber++;
  1023. return;
  1024. }
  1025. ++P;
  1026. }
  1027. }
  1028. // ------------------------------------------------------------------------------------------------
  1029. unsigned short XFileParser::ReadBinWord()
  1030. {
  1031. const unsigned char* q = (const unsigned char*) P;
  1032. unsigned short tmp = q[0] | (q[1] << 8);
  1033. P += 2;
  1034. return tmp;
  1035. }
  1036. // ------------------------------------------------------------------------------------------------
  1037. unsigned int XFileParser::ReadBinDWord()
  1038. {
  1039. const unsigned char* q = (const unsigned char*) P;
  1040. unsigned int tmp = q[0] | (q[1] << 8) | (q[2] << 16) | (q[3] << 24);
  1041. P += 4;
  1042. return tmp;
  1043. }
  1044. // ------------------------------------------------------------------------------------------------
  1045. unsigned int XFileParser::ReadInt()
  1046. {
  1047. if( mIsBinaryFormat)
  1048. {
  1049. if( mBinaryNumCount == 0)
  1050. {
  1051. unsigned short tmp = ReadBinWord(); // 0x06 or 0x03
  1052. if( tmp == 0x06) // array of ints follows
  1053. mBinaryNumCount = ReadBinDWord();
  1054. else // single int follows
  1055. mBinaryNumCount = 1;
  1056. }
  1057. --mBinaryNumCount;
  1058. return ReadBinDWord();
  1059. } else
  1060. {
  1061. FindNextNoneWhiteSpace();
  1062. // TODO: consider using strtol10s instead???
  1063. // check preceeding minus sign
  1064. bool isNegative = false;
  1065. if( *P == '-')
  1066. {
  1067. isNegative = true;
  1068. P++;
  1069. }
  1070. // at least one digit expected
  1071. if( !isdigit( *P))
  1072. ThrowException( "Number expected.");
  1073. // read digits
  1074. unsigned int number = 0;
  1075. while( P < End)
  1076. {
  1077. if( !isdigit( *P))
  1078. break;
  1079. number = number * 10 + (*P - 48);
  1080. P++;
  1081. }
  1082. CheckForSeparator();
  1083. return isNegative ? ((unsigned int) -int( number)) : number;
  1084. }
  1085. }
  1086. // ------------------------------------------------------------------------------------------------
  1087. float XFileParser::ReadFloat()
  1088. {
  1089. if( mIsBinaryFormat)
  1090. {
  1091. if( mBinaryNumCount == 0)
  1092. {
  1093. unsigned short tmp = ReadBinWord(); // 0x07 or 0x42
  1094. if( tmp == 0x07) // array of floats following
  1095. mBinaryNumCount = ReadBinDWord();
  1096. else // single float following
  1097. mBinaryNumCount = 1;
  1098. }
  1099. --mBinaryNumCount;
  1100. if( mBinaryFloatSize == 8)
  1101. {
  1102. float result = (float) (*(double*) P);
  1103. P += 8;
  1104. return result;
  1105. } else
  1106. {
  1107. float result = *(float*) P;
  1108. P += 4;
  1109. return result;
  1110. }
  1111. }
  1112. // text version
  1113. FindNextNoneWhiteSpace();
  1114. // check for various special strings to allow reading files from faulty exporters
  1115. // I mean you, Blender!
  1116. if( strncmp( P, "-1.#IND00", 9) == 0 || strncmp( P, "1.#IND00", 8) == 0)
  1117. {
  1118. P += 9;
  1119. CheckForSeparator();
  1120. return 0.0f;
  1121. } else
  1122. if( strncmp( P, "1.#QNAN0", 8) == 0)
  1123. {
  1124. P += 8;
  1125. CheckForSeparator();
  1126. return 0.0f;
  1127. }
  1128. float result = 0.0f;
  1129. P = fast_atof_move( P, result);
  1130. CheckForSeparator();
  1131. return result;
  1132. }
  1133. // ------------------------------------------------------------------------------------------------
  1134. aiVector2D XFileParser::ReadVector2()
  1135. {
  1136. aiVector2D vector;
  1137. vector.x = ReadFloat();
  1138. vector.y = ReadFloat();
  1139. CheckForSeparator();
  1140. return vector;
  1141. }
  1142. // ------------------------------------------------------------------------------------------------
  1143. aiVector3D XFileParser::ReadVector3()
  1144. {
  1145. aiVector3D vector;
  1146. vector.x = ReadFloat();
  1147. vector.y = ReadFloat();
  1148. vector.z = ReadFloat();
  1149. CheckForSeparator();
  1150. return vector;
  1151. }
  1152. // ------------------------------------------------------------------------------------------------
  1153. aiColor4D XFileParser::ReadRGBA()
  1154. {
  1155. aiColor4D color;
  1156. color.r = ReadFloat();
  1157. color.g = ReadFloat();
  1158. color.b = ReadFloat();
  1159. color.a = ReadFloat();
  1160. CheckForSeparator();
  1161. return color;
  1162. }
  1163. // ------------------------------------------------------------------------------------------------
  1164. aiColor3D XFileParser::ReadRGB()
  1165. {
  1166. aiColor3D color;
  1167. color.r = ReadFloat();
  1168. color.g = ReadFloat();
  1169. color.b = ReadFloat();
  1170. CheckForSeparator();
  1171. return color;
  1172. }
  1173. // ------------------------------------------------------------------------------------------------
  1174. // Throws an exception with a line number and the given text.
  1175. void XFileParser::ThrowException( const std::string& pText)
  1176. {
  1177. if( mIsBinaryFormat)
  1178. throw new ImportErrorException( pText);
  1179. else
  1180. throw new ImportErrorException( boost::str( boost::format( "Line %d: %s") % mLineNumber % pText));
  1181. }
  1182. // ------------------------------------------------------------------------------------------------
  1183. // Filters the imported hierarchy for some degenerated cases that some exporters produce.
  1184. void XFileParser::FilterHierarchy( XFile::Node* pNode)
  1185. {
  1186. // if the node has just a single unnamed child containing a mesh, remove
  1187. // the anonymous node inbetween. The 3DSMax kwXport plugin seems to produce this
  1188. // mess in some cases
  1189. if( pNode->mChildren.size() == 1)
  1190. {
  1191. XFile::Node* child = pNode->mChildren.front();
  1192. if( child->mName.length() == 0 && child->mMeshes.size() > 0)
  1193. {
  1194. // transfer its meshes to us
  1195. for( unsigned int a = 0; a < child->mMeshes.size(); a++)
  1196. pNode->mMeshes.push_back( child->mMeshes[a]);
  1197. child->mMeshes.clear();
  1198. // then kill it
  1199. delete child;
  1200. pNode->mChildren.clear();
  1201. }
  1202. }
  1203. // recurse
  1204. for( unsigned int a = 0; a < pNode->mChildren.size(); a++)
  1205. FilterHierarchy( pNode->mChildren[a]);
  1206. }
  1207. #endif // !! ASSIMP_BUILD_NO_X_IMPORTER