XFileParser.cpp 45 KB

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