XFileParser.cpp 40 KB

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