ASEParser.cpp 70 KB

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