XFileParser.cpp 39 KB

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