ASEParser.cpp 69 KB

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