XFileParser.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. // Destructor. Destroys all imported data along with it
  208. XFileParser::~XFileParser()
  209. {
  210. // kill everything we created
  211. delete mScene;
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. void XFileParser::ParseFile()
  215. {
  216. while( 1)
  217. {
  218. // read name of next object
  219. std::string objectName = GetNextToken();
  220. if (objectName.length() == 0)
  221. break;
  222. // parse specific object
  223. if( objectName == "template")
  224. ParseDataObjectTemplate();
  225. else
  226. if( objectName == "Frame")
  227. ParseDataObjectFrame( NULL);
  228. else
  229. if( objectName == "Mesh")
  230. {
  231. // some meshes have no frames at all
  232. Mesh* mesh = new Mesh;
  233. ParseDataObjectMesh( mesh);
  234. mScene->mGlobalMeshes.push_back( mesh);
  235. } else
  236. if( objectName == "AnimTicksPerSecond")
  237. ParseDataObjectAnimTicksPerSecond();
  238. else
  239. if( objectName == "AnimationSet")
  240. ParseDataObjectAnimationSet();
  241. else
  242. if( objectName == "Material")
  243. {
  244. // Material outside of a mesh or node
  245. Material material;
  246. ParseDataObjectMaterial( &material);
  247. mScene->mGlobalMaterials.push_back( material);
  248. } else
  249. if( objectName == "}")
  250. {
  251. // whatever?
  252. DefaultLogger::get()->warn("} found in dataObject");
  253. } else
  254. {
  255. // unknown format
  256. DefaultLogger::get()->warn("Unknown data object in animation of .x file");
  257. ParseUnknownDataObject();
  258. }
  259. }
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. void XFileParser::ParseDataObjectTemplate()
  263. {
  264. // parse a template data object. Currently not stored.
  265. std::string name;
  266. readHeadOfDataObject( &name);
  267. // read GUID
  268. std::string guid = GetNextToken();
  269. // read and ignore data members
  270. while(true)
  271. {
  272. std::string s = GetNextToken();
  273. if( s == "}")
  274. break;
  275. if( s.length() == 0)
  276. ThrowException( "Unexpected end of file reached while parsing template definition");
  277. }
  278. }
  279. // ------------------------------------------------------------------------------------------------
  280. void XFileParser::ParseDataObjectFrame( Node* pParent)
  281. {
  282. // A coordinate frame, or "frame of reference." The Frame template
  283. // is open and can contain any object. The Direct3D extensions (D3DX)
  284. // mesh-loading functions recognize Mesh, FrameTransformMatrix, and
  285. // Frame template instances as child objects when loading a Frame
  286. // instance.
  287. std::string name;
  288. readHeadOfDataObject(&name);
  289. // create a named node and place it at its parent, if given
  290. Node* node = new Node( pParent);
  291. node->mName = name;
  292. if( pParent)
  293. {
  294. pParent->mChildren.push_back( node);
  295. } else
  296. {
  297. // there might be multiple root nodes
  298. if( mScene->mRootNode != NULL)
  299. {
  300. // place a dummy root if not there
  301. if( mScene->mRootNode->mName != "$dummy_root")
  302. {
  303. Node* exroot = mScene->mRootNode;
  304. mScene->mRootNode = new Node( NULL);
  305. mScene->mRootNode->mName = "$dummy_root";
  306. mScene->mRootNode->mChildren.push_back( exroot);
  307. exroot->mParent = mScene->mRootNode;
  308. }
  309. // put the new node as its child instead
  310. mScene->mRootNode->mChildren.push_back( node);
  311. node->mParent = mScene->mRootNode;
  312. } else
  313. {
  314. // it's the first node imported. place it as root
  315. mScene->mRootNode = node;
  316. }
  317. }
  318. // Now inside a frame.
  319. // read tokens until closing brace is reached.
  320. while(true)
  321. {
  322. std::string objectName = GetNextToken();
  323. if (objectName.size() == 0)
  324. ThrowException( "Unexpected end of file reached while parsing frame");
  325. if( objectName == "}")
  326. break; // frame finished
  327. else
  328. if( objectName == "Frame")
  329. ParseDataObjectFrame( node); // child frame
  330. else
  331. if( objectName == "FrameTransformMatrix")
  332. ParseDataObjectTransformationMatrix( node->mTrafoMatrix);
  333. else
  334. if( objectName == "Mesh")
  335. {
  336. Mesh* mesh = new Mesh;
  337. node->mMeshes.push_back( mesh);
  338. ParseDataObjectMesh( mesh);
  339. } else
  340. {
  341. DefaultLogger::get()->warn("Unknown data object in frame in x file");
  342. ParseUnknownDataObject();
  343. }
  344. }
  345. }
  346. // ------------------------------------------------------------------------------------------------
  347. void XFileParser::ParseDataObjectTransformationMatrix( aiMatrix4x4& pMatrix)
  348. {
  349. // read header, we're not interested if it has a name
  350. readHeadOfDataObject();
  351. // read its components
  352. pMatrix.a1 = ReadFloat(); pMatrix.b1 = ReadFloat();
  353. pMatrix.c1 = ReadFloat(); pMatrix.d1 = ReadFloat();
  354. pMatrix.a2 = ReadFloat(); pMatrix.b2 = ReadFloat();
  355. pMatrix.c2 = ReadFloat(); pMatrix.d2 = ReadFloat();
  356. pMatrix.a3 = ReadFloat(); pMatrix.b3 = ReadFloat();
  357. pMatrix.c3 = ReadFloat(); pMatrix.d3 = ReadFloat();
  358. pMatrix.a4 = ReadFloat(); pMatrix.b4 = ReadFloat();
  359. pMatrix.c4 = ReadFloat(); pMatrix.d4 = ReadFloat();
  360. // trailing symbols
  361. CheckForSemicolon();
  362. CheckForClosingBrace();
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. void XFileParser::ParseDataObjectMesh( Mesh* pMesh)
  366. {
  367. std::string name;
  368. readHeadOfDataObject( &name);
  369. // read vertex count
  370. unsigned int numVertices = ReadInt();
  371. pMesh->mPositions.resize( numVertices);
  372. // read vertices
  373. for( unsigned int a = 0; a < numVertices; a++)
  374. pMesh->mPositions[a] = ReadVector3();
  375. // read position faces
  376. unsigned int numPosFaces = ReadInt();
  377. pMesh->mPosFaces.resize( numPosFaces);
  378. for( unsigned int a = 0; a < numPosFaces; a++)
  379. {
  380. unsigned int numIndices = ReadInt();
  381. if( numIndices < 3)
  382. ThrowException( boost::str( boost::format( "Invalid index count %1% for face %2%.") % numIndices % a));
  383. // read indices
  384. Face& face = pMesh->mPosFaces[a];
  385. for( unsigned int b = 0; b < numIndices; b++)
  386. face.mIndices.push_back( ReadInt());
  387. CheckForSeparator();
  388. }
  389. // here, other data objects may follow
  390. while(true)
  391. {
  392. std::string objectName = GetNextToken();
  393. if( objectName.size() == 0)
  394. ThrowException( "Unexpected end of file while parsing mesh structure");
  395. else
  396. if( objectName == "}")
  397. break; // mesh finished
  398. else
  399. if( objectName == "MeshNormals")
  400. ParseDataObjectMeshNormals( pMesh);
  401. else
  402. if( objectName == "MeshTextureCoords")
  403. ParseDataObjectMeshTextureCoords( pMesh);
  404. else
  405. if( objectName == "MeshVertexColors")
  406. ParseDataObjectMeshVertexColors( pMesh);
  407. else
  408. if( objectName == "MeshMaterialList")
  409. ParseDataObjectMeshMaterialList( pMesh);
  410. else
  411. if( objectName == "VertexDuplicationIndices")
  412. ParseUnknownDataObject(); // we'll ignore vertex duplication indices
  413. else
  414. if( objectName == "XSkinMeshHeader")
  415. ParseDataObjectSkinMeshHeader( pMesh);
  416. else
  417. if( objectName == "SkinWeights")
  418. ParseDataObjectSkinWeights( pMesh);
  419. else
  420. {
  421. DefaultLogger::get()->warn("Unknown data object in mesh in x file");
  422. ParseUnknownDataObject();
  423. }
  424. }
  425. }
  426. // ------------------------------------------------------------------------------------------------
  427. void XFileParser::ParseDataObjectSkinWeights( Mesh *pMesh)
  428. {
  429. readHeadOfDataObject();
  430. std::string transformNodeName;
  431. GetNextTokenAsString( transformNodeName);
  432. pMesh->mBones.push_back( Bone());
  433. Bone& bone = pMesh->mBones.back();
  434. bone.mName = transformNodeName;
  435. // read vertex weights
  436. unsigned int numWeights = ReadInt();
  437. bone.mWeights.reserve( numWeights);
  438. for( unsigned int a = 0; a < numWeights; a++)
  439. {
  440. BoneWeight weight;
  441. weight.mVertex = ReadInt();
  442. bone.mWeights.push_back( weight);
  443. }
  444. // read vertex weights
  445. for( unsigned int a = 0; a < numWeights; a++)
  446. bone.mWeights[a].mWeight = ReadFloat();
  447. // read matrix offset
  448. bone.mOffsetMatrix.a1 = ReadFloat(); bone.mOffsetMatrix.b1 = ReadFloat();
  449. bone.mOffsetMatrix.c1 = ReadFloat(); bone.mOffsetMatrix.d1 = ReadFloat();
  450. bone.mOffsetMatrix.a2 = ReadFloat(); bone.mOffsetMatrix.b2 = ReadFloat();
  451. bone.mOffsetMatrix.c2 = ReadFloat(); bone.mOffsetMatrix.d2 = ReadFloat();
  452. bone.mOffsetMatrix.a3 = ReadFloat(); bone.mOffsetMatrix.b3 = ReadFloat();
  453. bone.mOffsetMatrix.c3 = ReadFloat(); bone.mOffsetMatrix.d3 = ReadFloat();
  454. bone.mOffsetMatrix.a4 = ReadFloat(); bone.mOffsetMatrix.b4 = ReadFloat();
  455. bone.mOffsetMatrix.c4 = ReadFloat(); bone.mOffsetMatrix.d4 = ReadFloat();
  456. CheckForSemicolon();
  457. CheckForClosingBrace();
  458. }
  459. // ------------------------------------------------------------------------------------------------
  460. void XFileParser::ParseDataObjectSkinMeshHeader( Mesh* pMesh)
  461. {
  462. readHeadOfDataObject();
  463. /*unsigned int maxSkinWeightsPerVertex =*/ ReadInt();
  464. /*unsigned int maxSkinWeightsPerFace =*/ ReadInt();
  465. /*unsigned int numBonesInMesh = */ReadInt();
  466. CheckForClosingBrace();
  467. }
  468. // ------------------------------------------------------------------------------------------------
  469. void XFileParser::ParseDataObjectMeshNormals( Mesh* pMesh)
  470. {
  471. readHeadOfDataObject();
  472. // read count
  473. unsigned int numNormals = ReadInt();
  474. pMesh->mNormals.resize( numNormals);
  475. // read normal vectors
  476. for( unsigned int a = 0; a < numNormals; a++)
  477. pMesh->mNormals[a] = ReadVector3();
  478. // read normal indices
  479. unsigned int numFaces = ReadInt();
  480. if( numFaces != pMesh->mPosFaces.size())
  481. ThrowException( "Normal face count does not match vertex face count.");
  482. for( unsigned int a = 0; a < numFaces; a++)
  483. {
  484. unsigned int numIndices = ReadInt();
  485. pMesh->mNormFaces.push_back( Face());
  486. Face& face = pMesh->mNormFaces.back();
  487. for( unsigned int b = 0; b < numIndices; b++)
  488. face.mIndices.push_back( ReadInt());
  489. CheckForSeparator();
  490. }
  491. CheckForClosingBrace();
  492. }
  493. // ------------------------------------------------------------------------------------------------
  494. void XFileParser::ParseDataObjectMeshTextureCoords( Mesh* pMesh)
  495. {
  496. readHeadOfDataObject();
  497. std::vector<aiVector2D>& coords = pMesh->mTexCoords[pMesh->mNumTextures++];
  498. unsigned int numCoords = ReadInt();
  499. if( numCoords != pMesh->mPositions.size())
  500. ThrowException( "Texture coord count does not match vertex count");
  501. coords.resize( numCoords);
  502. for( unsigned int a = 0; a < numCoords; a++)
  503. coords[a] = ReadVector2();
  504. CheckForClosingBrace();
  505. }
  506. // ------------------------------------------------------------------------------------------------
  507. void XFileParser::ParseDataObjectMeshVertexColors( Mesh* pMesh)
  508. {
  509. readHeadOfDataObject();
  510. std::vector<aiColor4D>& colors = pMesh->mColors[pMesh->mNumColorSets++];
  511. unsigned int numColors = ReadInt();
  512. if( numColors != pMesh->mPositions.size())
  513. ThrowException( "Vertex color count does not match vertex count");
  514. colors.resize( numColors, aiColor4D( 0, 0, 0, 1));
  515. for( unsigned int a = 0; a < numColors; a++)
  516. {
  517. unsigned int index = ReadInt();
  518. if( index >= pMesh->mPositions.size())
  519. ThrowException( "Vertex color index out of bounds");
  520. colors[index] = ReadRGBA();
  521. // HACK: (thom) Maxon Cinema XPort plugin puts a third separator here, kwxPort puts a comma.
  522. // Ignore gracefully.
  523. if( !mIsBinaryFormat)
  524. {
  525. FindNextNoneWhiteSpace();
  526. if( *P == ';' || *P == ',')
  527. P++;
  528. }
  529. }
  530. CheckForClosingBrace();
  531. }
  532. // ------------------------------------------------------------------------------------------------
  533. void XFileParser::ParseDataObjectMeshMaterialList( Mesh* pMesh)
  534. {
  535. readHeadOfDataObject();
  536. // read material count
  537. /*unsigned int numMaterials =*/ ReadInt();
  538. // read non triangulated face material index count
  539. unsigned int numMatIndices = ReadInt();
  540. // some models have a material index count of 1... to be able to read them we
  541. // replicate this single material index on every face
  542. if( numMatIndices != pMesh->mPosFaces.size() && numMatIndices != 1)
  543. ThrowException( "Per-Face material index count does not match face count.");
  544. // read per-face material indices
  545. for( unsigned int a = 0; a < numMatIndices; a++)
  546. pMesh->mFaceMaterials.push_back( ReadInt());
  547. // in version 03.02, the face indices end with two semicolons.
  548. // commented out version check, as version 03.03 exported from blender also has 2 semicolons
  549. if( !mIsBinaryFormat) // && MajorVersion == 3 && MinorVersion <= 2)
  550. {
  551. if( *P == ';')
  552. ++P;
  553. }
  554. // if there was only a single material index, replicate it on all faces
  555. while( pMesh->mFaceMaterials.size() < pMesh->mPosFaces.size())
  556. pMesh->mFaceMaterials.push_back( pMesh->mFaceMaterials.front());
  557. // read following data objects
  558. while(true)
  559. {
  560. std::string objectName = GetNextToken();
  561. if( objectName.size() == 0)
  562. ThrowException( "Unexpected end of file while parsing mesh material list.");
  563. else
  564. if( objectName == "}")
  565. break; // material list finished
  566. else
  567. if( objectName == "{")
  568. {
  569. // template materials
  570. std::string matName = GetNextToken();
  571. Material material;
  572. material.mIsReference = true;
  573. material.mName = matName;
  574. pMesh->mMaterials.push_back( material);
  575. CheckForClosingBrace(); // skip }
  576. } else
  577. if( objectName == "Material")
  578. {
  579. pMesh->mMaterials.push_back( Material());
  580. ParseDataObjectMaterial( &pMesh->mMaterials.back());
  581. } else
  582. if( objectName == ";")
  583. {
  584. // ignore
  585. } else
  586. {
  587. DefaultLogger::get()->warn("Unknown data object in material list in x file");
  588. ParseUnknownDataObject();
  589. }
  590. }
  591. }
  592. // ------------------------------------------------------------------------------------------------
  593. void XFileParser::ParseDataObjectMaterial( Material* pMaterial)
  594. {
  595. std::string matName;
  596. readHeadOfDataObject( &matName);
  597. pMaterial->mName = matName;
  598. pMaterial->mIsReference = false;
  599. // read material values
  600. pMaterial->mDiffuse = ReadRGBA();
  601. pMaterial->mSpecularExponent = ReadFloat();
  602. pMaterial->mSpecular = ReadRGB();
  603. pMaterial->mEmissive = ReadRGB();
  604. // read other data objects
  605. while(true)
  606. {
  607. std::string objectName = GetNextToken();
  608. if( objectName.size() == 0)
  609. ThrowException( "Unexpected end of file while parsing mesh material");
  610. else
  611. if( objectName == "}")
  612. break; // material finished
  613. else
  614. if( objectName == "TextureFilename" || objectName == "TextureFileName")
  615. {
  616. // some exporters write "TextureFileName" instead.
  617. std::string texname;
  618. ParseDataObjectTextureFilename( texname);
  619. pMaterial->mTextures.push_back( TexEntry( texname));
  620. } else
  621. if( objectName == "NormalmapFilename" || objectName == "NormalmapFileName")
  622. {
  623. // one exporter writes out the normal map in a separate filename tag
  624. std::string texname;
  625. ParseDataObjectTextureFilename( texname);
  626. pMaterial->mTextures.push_back( TexEntry( texname, true));
  627. } else
  628. {
  629. DefaultLogger::get()->warn("Unknown data object in material in x file");
  630. ParseUnknownDataObject();
  631. }
  632. }
  633. }
  634. // ------------------------------------------------------------------------------------------------
  635. void XFileParser::ParseDataObjectAnimTicksPerSecond()
  636. {
  637. readHeadOfDataObject();
  638. mScene->mAnimTicksPerSecond = ReadInt();
  639. CheckForClosingBrace();
  640. }
  641. // ------------------------------------------------------------------------------------------------
  642. void XFileParser::ParseDataObjectAnimationSet()
  643. {
  644. std::string animName;
  645. readHeadOfDataObject( &animName);
  646. Animation* anim = new Animation;
  647. mScene->mAnims.push_back( anim);
  648. anim->mName = animName;
  649. while(true)
  650. {
  651. std::string objectName = GetNextToken();
  652. if( objectName.length() == 0)
  653. ThrowException( "Unexpected end of file while parsing animation set.");
  654. else
  655. if( objectName == "}")
  656. break; // animation set finished
  657. else
  658. if( objectName == "Animation")
  659. ParseDataObjectAnimation( anim);
  660. else
  661. {
  662. DefaultLogger::get()->warn("Unknown data object in animation set in x file");
  663. ParseUnknownDataObject();
  664. }
  665. }
  666. }
  667. // ------------------------------------------------------------------------------------------------
  668. void XFileParser::ParseDataObjectAnimation( Animation* pAnim)
  669. {
  670. readHeadOfDataObject();
  671. AnimBone* banim = new AnimBone;
  672. pAnim->mAnims.push_back( banim);
  673. while(true)
  674. {
  675. std::string objectName = GetNextToken();
  676. if( objectName.length() == 0)
  677. ThrowException( "Unexpected end of file while parsing animation.");
  678. else
  679. if( objectName == "}")
  680. break; // animation finished
  681. else
  682. if( objectName == "AnimationKey")
  683. ParseDataObjectAnimationKey( banim);
  684. else
  685. if( objectName == "AnimationOptions")
  686. ParseUnknownDataObject(); // not interested
  687. else
  688. if( objectName == "{")
  689. {
  690. // read frame name
  691. banim->mBoneName = GetNextToken();
  692. CheckForClosingBrace();
  693. } else
  694. {
  695. DefaultLogger::get()->warn("Unknown data object in animation in x file");
  696. ParseUnknownDataObject();
  697. }
  698. }
  699. }
  700. // ------------------------------------------------------------------------------------------------
  701. void XFileParser::ParseDataObjectAnimationKey( AnimBone* pAnimBone)
  702. {
  703. readHeadOfDataObject();
  704. // read key type
  705. unsigned int keyType = ReadInt();
  706. // read number of keys
  707. unsigned int numKeys = ReadInt();
  708. for( unsigned int a = 0; a < numKeys; a++)
  709. {
  710. // read time
  711. unsigned int time = ReadInt();
  712. // read keys
  713. switch( keyType)
  714. {
  715. case 0: // rotation quaternion
  716. {
  717. // read count
  718. if( ReadInt() != 4)
  719. ThrowException( "Invalid number of arguments for quaternion key in animation");
  720. aiQuatKey key;
  721. key.mTime = double( time);
  722. key.mValue.w = ReadFloat();
  723. key.mValue.x = ReadFloat();
  724. key.mValue.y = ReadFloat();
  725. key.mValue.z = ReadFloat();
  726. pAnimBone->mRotKeys.push_back( key);
  727. CheckForSemicolon();
  728. break;
  729. }
  730. case 1: // scale vector
  731. case 2: // position vector
  732. {
  733. // read count
  734. if( ReadInt() != 3)
  735. ThrowException( "Invalid number of arguments for vector key in animation");
  736. aiVectorKey key;
  737. key.mTime = double( time);
  738. key.mValue = ReadVector3();
  739. if( keyType == 2)
  740. pAnimBone->mPosKeys.push_back( key);
  741. else
  742. pAnimBone->mScaleKeys.push_back( key);
  743. break;
  744. }
  745. case 3: // combined transformation matrix
  746. case 4: // denoted both as 3 or as 4
  747. {
  748. // read count
  749. if( ReadInt() != 16)
  750. ThrowException( "Invalid number of arguments for matrix key in animation");
  751. // read matrix
  752. MatrixKey key;
  753. key.mTime = double( time);
  754. key.mMatrix.a1 = ReadFloat(); key.mMatrix.b1 = ReadFloat();
  755. key.mMatrix.c1 = ReadFloat(); key.mMatrix.d1 = ReadFloat();
  756. key.mMatrix.a2 = ReadFloat(); key.mMatrix.b2 = ReadFloat();
  757. key.mMatrix.c2 = ReadFloat(); key.mMatrix.d2 = ReadFloat();
  758. key.mMatrix.a3 = ReadFloat(); key.mMatrix.b3 = ReadFloat();
  759. key.mMatrix.c3 = ReadFloat(); key.mMatrix.d3 = ReadFloat();
  760. key.mMatrix.a4 = ReadFloat(); key.mMatrix.b4 = ReadFloat();
  761. key.mMatrix.c4 = ReadFloat(); key.mMatrix.d4 = ReadFloat();
  762. pAnimBone->mTrafoKeys.push_back( key);
  763. CheckForSemicolon();
  764. break;
  765. }
  766. default:
  767. ThrowException( boost::str( boost::format( "Unknown key type %1% in animation.") % keyType));
  768. break;
  769. } // end switch
  770. // key separator
  771. CheckForSeparator();
  772. }
  773. CheckForClosingBrace();
  774. }
  775. // ------------------------------------------------------------------------------------------------
  776. void XFileParser::ParseDataObjectTextureFilename( std::string& pName)
  777. {
  778. readHeadOfDataObject();
  779. GetNextTokenAsString( pName);
  780. CheckForClosingBrace();
  781. // FIX: some files (e.g. AnimationTest.x) have "" as texture file name
  782. if (!pName.length())
  783. {
  784. DefaultLogger::get()->warn("Length of texture file name is zero. Skipping this texture.");
  785. }
  786. // some exporters write double backslash paths out. We simply replace them if we find them
  787. while( pName.find( "\\\\") != std::string::npos)
  788. pName.replace( pName.find( "\\\\"), 2, "\\");
  789. }
  790. // ------------------------------------------------------------------------------------------------
  791. void XFileParser::ParseUnknownDataObject()
  792. {
  793. // find opening delimiter
  794. while( true)
  795. {
  796. std::string t = GetNextToken();
  797. if( t.length() == 0)
  798. ThrowException( "Unexpected end of file while parsing unknown segment.");
  799. if( t == "{")
  800. break;
  801. }
  802. unsigned int counter = 1;
  803. // parse until closing delimiter
  804. while( counter > 0)
  805. {
  806. std::string t = GetNextToken();
  807. if( t.length() == 0)
  808. ThrowException( "Unexpected end of file while parsing unknown segment.");
  809. if( t == "{")
  810. ++counter;
  811. else
  812. if( t == "}")
  813. --counter;
  814. }
  815. }
  816. // ------------------------------------------------------------------------------------------------
  817. //! checks for closing curly brace
  818. void XFileParser::CheckForClosingBrace()
  819. {
  820. if( GetNextToken() != "}")
  821. ThrowException( "Closing brace expected.");
  822. }
  823. // ------------------------------------------------------------------------------------------------
  824. //! checks for one following semicolon
  825. void XFileParser::CheckForSemicolon()
  826. {
  827. if( mIsBinaryFormat)
  828. return;
  829. if( GetNextToken() != ";")
  830. ThrowException( "Semicolon expected.");
  831. }
  832. // ------------------------------------------------------------------------------------------------
  833. //! checks for a separator char, either a ',' or a ';'
  834. void XFileParser::CheckForSeparator()
  835. {
  836. if( mIsBinaryFormat)
  837. return;
  838. std::string token = GetNextToken();
  839. if( token != "," && token != ";")
  840. ThrowException( "Separator character (';' or ',') expected.");
  841. }
  842. // ------------------------------------------------------------------------------------------------
  843. void XFileParser::readHeadOfDataObject( std::string* poName)
  844. {
  845. std::string nameOrBrace = GetNextToken();
  846. if( nameOrBrace != "{")
  847. {
  848. if( poName)
  849. *poName = nameOrBrace;
  850. if( GetNextToken() != "{")
  851. ThrowException( "Opening brace expected.");
  852. }
  853. }
  854. // ------------------------------------------------------------------------------------------------
  855. std::string XFileParser::GetNextToken()
  856. {
  857. std::string s;
  858. // process binary-formatted file
  859. if( mIsBinaryFormat)
  860. {
  861. // in binary mode it will only return NAME and STRING token
  862. // and (correctly) skip over other tokens.
  863. unsigned int tok = ReadBinWord();
  864. unsigned int len;
  865. // standalone tokens
  866. switch( tok)
  867. {
  868. case 1:
  869. // name token
  870. len = ReadBinDWord();
  871. s = std::string(P, len);
  872. P += len;
  873. return s;
  874. case 2:
  875. // string token
  876. len = ReadBinDWord();
  877. s = std::string(P, len);
  878. P += (len + 2);
  879. return s;
  880. case 3:
  881. // integer token
  882. P += 4;
  883. return "<integer>";
  884. case 5:
  885. // GUID token
  886. P += 16;
  887. return "<guid>";
  888. case 6:
  889. len = ReadBinDWord();
  890. P += (len * 4);
  891. return "<int_list>";
  892. case 7:
  893. len = ReadBinDWord();
  894. P += (len * mBinaryFloatSize);
  895. return "<flt_list>";
  896. case 0x0a:
  897. return "{";
  898. case 0x0b:
  899. return "}";
  900. case 0x0c:
  901. return "(";
  902. case 0x0d:
  903. return ")";
  904. case 0x0e:
  905. return "[";
  906. case 0x0f:
  907. return "]";
  908. case 0x10:
  909. return "<";
  910. case 0x11:
  911. return ">";
  912. case 0x12:
  913. return ".";
  914. case 0x13:
  915. return ",";
  916. case 0x14:
  917. return ";";
  918. case 0x1f:
  919. return "template";
  920. case 0x28:
  921. return "WORD";
  922. case 0x29:
  923. return "DWORD";
  924. case 0x2a:
  925. return "FLOAT";
  926. case 0x2b:
  927. return "DOUBLE";
  928. case 0x2c:
  929. return "CHAR";
  930. case 0x2d:
  931. return "UCHAR";
  932. case 0x2e:
  933. return "SWORD";
  934. case 0x2f:
  935. return "SDWORD";
  936. case 0x30:
  937. return "void";
  938. case 0x31:
  939. return "string";
  940. case 0x32:
  941. return "unicode";
  942. case 0x33:
  943. return "cstring";
  944. case 0x34:
  945. return "array";
  946. }
  947. }
  948. // process text-formatted file
  949. else
  950. {
  951. FindNextNoneWhiteSpace();
  952. if( P >= End)
  953. return s;
  954. while( (P < End) && !isspace( (unsigned char) *P))
  955. {
  956. // either keep token delimiters when already holding a token, or return if first valid char
  957. if( *P == ';' || *P == '}' || *P == '{' || *P == ',')
  958. {
  959. if( !s.size())
  960. s.append( P++, 1);
  961. break; // stop for delimiter
  962. }
  963. s.append( P++, 1);
  964. }
  965. }
  966. return s;
  967. }
  968. // ------------------------------------------------------------------------------------------------
  969. void XFileParser::FindNextNoneWhiteSpace()
  970. {
  971. if( mIsBinaryFormat)
  972. return;
  973. while( true)
  974. {
  975. while( P < End && isspace( (unsigned char) *P))
  976. {
  977. if( *P == '\n')
  978. mLineNumber++;
  979. ++P;
  980. }
  981. if( P >= End)
  982. return;
  983. // check if this is a comment
  984. if( (P[0] == '/' && P[1] == '/') || P[0] == '#')
  985. ReadUntilEndOfLine();
  986. else
  987. break;
  988. }
  989. }
  990. // ------------------------------------------------------------------------------------------------
  991. void XFileParser::GetNextTokenAsString( std::string& poString)
  992. {
  993. if( mIsBinaryFormat)
  994. {
  995. poString = GetNextToken();
  996. return;
  997. }
  998. FindNextNoneWhiteSpace();
  999. if( P >= End)
  1000. ThrowException( "Unexpected end of file while parsing string");
  1001. if( *P != '"')
  1002. ThrowException( "Expected quotation mark.");
  1003. ++P;
  1004. while( P < End && *P != '"')
  1005. poString.append( P++, 1);
  1006. if( P >= End-1)
  1007. ThrowException( "Unexpected end of file while parsing string");
  1008. if( P[1] != ';' || P[0] != '"')
  1009. ThrowException( "Expected quotation mark and semicolon at the end of a string.");
  1010. P+=2;
  1011. }
  1012. // ------------------------------------------------------------------------------------------------
  1013. void XFileParser::ReadUntilEndOfLine()
  1014. {
  1015. if( mIsBinaryFormat)
  1016. return;
  1017. while( P < End)
  1018. {
  1019. if( *P == '\n' || *P == '\r')
  1020. {
  1021. ++P; mLineNumber++;
  1022. return;
  1023. }
  1024. ++P;
  1025. }
  1026. }
  1027. // ------------------------------------------------------------------------------------------------
  1028. unsigned short XFileParser::ReadBinWord()
  1029. {
  1030. const unsigned char* q = (const unsigned char*) P;
  1031. unsigned short tmp = q[0] | (q[1] << 8);
  1032. P += 2;
  1033. return tmp;
  1034. }
  1035. // ------------------------------------------------------------------------------------------------
  1036. unsigned int XFileParser::ReadBinDWord()
  1037. {
  1038. const unsigned char* q = (const unsigned char*) P;
  1039. unsigned int tmp = q[0] | (q[1] << 8) | (q[2] << 16) | (q[3] << 24);
  1040. P += 4;
  1041. return tmp;
  1042. }
  1043. // ------------------------------------------------------------------------------------------------
  1044. unsigned int XFileParser::ReadInt()
  1045. {
  1046. if( mIsBinaryFormat)
  1047. {
  1048. if( mBinaryNumCount == 0)
  1049. {
  1050. unsigned short tmp = ReadBinWord(); // 0x06 or 0x03
  1051. if( tmp == 0x06) // array of ints follows
  1052. mBinaryNumCount = ReadBinDWord();
  1053. else // single int follows
  1054. mBinaryNumCount = 1;
  1055. }
  1056. --mBinaryNumCount;
  1057. return ReadBinDWord();
  1058. } else
  1059. {
  1060. FindNextNoneWhiteSpace();
  1061. // TODO: consider using strtol10s instead???
  1062. // check preceeding minus sign
  1063. bool isNegative = false;
  1064. if( *P == '-')
  1065. {
  1066. isNegative = true;
  1067. P++;
  1068. }
  1069. // at least one digit expected
  1070. if( !isdigit( *P))
  1071. ThrowException( "Number expected.");
  1072. // read digits
  1073. unsigned int number = 0;
  1074. while( P < End)
  1075. {
  1076. if( !isdigit( *P))
  1077. break;
  1078. number = number * 10 + (*P - 48);
  1079. P++;
  1080. }
  1081. CheckForSeparator();
  1082. return isNegative ? ((unsigned int) -int( number)) : number;
  1083. }
  1084. }
  1085. // ------------------------------------------------------------------------------------------------
  1086. float XFileParser::ReadFloat()
  1087. {
  1088. if( mIsBinaryFormat)
  1089. {
  1090. if( mBinaryNumCount == 0)
  1091. {
  1092. unsigned short tmp = ReadBinWord(); // 0x07 or 0x42
  1093. if( tmp == 0x07) // array of floats following
  1094. mBinaryNumCount = ReadBinDWord();
  1095. else // single float following
  1096. mBinaryNumCount = 1;
  1097. }
  1098. --mBinaryNumCount;
  1099. if( mBinaryFloatSize == 8)
  1100. {
  1101. float result = (float) (*(double*) P);
  1102. P += 8;
  1103. return result;
  1104. } else
  1105. {
  1106. float result = *(float*) P;
  1107. P += 4;
  1108. return result;
  1109. }
  1110. }
  1111. // text version
  1112. FindNextNoneWhiteSpace();
  1113. // check for various special strings to allow reading files from faulty exporters
  1114. // I mean you, Blender!
  1115. if( strncmp( P, "-1.#IND00", 9) == 0 || strncmp( P, "1.#IND00", 8) == 0)
  1116. {
  1117. P += 9;
  1118. CheckForSeparator();
  1119. return 0.0f;
  1120. } else
  1121. if( strncmp( P, "1.#QNAN0", 8) == 0)
  1122. {
  1123. P += 8;
  1124. CheckForSeparator();
  1125. return 0.0f;
  1126. }
  1127. float result = 0.0f;
  1128. P = fast_atof_move( P, result);
  1129. CheckForSeparator();
  1130. return result;
  1131. }
  1132. // ------------------------------------------------------------------------------------------------
  1133. aiVector2D XFileParser::ReadVector2()
  1134. {
  1135. aiVector2D vector;
  1136. vector.x = ReadFloat();
  1137. vector.y = ReadFloat();
  1138. CheckForSeparator();
  1139. return vector;
  1140. }
  1141. // ------------------------------------------------------------------------------------------------
  1142. aiVector3D XFileParser::ReadVector3()
  1143. {
  1144. aiVector3D vector;
  1145. vector.x = ReadFloat();
  1146. vector.y = ReadFloat();
  1147. vector.z = ReadFloat();
  1148. CheckForSeparator();
  1149. return vector;
  1150. }
  1151. // ------------------------------------------------------------------------------------------------
  1152. aiColor4D XFileParser::ReadRGBA()
  1153. {
  1154. aiColor4D color;
  1155. color.r = ReadFloat();
  1156. color.g = ReadFloat();
  1157. color.b = ReadFloat();
  1158. color.a = ReadFloat();
  1159. CheckForSeparator();
  1160. return color;
  1161. }
  1162. // ------------------------------------------------------------------------------------------------
  1163. aiColor3D XFileParser::ReadRGB()
  1164. {
  1165. aiColor3D color;
  1166. color.r = ReadFloat();
  1167. color.g = ReadFloat();
  1168. color.b = ReadFloat();
  1169. CheckForSeparator();
  1170. return color;
  1171. }
  1172. // ------------------------------------------------------------------------------------------------
  1173. // Throws an exception with a line number and the given text.
  1174. void XFileParser::ThrowException( const std::string& pText)
  1175. {
  1176. if( mIsBinaryFormat)
  1177. throw new ImportErrorException( pText);
  1178. else
  1179. throw new ImportErrorException( boost::str( boost::format( "Line %d: %s") % mLineNumber % pText));
  1180. }
  1181. // ------------------------------------------------------------------------------------------------
  1182. // Filters the imported hierarchy for some degenerated cases that some exporters produce.
  1183. void XFileParser::FilterHierarchy( XFile::Node* pNode)
  1184. {
  1185. // if the node has just a single unnamed child containing a mesh, remove
  1186. // the anonymous node inbetween. The 3DSMax kwXport plugin seems to produce this
  1187. // mess in some cases
  1188. if( pNode->mChildren.size() == 1)
  1189. {
  1190. XFile::Node* child = pNode->mChildren.front();
  1191. if( child->mName.length() == 0 && child->mMeshes.size() > 0)
  1192. {
  1193. // transfer its meshes to us
  1194. for( unsigned int a = 0; a < child->mMeshes.size(); a++)
  1195. pNode->mMeshes.push_back( child->mMeshes[a]);
  1196. child->mMeshes.clear();
  1197. // then kill it
  1198. delete child;
  1199. pNode->mChildren.clear();
  1200. }
  1201. }
  1202. // recurse
  1203. for( unsigned int a = 0; a < pNode->mChildren.size(); a++)
  1204. FilterHierarchy( pNode->mChildren[a]);
  1205. }
  1206. #endif // !! ASSIMP_BUILD_NO_X_IMPORTER