XFileParser.cpp 45 KB

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