ASEParser.cpp 44 KB

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