XFileParser.cpp 45 KB

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