XFileParser.cpp 40 KB

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