XFileParser.cpp 40 KB

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