ASEParser.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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 ASEParser.cpp
  35. * @brief Implementation of the ASE parser class
  36. */
  37. #ifndef ASSIMP_BUILD_NO_ASE_IMPORTER
  38. #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
  39. // internal headers
  40. #include "ASELoader.h"
  41. #include "PostProcessing/TextureTransform.h"
  42. #include <assimp/fast_atof.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. namespace Assimp {
  45. using namespace Assimp::ASE;
  46. // ------------------------------------------------------------------------------------------------
  47. // Begin an ASE parsing function
  48. #define AI_ASE_PARSER_INIT() \
  49. int iDepth = 0;
  50. // ------------------------------------------------------------------------------------------------
  51. // Handle a "top-level" section in the file. EOF is no error in this case.
  52. #define AI_ASE_HANDLE_TOP_LEVEL_SECTION() \
  53. else if ('{' == *filePtr) iDepth++; \
  54. else if ('}' == *filePtr) { \
  55. if (0 == --iDepth) { \
  56. ++filePtr; \
  57. SkipToNextToken(); \
  58. return; \
  59. } \
  60. } \
  61. if ('\0' == *filePtr) { \
  62. return; \
  63. } \
  64. if (IsLineEnd(*filePtr) && !bLastWasEndLine) { \
  65. ++iLineNumber; \
  66. bLastWasEndLine = true; \
  67. } else \
  68. bLastWasEndLine = false; \
  69. ++filePtr;
  70. // ------------------------------------------------------------------------------------------------
  71. // Handle a nested section in the file. EOF is an error in this case
  72. // @param level "Depth" of the section
  73. // @param msg Full name of the section (including the asterisk)
  74. #define AI_ASE_HANDLE_SECTION(level, msg) \
  75. if ('{' == *filePtr) \
  76. iDepth++; \
  77. else if ('}' == *filePtr) { \
  78. if (0 == --iDepth) { \
  79. ++filePtr; \
  80. SkipToNextToken(); \
  81. return; \
  82. } \
  83. } else if ('\0' == *filePtr) { \
  84. LogError("Encountered unexpected EOL while parsing a " msg \
  85. " chunk (Level " level ")"); \
  86. } \
  87. if (IsLineEnd(*filePtr) && !bLastWasEndLine) { \
  88. ++iLineNumber; \
  89. bLastWasEndLine = true; \
  90. } else \
  91. bLastWasEndLine = false; \
  92. ++filePtr;
  93. // ------------------------------------------------------------------------------------------------
  94. Parser::Parser(const char *szFile, unsigned int fileFormatDefault) {
  95. ai_assert(nullptr != szFile);
  96. filePtr = szFile;
  97. iFileFormat = fileFormatDefault;
  98. // make sure that the color values are invalid
  99. m_clrBackground.r = get_qnan();
  100. m_clrAmbient.r = get_qnan();
  101. // setup some default values
  102. iLineNumber = 0;
  103. iFirstFrame = 0;
  104. iLastFrame = 0;
  105. iFrameSpeed = 30; // use 30 as default value for this property
  106. iTicksPerFrame = 1; // use 1 as default value for this property
  107. bLastWasEndLine = false; // need to handle \r\n seqs due to binary file mapping
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. void Parser::LogWarning(const char *szWarn) {
  111. ai_assert(nullptr != szWarn);
  112. char szTemp[2048];
  113. #if _MSC_VER >= 1400
  114. sprintf_s(szTemp, "Line %u: %s", iLineNumber, szWarn);
  115. #else
  116. ai_snprintf(szTemp, sizeof(szTemp), "Line %u: %s", iLineNumber, szWarn);
  117. #endif
  118. // output the warning to the logger ...
  119. ASSIMP_LOG_WARN(szTemp);
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. void Parser::LogInfo(const char *szWarn) {
  123. ai_assert(nullptr != szWarn);
  124. char szTemp[1024];
  125. #if _MSC_VER >= 1400
  126. sprintf_s(szTemp, "Line %u: %s", iLineNumber, szWarn);
  127. #else
  128. ai_snprintf(szTemp, 1024, "Line %u: %s", iLineNumber, szWarn);
  129. #endif
  130. // output the information to the logger ...
  131. ASSIMP_LOG_INFO(szTemp);
  132. }
  133. // ------------------------------------------------------------------------------------------------
  134. AI_WONT_RETURN void Parser::LogError(const char *szWarn) {
  135. ai_assert(nullptr != szWarn);
  136. char szTemp[1024];
  137. #if _MSC_VER >= 1400
  138. sprintf_s(szTemp, "Line %u: %s", iLineNumber, szWarn);
  139. #else
  140. ai_snprintf(szTemp, 1024, "Line %u: %s", iLineNumber, szWarn);
  141. #endif
  142. // throw an exception
  143. throw DeadlyImportError(szTemp);
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. bool Parser::SkipToNextToken() {
  147. while (true) {
  148. char me = *filePtr;
  149. // increase the line number counter if necessary
  150. if (IsLineEnd(me) && !bLastWasEndLine) {
  151. ++iLineNumber;
  152. bLastWasEndLine = true;
  153. } else
  154. bLastWasEndLine = false;
  155. if ('*' == me || '}' == me || '{' == me) return true;
  156. if ('\0' == me) return false;
  157. ++filePtr;
  158. }
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. bool Parser::SkipSection() {
  162. // must handle subsections ...
  163. int iCnt = 0;
  164. while (true) {
  165. if ('}' == *filePtr) {
  166. --iCnt;
  167. if (0 == iCnt) {
  168. // go to the next valid token ...
  169. ++filePtr;
  170. SkipToNextToken();
  171. return true;
  172. }
  173. } else if ('{' == *filePtr) {
  174. ++iCnt;
  175. } else if ('\0' == *filePtr) {
  176. LogWarning("Unable to parse block: Unexpected EOF, closing bracket \'}\' was expected [#1]");
  177. return false;
  178. } else if (IsLineEnd(*filePtr))
  179. ++iLineNumber;
  180. ++filePtr;
  181. }
  182. }
  183. // ------------------------------------------------------------------------------------------------
  184. void Parser::Parse() {
  185. AI_ASE_PARSER_INIT();
  186. while (true) {
  187. if ('*' == *filePtr) {
  188. ++filePtr;
  189. // Version should be 200. Validate this ...
  190. if (TokenMatch(filePtr, "3DSMAX_ASCIIEXPORT", 18)) {
  191. unsigned int fmt;
  192. ParseLV4MeshLong(fmt);
  193. if (fmt > 200) {
  194. LogWarning("Unknown file format version: *3DSMAX_ASCIIEXPORT should \
  195. be <= 200");
  196. }
  197. // *************************************************************
  198. // - fmt will be 0 if we're unable to read the version number
  199. // there are some faulty files without a version number ...
  200. // in this case we'll guess the exact file format by looking
  201. // at the file extension (ASE, ASK, ASC)
  202. // *************************************************************
  203. if (fmt) {
  204. iFileFormat = fmt;
  205. }
  206. continue;
  207. }
  208. // main scene information
  209. if (TokenMatch(filePtr, "SCENE", 5)) {
  210. ParseLV1SceneBlock();
  211. continue;
  212. }
  213. // "group" - no implementation yet, in facte
  214. // we're just ignoring them for the moment
  215. if (TokenMatch(filePtr, "GROUP", 5)) {
  216. Parse();
  217. continue;
  218. }
  219. // material list
  220. if (TokenMatch(filePtr, "MATERIAL_LIST", 13)) {
  221. ParseLV1MaterialListBlock();
  222. continue;
  223. }
  224. // geometric object (mesh)
  225. if (TokenMatch(filePtr, "GEOMOBJECT", 10))
  226. {
  227. m_vMeshes.emplace_back("UNNAMED");
  228. ParseLV1ObjectBlock(m_vMeshes.back());
  229. continue;
  230. }
  231. // helper object = dummy in the hierarchy
  232. if (TokenMatch(filePtr, "HELPEROBJECT", 12))
  233. {
  234. m_vDummies.emplace_back();
  235. ParseLV1ObjectBlock(m_vDummies.back());
  236. continue;
  237. }
  238. // light object
  239. if (TokenMatch(filePtr, "LIGHTOBJECT", 11))
  240. {
  241. m_vLights.emplace_back("UNNAMED");
  242. ParseLV1ObjectBlock(m_vLights.back());
  243. continue;
  244. }
  245. // camera object
  246. if (TokenMatch(filePtr, "CAMERAOBJECT", 12)) {
  247. m_vCameras.emplace_back("UNNAMED");
  248. ParseLV1ObjectBlock(m_vCameras.back());
  249. continue;
  250. }
  251. // comment - print it on the console
  252. if (TokenMatch(filePtr, "COMMENT", 7)) {
  253. std::string out = "<unknown>";
  254. ParseString(out, "*COMMENT");
  255. LogInfo(("Comment: " + out).c_str());
  256. continue;
  257. }
  258. // ASC bone weights
  259. if (AI_ASE_IS_OLD_FILE_FORMAT() && TokenMatch(filePtr, "MESH_SOFTSKINVERTS", 18)) {
  260. ParseLV1SoftSkinBlock();
  261. }
  262. }
  263. AI_ASE_HANDLE_TOP_LEVEL_SECTION();
  264. }
  265. }
  266. // ------------------------------------------------------------------------------------------------
  267. void Parser::ParseLV1SoftSkinBlock() {
  268. // TODO: fix line counting here
  269. // **************************************************************
  270. // The soft skin block is formatted differently. There are no
  271. // nested sections supported and the single elements aren't
  272. // marked by keywords starting with an asterisk.
  273. /**
  274. FORMAT BEGIN
  275. *MESH_SOFTSKINVERTS {
  276. <nodename>
  277. <number of vertices>
  278. [for <number of vertices> times:]
  279. <number of weights> [for <number of weights> times:] <bone name> <weight>
  280. }
  281. FORMAT END
  282. */
  283. // **************************************************************
  284. while (true) {
  285. if (*filePtr == '}') {
  286. ++filePtr;
  287. return;
  288. } else if (*filePtr == '\0')
  289. return;
  290. else if (*filePtr == '{')
  291. ++filePtr;
  292. else // if (!IsSpace(*filePtr) && !IsLineEnd(*filePtr))
  293. {
  294. ASE::Mesh *curMesh = nullptr;
  295. unsigned int numVerts = 0;
  296. const char *sz = filePtr;
  297. while (!IsSpaceOrNewLine(*filePtr))
  298. ++filePtr;
  299. const unsigned int diff = (unsigned int)(filePtr - sz);
  300. if (diff) {
  301. std::string name = std::string(sz, diff);
  302. for (std::vector<ASE::Mesh>::iterator it = m_vMeshes.begin();
  303. it != m_vMeshes.end(); ++it) {
  304. if ((*it).mName == name) {
  305. curMesh = &(*it);
  306. break;
  307. }
  308. }
  309. if (!curMesh) {
  310. LogWarning("Encountered unknown mesh in *MESH_SOFTSKINVERTS section");
  311. // Skip the mesh data - until we find a new mesh
  312. // or the end of the *MESH_SOFTSKINVERTS section
  313. while (true) {
  314. SkipSpacesAndLineEnd(&filePtr);
  315. if (*filePtr == '}') {
  316. ++filePtr;
  317. return;
  318. } else if (!IsNumeric(*filePtr))
  319. break;
  320. SkipLine(&filePtr);
  321. }
  322. } else {
  323. SkipSpacesAndLineEnd(&filePtr);
  324. ParseLV4MeshLong(numVerts);
  325. // Reserve enough storage
  326. curMesh->mBoneVertices.reserve(numVerts);
  327. for (unsigned int i = 0; i < numVerts; ++i) {
  328. SkipSpacesAndLineEnd(&filePtr);
  329. unsigned int numWeights;
  330. ParseLV4MeshLong(numWeights);
  331. curMesh->mBoneVertices.emplace_back();
  332. ASE::BoneVertex &vert = curMesh->mBoneVertices.back();
  333. // Reserve enough storage
  334. vert.mBoneWeights.reserve(numWeights);
  335. std::string bone;
  336. for (unsigned int w = 0; w < numWeights; ++w) {
  337. bone.clear();
  338. ParseString(bone, "*MESH_SOFTSKINVERTS.Bone");
  339. // Find the bone in the mesh's list
  340. std::pair<int, ai_real> me;
  341. me.first = -1;
  342. for (unsigned int n = 0; n < curMesh->mBones.size(); ++n) {
  343. if (curMesh->mBones[n].mName == bone) {
  344. me.first = n;
  345. break;
  346. }
  347. }
  348. if (-1 == me.first) {
  349. // We don't have this bone yet, so add it to the list
  350. me.first = static_cast<int>(curMesh->mBones.size());
  351. curMesh->mBones.emplace_back(bone);
  352. }
  353. ParseLV4MeshFloat(me.second);
  354. // Add the new bone weight to list
  355. vert.mBoneWeights.push_back(me);
  356. }
  357. }
  358. }
  359. }
  360. }
  361. if (*filePtr == '\0')
  362. return;
  363. ++filePtr;
  364. SkipSpacesAndLineEnd(&filePtr);
  365. }
  366. }
  367. // ------------------------------------------------------------------------------------------------
  368. void Parser::ParseLV1SceneBlock() {
  369. AI_ASE_PARSER_INIT();
  370. while (true) {
  371. if ('*' == *filePtr) {
  372. ++filePtr;
  373. if (TokenMatch(filePtr, "SCENE_BACKGROUND_STATIC", 23))
  374. {
  375. // parse a color triple and assume it is really the bg color
  376. ParseLV4MeshFloatTriple(&m_clrBackground.r);
  377. continue;
  378. }
  379. if (TokenMatch(filePtr, "SCENE_AMBIENT_STATIC", 20))
  380. {
  381. // parse a color triple and assume it is really the bg color
  382. ParseLV4MeshFloatTriple(&m_clrAmbient.r);
  383. continue;
  384. }
  385. if (TokenMatch(filePtr, "SCENE_FIRSTFRAME", 16)) {
  386. ParseLV4MeshLong(iFirstFrame);
  387. continue;
  388. }
  389. if (TokenMatch(filePtr, "SCENE_LASTFRAME", 15)) {
  390. ParseLV4MeshLong(iLastFrame);
  391. continue;
  392. }
  393. if (TokenMatch(filePtr, "SCENE_FRAMESPEED", 16)) {
  394. ParseLV4MeshLong(iFrameSpeed);
  395. continue;
  396. }
  397. if (TokenMatch(filePtr, "SCENE_TICKSPERFRAME", 19)) {
  398. ParseLV4MeshLong(iTicksPerFrame);
  399. continue;
  400. }
  401. }
  402. AI_ASE_HANDLE_TOP_LEVEL_SECTION();
  403. }
  404. }
  405. // ------------------------------------------------------------------------------------------------
  406. void Parser::ParseLV1MaterialListBlock() {
  407. AI_ASE_PARSER_INIT();
  408. unsigned int iMaterialCount = 0;
  409. unsigned int iOldMaterialCount = (unsigned int)m_vMaterials.size();
  410. while (true) {
  411. if ('*' == *filePtr) {
  412. ++filePtr;
  413. if (TokenMatch(filePtr, "MATERIAL_COUNT", 14)) {
  414. ParseLV4MeshLong(iMaterialCount);
  415. if (UINT_MAX - iOldMaterialCount < iMaterialCount) {
  416. LogWarning("Out of range: material index is too large");
  417. return;
  418. }
  419. // now allocate enough storage to hold all materials
  420. m_vMaterials.resize(iOldMaterialCount + iMaterialCount, Material("INVALID"));
  421. continue;
  422. }
  423. if (TokenMatch(filePtr, "MATERIAL", 8)) {
  424. unsigned int iIndex = 0;
  425. ParseLV4MeshLong(iIndex);
  426. if (iIndex >= iMaterialCount) {
  427. LogWarning("Out of range: material index is too large");
  428. iIndex = iMaterialCount - 1;
  429. return;
  430. }
  431. // get a reference to the material
  432. Material &sMat = m_vMaterials[iIndex + iOldMaterialCount];
  433. // parse the material block
  434. ParseLV2MaterialBlock(sMat);
  435. continue;
  436. }
  437. if( iDepth == 1 ){
  438. // CRUDE HACK: support missing brace after "Ascii Scene Exporter v2.51"
  439. LogWarning("Missing closing brace in material list");
  440. --filePtr;
  441. return;
  442. }
  443. }
  444. AI_ASE_HANDLE_TOP_LEVEL_SECTION();
  445. }
  446. }
  447. // ------------------------------------------------------------------------------------------------
  448. void Parser::ParseLV2MaterialBlock(ASE::Material &mat) {
  449. AI_ASE_PARSER_INIT();
  450. unsigned int iNumSubMaterials = 0;
  451. while (true) {
  452. if ('*' == *filePtr) {
  453. ++filePtr;
  454. if (TokenMatch(filePtr, "MATERIAL_NAME", 13)) {
  455. if (!ParseString(mat.mName, "*MATERIAL_NAME"))
  456. SkipToNextToken();
  457. continue;
  458. }
  459. // ambient material color
  460. if (TokenMatch(filePtr, "MATERIAL_AMBIENT", 16)) {
  461. ParseLV4MeshFloatTriple(&mat.mAmbient.r);
  462. continue;
  463. }
  464. // diffuse material color
  465. if (TokenMatch(filePtr, "MATERIAL_DIFFUSE", 16)) {
  466. ParseLV4MeshFloatTriple(&mat.mDiffuse.r);
  467. continue;
  468. }
  469. // specular material color
  470. if (TokenMatch(filePtr, "MATERIAL_SPECULAR", 17)) {
  471. ParseLV4MeshFloatTriple(&mat.mSpecular.r);
  472. continue;
  473. }
  474. // material shading type
  475. if (TokenMatch(filePtr, "MATERIAL_SHADING", 16)) {
  476. if (TokenMatch(filePtr, "Blinn", 5)) {
  477. mat.mShading = Discreet3DS::Blinn;
  478. } else if (TokenMatch(filePtr, "Phong", 5)) {
  479. mat.mShading = Discreet3DS::Phong;
  480. } else if (TokenMatch(filePtr, "Flat", 4)) {
  481. mat.mShading = Discreet3DS::Flat;
  482. } else if (TokenMatch(filePtr, "Wire", 4)) {
  483. mat.mShading = Discreet3DS::Wire;
  484. } else {
  485. // assume gouraud shading
  486. mat.mShading = Discreet3DS::Gouraud;
  487. SkipToNextToken();
  488. }
  489. continue;
  490. }
  491. // material transparency
  492. if (TokenMatch(filePtr, "MATERIAL_TRANSPARENCY", 21)) {
  493. ParseLV4MeshFloat(mat.mTransparency);
  494. mat.mTransparency = ai_real(1.0) - mat.mTransparency;
  495. continue;
  496. }
  497. // material self illumination
  498. if (TokenMatch(filePtr, "MATERIAL_SELFILLUM", 18)) {
  499. ai_real f = 0.0;
  500. ParseLV4MeshFloat(f);
  501. mat.mEmissive.r = f;
  502. mat.mEmissive.g = f;
  503. mat.mEmissive.b = f;
  504. continue;
  505. }
  506. // material shininess
  507. if (TokenMatch(filePtr, "MATERIAL_SHINE", 14)) {
  508. ParseLV4MeshFloat(mat.mSpecularExponent);
  509. mat.mSpecularExponent *= 15;
  510. continue;
  511. }
  512. // two-sided material
  513. if (TokenMatch(filePtr, "MATERIAL_TWOSIDED", 17)) {
  514. mat.mTwoSided = true;
  515. continue;
  516. }
  517. // material shininess strength
  518. if (TokenMatch(filePtr, "MATERIAL_SHINESTRENGTH", 22)) {
  519. ParseLV4MeshFloat(mat.mShininessStrength);
  520. continue;
  521. }
  522. // diffuse color map
  523. if (TokenMatch(filePtr, "MAP_DIFFUSE", 11)) {
  524. // parse the texture block
  525. ParseLV3MapBlock(mat.sTexDiffuse);
  526. continue;
  527. }
  528. // ambient color map
  529. if (TokenMatch(filePtr, "MAP_AMBIENT", 11)) {
  530. // parse the texture block
  531. ParseLV3MapBlock(mat.sTexAmbient);
  532. continue;
  533. }
  534. // specular color map
  535. if (TokenMatch(filePtr, "MAP_SPECULAR", 12)) {
  536. // parse the texture block
  537. ParseLV3MapBlock(mat.sTexSpecular);
  538. continue;
  539. }
  540. // opacity map
  541. if (TokenMatch(filePtr, "MAP_OPACITY", 11)) {
  542. // parse the texture block
  543. ParseLV3MapBlock(mat.sTexOpacity);
  544. continue;
  545. }
  546. // emissive map
  547. if (TokenMatch(filePtr, "MAP_SELFILLUM", 13)) {
  548. // parse the texture block
  549. ParseLV3MapBlock(mat.sTexEmissive);
  550. continue;
  551. }
  552. // bump map
  553. if (TokenMatch(filePtr, "MAP_BUMP", 8)) {
  554. // parse the texture block
  555. ParseLV3MapBlock(mat.sTexBump);
  556. }
  557. // specular/shininess map
  558. if (TokenMatch(filePtr, "MAP_SHINESTRENGTH", 17)) {
  559. // parse the texture block
  560. ParseLV3MapBlock(mat.sTexShininess);
  561. continue;
  562. }
  563. // number of submaterials
  564. if (TokenMatch(filePtr, "NUMSUBMTLS", 10)) {
  565. ParseLV4MeshLong(iNumSubMaterials);
  566. // allocate enough storage
  567. mat.avSubMaterials.resize(iNumSubMaterials, Material("INVALID SUBMATERIAL"));
  568. }
  569. // submaterial chunks
  570. if (TokenMatch(filePtr, "SUBMATERIAL", 11)) {
  571. unsigned int iIndex = 0;
  572. ParseLV4MeshLong(iIndex);
  573. if (iIndex >= iNumSubMaterials) {
  574. LogWarning("Out of range: submaterial index is too large");
  575. iIndex = iNumSubMaterials - 1;
  576. }
  577. // get a reference to the material
  578. if (iIndex < mat.avSubMaterials.size()) {
  579. Material &sMat = mat.avSubMaterials[iIndex];
  580. // parse the material block
  581. ParseLV2MaterialBlock(sMat);
  582. }
  583. continue;
  584. }
  585. }
  586. AI_ASE_HANDLE_SECTION("2", "*MATERIAL");
  587. }
  588. }
  589. // ------------------------------------------------------------------------------------------------
  590. void Parser::ParseLV3MapBlock(Texture &map) {
  591. AI_ASE_PARSER_INIT();
  592. // ***********************************************************
  593. // *BITMAP should not be there if *MAP_CLASS is not BITMAP,
  594. // but we need to expect that case ... if the path is
  595. // empty the texture won't be used later.
  596. // ***********************************************************
  597. bool parsePath = true;
  598. std::string temp;
  599. while (true) {
  600. if ('*' == *filePtr) {
  601. ++filePtr;
  602. // type of map
  603. if (TokenMatch(filePtr, "MAP_CLASS", 9)) {
  604. temp.clear();
  605. if (!ParseString(temp, "*MAP_CLASS"))
  606. SkipToNextToken();
  607. if (temp != "Bitmap" && temp != "Normal Bump") {
  608. ASSIMP_LOG_WARN("ASE: Skipping unknown map type: ", temp);
  609. parsePath = false;
  610. }
  611. continue;
  612. }
  613. // path to the texture
  614. if (parsePath && TokenMatch(filePtr, "BITMAP", 6)) {
  615. if (!ParseString(map.mMapName, "*BITMAP"))
  616. SkipToNextToken();
  617. if (map.mMapName == "None") {
  618. // Files with 'None' as map name are produced by
  619. // an Maja to ASE exporter which name I forgot ..
  620. ASSIMP_LOG_WARN("ASE: Skipping invalid map entry");
  621. map.mMapName = std::string();
  622. }
  623. continue;
  624. }
  625. // offset on the u axis
  626. if (TokenMatch(filePtr, "UVW_U_OFFSET", 12)) {
  627. ParseLV4MeshFloat(map.mOffsetU);
  628. continue;
  629. }
  630. // offset on the v axis
  631. if (TokenMatch(filePtr, "UVW_V_OFFSET", 12)) {
  632. ParseLV4MeshFloat(map.mOffsetV);
  633. continue;
  634. }
  635. // tiling on the u axis
  636. if (TokenMatch(filePtr, "UVW_U_TILING", 12)) {
  637. ParseLV4MeshFloat(map.mScaleU);
  638. continue;
  639. }
  640. // tiling on the v axis
  641. if (TokenMatch(filePtr, "UVW_V_TILING", 12)) {
  642. ParseLV4MeshFloat(map.mScaleV);
  643. continue;
  644. }
  645. // rotation around the z-axis
  646. if (TokenMatch(filePtr, "UVW_ANGLE", 9)) {
  647. ParseLV4MeshFloat(map.mRotation);
  648. continue;
  649. }
  650. // map blending factor
  651. if (TokenMatch(filePtr, "MAP_AMOUNT", 10)) {
  652. ParseLV4MeshFloat(map.mTextureBlend);
  653. continue;
  654. }
  655. }
  656. AI_ASE_HANDLE_SECTION("3", "*MAP_XXXXXX");
  657. }
  658. }
  659. // ------------------------------------------------------------------------------------------------
  660. bool Parser::ParseString(std::string &out, const char *szName) {
  661. char szBuffer[1024];
  662. if (!SkipSpaces(&filePtr)) {
  663. ai_snprintf(szBuffer, 1024, "Unable to parse %s block: Unexpected EOL", szName);
  664. LogWarning(szBuffer);
  665. return false;
  666. }
  667. // there must be '"'
  668. if ('\"' != *filePtr) {
  669. ai_snprintf(szBuffer, 1024, "Unable to parse %s block: Strings are expected "
  670. "to be enclosed in double quotation marks",
  671. szName);
  672. LogWarning(szBuffer);
  673. return false;
  674. }
  675. ++filePtr;
  676. const char *sz = filePtr;
  677. while (true) {
  678. if ('\"' == *sz)
  679. break;
  680. else if ('\0' == *sz) {
  681. ai_snprintf(szBuffer, 1024, "Unable to parse %s block: Strings are expected to "
  682. "be enclosed in double quotation marks but EOF was reached before "
  683. "a closing quotation mark was encountered",
  684. szName);
  685. LogWarning(szBuffer);
  686. return false;
  687. }
  688. sz++;
  689. }
  690. out = std::string(filePtr, (uintptr_t)sz - (uintptr_t)filePtr);
  691. filePtr = sz + 1;
  692. return true;
  693. }
  694. // ------------------------------------------------------------------------------------------------
  695. void Parser::ParseLV1ObjectBlock(ASE::BaseNode &node) {
  696. AI_ASE_PARSER_INIT();
  697. while (true) {
  698. if ('*' == *filePtr) {
  699. ++filePtr;
  700. // first process common tokens such as node name and transform
  701. // name of the mesh/node
  702. if (TokenMatch(filePtr, "NODE_NAME", 9)) {
  703. if (!ParseString(node.mName, "*NODE_NAME"))
  704. SkipToNextToken();
  705. continue;
  706. }
  707. // name of the parent of the node
  708. if (TokenMatch(filePtr, "NODE_PARENT", 11)) {
  709. if (!ParseString(node.mParent, "*NODE_PARENT"))
  710. SkipToNextToken();
  711. continue;
  712. }
  713. // transformation matrix of the node
  714. if (TokenMatch(filePtr, "NODE_TM", 7)) {
  715. ParseLV2NodeTransformBlock(node);
  716. continue;
  717. }
  718. // animation data of the node
  719. if (TokenMatch(filePtr, "TM_ANIMATION", 12)) {
  720. ParseLV2AnimationBlock(node);
  721. continue;
  722. }
  723. if (node.mType == BaseNode::Light) {
  724. // light settings
  725. if (TokenMatch(filePtr, "LIGHT_SETTINGS", 14)) {
  726. ParseLV2LightSettingsBlock((ASE::Light &)node);
  727. continue;
  728. }
  729. // type of the light source
  730. if (TokenMatch(filePtr, "LIGHT_TYPE", 10)) {
  731. if (!ASSIMP_strincmp("omni", filePtr, 4)) {
  732. ((ASE::Light &)node).mLightType = ASE::Light::OMNI;
  733. } else if (!ASSIMP_strincmp("target", filePtr, 6)) {
  734. ((ASE::Light &)node).mLightType = ASE::Light::TARGET;
  735. } else if (!ASSIMP_strincmp("free", filePtr, 4)) {
  736. ((ASE::Light &)node).mLightType = ASE::Light::FREE;
  737. } else if (!ASSIMP_strincmp("directional", filePtr, 11)) {
  738. ((ASE::Light &)node).mLightType = ASE::Light::DIRECTIONAL;
  739. } else {
  740. LogWarning("Unknown kind of light source");
  741. }
  742. continue;
  743. }
  744. } else if (node.mType == BaseNode::Camera) {
  745. // Camera settings
  746. if (TokenMatch(filePtr, "CAMERA_SETTINGS", 15)) {
  747. ParseLV2CameraSettingsBlock((ASE::Camera &)node);
  748. continue;
  749. } else if (TokenMatch(filePtr, "CAMERA_TYPE", 11)) {
  750. if (!ASSIMP_strincmp("target", filePtr, 6)) {
  751. ((ASE::Camera &)node).mCameraType = ASE::Camera::TARGET;
  752. } else if (!ASSIMP_strincmp("free", filePtr, 4)) {
  753. ((ASE::Camera &)node).mCameraType = ASE::Camera::FREE;
  754. } else {
  755. LogWarning("Unknown kind of camera");
  756. }
  757. continue;
  758. }
  759. } else if (node.mType == BaseNode::Mesh) {
  760. // mesh data
  761. // FIX: Older files use MESH_SOFTSKIN
  762. if (TokenMatch(filePtr, "MESH", 4) ||
  763. TokenMatch(filePtr, "MESH_SOFTSKIN", 13)) {
  764. ParseLV2MeshBlock((ASE::Mesh &)node);
  765. continue;
  766. }
  767. // mesh material index
  768. if (TokenMatch(filePtr, "MATERIAL_REF", 12)) {
  769. ParseLV4MeshLong(((ASE::Mesh &)node).iMaterialIndex);
  770. continue;
  771. }
  772. }
  773. }
  774. AI_ASE_HANDLE_TOP_LEVEL_SECTION();
  775. }
  776. }
  777. // ------------------------------------------------------------------------------------------------
  778. void Parser::ParseLV2CameraSettingsBlock(ASE::Camera &camera) {
  779. AI_ASE_PARSER_INIT();
  780. while (true) {
  781. if ('*' == *filePtr) {
  782. ++filePtr;
  783. if (TokenMatch(filePtr, "CAMERA_NEAR", 11)) {
  784. ParseLV4MeshFloat(camera.mNear);
  785. continue;
  786. }
  787. if (TokenMatch(filePtr, "CAMERA_FAR", 10)) {
  788. ParseLV4MeshFloat(camera.mFar);
  789. continue;
  790. }
  791. if (TokenMatch(filePtr, "CAMERA_FOV", 10)) {
  792. ParseLV4MeshFloat(camera.mFOV);
  793. continue;
  794. }
  795. }
  796. AI_ASE_HANDLE_SECTION("2", "CAMERA_SETTINGS");
  797. }
  798. }
  799. // ------------------------------------------------------------------------------------------------
  800. void Parser::ParseLV2LightSettingsBlock(ASE::Light &light) {
  801. AI_ASE_PARSER_INIT();
  802. while (true) {
  803. if ('*' == *filePtr) {
  804. ++filePtr;
  805. if (TokenMatch(filePtr, "LIGHT_COLOR", 11)) {
  806. ParseLV4MeshFloatTriple(&light.mColor.r);
  807. continue;
  808. }
  809. if (TokenMatch(filePtr, "LIGHT_INTENS", 12)) {
  810. ParseLV4MeshFloat(light.mIntensity);
  811. continue;
  812. }
  813. if (TokenMatch(filePtr, "LIGHT_HOTSPOT", 13)) {
  814. ParseLV4MeshFloat(light.mAngle);
  815. continue;
  816. }
  817. if (TokenMatch(filePtr, "LIGHT_FALLOFF", 13)) {
  818. ParseLV4MeshFloat(light.mFalloff);
  819. continue;
  820. }
  821. }
  822. AI_ASE_HANDLE_SECTION("2", "LIGHT_SETTINGS");
  823. }
  824. }
  825. // ------------------------------------------------------------------------------------------------
  826. void Parser::ParseLV2AnimationBlock(ASE::BaseNode &mesh) {
  827. AI_ASE_PARSER_INIT();
  828. ASE::Animation *anim = &mesh.mAnim;
  829. while (true) {
  830. if ('*' == *filePtr) {
  831. ++filePtr;
  832. if (TokenMatch(filePtr, "NODE_NAME", 9)) {
  833. std::string temp;
  834. if (!ParseString(temp, "*NODE_NAME"))
  835. SkipToNextToken();
  836. // If the name of the node contains .target it
  837. // represents an animated camera or spot light
  838. // target.
  839. if (std::string::npos != temp.find(".Target")) {
  840. if ((mesh.mType != BaseNode::Camera || ((ASE::Camera &)mesh).mCameraType != ASE::Camera::TARGET) &&
  841. (mesh.mType != BaseNode::Light || ((ASE::Light &)mesh).mLightType != ASE::Light::TARGET)) {
  842. ASSIMP_LOG_ERROR("ASE: Found target animation channel "
  843. "but the node is neither a camera nor a spot light");
  844. anim = nullptr;
  845. } else
  846. anim = &mesh.mTargetAnim;
  847. }
  848. continue;
  849. }
  850. // position keyframes
  851. if (TokenMatch(filePtr, "CONTROL_POS_TRACK", 17) ||
  852. TokenMatch(filePtr, "CONTROL_POS_BEZIER", 18) ||
  853. TokenMatch(filePtr, "CONTROL_POS_TCB", 15)) {
  854. if (!anim)
  855. SkipSection();
  856. else
  857. ParseLV3PosAnimationBlock(*anim);
  858. continue;
  859. }
  860. // scaling keyframes
  861. if (TokenMatch(filePtr, "CONTROL_SCALE_TRACK", 19) ||
  862. TokenMatch(filePtr, "CONTROL_SCALE_BEZIER", 20) ||
  863. TokenMatch(filePtr, "CONTROL_SCALE_TCB", 17)) {
  864. if (!anim || anim == &mesh.mTargetAnim) {
  865. // Target animation channels may have no rotation channels
  866. ASSIMP_LOG_ERROR("ASE: Ignoring scaling channel in target animation");
  867. SkipSection();
  868. } else
  869. ParseLV3ScaleAnimationBlock(*anim);
  870. continue;
  871. }
  872. // rotation keyframes
  873. if (TokenMatch(filePtr, "CONTROL_ROT_TRACK", 17) ||
  874. TokenMatch(filePtr, "CONTROL_ROT_BEZIER", 18) ||
  875. TokenMatch(filePtr, "CONTROL_ROT_TCB", 15)) {
  876. if (!anim || anim == &mesh.mTargetAnim) {
  877. // Target animation channels may have no rotation channels
  878. ASSIMP_LOG_ERROR("ASE: Ignoring rotation channel in target animation");
  879. SkipSection();
  880. } else
  881. ParseLV3RotAnimationBlock(*anim);
  882. continue;
  883. }
  884. }
  885. AI_ASE_HANDLE_SECTION("2", "TM_ANIMATION");
  886. }
  887. }
  888. // ------------------------------------------------------------------------------------------------
  889. void Parser::ParseLV3ScaleAnimationBlock(ASE::Animation &anim) {
  890. AI_ASE_PARSER_INIT();
  891. unsigned int iIndex;
  892. while (true) {
  893. if ('*' == *filePtr) {
  894. ++filePtr;
  895. bool b = false;
  896. // For the moment we're just reading the three floats -
  897. // we ignore the additional information for bezier's and TCBs
  898. // simple scaling keyframe
  899. if (TokenMatch(filePtr, "CONTROL_SCALE_SAMPLE", 20)) {
  900. b = true;
  901. anim.mScalingType = ASE::Animation::TRACK;
  902. }
  903. // Bezier scaling keyframe
  904. if (TokenMatch(filePtr, "CONTROL_BEZIER_SCALE_KEY", 24)) {
  905. b = true;
  906. anim.mScalingType = ASE::Animation::BEZIER;
  907. }
  908. // TCB scaling keyframe
  909. if (TokenMatch(filePtr, "CONTROL_TCB_SCALE_KEY", 21)) {
  910. b = true;
  911. anim.mScalingType = ASE::Animation::TCB;
  912. }
  913. if (b) {
  914. anim.akeyScaling.emplace_back();
  915. aiVectorKey &key = anim.akeyScaling.back();
  916. ParseLV4MeshFloatTriple(&key.mValue.x, iIndex);
  917. key.mTime = (double)iIndex;
  918. }
  919. }
  920. AI_ASE_HANDLE_SECTION("3", "*CONTROL_POS_TRACK");
  921. }
  922. }
  923. // ------------------------------------------------------------------------------------------------
  924. void Parser::ParseLV3PosAnimationBlock(ASE::Animation &anim) {
  925. AI_ASE_PARSER_INIT();
  926. unsigned int iIndex;
  927. while (true) {
  928. if ('*' == *filePtr) {
  929. ++filePtr;
  930. bool b = false;
  931. // For the moment we're just reading the three floats -
  932. // we ignore the additional information for bezier's and TCBs
  933. // simple scaling keyframe
  934. if (TokenMatch(filePtr, "CONTROL_POS_SAMPLE", 18)) {
  935. b = true;
  936. anim.mPositionType = ASE::Animation::TRACK;
  937. }
  938. // Bezier scaling keyframe
  939. if (TokenMatch(filePtr, "CONTROL_BEZIER_POS_KEY", 22)) {
  940. b = true;
  941. anim.mPositionType = ASE::Animation::BEZIER;
  942. }
  943. // TCB scaling keyframe
  944. if (TokenMatch(filePtr, "CONTROL_TCB_POS_KEY", 19)) {
  945. b = true;
  946. anim.mPositionType = ASE::Animation::TCB;
  947. }
  948. if (b) {
  949. anim.akeyPositions.emplace_back();
  950. aiVectorKey &key = anim.akeyPositions.back();
  951. ParseLV4MeshFloatTriple(&key.mValue.x, iIndex);
  952. key.mTime = (double)iIndex;
  953. }
  954. }
  955. AI_ASE_HANDLE_SECTION("3", "*CONTROL_POS_TRACK");
  956. }
  957. }
  958. // ------------------------------------------------------------------------------------------------
  959. void Parser::ParseLV3RotAnimationBlock(ASE::Animation &anim) {
  960. AI_ASE_PARSER_INIT();
  961. unsigned int iIndex;
  962. while (true) {
  963. if ('*' == *filePtr) {
  964. ++filePtr;
  965. bool b = false;
  966. // For the moment we're just reading the floats -
  967. // we ignore the additional information for bezier's and TCBs
  968. // simple scaling keyframe
  969. if (TokenMatch(filePtr, "CONTROL_ROT_SAMPLE", 18)) {
  970. b = true;
  971. anim.mRotationType = ASE::Animation::TRACK;
  972. }
  973. // Bezier scaling keyframe
  974. if (TokenMatch(filePtr, "CONTROL_BEZIER_ROT_KEY", 22)) {
  975. b = true;
  976. anim.mRotationType = ASE::Animation::BEZIER;
  977. }
  978. // TCB scaling keyframe
  979. if (TokenMatch(filePtr, "CONTROL_TCB_ROT_KEY", 19)) {
  980. b = true;
  981. anim.mRotationType = ASE::Animation::TCB;
  982. }
  983. if (b) {
  984. anim.akeyRotations.emplace_back();
  985. aiQuatKey &key = anim.akeyRotations.back();
  986. aiVector3D v;
  987. ai_real f;
  988. ParseLV4MeshFloatTriple(&v.x, iIndex);
  989. ParseLV4MeshFloat(f);
  990. key.mTime = (double)iIndex;
  991. key.mValue = aiQuaternion(v, f);
  992. }
  993. }
  994. AI_ASE_HANDLE_SECTION("3", "*CONTROL_ROT_TRACK");
  995. }
  996. }
  997. // ------------------------------------------------------------------------------------------------
  998. void Parser::ParseLV2NodeTransformBlock(ASE::BaseNode &mesh) {
  999. AI_ASE_PARSER_INIT();
  1000. int mode = 0;
  1001. while (true) {
  1002. if ('*' == *filePtr) {
  1003. ++filePtr;
  1004. // name of the node
  1005. if (TokenMatch(filePtr, "NODE_NAME", 9)) {
  1006. std::string temp;
  1007. if (!ParseString(temp, "*NODE_NAME"))
  1008. SkipToNextToken();
  1009. std::string::size_type s;
  1010. if (temp == mesh.mName) {
  1011. mode = 1;
  1012. } else if (std::string::npos != (s = temp.find(".Target")) &&
  1013. mesh.mName == temp.substr(0, s)) {
  1014. // This should be either a target light or a target camera
  1015. if ((mesh.mType == BaseNode::Light && ((ASE::Light &)mesh).mLightType == ASE::Light::TARGET) ||
  1016. (mesh.mType == BaseNode::Camera && ((ASE::Camera &)mesh).mCameraType == ASE::Camera::TARGET)) {
  1017. mode = 2;
  1018. } else {
  1019. ASSIMP_LOG_ERROR("ASE: Ignoring target transform, "
  1020. "this is no spot light or target camera");
  1021. }
  1022. } else {
  1023. ASSIMP_LOG_ERROR("ASE: Unknown node transformation: ", temp);
  1024. // mode = 0
  1025. }
  1026. continue;
  1027. }
  1028. if (mode) {
  1029. // fourth row of the transformation matrix - and also the
  1030. // only information here that is interesting for targets
  1031. if (TokenMatch(filePtr, "TM_ROW3", 7)) {
  1032. ParseLV4MeshFloatTriple((mode == 1 ? mesh.mTransform[3] : &mesh.mTargetPosition.x));
  1033. continue;
  1034. }
  1035. if (mode == 1) {
  1036. // first row of the transformation matrix
  1037. if (TokenMatch(filePtr, "TM_ROW0", 7)) {
  1038. ParseLV4MeshFloatTriple(mesh.mTransform[0]);
  1039. continue;
  1040. }
  1041. // second row of the transformation matrix
  1042. if (TokenMatch(filePtr, "TM_ROW1", 7)) {
  1043. ParseLV4MeshFloatTriple(mesh.mTransform[1]);
  1044. continue;
  1045. }
  1046. // third row of the transformation matrix
  1047. if (TokenMatch(filePtr, "TM_ROW2", 7)) {
  1048. ParseLV4MeshFloatTriple(mesh.mTransform[2]);
  1049. continue;
  1050. }
  1051. // inherited position axes
  1052. if (TokenMatch(filePtr, "INHERIT_POS", 11)) {
  1053. unsigned int aiVal[3];
  1054. ParseLV4MeshLongTriple(aiVal);
  1055. for (unsigned int i = 0; i < 3; ++i)
  1056. mesh.inherit.abInheritPosition[i] = aiVal[i] != 0;
  1057. continue;
  1058. }
  1059. // inherited rotation axes
  1060. if (TokenMatch(filePtr, "INHERIT_ROT", 11)) {
  1061. unsigned int aiVal[3];
  1062. ParseLV4MeshLongTriple(aiVal);
  1063. for (unsigned int i = 0; i < 3; ++i)
  1064. mesh.inherit.abInheritRotation[i] = aiVal[i] != 0;
  1065. continue;
  1066. }
  1067. // inherited scaling axes
  1068. if (TokenMatch(filePtr, "INHERIT_SCL", 11)) {
  1069. unsigned int aiVal[3];
  1070. ParseLV4MeshLongTriple(aiVal);
  1071. for (unsigned int i = 0; i < 3; ++i)
  1072. mesh.inherit.abInheritScaling[i] = aiVal[i] != 0;
  1073. continue;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. AI_ASE_HANDLE_SECTION("2", "*NODE_TM");
  1079. }
  1080. }
  1081. // ------------------------------------------------------------------------------------------------
  1082. void Parser::ParseLV2MeshBlock(ASE::Mesh &mesh) {
  1083. AI_ASE_PARSER_INIT();
  1084. unsigned int iNumVertices = 0;
  1085. unsigned int iNumFaces = 0;
  1086. unsigned int iNumTVertices = 0;
  1087. unsigned int iNumTFaces = 0;
  1088. unsigned int iNumCVertices = 0;
  1089. unsigned int iNumCFaces = 0;
  1090. while (true) {
  1091. if ('*' == *filePtr) {
  1092. ++filePtr;
  1093. // Number of vertices in the mesh
  1094. if (TokenMatch(filePtr, "MESH_NUMVERTEX", 14)) {
  1095. ParseLV4MeshLong(iNumVertices);
  1096. continue;
  1097. }
  1098. // Number of texture coordinates in the mesh
  1099. if (TokenMatch(filePtr, "MESH_NUMTVERTEX", 15)) {
  1100. ParseLV4MeshLong(iNumTVertices);
  1101. continue;
  1102. }
  1103. // Number of vertex colors in the mesh
  1104. if (TokenMatch(filePtr, "MESH_NUMCVERTEX", 15)) {
  1105. ParseLV4MeshLong(iNumCVertices);
  1106. continue;
  1107. }
  1108. // Number of regular faces in the mesh
  1109. if (TokenMatch(filePtr, "MESH_NUMFACES", 13)) {
  1110. ParseLV4MeshLong(iNumFaces);
  1111. continue;
  1112. }
  1113. // Number of UVWed faces in the mesh
  1114. if (TokenMatch(filePtr, "MESH_NUMTVFACES", 15)) {
  1115. ParseLV4MeshLong(iNumTFaces);
  1116. continue;
  1117. }
  1118. // Number of colored faces in the mesh
  1119. if (TokenMatch(filePtr, "MESH_NUMCVFACES", 15)) {
  1120. ParseLV4MeshLong(iNumCFaces);
  1121. continue;
  1122. }
  1123. // mesh vertex list block
  1124. if (TokenMatch(filePtr, "MESH_VERTEX_LIST", 16)) {
  1125. ParseLV3MeshVertexListBlock(iNumVertices, mesh);
  1126. continue;
  1127. }
  1128. // mesh face list block
  1129. if (TokenMatch(filePtr, "MESH_FACE_LIST", 14)) {
  1130. ParseLV3MeshFaceListBlock(iNumFaces, mesh);
  1131. continue;
  1132. }
  1133. // mesh texture vertex list block
  1134. if (TokenMatch(filePtr, "MESH_TVERTLIST", 14)) {
  1135. ParseLV3MeshTListBlock(iNumTVertices, mesh);
  1136. continue;
  1137. }
  1138. // mesh texture face block
  1139. if (TokenMatch(filePtr, "MESH_TFACELIST", 14)) {
  1140. ParseLV3MeshTFaceListBlock(iNumTFaces, mesh);
  1141. continue;
  1142. }
  1143. // mesh color vertex list block
  1144. if (TokenMatch(filePtr, "MESH_CVERTLIST", 14)) {
  1145. ParseLV3MeshCListBlock(iNumCVertices, mesh);
  1146. continue;
  1147. }
  1148. // mesh color face block
  1149. if (TokenMatch(filePtr, "MESH_CFACELIST", 14)) {
  1150. ParseLV3MeshCFaceListBlock(iNumCFaces, mesh);
  1151. continue;
  1152. }
  1153. // mesh normals
  1154. if (TokenMatch(filePtr, "MESH_NORMALS", 12)) {
  1155. ParseLV3MeshNormalListBlock(mesh);
  1156. continue;
  1157. }
  1158. // another mesh UV channel ...
  1159. if (TokenMatch(filePtr, "MESH_MAPPINGCHANNEL", 19)) {
  1160. unsigned int iIndex(0);
  1161. ParseLV4MeshLong(iIndex);
  1162. if (0 == iIndex) {
  1163. LogWarning("Mapping channel has an invalid index. Skipping UV channel");
  1164. // skip it ...
  1165. SkipSection();
  1166. } else {
  1167. if (iIndex < 2) {
  1168. LogWarning("Mapping channel has an invalid index. Skipping UV channel");
  1169. // skip it ...
  1170. SkipSection();
  1171. }
  1172. if (iIndex > AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  1173. LogWarning("Too many UV channels specified. Skipping channel ..");
  1174. // skip it ...
  1175. SkipSection();
  1176. } else {
  1177. // parse the mapping channel
  1178. ParseLV3MappingChannel(iIndex - 1, mesh);
  1179. }
  1180. continue;
  1181. }
  1182. }
  1183. // mesh animation keyframe. Not supported
  1184. if (TokenMatch(filePtr, "MESH_ANIMATION", 14)) {
  1185. LogWarning("Found *MESH_ANIMATION element in ASE/ASK file. "
  1186. "Keyframe animation is not supported by Assimp, this element "
  1187. "will be ignored");
  1188. //SkipSection();
  1189. continue;
  1190. }
  1191. if (TokenMatch(filePtr, "MESH_WEIGHTS", 12)) {
  1192. ParseLV3MeshWeightsBlock(mesh);
  1193. continue;
  1194. }
  1195. }
  1196. AI_ASE_HANDLE_SECTION("2", "*MESH");
  1197. }
  1198. }
  1199. // ------------------------------------------------------------------------------------------------
  1200. void Parser::ParseLV3MeshWeightsBlock(ASE::Mesh &mesh) {
  1201. AI_ASE_PARSER_INIT();
  1202. unsigned int iNumVertices = 0, iNumBones = 0;
  1203. while (true) {
  1204. if ('*' == *filePtr) {
  1205. ++filePtr;
  1206. // Number of bone vertices ...
  1207. if (TokenMatch(filePtr, "MESH_NUMVERTEX", 14)) {
  1208. ParseLV4MeshLong(iNumVertices);
  1209. continue;
  1210. }
  1211. // Number of bones
  1212. if (TokenMatch(filePtr, "MESH_NUMBONE", 12)) {
  1213. ParseLV4MeshLong(iNumBones);
  1214. continue;
  1215. }
  1216. // parse the list of bones
  1217. if (TokenMatch(filePtr, "MESH_BONE_LIST", 14)) {
  1218. ParseLV4MeshBones(iNumBones, mesh);
  1219. continue;
  1220. }
  1221. // parse the list of bones vertices
  1222. if (TokenMatch(filePtr, "MESH_BONE_VERTEX_LIST", 21)) {
  1223. ParseLV4MeshBonesVertices(iNumVertices, mesh);
  1224. continue;
  1225. }
  1226. }
  1227. AI_ASE_HANDLE_SECTION("3", "*MESH_WEIGHTS");
  1228. }
  1229. }
  1230. // ------------------------------------------------------------------------------------------------
  1231. void Parser::ParseLV4MeshBones(unsigned int iNumBones, ASE::Mesh &mesh) {
  1232. AI_ASE_PARSER_INIT();
  1233. mesh.mBones.resize(iNumBones, Bone("UNNAMED"));
  1234. while (true) {
  1235. if ('*' == *filePtr) {
  1236. ++filePtr;
  1237. // Mesh bone with name ...
  1238. if (TokenMatch(filePtr, "MESH_BONE_NAME", 14)) {
  1239. // parse an index ...
  1240. if (SkipSpaces(&filePtr)) {
  1241. unsigned int iIndex = strtoul10(filePtr, &filePtr);
  1242. if (iIndex >= iNumBones) {
  1243. LogWarning("Bone index is out of bounds");
  1244. continue;
  1245. }
  1246. if (!ParseString(mesh.mBones[iIndex].mName, "*MESH_BONE_NAME"))
  1247. SkipToNextToken();
  1248. continue;
  1249. }
  1250. }
  1251. }
  1252. AI_ASE_HANDLE_SECTION("3", "*MESH_BONE_LIST");
  1253. }
  1254. }
  1255. // ------------------------------------------------------------------------------------------------
  1256. void Parser::ParseLV4MeshBonesVertices(unsigned int iNumVertices, ASE::Mesh &mesh) {
  1257. AI_ASE_PARSER_INIT();
  1258. mesh.mBoneVertices.resize(iNumVertices);
  1259. while (true) {
  1260. if ('*' == *filePtr) {
  1261. ++filePtr;
  1262. // Mesh bone vertex
  1263. if (TokenMatch(filePtr, "MESH_BONE_VERTEX", 16)) {
  1264. // read the vertex index
  1265. unsigned int iIndex = strtoul10(filePtr, &filePtr);
  1266. if (iIndex >= mesh.mPositions.size()) {
  1267. iIndex = (unsigned int)mesh.mPositions.size() - 1;
  1268. LogWarning("Bone vertex index is out of bounds. Using the largest valid "
  1269. "bone vertex index instead");
  1270. }
  1271. // --- ignored
  1272. ai_real afVert[3];
  1273. ParseLV4MeshFloatTriple(afVert);
  1274. std::pair<int, float> pairOut;
  1275. while (true) {
  1276. // first parse the bone index ...
  1277. if (!SkipSpaces(&filePtr)) break;
  1278. pairOut.first = strtoul10(filePtr, &filePtr);
  1279. // then parse the vertex weight
  1280. if (!SkipSpaces(&filePtr)) break;
  1281. filePtr = fast_atoreal_move<float>(filePtr, pairOut.second);
  1282. // -1 marks unused entries
  1283. if (-1 != pairOut.first) {
  1284. mesh.mBoneVertices[iIndex].mBoneWeights.push_back(pairOut);
  1285. }
  1286. }
  1287. continue;
  1288. }
  1289. }
  1290. AI_ASE_HANDLE_SECTION("4", "*MESH_BONE_VERTEX");
  1291. }
  1292. }
  1293. // ------------------------------------------------------------------------------------------------
  1294. void Parser::ParseLV3MeshVertexListBlock(
  1295. unsigned int iNumVertices, ASE::Mesh &mesh) {
  1296. AI_ASE_PARSER_INIT();
  1297. // allocate enough storage in the array
  1298. mesh.mPositions.resize(iNumVertices);
  1299. while (true) {
  1300. if ('*' == *filePtr) {
  1301. ++filePtr;
  1302. // Vertex entry
  1303. if (TokenMatch(filePtr, "MESH_VERTEX", 11)) {
  1304. aiVector3D vTemp;
  1305. unsigned int iIndex;
  1306. ParseLV4MeshFloatTriple(&vTemp.x, iIndex);
  1307. if (iIndex >= iNumVertices) {
  1308. LogWarning("Invalid vertex index. It will be ignored");
  1309. } else
  1310. mesh.mPositions[iIndex] = vTemp;
  1311. continue;
  1312. }
  1313. }
  1314. AI_ASE_HANDLE_SECTION("3", "*MESH_VERTEX_LIST");
  1315. }
  1316. }
  1317. // ------------------------------------------------------------------------------------------------
  1318. void Parser::ParseLV3MeshFaceListBlock(unsigned int iNumFaces, ASE::Mesh &mesh) {
  1319. AI_ASE_PARSER_INIT();
  1320. // allocate enough storage in the face array
  1321. mesh.mFaces.resize(iNumFaces);
  1322. while (true) {
  1323. if ('*' == *filePtr) {
  1324. ++filePtr;
  1325. // Face entry
  1326. if (TokenMatch(filePtr, "MESH_FACE", 9)) {
  1327. ASE::Face mFace;
  1328. ParseLV4MeshFace(mFace);
  1329. if (mFace.iFace >= iNumFaces) {
  1330. LogWarning("Face has an invalid index. It will be ignored");
  1331. } else
  1332. mesh.mFaces[mFace.iFace] = mFace;
  1333. continue;
  1334. }
  1335. }
  1336. AI_ASE_HANDLE_SECTION("3", "*MESH_FACE_LIST");
  1337. }
  1338. }
  1339. // ------------------------------------------------------------------------------------------------
  1340. void Parser::ParseLV3MeshTListBlock(unsigned int iNumVertices,
  1341. ASE::Mesh &mesh, unsigned int iChannel) {
  1342. AI_ASE_PARSER_INIT();
  1343. // allocate enough storage in the array
  1344. mesh.amTexCoords[iChannel].resize(iNumVertices);
  1345. while (true) {
  1346. if ('*' == *filePtr) {
  1347. ++filePtr;
  1348. // Vertex entry
  1349. if (TokenMatch(filePtr, "MESH_TVERT", 10)) {
  1350. aiVector3D vTemp;
  1351. unsigned int iIndex;
  1352. ParseLV4MeshFloatTriple(&vTemp.x, iIndex);
  1353. if (iIndex >= iNumVertices) {
  1354. LogWarning("Tvertex has an invalid index. It will be ignored");
  1355. } else
  1356. mesh.amTexCoords[iChannel][iIndex] = vTemp;
  1357. if (0.0f != vTemp.z) {
  1358. // we need 3 coordinate channels
  1359. mesh.mNumUVComponents[iChannel] = 3;
  1360. }
  1361. continue;
  1362. }
  1363. }
  1364. AI_ASE_HANDLE_SECTION("3", "*MESH_TVERT_LIST");
  1365. }
  1366. }
  1367. // ------------------------------------------------------------------------------------------------
  1368. void Parser::ParseLV3MeshTFaceListBlock(unsigned int iNumFaces,
  1369. ASE::Mesh &mesh, unsigned int iChannel) {
  1370. AI_ASE_PARSER_INIT();
  1371. while (true) {
  1372. if ('*' == *filePtr) {
  1373. ++filePtr;
  1374. // Face entry
  1375. if (TokenMatch(filePtr, "MESH_TFACE", 10)) {
  1376. unsigned int aiValues[3];
  1377. unsigned int iIndex = 0;
  1378. ParseLV4MeshLongTriple(aiValues, iIndex);
  1379. if (iIndex >= iNumFaces || iIndex >= mesh.mFaces.size()) {
  1380. LogWarning("UV-Face has an invalid index. It will be ignored");
  1381. } else {
  1382. // copy UV indices
  1383. mesh.mFaces[iIndex].amUVIndices[iChannel][0] = aiValues[0];
  1384. mesh.mFaces[iIndex].amUVIndices[iChannel][1] = aiValues[1];
  1385. mesh.mFaces[iIndex].amUVIndices[iChannel][2] = aiValues[2];
  1386. }
  1387. continue;
  1388. }
  1389. }
  1390. AI_ASE_HANDLE_SECTION("3", "*MESH_TFACE_LIST");
  1391. }
  1392. }
  1393. // ------------------------------------------------------------------------------------------------
  1394. void Parser::ParseLV3MappingChannel(unsigned int iChannel, ASE::Mesh &mesh) {
  1395. AI_ASE_PARSER_INIT();
  1396. unsigned int iNumTVertices = 0;
  1397. unsigned int iNumTFaces = 0;
  1398. while (true) {
  1399. if ('*' == *filePtr) {
  1400. ++filePtr;
  1401. // Number of texture coordinates in the mesh
  1402. if (TokenMatch(filePtr, "MESH_NUMTVERTEX", 15)) {
  1403. ParseLV4MeshLong(iNumTVertices);
  1404. continue;
  1405. }
  1406. // Number of UVWed faces in the mesh
  1407. if (TokenMatch(filePtr, "MESH_NUMTVFACES", 15)) {
  1408. ParseLV4MeshLong(iNumTFaces);
  1409. continue;
  1410. }
  1411. // mesh texture vertex list block
  1412. if (TokenMatch(filePtr, "MESH_TVERTLIST", 14)) {
  1413. ParseLV3MeshTListBlock(iNumTVertices, mesh, iChannel);
  1414. continue;
  1415. }
  1416. // mesh texture face block
  1417. if (TokenMatch(filePtr, "MESH_TFACELIST", 14)) {
  1418. ParseLV3MeshTFaceListBlock(iNumTFaces, mesh, iChannel);
  1419. continue;
  1420. }
  1421. }
  1422. AI_ASE_HANDLE_SECTION("3", "*MESH_MAPPING_CHANNEL");
  1423. }
  1424. }
  1425. // ------------------------------------------------------------------------------------------------
  1426. void Parser::ParseLV3MeshCListBlock(unsigned int iNumVertices, ASE::Mesh &mesh) {
  1427. AI_ASE_PARSER_INIT();
  1428. // allocate enough storage in the array
  1429. mesh.mVertexColors.resize(iNumVertices);
  1430. while (true) {
  1431. if ('*' == *filePtr) {
  1432. ++filePtr;
  1433. // Vertex entry
  1434. if (TokenMatch(filePtr, "MESH_VERTCOL", 12)) {
  1435. aiColor4D vTemp;
  1436. vTemp.a = 1.0f;
  1437. unsigned int iIndex;
  1438. ParseLV4MeshFloatTriple(&vTemp.r, iIndex);
  1439. if (iIndex >= iNumVertices) {
  1440. LogWarning("Vertex color has an invalid index. It will be ignored");
  1441. } else
  1442. mesh.mVertexColors[iIndex] = vTemp;
  1443. continue;
  1444. }
  1445. }
  1446. AI_ASE_HANDLE_SECTION("3", "*MESH_CVERTEX_LIST");
  1447. }
  1448. }
  1449. // ------------------------------------------------------------------------------------------------
  1450. void Parser::ParseLV3MeshCFaceListBlock(unsigned int iNumFaces, ASE::Mesh &mesh) {
  1451. AI_ASE_PARSER_INIT();
  1452. while (true) {
  1453. if ('*' == *filePtr) {
  1454. ++filePtr;
  1455. // Face entry
  1456. if (TokenMatch(filePtr, "MESH_CFACE", 10)) {
  1457. unsigned int aiValues[3];
  1458. unsigned int iIndex = 0;
  1459. ParseLV4MeshLongTriple(aiValues, iIndex);
  1460. if (iIndex >= iNumFaces || iIndex >= mesh.mFaces.size()) {
  1461. LogWarning("UV-Face has an invalid index. It will be ignored");
  1462. } else {
  1463. // copy color indices
  1464. mesh.mFaces[iIndex].mColorIndices[0] = aiValues[0];
  1465. mesh.mFaces[iIndex].mColorIndices[1] = aiValues[1];
  1466. mesh.mFaces[iIndex].mColorIndices[2] = aiValues[2];
  1467. }
  1468. continue;
  1469. }
  1470. }
  1471. AI_ASE_HANDLE_SECTION("3", "*MESH_CFACE_LIST");
  1472. }
  1473. }
  1474. // ------------------------------------------------------------------------------------------------
  1475. void Parser::ParseLV3MeshNormalListBlock(ASE::Mesh &sMesh) {
  1476. AI_ASE_PARSER_INIT();
  1477. // Allocate enough storage for the normals
  1478. sMesh.mNormals.resize(sMesh.mFaces.size() * 3, aiVector3D(0.f, 0.f, 0.f));
  1479. unsigned int index, faceIdx = UINT_MAX;
  1480. // FIXME: rewrite this and find out how to interpret the normals
  1481. // correctly. This is crap.
  1482. // Smooth the vertex and face normals together. The result
  1483. // will be edgy then, but otherwise everything would be soft ...
  1484. while (true) {
  1485. if ('*' == *filePtr) {
  1486. ++filePtr;
  1487. if (faceIdx != UINT_MAX && TokenMatch(filePtr, "MESH_VERTEXNORMAL", 17)) {
  1488. aiVector3D vNormal;
  1489. ParseLV4MeshFloatTriple(&vNormal.x, index);
  1490. if (faceIdx >= sMesh.mFaces.size())
  1491. continue;
  1492. // Make sure we assign it to the correct face
  1493. const ASE::Face &face = sMesh.mFaces[faceIdx];
  1494. if (index == face.mIndices[0])
  1495. index = 0;
  1496. else if (index == face.mIndices[1])
  1497. index = 1;
  1498. else if (index == face.mIndices[2])
  1499. index = 2;
  1500. else {
  1501. ASSIMP_LOG_ERROR("ASE: Invalid vertex index in MESH_VERTEXNORMAL section");
  1502. continue;
  1503. }
  1504. // We'll renormalize later
  1505. sMesh.mNormals[faceIdx * 3 + index] += vNormal;
  1506. continue;
  1507. }
  1508. if (TokenMatch(filePtr, "MESH_FACENORMAL", 15)) {
  1509. aiVector3D vNormal;
  1510. ParseLV4MeshFloatTriple(&vNormal.x, faceIdx);
  1511. if (faceIdx >= sMesh.mFaces.size()) {
  1512. ASSIMP_LOG_ERROR("ASE: Invalid vertex index in MESH_FACENORMAL section");
  1513. continue;
  1514. }
  1515. // We'll renormalize later
  1516. sMesh.mNormals[faceIdx * 3] += vNormal;
  1517. sMesh.mNormals[faceIdx * 3 + 1] += vNormal;
  1518. sMesh.mNormals[faceIdx * 3 + 2] += vNormal;
  1519. continue;
  1520. }
  1521. }
  1522. AI_ASE_HANDLE_SECTION("3", "*MESH_NORMALS");
  1523. }
  1524. }
  1525. // ------------------------------------------------------------------------------------------------
  1526. void Parser::ParseLV4MeshFace(ASE::Face &out) {
  1527. // skip spaces and tabs
  1528. if (!SkipSpaces(&filePtr)) {
  1529. LogWarning("Unable to parse *MESH_FACE Element: Unexpected EOL [#1]");
  1530. SkipToNextToken();
  1531. return;
  1532. }
  1533. // parse the face index
  1534. out.iFace = strtoul10(filePtr, &filePtr);
  1535. // next character should be ':'
  1536. if (!SkipSpaces(&filePtr)) {
  1537. // FIX: there are some ASE files which haven't got : here ....
  1538. LogWarning("Unable to parse *MESH_FACE Element: Unexpected EOL. \':\' expected [#2]");
  1539. SkipToNextToken();
  1540. return;
  1541. }
  1542. // FIX: There are some ASE files which haven't got ':' here
  1543. if (':' == *filePtr) ++filePtr;
  1544. // Parse all mesh indices
  1545. for (unsigned int i = 0; i < 3; ++i) {
  1546. unsigned int iIndex = 0;
  1547. if (!SkipSpaces(&filePtr)) {
  1548. LogWarning("Unable to parse *MESH_FACE Element: Unexpected EOL");
  1549. SkipToNextToken();
  1550. return;
  1551. }
  1552. switch (*filePtr) {
  1553. case 'A':
  1554. case 'a':
  1555. break;
  1556. case 'B':
  1557. case 'b':
  1558. iIndex = 1;
  1559. break;
  1560. case 'C':
  1561. case 'c':
  1562. iIndex = 2;
  1563. break;
  1564. default:
  1565. LogWarning("Unable to parse *MESH_FACE Element: Unexpected EOL. "
  1566. "A,B or C expected [#3]");
  1567. SkipToNextToken();
  1568. return;
  1569. };
  1570. ++filePtr;
  1571. // next character should be ':'
  1572. if (!SkipSpaces(&filePtr) || ':' != *filePtr) {
  1573. LogWarning("Unable to parse *MESH_FACE Element: "
  1574. "Unexpected EOL. \':\' expected [#2]");
  1575. SkipToNextToken();
  1576. return;
  1577. }
  1578. ++filePtr;
  1579. if (!SkipSpaces(&filePtr)) {
  1580. LogWarning("Unable to parse *MESH_FACE Element: Unexpected EOL. "
  1581. "Vertex index ecpected [#4]");
  1582. SkipToNextToken();
  1583. return;
  1584. }
  1585. out.mIndices[iIndex] = strtoul10(filePtr, &filePtr);
  1586. }
  1587. // now we need to skip the AB, BC, CA blocks.
  1588. while (true) {
  1589. if ('*' == *filePtr) break;
  1590. if (IsLineEnd(*filePtr)) {
  1591. //iLineNumber++;
  1592. return;
  1593. }
  1594. filePtr++;
  1595. }
  1596. // parse the smoothing group of the face
  1597. if (TokenMatch(filePtr, "*MESH_SMOOTHING", 15)) {
  1598. if (!SkipSpaces(&filePtr)) {
  1599. LogWarning("Unable to parse *MESH_SMOOTHING Element: "
  1600. "Unexpected EOL. Smoothing group(s) expected [#5]");
  1601. SkipToNextToken();
  1602. return;
  1603. }
  1604. // Parse smoothing groups until we don't anymore see commas
  1605. // FIX: There needn't always be a value, sad but true
  1606. while (true) {
  1607. if (*filePtr < '9' && *filePtr >= '0') {
  1608. uint32_t value = strtoul10(filePtr, &filePtr);
  1609. if (value < 32) {
  1610. out.iSmoothGroup |= (1 << strtoul10(filePtr, &filePtr));
  1611. } else {
  1612. const std::string message = std::string("Unable to set smooth group, value with ") + ai_to_string(value) + std::string(" out of range");
  1613. LogWarning(message.c_str());
  1614. }
  1615. }
  1616. SkipSpaces(&filePtr);
  1617. if (',' != *filePtr) {
  1618. break;
  1619. }
  1620. ++filePtr;
  1621. SkipSpaces(&filePtr);
  1622. }
  1623. }
  1624. // *MESH_MTLID is optional, too
  1625. while (true) {
  1626. if ('*' == *filePtr) {
  1627. break;
  1628. }
  1629. if (IsLineEnd(*filePtr)) {
  1630. return;
  1631. }
  1632. filePtr++;
  1633. }
  1634. if (TokenMatch(filePtr, "*MESH_MTLID", 11)) {
  1635. if (!SkipSpaces(&filePtr)) {
  1636. LogWarning("Unable to parse *MESH_MTLID Element: Unexpected EOL. "
  1637. "Material index expected [#6]");
  1638. SkipToNextToken();
  1639. return;
  1640. }
  1641. out.iMaterial = strtoul10(filePtr, &filePtr);
  1642. }
  1643. return;
  1644. }
  1645. // ------------------------------------------------------------------------------------------------
  1646. void Parser::ParseLV4MeshLongTriple(unsigned int *apOut) {
  1647. ai_assert(nullptr != apOut);
  1648. for (unsigned int i = 0; i < 3; ++i)
  1649. ParseLV4MeshLong(apOut[i]);
  1650. }
  1651. // ------------------------------------------------------------------------------------------------
  1652. void Parser::ParseLV4MeshLongTriple(unsigned int *apOut, unsigned int &rIndexOut) {
  1653. ai_assert(nullptr != apOut);
  1654. // parse the index
  1655. ParseLV4MeshLong(rIndexOut);
  1656. // parse the three others
  1657. ParseLV4MeshLongTriple(apOut);
  1658. }
  1659. // ------------------------------------------------------------------------------------------------
  1660. void Parser::ParseLV4MeshFloatTriple(ai_real *apOut, unsigned int &rIndexOut) {
  1661. ai_assert(nullptr != apOut);
  1662. // parse the index
  1663. ParseLV4MeshLong(rIndexOut);
  1664. // parse the three others
  1665. ParseLV4MeshFloatTriple(apOut);
  1666. }
  1667. // ------------------------------------------------------------------------------------------------
  1668. void Parser::ParseLV4MeshFloatTriple(ai_real *apOut) {
  1669. ai_assert(nullptr != apOut);
  1670. for (unsigned int i = 0; i < 3; ++i) {
  1671. ParseLV4MeshFloat(apOut[i]);
  1672. }
  1673. }
  1674. // ------------------------------------------------------------------------------------------------
  1675. void Parser::ParseLV4MeshFloat(ai_real &fOut) {
  1676. // skip spaces and tabs
  1677. if (!SkipSpaces(&filePtr)) {
  1678. // LOG
  1679. LogWarning("Unable to parse float: unexpected EOL [#1]");
  1680. fOut = 0.0;
  1681. ++iLineNumber;
  1682. return;
  1683. }
  1684. // parse the first float
  1685. filePtr = fast_atoreal_move<ai_real>(filePtr, fOut);
  1686. }
  1687. // ------------------------------------------------------------------------------------------------
  1688. void Parser::ParseLV4MeshLong(unsigned int &iOut) {
  1689. // Skip spaces and tabs
  1690. if (!SkipSpaces(&filePtr)) {
  1691. // LOG
  1692. LogWarning("Unable to parse long: unexpected EOL [#1]");
  1693. iOut = 0;
  1694. ++iLineNumber;
  1695. return;
  1696. }
  1697. // parse the value
  1698. iOut = strtoul10(filePtr, &filePtr);
  1699. }
  1700. }
  1701. #endif // ASSIMP_BUILD_NO_3DS_IMPORTER
  1702. #endif // !! ASSIMP_BUILD_NO_BASE_IMPORTER