XFileParser.cpp 40 KB

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