ASEParser.cpp 69 KB

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