ASEParser.cpp 69 KB

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