XFileParser.cpp 39 KB

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