ASEParser.cpp 55 KB

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