MDLLoader.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file MDLLoader.cpp
  35. * @brief Implementation of the main parts of the MDL importer class
  36. * *TODO* Cleanup and further testing of some parts necessary
  37. */
  38. // internal headers
  39. #include "AssimpPCH.h"
  40. #ifndef ASSIMP_BUILD_NO_MDL_IMPORTER
  41. #include "MDLLoader.h"
  42. #include "MDLDefaultColorMap.h"
  43. #include "MD2FileData.h"
  44. using namespace Assimp;
  45. // ------------------------------------------------------------------------------------------------
  46. // Ugly stuff ... nevermind
  47. #define _AI_MDL7_ACCESS(_data, _index, _limit, _type) \
  48. (*((const _type*)(((const char*)_data) + _index * _limit)))
  49. #define _AI_MDL7_ACCESS_PTR(_data, _index, _limit, _type) \
  50. ((BE_NCONST _type*)(((const char*)_data) + _index * _limit))
  51. #define _AI_MDL7_ACCESS_VERT(_data, _index, _limit) \
  52. _AI_MDL7_ACCESS(_data,_index,_limit,MDL::Vertex_MDL7)
  53. // ------------------------------------------------------------------------------------------------
  54. // Constructor to be privately used by Importer
  55. MDLImporter::MDLImporter()
  56. {}
  57. // ------------------------------------------------------------------------------------------------
  58. // Destructor, private as well
  59. MDLImporter::~MDLImporter()
  60. {}
  61. // ------------------------------------------------------------------------------------------------
  62. // Returns whether the class can handle the format of the given file.
  63. bool MDLImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  64. {
  65. (void)pIOHandler; //this avoids the compiler warning of unused element
  66. // simple check of file extension is enough for the moment
  67. std::string::size_type pos = pFile.find_last_of('.');
  68. // no file extension - can't read
  69. if( pos == std::string::npos)
  70. return false;
  71. std::string extension = pFile.substr( pos);
  72. for( std::string::iterator it = extension.begin(); it != extension.end(); ++it)
  73. *it = tolower( *it);
  74. return extension == ".mdl";
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Setup configuration properties
  78. void MDLImporter::SetupProperties(const Importer* pImp)
  79. {
  80. // The AI_CONFIG_IMPORT_MDL_KEYFRAME option overrides the
  81. // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
  82. if(0xffffffff == (configFrameID = pImp->GetPropertyInteger(
  83. AI_CONFIG_IMPORT_MDL_KEYFRAME,0xffffffff)))
  84. {
  85. configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
  86. }
  87. configPalette = pImp->GetPropertyString(AI_CONFIG_IMPORT_MDL_COLORMAP,"colormap.lmp");
  88. }
  89. // ------------------------------------------------------------------------------------------------
  90. // Get a list of all supported extensions
  91. void MDLImporter::GetExtensionList(std::string& append)
  92. {
  93. append.append( ".mdl" );
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Imports the given file into the given scene structure.
  97. void MDLImporter::InternReadFile( const std::string& pFile,
  98. aiScene* _pScene, IOSystem* _pIOHandler)
  99. {
  100. pScene = _pScene;
  101. pIOHandler = _pIOHandler;
  102. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  103. // Check whether we can read from the file
  104. if( file.get() == NULL)
  105. throw new ImportErrorException( "Failed to open MDL file " + pFile + ".");
  106. // This should work for all other types of MDL files, too ...
  107. // the quake header is one of the smallest, afaik
  108. iFileSize = (unsigned int)file->FileSize();
  109. if( iFileSize < sizeof(MDL::Header))
  110. throw new ImportErrorException( "MDL File is too small.");
  111. // Allocate storage and copy the contents of the file to a memory buffer
  112. mBuffer = new unsigned char[iFileSize+1];
  113. file->Read( (void*)mBuffer, 1, iFileSize);
  114. // Append a binary zero to the end of the buffer.
  115. // this is just for safety that string parsing routines
  116. // find the end of the buffer ...
  117. mBuffer[this->iFileSize] = '\0';
  118. const uint32_t iMagicWord = *((uint32_t*)this->mBuffer);
  119. // Determine the file subtype and call the appropriate member function
  120. try {
  121. // Original Quake1 format
  122. if (AI_MDL_MAGIC_NUMBER_BE == iMagicWord ||
  123. AI_MDL_MAGIC_NUMBER_LE == iMagicWord)
  124. {
  125. DefaultLogger::get()->debug("MDL subtype: Quake 1, magic word is IDPO");
  126. iGSFileVersion = 0;
  127. InternReadFile_Quake1();
  128. }
  129. // GameStudio A<old> MDL2 format - used by some test models that come with 3DGS
  130. else if (AI_MDL_MAGIC_NUMBER_BE_GS3 == iMagicWord ||
  131. AI_MDL_MAGIC_NUMBER_LE_GS3 == iMagicWord)
  132. {
  133. DefaultLogger::get()->debug("MDL subtype: 3D GameStudio A2, magic word is MDL2");
  134. iGSFileVersion = 2;
  135. InternReadFile_Quake1();
  136. }
  137. // GameStudio A4 MDL3 format
  138. else if (AI_MDL_MAGIC_NUMBER_BE_GS4 == iMagicWord ||
  139. AI_MDL_MAGIC_NUMBER_LE_GS4 == iMagicWord)
  140. {
  141. DefaultLogger::get()->debug("MDL subtype: 3D GameStudio A4, magic word is MDL3");
  142. iGSFileVersion = 3;
  143. InternReadFile_3DGS_MDL345();
  144. }
  145. // GameStudio A5+ MDL4 format
  146. else if (AI_MDL_MAGIC_NUMBER_BE_GS5a == iMagicWord ||
  147. AI_MDL_MAGIC_NUMBER_LE_GS5a == iMagicWord)
  148. {
  149. DefaultLogger::get()->debug("MDL subtype: 3D GameStudio A4, magic word is MDL4");
  150. iGSFileVersion = 4;
  151. InternReadFile_3DGS_MDL345();
  152. }
  153. // GameStudio A5+ MDL5 format
  154. else if (AI_MDL_MAGIC_NUMBER_BE_GS5b == iMagicWord ||
  155. AI_MDL_MAGIC_NUMBER_LE_GS5b == iMagicWord)
  156. {
  157. DefaultLogger::get()->debug("MDL subtype: 3D GameStudio A5, magic word is MDL5");
  158. iGSFileVersion = 5;
  159. InternReadFile_3DGS_MDL345();
  160. }
  161. // GameStudio A7 MDL7 format
  162. else if (AI_MDL_MAGIC_NUMBER_BE_GS7 == iMagicWord ||
  163. AI_MDL_MAGIC_NUMBER_LE_GS7 == iMagicWord)
  164. {
  165. DefaultLogger::get()->debug("MDL subtype: 3D GameStudio A7, magic word is MDL7");
  166. iGSFileVersion = 7;
  167. InternReadFile_3DGS_MDL7();
  168. }
  169. // IDST/IDSQ Format (CS:S/HL², etc ...)
  170. else if (AI_MDL_MAGIC_NUMBER_BE_HL2a == iMagicWord ||
  171. AI_MDL_MAGIC_NUMBER_LE_HL2a == iMagicWord ||
  172. AI_MDL_MAGIC_NUMBER_BE_HL2b == iMagicWord ||
  173. AI_MDL_MAGIC_NUMBER_LE_HL2b == iMagicWord)
  174. {
  175. DefaultLogger::get()->debug("MDL subtype: CS:S\\HL², magic word is IDST/IDSQ");
  176. iGSFileVersion = 0;
  177. InternReadFile_HL2();
  178. }
  179. else
  180. {
  181. // print the magic word to the logger
  182. char szBuffer[5];
  183. szBuffer[0] = ((char*)&iMagicWord)[0];
  184. szBuffer[1] = ((char*)&iMagicWord)[1];
  185. szBuffer[2] = ((char*)&iMagicWord)[2];
  186. szBuffer[3] = ((char*)&iMagicWord)[3];
  187. szBuffer[4] = '\0';
  188. // we're definitely unable to load this file
  189. throw new ImportErrorException( "Unknown MDL subformat " + pFile +
  190. ". Magic word (" + szBuffer + ") is not known");
  191. }
  192. }
  193. catch (ImportErrorException* ex) {
  194. delete[] this->mBuffer;
  195. AI_DEBUG_INVALIDATE_PTR(this->mBuffer);
  196. AI_DEBUG_INVALIDATE_PTR(this->pIOHandler);
  197. AI_DEBUG_INVALIDATE_PTR(this->pScene);
  198. throw ex;
  199. }
  200. // delete the file buffer and cleanup
  201. delete[] this->mBuffer;
  202. AI_DEBUG_INVALIDATE_PTR(this->mBuffer);
  203. AI_DEBUG_INVALIDATE_PTR(this->pIOHandler);
  204. AI_DEBUG_INVALIDATE_PTR(this->pScene);
  205. }
  206. // ------------------------------------------------------------------------------------------------
  207. // Check whether we're still inside the valid file range
  208. void MDLImporter::SizeCheck(const void* szPos)
  209. {
  210. if (!szPos || (const unsigned char*)szPos > this->mBuffer + this->iFileSize)
  211. {
  212. throw new ImportErrorException("Invalid MDL file. The file is too small "
  213. "or contains invalid data.");
  214. }
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. // Just for debgging purposes
  218. void MDLImporter::SizeCheck(const void* szPos, const char* szFile, unsigned int iLine)
  219. {
  220. ai_assert(NULL != szFile);
  221. if (!szPos || (const unsigned char*)szPos > mBuffer + iFileSize)
  222. {
  223. // remove a directory if there is one
  224. const char* szFilePtr = ::strrchr(szFile,'\\');
  225. if (!szFilePtr) {
  226. if(!(szFilePtr = ::strrchr(szFile,'/')))
  227. szFilePtr = szFile;
  228. }
  229. if (szFilePtr)++szFilePtr;
  230. char szBuffer[1024];
  231. ::sprintf(szBuffer,"Invalid MDL file. The file is too small "
  232. "or contains invalid data (File: %s Line: %i)",szFilePtr,iLine);
  233. throw new ImportErrorException(szBuffer);
  234. }
  235. }
  236. // ------------------------------------------------------------------------------------------------
  237. // Validate a quake file header
  238. void MDLImporter::ValidateHeader_Quake1(const MDL::Header* pcHeader)
  239. {
  240. // some values may not be NULL
  241. if (!pcHeader->num_frames)
  242. throw new ImportErrorException( "[Quake 1 MDL] There are no frames in the file");
  243. if (!pcHeader->num_verts)
  244. throw new ImportErrorException( "[Quake 1 MDL] There are no vertices in the file");
  245. if (!pcHeader->num_tris)
  246. throw new ImportErrorException( "[Quake 1 MDL] There are no triangles in the file");
  247. // check whether the maxima are exceeded ...however, this applies for Quake 1 MDLs only
  248. if (!this->iGSFileVersion)
  249. {
  250. if (pcHeader->num_verts > AI_MDL_MAX_VERTS)
  251. DefaultLogger::get()->warn("Quake 1 MDL model has more than AI_MDL_MAX_VERTS vertices");
  252. if (pcHeader->num_tris > AI_MDL_MAX_TRIANGLES)
  253. DefaultLogger::get()->warn("Quake 1 MDL model has more than AI_MDL_MAX_TRIANGLES triangles");
  254. if (pcHeader->num_frames > AI_MDL_MAX_FRAMES)
  255. DefaultLogger::get()->warn("Quake 1 MDL model has more than AI_MDL_MAX_FRAMES frames");
  256. // (this does not apply for 3DGS MDLs)
  257. if (!this->iGSFileVersion && pcHeader->version != AI_MDL_VERSION)
  258. DefaultLogger::get()->warn("Quake 1 MDL model has an unknown version: AI_MDL_VERSION (=6) is "
  259. "the expected file format version");
  260. if(pcHeader->num_skins && (!pcHeader->skinwidth || !pcHeader->skinheight))
  261. DefaultLogger::get()->warn("Skin width or height are 0");
  262. }
  263. }
  264. #ifdef AI_BUILD_BIG_ENDIAN
  265. // ------------------------------------------------------------------------------------------------
  266. void FlipQuakeHeader(BE_NCONST MDL::Header* pcHeader)
  267. {
  268. AI_SWAP4( pcHeader->ident);
  269. AI_SWAP4( pcHeader->version);
  270. AI_SWAP4( pcHeader->boundingradius);
  271. AI_SWAP4( pcHeader->flags);
  272. AI_SWAP4( pcHeader->num_frames);
  273. AI_SWAP4( pcHeader->num_skins);
  274. AI_SWAP4( pcHeader->num_tris);
  275. AI_SWAP4( pcHeader->num_verts);
  276. for (unsigned int i = 0; i < 3;++i)
  277. {
  278. AI_SWAP4( pcHeader->scale[i]);
  279. AI_SWAP4( pcHeader->translate[i]);
  280. }
  281. AI_SWAP4( pcHeader->size);
  282. AI_SWAP4( pcHeader->skinheight);
  283. AI_SWAP4( pcHeader->skinwidth);
  284. AI_SWAP4( pcHeader->synctype);
  285. }
  286. #endif
  287. // ------------------------------------------------------------------------------------------------
  288. // Read a Quake 1 file
  289. void MDLImporter::InternReadFile_Quake1( )
  290. {
  291. ai_assert(NULL != pScene);
  292. BE_NCONST MDL::Header *pcHeader = (BE_NCONST MDL::Header*)this->mBuffer;
  293. #ifdef AI_BUILD_BIG_ENDIAN
  294. FlipQuakeHeader(pcHeader);
  295. #endif
  296. ValidateHeader_Quake1(pcHeader);
  297. // current cursor position in the file
  298. const unsigned char* szCurrent = (const unsigned char*)(pcHeader+1);
  299. // need to read all textures
  300. for (unsigned int i = 0; i < (unsigned int)pcHeader->num_skins;++i)
  301. {
  302. union{BE_NCONST MDL::Skin* pcSkin;BE_NCONST MDL::GroupSkin* pcGroupSkin;};
  303. pcSkin = (BE_NCONST MDL::Skin*)szCurrent;
  304. AI_SWAP4( pcSkin->group );
  305. // Quake 1 groupskins
  306. if (1 == pcSkin->group)
  307. {
  308. AI_SWAP4( pcGroupSkin->nb );
  309. // need to skip multiple images
  310. const unsigned int iNumImages = (unsigned int)pcGroupSkin->nb;
  311. szCurrent += sizeof(uint32_t) * 2;
  312. if (0 != iNumImages)
  313. {
  314. if (!i) {
  315. // however, create only one output image (the first)
  316. this->CreateTextureARGB8_3DGS_MDL3(szCurrent + iNumImages * sizeof(float));
  317. }
  318. // go to the end of the skin section / the beginning of the next skin
  319. szCurrent += pcHeader->skinheight * pcHeader->skinwidth +
  320. sizeof(float) * iNumImages;
  321. }
  322. }
  323. // 3DGS has a few files that are using other 3DGS like texture formats here
  324. else
  325. {
  326. szCurrent += sizeof(uint32_t);
  327. unsigned int iSkip = i ? 0xffffffff : 0;
  328. this->CreateTexture_3DGS_MDL4(szCurrent,pcSkin->group,&iSkip);
  329. szCurrent += iSkip;
  330. }
  331. }
  332. // get a pointer to the texture coordinates
  333. BE_NCONST MDL::TexCoord* pcTexCoords = (BE_NCONST MDL::TexCoord*)szCurrent;
  334. szCurrent += sizeof(MDL::TexCoord) * pcHeader->num_verts;
  335. // get a pointer to the triangles
  336. BE_NCONST MDL::Triangle* pcTriangles = (BE_NCONST MDL::Triangle*)szCurrent;
  337. szCurrent += sizeof(MDL::Triangle) * pcHeader->num_tris;
  338. VALIDATE_FILE_SIZE(szCurrent);
  339. // now get a pointer to the first frame in the file
  340. BE_NCONST MDL::Frame* pcFrames = (BE_NCONST MDL::Frame*)szCurrent;
  341. BE_NCONST MDL::SimpleFrame* pcFirstFrame;
  342. if (0 == pcFrames->type)
  343. {
  344. // get address of single frame
  345. pcFirstFrame = &pcFrames->frame;
  346. }
  347. else
  348. {
  349. // get the first frame in the group
  350. BE_NCONST MDL::GroupFrame* pcFrames2 = (BE_NCONST MDL::GroupFrame*)pcFrames;
  351. pcFirstFrame = (BE_NCONST MDL::SimpleFrame*)(&pcFrames2->time + pcFrames->type);
  352. }
  353. BE_NCONST MDL::Vertex* pcVertices = (BE_NCONST MDL::Vertex*) ((pcFirstFrame->name) + sizeof(pcFirstFrame->name));
  354. VALIDATE_FILE_SIZE((const unsigned char*)(pcVertices + pcHeader->num_verts));
  355. #ifdef AI_BUILD_BIG_ENDIAN
  356. for (int i = 0; i<pcHeader->num_verts;++i)
  357. {
  358. AI_SWAP4( pcTexCoords[i].onseam );
  359. AI_SWAP4( pcTexCoords[i].s );
  360. AI_SWAP4( pcTexCoords[i].t );
  361. }
  362. for (int i = 0; i<pcHeader->num_tris;++i)
  363. {
  364. AI_SWAP4( pcTriangles[i].facesfront);
  365. AI_SWAP4( pcTriangles[i].vertex[0]);
  366. AI_SWAP4( pcTriangles[i].vertex[1]);
  367. AI_SWAP4( pcTriangles[i].vertex[2]);
  368. }
  369. #endif
  370. // setup materials
  371. SetupMaterialProperties_3DGS_MDL5_Quake1();
  372. // allocate enough storage to hold all vertices and triangles
  373. aiMesh* pcMesh = new aiMesh();
  374. pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  375. pcMesh->mNumVertices = pcHeader->num_tris * 3;
  376. pcMesh->mNumFaces = pcHeader->num_tris;
  377. pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
  378. pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
  379. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  380. pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
  381. pcMesh->mNumUVComponents[0] = 2;
  382. // there won't be more than one mesh inside the file
  383. pScene->mRootNode = new aiNode();
  384. pScene->mRootNode->mNumMeshes = 1;
  385. pScene->mRootNode->mMeshes = new unsigned int[1];
  386. pScene->mRootNode->mMeshes[0] = 0;
  387. pScene->mNumMeshes = 1;
  388. pScene->mMeshes = new aiMesh*[1];
  389. pScene->mMeshes[0] = pcMesh;
  390. // now iterate through all triangles
  391. unsigned int iCurrent = 0;
  392. for (unsigned int i = 0; i < (unsigned int) pcHeader->num_tris;++i)
  393. {
  394. pcMesh->mFaces[i].mIndices = new unsigned int[3];
  395. pcMesh->mFaces[i].mNumIndices = 3;
  396. unsigned int iTemp = iCurrent;
  397. for (unsigned int c = 0; c < 3;++c,++iCurrent)
  398. {
  399. pcMesh->mFaces[i].mIndices[c] = iCurrent;
  400. // read vertices
  401. unsigned int iIndex = pcTriangles->vertex[c];
  402. if (iIndex >= (unsigned int)pcHeader->num_verts)
  403. {
  404. iIndex = pcHeader->num_verts-1;
  405. DefaultLogger::get()->warn("Index overflow in Q1-MDL vertex list.");
  406. }
  407. aiVector3D& vec = pcMesh->mVertices[iCurrent];
  408. vec.x = (float)pcVertices[iIndex].v[0] * pcHeader->scale[0];
  409. vec.x += pcHeader->translate[0];
  410. vec.y = (float)pcVertices[iIndex].v[1] * pcHeader->scale[1];
  411. vec.y += pcHeader->translate[1];
  412. vec.y *= -1.0f;
  413. vec.z = (float)pcVertices[iIndex].v[2] * pcHeader->scale[2];
  414. vec.z += pcHeader->translate[2];
  415. // read the normal vector from the precalculated normal table
  416. MD2::LookupNormalIndex(pcVertices[iIndex].normalIndex,pcMesh->mNormals[iCurrent]);
  417. pcMesh->mNormals[iCurrent].y *= -1.0f;
  418. // read texture coordinates
  419. float s = (float)pcTexCoords[iIndex].s;
  420. float t = (float)pcTexCoords[iIndex].t;
  421. // translate texture coordinates
  422. if (0 == pcTriangles->facesfront &&
  423. 0 != pcTexCoords[iIndex].onseam)
  424. {
  425. s += pcHeader->skinwidth * 0.5f;
  426. }
  427. // Scale s and t to range from 0.0 to 1.0
  428. pcMesh->mTextureCoords[0][iCurrent].x = (s + 0.5f) / pcHeader->skinwidth;
  429. pcMesh->mTextureCoords[0][iCurrent].y = 1.0f-(t + 0.5f) / pcHeader->skinheight;
  430. }
  431. pcMesh->mFaces[i].mIndices[0] = iTemp+2;
  432. pcMesh->mFaces[i].mIndices[1] = iTemp+1;
  433. pcMesh->mFaces[i].mIndices[2] = iTemp+0;
  434. pcTriangles++;
  435. }
  436. return;
  437. }
  438. // ------------------------------------------------------------------------------------------------
  439. // Setup material properties for Quake and older GameStudio files
  440. void MDLImporter::SetupMaterialProperties_3DGS_MDL5_Quake1( )
  441. {
  442. const MDL::Header* const pcHeader = (const MDL::Header*)this->mBuffer;
  443. // allocate ONE material
  444. pScene->mMaterials = new aiMaterial*[1];
  445. pScene->mMaterials[0] = new MaterialHelper();
  446. pScene->mNumMaterials = 1;
  447. // setup the material's properties
  448. const int iMode = (int)aiShadingMode_Gouraud;
  449. MaterialHelper* const pcHelper = (MaterialHelper*)pScene->mMaterials[0];
  450. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  451. aiColor4D clr;
  452. if (0 != pcHeader->num_skins && pScene->mNumTextures) {
  453. // can we replace the texture with a single color?
  454. clr = this->ReplaceTextureWithColor(pScene->mTextures[0]);
  455. if (is_not_qnan(clr.r))
  456. {
  457. delete pScene->mTextures[0];
  458. delete[] pScene->mTextures;
  459. pScene->mNumTextures = 0;
  460. }
  461. else
  462. {
  463. clr.b = clr.a = clr.g = clr.r = 1.0f;
  464. aiString szString;
  465. ::memcpy(szString.data,AI_MAKE_EMBEDDED_TEXNAME(0),3);
  466. szString.length = 2;
  467. pcHelper->AddProperty(&szString,AI_MATKEY_TEXTURE_DIFFUSE(0));
  468. }
  469. }
  470. pcHelper->AddProperty<aiColor4D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  471. pcHelper->AddProperty<aiColor4D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  472. clr.r *= 0.05f;clr.g *= 0.05f;
  473. clr.b *= 0.05f;clr.a = 1.0f;
  474. pcHelper->AddProperty<aiColor4D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  475. }
  476. // ------------------------------------------------------------------------------------------------
  477. // Read a MDL 3,4,5 file
  478. void MDLImporter::InternReadFile_3DGS_MDL345( )
  479. {
  480. ai_assert(NULL != pScene);
  481. // the header of MDL 3/4/5 is nearly identical to the original Quake1 header
  482. BE_NCONST MDL::Header *pcHeader = (BE_NCONST MDL::Header*)this->mBuffer;
  483. #ifdef AI_BUILD_BIG_ENDIAN
  484. FlipQuakeHeader(pcHeader);
  485. #endif
  486. ValidateHeader_Quake1(pcHeader);
  487. // current cursor position in the file
  488. const unsigned char* szCurrent = (const unsigned char*)(pcHeader+1);
  489. // need to read all textures
  490. for (unsigned int i = 0; i < (unsigned int)pcHeader->num_skins;++i) {
  491. BE_NCONST MDL::Skin* pcSkin;
  492. pcSkin = (BE_NCONST MDL::Skin*)szCurrent;
  493. AI_SWAP4( pcSkin->group);
  494. // create one output image
  495. unsigned int iSkip = i ? 0xffffffff : 0;
  496. if (5 <= iGSFileVersion)
  497. {
  498. // MDL5 format could contain MIPmaps
  499. CreateTexture_3DGS_MDL5((unsigned char*)pcSkin + sizeof(uint32_t),
  500. pcSkin->group,&iSkip);
  501. }
  502. else {
  503. CreateTexture_3DGS_MDL4((unsigned char*)pcSkin + sizeof(uint32_t),
  504. pcSkin->group,&iSkip);
  505. }
  506. // need to skip one image
  507. szCurrent += iSkip + sizeof(uint32_t);
  508. }
  509. // get a pointer to the texture coordinates
  510. BE_NCONST MDL::TexCoord_MDL3* pcTexCoords = (BE_NCONST MDL::TexCoord_MDL3*)szCurrent;
  511. szCurrent += sizeof(MDL::TexCoord_MDL3) * pcHeader->synctype;
  512. // NOTE: for MDLn formats "synctype" corresponds to the number of UV coords
  513. // get a pointer to the triangles
  514. BE_NCONST MDL::Triangle_MDL3* pcTriangles = (BE_NCONST MDL::Triangle_MDL3*)szCurrent;
  515. szCurrent += sizeof(MDL::Triangle_MDL3) * pcHeader->num_tris;
  516. #ifdef AI_BUILD_BIG_ENDIAN
  517. for (int i = 0; i<pcHeader->synctype;++i)
  518. {
  519. AI_SWAP2( pcTexCoords[i].u );
  520. AI_SWAP2( pcTexCoords[i].v );
  521. }
  522. for (int i = 0; i<pcHeader->num_tris;++i)
  523. {
  524. AI_SWAP2( pcTriangles[i].index_xyz[0]);
  525. AI_SWAP2( pcTriangles[i].index_xyz[1]);
  526. AI_SWAP2( pcTriangles[i].index_xyz[2]);
  527. AI_SWAP2( pcTriangles[i].index_uv[0]);
  528. AI_SWAP2( pcTriangles[i].index_uv[1]);
  529. AI_SWAP2( pcTriangles[i].index_uv[2]);
  530. }
  531. #endif
  532. VALIDATE_FILE_SIZE(szCurrent);
  533. // setup materials
  534. SetupMaterialProperties_3DGS_MDL5_Quake1();
  535. // allocate enough storage to hold all vertices and triangles
  536. aiMesh* pcMesh = new aiMesh();
  537. pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  538. pcMesh->mNumVertices = pcHeader->num_tris * 3;
  539. pcMesh->mNumFaces = pcHeader->num_tris;
  540. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  541. // there won't be more than one mesh inside the file
  542. pScene->mRootNode = new aiNode();
  543. pScene->mRootNode->mNumMeshes = 1;
  544. pScene->mRootNode->mMeshes = new unsigned int[1];
  545. pScene->mRootNode->mMeshes[0] = 0;
  546. pScene->mNumMeshes = 1;
  547. pScene->mMeshes = new aiMesh*[1];
  548. pScene->mMeshes[0] = pcMesh;
  549. // allocate output storage
  550. pcMesh->mNumVertices = (unsigned int)pcHeader->num_tris*3;
  551. pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
  552. pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
  553. if (pcHeader->synctype)
  554. {
  555. pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
  556. pcMesh->mNumUVComponents[0] = 2;
  557. }
  558. // now get a pointer to the first frame in the file
  559. BE_NCONST MDL::Frame* pcFrames = (BE_NCONST MDL::Frame*)szCurrent;
  560. AI_SWAP4(pcFrames->type);
  561. // byte packed vertices
  562. // FIXME: these two snippets are nearly totally identical ...
  563. /////////////////////////////////////////////////////////////////////////////////////
  564. if (0 == pcFrames->type || 3 >= this->iGSFileVersion)
  565. {
  566. const MDL::SimpleFrame* pcFirstFrame = (const MDL::SimpleFrame*)(szCurrent + sizeof(uint32_t));
  567. const MDL::Vertex* pcVertices = (const MDL::Vertex*) ((pcFirstFrame->name) + sizeof(pcFirstFrame->name));
  568. VALIDATE_FILE_SIZE(pcVertices + pcHeader->num_verts);
  569. // now iterate through all triangles
  570. unsigned int iCurrent = 0;
  571. for (unsigned int i = 0; i < (unsigned int) pcHeader->num_tris;++i)
  572. {
  573. pcMesh->mFaces[i].mIndices = new unsigned int[3];
  574. pcMesh->mFaces[i].mNumIndices = 3;
  575. unsigned int iTemp = iCurrent;
  576. for (unsigned int c = 0; c < 3;++c,++iCurrent)
  577. {
  578. // read vertices
  579. unsigned int iIndex = pcTriangles->index_xyz[c];
  580. if (iIndex >= (unsigned int)pcHeader->num_verts)
  581. {
  582. iIndex = pcHeader->num_verts-1;
  583. DefaultLogger::get()->warn("Index overflow in MDLn vertex list");
  584. }
  585. aiVector3D& vec = pcMesh->mVertices[iCurrent];
  586. vec.x = (float)pcVertices[iIndex].v[0] * pcHeader->scale[0];
  587. vec.x += pcHeader->translate[0];
  588. vec.y = (float)pcVertices[iIndex].v[1] * pcHeader->scale[1];
  589. vec.y += pcHeader->translate[1];
  590. vec.y *= -1.0f;
  591. vec.z = (float)pcVertices[iIndex].v[2] * pcHeader->scale[2];
  592. vec.z += pcHeader->translate[2];
  593. // read the normal vector from the precalculated normal table
  594. MD2::LookupNormalIndex(pcVertices[iIndex].normalIndex,pcMesh->mNormals[iCurrent]);
  595. pcMesh->mNormals[iCurrent].y *= -1.0f;
  596. // read texture coordinates
  597. if (pcHeader->synctype) {
  598. ImportUVCoordinate_3DGS_MDL345(pcMesh->mTextureCoords[0][iCurrent],
  599. pcTexCoords,pcTriangles->index_uv[c]);
  600. }
  601. }
  602. pcMesh->mFaces[i].mIndices[0] = iTemp+2;
  603. pcMesh->mFaces[i].mIndices[1] = iTemp+1;
  604. pcMesh->mFaces[i].mIndices[2] = iTemp+0;
  605. pcTriangles++;
  606. }
  607. }
  608. // short packed vertices
  609. /////////////////////////////////////////////////////////////////////////////////////
  610. else
  611. {
  612. // now get a pointer to the first frame in the file
  613. const MDL::SimpleFrame_MDLn_SP* pcFirstFrame = (const MDL::SimpleFrame_MDLn_SP*) (szCurrent + sizeof(uint32_t));
  614. // get a pointer to the vertices
  615. const MDL::Vertex_MDL4* pcVertices = (const MDL::Vertex_MDL4*) ((pcFirstFrame->name) +
  616. sizeof(pcFirstFrame->name));
  617. VALIDATE_FILE_SIZE(pcVertices + pcHeader->num_verts);
  618. // now iterate through all triangles
  619. unsigned int iCurrent = 0;
  620. for (unsigned int i = 0; i < (unsigned int) pcHeader->num_tris;++i)
  621. {
  622. pcMesh->mFaces[i].mIndices = new unsigned int[3];
  623. pcMesh->mFaces[i].mNumIndices = 3;
  624. unsigned int iTemp = iCurrent;
  625. for (unsigned int c = 0; c < 3;++c,++iCurrent)
  626. {
  627. // read vertices
  628. unsigned int iIndex = pcTriangles->index_xyz[c];
  629. if (iIndex >= (unsigned int)pcHeader->num_verts)
  630. {
  631. iIndex = pcHeader->num_verts-1;
  632. DefaultLogger::get()->warn("Index overflow in MDLn vertex list");
  633. }
  634. aiVector3D& vec = pcMesh->mVertices[iCurrent];
  635. vec.x = (float)pcVertices[iIndex].v[0] * pcHeader->scale[0];
  636. vec.x += pcHeader->translate[0];
  637. vec.y = (float)pcVertices[iIndex].v[1] * pcHeader->scale[1];
  638. vec.y += pcHeader->translate[1];
  639. vec.y *= -1.0f;
  640. vec.z = (float)pcVertices[iIndex].v[2] * pcHeader->scale[2];
  641. vec.z += pcHeader->translate[2];
  642. // read the normal vector from the precalculated normal table
  643. MD2::LookupNormalIndex(pcVertices[iIndex].normalIndex,pcMesh->mNormals[iCurrent]);
  644. pcMesh->mNormals[iCurrent].y *= -1.0f;
  645. // read texture coordinates
  646. if (pcHeader->synctype) {
  647. ImportUVCoordinate_3DGS_MDL345(pcMesh->mTextureCoords[0][iCurrent],
  648. pcTexCoords,pcTriangles->index_uv[c]);
  649. }
  650. }
  651. pcMesh->mFaces[i].mIndices[0] = iTemp+2;
  652. pcMesh->mFaces[i].mIndices[1] = iTemp+1;
  653. pcMesh->mFaces[i].mIndices[2] = iTemp+0;
  654. pcTriangles++;
  655. }
  656. }
  657. // For MDL5 we will need to build valid texture coordinates
  658. // basing upon the file loaded (only support one file as skin)
  659. if (0x5 == this->iGSFileVersion)
  660. this->CalculateUVCoordinates_MDL5();
  661. return;
  662. }
  663. // ------------------------------------------------------------------------------------------------
  664. // Get a single UV coordinate for Quake and older GameStudio files
  665. void MDLImporter::ImportUVCoordinate_3DGS_MDL345(
  666. aiVector3D& vOut,
  667. const MDL::TexCoord_MDL3* pcSrc,
  668. unsigned int iIndex)
  669. {
  670. ai_assert(NULL != pcSrc);
  671. const MDL::Header* const pcHeader = (const MDL::Header*)this->mBuffer;
  672. // validate UV indices
  673. if (iIndex >= (unsigned int) pcHeader->synctype)
  674. {
  675. iIndex = pcHeader->synctype-1;
  676. DefaultLogger::get()->warn("Index overflow in MDLn UV coord list");
  677. }
  678. float s = (float)pcSrc[iIndex].u;
  679. float t = (float)pcSrc[iIndex].v;
  680. // Scale s and t to range from 0.0 to 1.0
  681. if (0x5 != this->iGSFileVersion) {
  682. s = (s + 0.5f) / pcHeader->skinwidth;
  683. t = 1.0f-(t + 0.5f) / pcHeader->skinheight;
  684. }
  685. vOut.x = s;
  686. vOut.y = t;
  687. vOut.z = 0.0f;
  688. }
  689. // ------------------------------------------------------------------------------------------------
  690. // Compute UV coordinates for a MDL5 file
  691. void MDLImporter::CalculateUVCoordinates_MDL5()
  692. {
  693. const MDL::Header* const pcHeader = (const MDL::Header*)this->mBuffer;
  694. if (pcHeader->num_skins && this->pScene->mNumTextures)
  695. {
  696. const aiTexture* pcTex = this->pScene->mTextures[0];
  697. // if the file is loaded in DDS format: get the size of the
  698. // texture from the header of the DDS file
  699. // skip three DWORDs and read first height, then the width
  700. unsigned int iWidth, iHeight;
  701. if (!pcTex->mHeight)
  702. {
  703. const uint32_t* piPtr = (uint32_t*)pcTex->pcData;
  704. piPtr += 3;
  705. iHeight = (unsigned int)*piPtr++;
  706. iWidth = (unsigned int)*piPtr;
  707. if (!iHeight || !iWidth)
  708. {
  709. DefaultLogger::get()->warn("Either the width or the height of the "
  710. "embedded DDS texture is zero. Unable to compute final texture "
  711. "coordinates. The texture coordinates remain in their original "
  712. "0-x/0-y (x,y = texture size) range.");
  713. iWidth = 1;
  714. iHeight = 1;
  715. }
  716. }
  717. else
  718. {
  719. iWidth = pcTex->mWidth;
  720. iHeight = pcTex->mHeight;
  721. }
  722. if (1 != iWidth || 1 != iHeight) {
  723. const float fWidth = (float)iWidth;
  724. const float fHeight = (float)iHeight;
  725. aiMesh* pcMesh = this->pScene->mMeshes[0];
  726. for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
  727. {
  728. pcMesh->mTextureCoords[0][i].x /= fWidth;
  729. pcMesh->mTextureCoords[0][i].y /= fHeight;
  730. pcMesh->mTextureCoords[0][i].y = 1.0f - pcMesh->mTextureCoords[0][i].y; // DX to OGL
  731. }
  732. }
  733. }
  734. }
  735. // ------------------------------------------------------------------------------------------------
  736. // Validate the header of a MDL7 file
  737. void MDLImporter::ValidateHeader_3DGS_MDL7(const MDL::Header_MDL7* pcHeader)
  738. {
  739. ai_assert(NULL != pcHeader);
  740. // There are some fixed sizes ...
  741. if (sizeof(MDL::ColorValue_MDL7) != pcHeader->colorvalue_stc_size) {
  742. throw new ImportErrorException(
  743. "[3DGS MDL7] sizeof(MDL::ColorValue_MDL7) != pcHeader->colorvalue_stc_size");
  744. }
  745. if (sizeof(MDL::TexCoord_MDL7) != pcHeader->skinpoint_stc_size) {
  746. throw new ImportErrorException(
  747. "[3DGS MDL7] sizeof(MDL::TexCoord_MDL7) != pcHeader->skinpoint_stc_size");
  748. }
  749. if (sizeof(MDL::Skin_MDL7) != pcHeader->skin_stc_size) {
  750. throw new ImportErrorException(
  751. "sizeof(MDL::Skin_MDL7) != pcHeader->skin_stc_size");
  752. }
  753. // if there are no groups ... how should we load such a file?
  754. if(!pcHeader->groups_num) {
  755. throw new ImportErrorException( "[3DGS MDL7] No frames found");
  756. }
  757. }
  758. // ------------------------------------------------------------------------------------------------
  759. // resolve bone animation matrices
  760. void MDLImporter::CalcAbsBoneMatrices_3DGS_MDL7(MDL::IntBone_MDL7** apcOutBones)
  761. {
  762. const MDL::Header_MDL7 *pcHeader = (const MDL::Header_MDL7*)this->mBuffer;
  763. const MDL::Bone_MDL7* pcBones = (const MDL::Bone_MDL7*)(pcHeader+1);
  764. ai_assert(NULL != apcOutBones);
  765. // first find the bone that has NO parent, calculate the
  766. // animation matrix for it, then go on and search for the next parent
  767. // index (0) and so on until we can't find a new node.
  768. uint16_t iParent = 0xffff;
  769. uint32_t iIterations = 0;
  770. while (iIterations++ < pcHeader->bones_num)
  771. {
  772. for (uint32_t iBone = 0; iBone < pcHeader->bones_num;++iBone)
  773. {
  774. BE_NCONST MDL::Bone_MDL7* pcBone = _AI_MDL7_ACCESS_PTR(pcBones,iBone,
  775. pcHeader->bone_stc_size,MDL::Bone_MDL7);
  776. AI_SWAP2(pcBone->parent_index);
  777. AI_SWAP4(pcBone->x);
  778. AI_SWAP4(pcBone->y);
  779. AI_SWAP4(pcBone->z);
  780. if (iParent == pcBone->parent_index)
  781. {
  782. // MDL7 readme
  783. ////////////////////////////////////////////////////////////////
  784. /*
  785. The animation matrix is then calculated the following way:
  786. vector3 bPos = <absolute bone position>
  787. matrix44 laM; // local animation matrix
  788. sphrvector key_rotate = <bone rotation>
  789. matrix44 m1,m2;
  790. create_trans_matrix(m1, -bPos.x, -bPos.y, -bPos.z);
  791. create_trans_matrix(m2, -bPos.x, -bPos.y, -bPos.z);
  792. create_rotation_matrix(laM,key_rotate);
  793. laM = sm1 * laM;
  794. laM = laM * sm2;
  795. */
  796. /////////////////////////////////////////////////////////////////
  797. MDL::IntBone_MDL7* const pcOutBone = apcOutBones[iBone];
  798. // store the parent index of the bone
  799. pcOutBone->iParent = pcBone->parent_index;
  800. if (0xffff != iParent)
  801. {
  802. const MDL::IntBone_MDL7* pcParentBone = apcOutBones[iParent];
  803. pcOutBone->mOffsetMatrix.a4 = -pcParentBone->vPosition.x;
  804. pcOutBone->mOffsetMatrix.b4 = -pcParentBone->vPosition.y;
  805. pcOutBone->mOffsetMatrix.c4 = -pcParentBone->vPosition.z;
  806. }
  807. pcOutBone->vPosition.x = pcBone->x;
  808. pcOutBone->vPosition.y = pcBone->y;
  809. pcOutBone->vPosition.z = pcBone->z;
  810. pcOutBone->mOffsetMatrix.a4 -= pcBone->x;
  811. pcOutBone->mOffsetMatrix.b4 -= pcBone->y;
  812. pcOutBone->mOffsetMatrix.c4 -= pcBone->z;
  813. if (AI_MDL7_BONE_STRUCT_SIZE__NAME_IS_NOT_THERE == pcHeader->bone_stc_size)
  814. {
  815. // no real name for our poor bone is specified :-(
  816. pcOutBone->mName.length = ::sprintf(pcOutBone->mName.data,
  817. "UnnamedBone_%i",iBone);
  818. }
  819. else
  820. {
  821. // Make sure we won't run over the buffer's end if there is no
  822. // terminal 0 character (however the documentation says there
  823. // should be one)
  824. uint32_t iMaxLen = pcHeader->bone_stc_size-16;
  825. for (uint32_t qq = 0; qq < iMaxLen;++qq) {
  826. if (!pcBone->name[qq]) {
  827. iMaxLen = qq;
  828. break;
  829. }
  830. }
  831. // store the name of the bone
  832. pcOutBone->mName.length = (size_t)iMaxLen;
  833. ::memcpy(pcOutBone->mName.data,pcBone->name,pcOutBone->mName.length);
  834. pcOutBone->mName.data[pcOutBone->mName.length] = '\0';
  835. }
  836. }
  837. }
  838. ++iParent;
  839. }
  840. }
  841. // ------------------------------------------------------------------------------------------------
  842. // read bones from a MDL7 file
  843. MDL::IntBone_MDL7** MDLImporter::LoadBones_3DGS_MDL7()
  844. {
  845. const MDL::Header_MDL7 *pcHeader = (const MDL::Header_MDL7*)this->mBuffer;
  846. if (pcHeader->bones_num)
  847. {
  848. // validate the size of the bone data structure in the file
  849. if (AI_MDL7_BONE_STRUCT_SIZE__NAME_IS_20_CHARS != pcHeader->bone_stc_size &&
  850. AI_MDL7_BONE_STRUCT_SIZE__NAME_IS_32_CHARS != pcHeader->bone_stc_size &&
  851. AI_MDL7_BONE_STRUCT_SIZE__NAME_IS_NOT_THERE != pcHeader->bone_stc_size)
  852. {
  853. DefaultLogger::get()->warn("Unknown size of bone data structure");
  854. return NULL;
  855. }
  856. MDL::IntBone_MDL7** apcBonesOut = new MDL::IntBone_MDL7*[pcHeader->bones_num];
  857. for (uint32_t crank = 0; crank < pcHeader->bones_num;++crank)
  858. apcBonesOut[crank] = new MDL::IntBone_MDL7();
  859. // and calculate absolute bone offset matrices ...
  860. CalcAbsBoneMatrices_3DGS_MDL7(apcBonesOut);
  861. return apcBonesOut;
  862. }
  863. return NULL;
  864. }
  865. // ------------------------------------------------------------------------------------------------
  866. // read faces from a MDL7 file
  867. void MDLImporter::ReadFaces_3DGS_MDL7(
  868. const MDL::IntGroupInfo_MDL7& groupInfo,
  869. MDL::IntGroupData_MDL7& groupData)
  870. {
  871. const MDL::Header_MDL7 *pcHeader = (const MDL::Header_MDL7*)this->mBuffer;
  872. BE_NCONST MDL::Triangle_MDL7* pcGroupTris = groupInfo.pcGroupTris;
  873. // iterate through all triangles and build valid display lists
  874. unsigned int iOutIndex = 0;
  875. for (unsigned int iTriangle = 0; iTriangle < (unsigned int)groupInfo.pcGroup->numtris; ++iTriangle)
  876. {
  877. AI_SWAP2(pcGroupTris->v_index[0]);
  878. AI_SWAP2(pcGroupTris->v_index[1]);
  879. AI_SWAP2(pcGroupTris->v_index[2]);
  880. // iterate through all indices of the current triangle
  881. for (unsigned int c = 0; c < 3;++c,++iOutIndex)
  882. {
  883. // validate the vertex index
  884. unsigned int iIndex = pcGroupTris->v_index[c];
  885. if(iIndex > (unsigned int)groupInfo.pcGroup->numverts)
  886. {
  887. // (we might need to read this section a second time - to process frame vertices correctly)
  888. const_cast<MDL::Triangle_MDL7*>(pcGroupTris)->v_index[c] = iIndex = groupInfo.pcGroup->numverts-1;
  889. DefaultLogger::get()->warn("Index overflow in MDL7 vertex list");
  890. }
  891. // write the output face index
  892. groupData.pcFaces[iTriangle].mIndices[2-c] = iOutIndex;
  893. aiVector3D& vPosition = groupData.vPositions[ iOutIndex ];
  894. vPosition.x = _AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex, pcHeader->mainvertex_stc_size) .x;
  895. vPosition.y = -1.0f*_AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex,pcHeader->mainvertex_stc_size) .y;
  896. vPosition.z = _AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex,pcHeader->mainvertex_stc_size) .z;
  897. // if we have bones, save the index
  898. if (!groupData.aiBones.empty())
  899. groupData.aiBones[iOutIndex] = _AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,
  900. iIndex,pcHeader->mainvertex_stc_size).vertindex;
  901. // now read the normal vector
  902. if (AI_MDL7_FRAMEVERTEX030305_STCSIZE <= pcHeader->mainvertex_stc_size)
  903. {
  904. // read the full normal vector
  905. aiVector3D& vNormal = groupData.vNormals[ iOutIndex ];
  906. vNormal.x = _AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex,pcHeader->mainvertex_stc_size) .norm[0];
  907. AI_SWAP4(vNormal.x);
  908. vNormal.y = -1.0f*_AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex,pcHeader->mainvertex_stc_size) .norm[1];
  909. AI_SWAP4(vNormal.y);
  910. vNormal.z = _AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex,pcHeader->mainvertex_stc_size) .norm[2];
  911. AI_SWAP4(vNormal.z);
  912. }
  913. else if (AI_MDL7_FRAMEVERTEX120503_STCSIZE <= pcHeader->mainvertex_stc_size)
  914. {
  915. // read the normal vector from Quake2's smart table
  916. aiVector3D& vNormal = groupData.vNormals[ iOutIndex ];
  917. MD2::LookupNormalIndex(_AI_MDL7_ACCESS_VERT(groupInfo.pcGroupVerts,iIndex,
  918. pcHeader->mainvertex_stc_size) .norm162index,vNormal);
  919. vNormal.y *= -1.0f;
  920. }
  921. // validate and process the first uv coordinate set
  922. if (pcHeader->triangle_stc_size >= AI_MDL7_TRIANGLE_STD_SIZE_ONE_UV)
  923. {
  924. if (groupInfo.pcGroup->num_stpts)
  925. {
  926. AI_SWAP2(pcGroupTris->skinsets[0].st_index[0]);
  927. AI_SWAP2(pcGroupTris->skinsets[0].st_index[1]);
  928. AI_SWAP2(pcGroupTris->skinsets[0].st_index[2]);
  929. iIndex = pcGroupTris->skinsets[0].st_index[c];
  930. if(iIndex > (unsigned int)groupInfo.pcGroup->num_stpts)
  931. {
  932. iIndex = groupInfo.pcGroup->num_stpts-1;
  933. DefaultLogger::get()->warn("Index overflow in MDL7 UV coordinate list (#1)");
  934. }
  935. float u = groupInfo.pcGroupUVs[iIndex].u;
  936. float v = 1.0f-groupInfo.pcGroupUVs[iIndex].v; // DX to OGL
  937. groupData.vTextureCoords1[iOutIndex].x = u;
  938. groupData.vTextureCoords1[iOutIndex].y = v;
  939. }
  940. // assign the material index, but only if it is existing
  941. if (pcHeader->triangle_stc_size >= AI_MDL7_TRIANGLE_STD_SIZE_ONE_UV_WITH_MATINDEX){
  942. AI_SWAP4(pcGroupTris->skinsets[0].material);
  943. groupData.pcFaces[iTriangle].iMatIndex[0] = pcGroupTris->skinsets[0].material;
  944. }
  945. }
  946. // validate and process the second uv coordinate set
  947. if (pcHeader->triangle_stc_size >= AI_MDL7_TRIANGLE_STD_SIZE_TWO_UV)
  948. {
  949. if (groupInfo.pcGroup->num_stpts)
  950. {
  951. AI_SWAP2(pcGroupTris->skinsets[1].st_index[0]);
  952. AI_SWAP2(pcGroupTris->skinsets[1].st_index[1]);
  953. AI_SWAP2(pcGroupTris->skinsets[1].st_index[2]);
  954. AI_SWAP4(pcGroupTris->skinsets[1].material);
  955. iIndex = pcGroupTris->skinsets[1].st_index[c];
  956. if(iIndex > (unsigned int)groupInfo.pcGroup->num_stpts)
  957. {
  958. iIndex = groupInfo.pcGroup->num_stpts-1;
  959. DefaultLogger::get()->warn("Index overflow in MDL7 UV coordinate list (#2)");
  960. }
  961. float u = groupInfo.pcGroupUVs[ iIndex ].u;
  962. float v = 1.0f-groupInfo.pcGroupUVs[ iIndex ].v;
  963. groupData.vTextureCoords2[ iOutIndex ].x = u;
  964. groupData.vTextureCoords2[ iOutIndex ].y = v; // DX to OGL
  965. // check whether we do really need the second texture
  966. // coordinate set ... wastes memory and loading time
  967. if (0 != iIndex && (u != groupData.vTextureCoords1[ iOutIndex ].x ||
  968. v != groupData.vTextureCoords1[ iOutIndex ].y ) )
  969. groupData.bNeed2UV = true;
  970. // if the material differs, we need a second skin, too
  971. if (pcGroupTris->skinsets[ 1 ].material != pcGroupTris->skinsets[ 0 ].material)
  972. groupData.bNeed2UV = true;
  973. }
  974. // assign the material index
  975. groupData.pcFaces[ iTriangle ].iMatIndex[ 1 ] = pcGroupTris->skinsets[ 1 ].material;
  976. }
  977. }
  978. // get the next triangle in the list
  979. pcGroupTris = (BE_NCONST MDL::Triangle_MDL7*)((const char*)pcGroupTris +
  980. pcHeader->triangle_stc_size);
  981. }
  982. }
  983. // ------------------------------------------------------------------------------------------------
  984. // handle frames in a MDL7 file
  985. bool MDLImporter::ProcessFrames_3DGS_MDL7(const MDL::IntGroupInfo_MDL7& groupInfo,
  986. MDL::IntGroupData_MDL7& groupData,
  987. MDL::IntSharedData_MDL7& shared,
  988. const unsigned char* szCurrent,
  989. const unsigned char** szCurrentOut)
  990. {
  991. ai_assert(NULL != szCurrent && NULL != szCurrentOut);
  992. const MDL::Header_MDL7 *pcHeader = (const MDL::Header_MDL7*)mBuffer;
  993. // if we have no bones we can simply skip all frames,
  994. // otherwise we'll need to process them.
  995. // FIX: If we need another frame than the first we must apply frame vertex replacements ...
  996. for(unsigned int iFrame = 0; iFrame < (unsigned int)groupInfo.pcGroup->numframes;++iFrame)
  997. {
  998. MDL::IntFrameInfo_MDL7 frame ((BE_NCONST MDL::Frame_MDL7*)szCurrent,iFrame);
  999. AI_SWAP4(frame.pcFrame->vertices_count);
  1000. AI_SWAP4(frame.pcFrame->transmatrix_count);
  1001. const unsigned int iAdd = pcHeader->frame_stc_size +
  1002. frame.pcFrame->vertices_count * pcHeader->framevertex_stc_size +
  1003. frame.pcFrame->transmatrix_count * pcHeader->bonetrans_stc_size;
  1004. if (((const char*)szCurrent - (const char*)pcHeader) + iAdd > (unsigned int)pcHeader->data_size) {
  1005. DefaultLogger::get()->warn("Index overflow in frame area. "
  1006. "Ignoring all frames and all further mesh groups, too.");
  1007. // don't parse more groups if we can't even read one
  1008. // FIXME: sometimes this seems to occur even for valid files ...
  1009. *szCurrentOut = szCurrent;
  1010. return false;
  1011. }
  1012. // our output frame?
  1013. if (configFrameID == iFrame)
  1014. {
  1015. BE_NCONST MDL::Vertex_MDL7* pcFrameVertices = (BE_NCONST MDL::Vertex_MDL7*)(szCurrent+pcHeader->frame_stc_size);
  1016. for (unsigned int qq = 0; qq < frame.pcFrame->vertices_count;++qq)
  1017. {
  1018. // I assume this are simple replacements for normal
  1019. // vertices, the bone index serving as the index of the
  1020. // vertex to be replaced.
  1021. uint16_t iIndex = _AI_MDL7_ACCESS(pcFrameVertices,qq,
  1022. pcHeader->framevertex_stc_size,MDL::Vertex_MDL7).vertindex;
  1023. AI_SWAP2(iIndex);
  1024. if (iIndex >= groupInfo.pcGroup->numverts)
  1025. {
  1026. DefaultLogger::get()->warn("Invalid vertex index in frame vertex section");
  1027. continue;
  1028. }
  1029. aiVector3D vPosition,vNormal;
  1030. vPosition.x = _AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,pcHeader->framevertex_stc_size) .x;
  1031. AI_SWAP4(vPosition.x);
  1032. vPosition.y = -1.0f*_AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,pcHeader->framevertex_stc_size) .y;
  1033. AI_SWAP4(vPosition.y);
  1034. vPosition.z = _AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,pcHeader->framevertex_stc_size) .z;
  1035. AI_SWAP4(vPosition.z);
  1036. // now read the normal vector
  1037. if (AI_MDL7_FRAMEVERTEX030305_STCSIZE <= pcHeader->mainvertex_stc_size)
  1038. {
  1039. // read the full normal vector
  1040. vNormal.x = _AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,pcHeader->framevertex_stc_size) .norm[0];
  1041. AI_SWAP4(vNormal.x);
  1042. vNormal.y = -1.0f* _AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,pcHeader->framevertex_stc_size) .norm[1];
  1043. AI_SWAP4(vNormal.y);
  1044. vNormal.z = _AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,pcHeader->framevertex_stc_size) .norm[2];
  1045. AI_SWAP4(vNormal.z);
  1046. }
  1047. else if (AI_MDL7_FRAMEVERTEX120503_STCSIZE <= pcHeader->mainvertex_stc_size)
  1048. {
  1049. // read the normal vector from Quake2's smart table
  1050. MD2::LookupNormalIndex(_AI_MDL7_ACCESS_VERT(pcFrameVertices,qq,
  1051. pcHeader->framevertex_stc_size) .norm162index,vNormal);
  1052. vNormal.y *= -1.0f;
  1053. }
  1054. // FIXME: O(n^2) at the moment ...
  1055. BE_NCONST MDL::Triangle_MDL7* pcGroupTris = groupInfo.pcGroupTris;
  1056. unsigned int iOutIndex = 0;
  1057. for (unsigned int iTriangle = 0; iTriangle < (unsigned int)groupInfo.pcGroup->numtris; ++iTriangle)
  1058. {
  1059. // iterate through all indices of the current triangle
  1060. for (unsigned int c = 0; c < 3;++c,++iOutIndex)
  1061. {
  1062. // replace the vertex with the new data
  1063. const unsigned int iCurIndex = pcGroupTris->v_index[c];
  1064. if (iCurIndex == iIndex)
  1065. {
  1066. groupData.vPositions[iOutIndex] = vPosition;
  1067. groupData.vNormals[iOutIndex] = vNormal;
  1068. }
  1069. }
  1070. // get the next triangle in the list
  1071. pcGroupTris = (BE_NCONST MDL::Triangle_MDL7*)((const char*)
  1072. pcGroupTris + pcHeader->triangle_stc_size);
  1073. }
  1074. }
  1075. }
  1076. // parse bone trafo matrix keys (only if there are bones ...)
  1077. if (shared.apcOutBones)ParseBoneTrafoKeys_3DGS_MDL7(groupInfo,frame,shared);
  1078. szCurrent += iAdd;
  1079. }
  1080. *szCurrentOut = szCurrent;
  1081. return true;
  1082. }
  1083. // ------------------------------------------------------------------------------------------------
  1084. // Sort faces by material, handle multiple UVs correctly
  1085. void MDLImporter::SortByMaterials_3DGS_MDL7(
  1086. const MDL::IntGroupInfo_MDL7& groupInfo,
  1087. MDL::IntGroupData_MDL7& groupData,
  1088. MDL::IntSplittedGroupData_MDL7& splittedGroupData)
  1089. {
  1090. const unsigned int iNumMaterials = (unsigned int)splittedGroupData.shared.pcMats.size();
  1091. if (!groupData.bNeed2UV)
  1092. {
  1093. // if we don't need a second set of texture coordinates there is no reason to keep it in memory ...
  1094. groupData.vTextureCoords2.clear();
  1095. // allocate the array
  1096. splittedGroupData.aiSplit = new std::vector<unsigned int>*[iNumMaterials];
  1097. for (unsigned int m = 0; m < iNumMaterials;++m)
  1098. splittedGroupData.aiSplit[m] = new std::vector<unsigned int>();
  1099. // iterate through all faces and sort by material
  1100. for (unsigned int iFace = 0; iFace < (unsigned int)groupInfo.pcGroup->numtris;++iFace)
  1101. {
  1102. // check range
  1103. if (groupData.pcFaces[iFace].iMatIndex[0] >= iNumMaterials)
  1104. {
  1105. // use the last material instead
  1106. splittedGroupData.aiSplit[iNumMaterials-1]->push_back(iFace);
  1107. // sometimes MED writes -1, but normally only if there is only
  1108. // one skin assigned. No warning in this case
  1109. if(0xFFFFFFFF != groupData.pcFaces[iFace].iMatIndex[0])
  1110. DefaultLogger::get()->warn("Index overflow in MDL7 material list [#0]");
  1111. }
  1112. else splittedGroupData.aiSplit[groupData.pcFaces[iFace].
  1113. iMatIndex[0]]->push_back(iFace);
  1114. }
  1115. }
  1116. else
  1117. {
  1118. // we need to build combined materials for each combination of
  1119. std::vector<MDL::IntMaterial_MDL7> avMats;
  1120. avMats.reserve(iNumMaterials*2);
  1121. // fixme: why on the heap?
  1122. std::vector<std::vector<unsigned int>* > aiTempSplit(iNumMaterials*2);
  1123. for (unsigned int m = 0; m < iNumMaterials;++m)
  1124. aiTempSplit[m] = new std::vector<unsigned int>();
  1125. // iterate through all faces and sort by material
  1126. for (unsigned int iFace = 0; iFace < (unsigned int)groupInfo.pcGroup->numtris;++iFace)
  1127. {
  1128. // check range
  1129. unsigned int iMatIndex = groupData.pcFaces[iFace].iMatIndex[0];
  1130. if (iMatIndex >= iNumMaterials)
  1131. {
  1132. // sometimes MED writes -1, but normally only if there is only
  1133. // one skin assigned. No warning in this case
  1134. if(0xffffffff != iMatIndex)
  1135. DefaultLogger::get()->warn("Index overflow in MDL7 material list [#1]");
  1136. iMatIndex = iNumMaterials-1;
  1137. }
  1138. unsigned int iMatIndex2 = groupData.pcFaces[iFace].iMatIndex[1];
  1139. unsigned int iNum = iMatIndex;
  1140. if (0xffffffff != iMatIndex2 && iMatIndex != iMatIndex2)
  1141. {
  1142. if (iMatIndex2 >= iNumMaterials)
  1143. {
  1144. // sometimes MED writes -1, but normally only if there is only
  1145. // one skin assigned. No warning in this case
  1146. DefaultLogger::get()->warn("Index overflow in MDL7 material list [#2]");
  1147. iMatIndex2 = iNumMaterials-1;
  1148. }
  1149. // do a slow seach in the list ...
  1150. iNum = 0;
  1151. bool bFound = false;
  1152. for (std::vector<MDL::IntMaterial_MDL7>::iterator
  1153. i = avMats.begin();
  1154. i != avMats.end();++i,++iNum)
  1155. {
  1156. if ((*i).iOldMatIndices[0] == iMatIndex &&
  1157. (*i).iOldMatIndices[1] == iMatIndex2)
  1158. {
  1159. // reuse this material
  1160. bFound = true;
  1161. break;
  1162. }
  1163. }
  1164. if (!bFound)
  1165. {
  1166. // build a new material ...
  1167. MDL::IntMaterial_MDL7 sHelper;
  1168. sHelper.pcMat = new MaterialHelper();
  1169. sHelper.iOldMatIndices[0] = iMatIndex;
  1170. sHelper.iOldMatIndices[1] = iMatIndex2;
  1171. JoinSkins_3DGS_MDL7(splittedGroupData.shared.pcMats[iMatIndex],
  1172. splittedGroupData.shared.pcMats[iMatIndex2],sHelper.pcMat);
  1173. // and add it to the list
  1174. avMats.push_back(sHelper);
  1175. iNum = (unsigned int)avMats.size()-1;
  1176. }
  1177. // adjust the size of the file array
  1178. if (iNum == aiTempSplit.size())
  1179. aiTempSplit.push_back(new std::vector<unsigned int>());
  1180. }
  1181. aiTempSplit[iNum]->push_back(iFace);
  1182. }
  1183. // now add the newly created materials to the old list
  1184. if (0 == groupInfo.iIndex)
  1185. {
  1186. splittedGroupData.shared.pcMats.resize(avMats.size());
  1187. for (unsigned int o = 0; o < avMats.size();++o)
  1188. splittedGroupData.shared.pcMats[o] = avMats[o].pcMat;
  1189. }
  1190. else
  1191. {
  1192. // TODO: This might result in redundant materials ...
  1193. splittedGroupData.shared.pcMats.resize(iNumMaterials + avMats.size());
  1194. for (unsigned int o = iNumMaterials; o < avMats.size();++o)
  1195. splittedGroupData.shared.pcMats[o] = avMats[o].pcMat;
  1196. }
  1197. // and build the final face-to-material array
  1198. splittedGroupData.aiSplit = new std::vector<unsigned int>*[aiTempSplit.size()];
  1199. for (unsigned int m = 0; m < iNumMaterials;++m)
  1200. splittedGroupData.aiSplit[m] = aiTempSplit[m];
  1201. }
  1202. }
  1203. // ------------------------------------------------------------------------------------------------
  1204. // Read a MDL7 file
  1205. void MDLImporter::InternReadFile_3DGS_MDL7( )
  1206. {
  1207. ai_assert(NULL != pScene);
  1208. MDL::IntSharedData_MDL7 sharedData;
  1209. // current cursor position in the file
  1210. BE_NCONST MDL::Header_MDL7 *pcHeader = (BE_NCONST MDL::Header_MDL7*)this->mBuffer;
  1211. const unsigned char* szCurrent = (const unsigned char*)(pcHeader+1);
  1212. AI_SWAP4(pcHeader->version);
  1213. AI_SWAP4(pcHeader->bones_num);
  1214. AI_SWAP4(pcHeader->groups_num);
  1215. AI_SWAP4(pcHeader->data_size);
  1216. AI_SWAP4(pcHeader->entlump_size);
  1217. AI_SWAP4(pcHeader->medlump_size);
  1218. AI_SWAP2(pcHeader->bone_stc_size);
  1219. AI_SWAP2(pcHeader->skin_stc_size);
  1220. AI_SWAP2(pcHeader->colorvalue_stc_size);
  1221. AI_SWAP2(pcHeader->material_stc_size);
  1222. AI_SWAP2(pcHeader->skinpoint_stc_size);
  1223. AI_SWAP2(pcHeader->triangle_stc_size);
  1224. AI_SWAP2(pcHeader->mainvertex_stc_size);
  1225. AI_SWAP2(pcHeader->framevertex_stc_size);
  1226. AI_SWAP2(pcHeader->bonetrans_stc_size);
  1227. AI_SWAP2(pcHeader->frame_stc_size);
  1228. // validate the header of the file. There are some structure
  1229. // sizes that are expected by the loader to be constant
  1230. this->ValidateHeader_3DGS_MDL7(pcHeader);
  1231. // load all bones (they are shared by all groups, so
  1232. // we'll need to add them to all groups/meshes later)
  1233. // apcBonesOut is a list of all bones or NULL if they could not been loaded
  1234. szCurrent += pcHeader->bones_num * pcHeader->bone_stc_size;
  1235. sharedData.apcOutBones = this->LoadBones_3DGS_MDL7();
  1236. // vector to held all created meshes
  1237. std::vector<aiMesh*>* avOutList;
  1238. // 3 meshes per group - that should be OK for most models
  1239. avOutList = new std::vector<aiMesh*>[pcHeader->groups_num];
  1240. for (uint32_t i = 0; i < pcHeader->groups_num;++i)
  1241. avOutList[i].reserve(3);
  1242. // buffer to held the names of all groups in the file
  1243. char* aszGroupNameBuffer = new char[AI_MDL7_MAX_GROUPNAMESIZE*pcHeader->groups_num];
  1244. // read all groups
  1245. for (unsigned int iGroup = 0; iGroup < (unsigned int)pcHeader->groups_num;++iGroup)
  1246. {
  1247. MDL::IntGroupInfo_MDL7 groupInfo((BE_NCONST MDL::Group_MDL7*)szCurrent,iGroup);
  1248. szCurrent = (const unsigned char*)(groupInfo.pcGroup+1);
  1249. VALIDATE_FILE_SIZE(szCurrent);
  1250. AI_SWAP4(groupInfo.pcGroup->groupdata_size);
  1251. AI_SWAP4(groupInfo.pcGroup->numskins);
  1252. AI_SWAP4(groupInfo.pcGroup->num_stpts);
  1253. AI_SWAP4(groupInfo.pcGroup->numtris);
  1254. AI_SWAP4(groupInfo.pcGroup->numverts);
  1255. AI_SWAP4(groupInfo.pcGroup->numframes);
  1256. if (1 != groupInfo.pcGroup->typ)
  1257. {
  1258. // Not a triangle-based mesh
  1259. DefaultLogger::get()->warn("[3DGS MDL7] Not a triangle mesh group. Continuing happily");
  1260. }
  1261. // store the name of the group
  1262. const unsigned int ofs = iGroup*AI_MDL7_MAX_GROUPNAMESIZE;
  1263. ::memcpy(&aszGroupNameBuffer[ofs],
  1264. groupInfo.pcGroup->name,AI_MDL7_MAX_GROUPNAMESIZE);
  1265. // make sure '\0' is at the end
  1266. aszGroupNameBuffer[ofs+AI_MDL7_MAX_GROUPNAMESIZE-1] = '\0';
  1267. // read all skins
  1268. sharedData.pcMats.reserve(sharedData.pcMats.size() + groupInfo.pcGroup->numskins);
  1269. sharedData.abNeedMaterials.resize(sharedData.abNeedMaterials.size() +
  1270. groupInfo.pcGroup->numskins,false);
  1271. for (unsigned int iSkin = 0; iSkin < (unsigned int)groupInfo.pcGroup->numskins;++iSkin)
  1272. {
  1273. this->ParseSkinLump_3DGS_MDL7(szCurrent,&szCurrent,sharedData.pcMats);
  1274. }
  1275. // if we have absolutely no skin loaded we need to generate a default material
  1276. if (sharedData.pcMats.empty())
  1277. {
  1278. const int iMode = (int)aiShadingMode_Gouraud;
  1279. sharedData.pcMats.push_back(new MaterialHelper());
  1280. MaterialHelper* pcHelper = (MaterialHelper*)sharedData.pcMats[0];
  1281. pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  1282. aiColor3D clr;
  1283. clr.b = clr.g = clr.r = 0.6f;
  1284. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  1285. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  1286. clr.b = clr.g = clr.r = 0.05f;
  1287. pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  1288. aiString szName;
  1289. szName.Set(AI_DEFAULT_MATERIAL_NAME);
  1290. pcHelper->AddProperty(&szName,AI_MATKEY_NAME);
  1291. sharedData.abNeedMaterials.resize(1,false);
  1292. }
  1293. // now get a pointer to all texture coords in the group
  1294. groupInfo.pcGroupUVs = (BE_NCONST MDL::TexCoord_MDL7*)szCurrent;
  1295. for(int i = 0; i < groupInfo.pcGroup->num_stpts; ++i){
  1296. AI_SWAP4(groupInfo.pcGroupUVs[i].u);
  1297. AI_SWAP4(groupInfo.pcGroupUVs[i].v);
  1298. }
  1299. szCurrent += pcHeader->skinpoint_stc_size * groupInfo.pcGroup->num_stpts;
  1300. // now get a pointer to all triangle in the group
  1301. groupInfo.pcGroupTris = (BE_NCONST MDL::Triangle_MDL7*)szCurrent;
  1302. szCurrent += pcHeader->triangle_stc_size * groupInfo.pcGroup->numtris;
  1303. // now get a pointer to all vertices in the group
  1304. groupInfo.pcGroupVerts = (BE_NCONST MDL::Vertex_MDL7*)szCurrent;
  1305. for(int i = 0; i < groupInfo.pcGroup->numverts; ++i){
  1306. AI_SWAP4(groupInfo.pcGroupVerts[i].x);
  1307. AI_SWAP4(groupInfo.pcGroupVerts[i].y);
  1308. AI_SWAP4(groupInfo.pcGroupVerts[i].z);
  1309. AI_SWAP2(groupInfo.pcGroupVerts[i].vertindex);
  1310. //We can not swap the normal information now as we don't know which of the two kinds it is
  1311. }
  1312. szCurrent += pcHeader->mainvertex_stc_size * groupInfo.pcGroup->numverts;
  1313. VALIDATE_FILE_SIZE(szCurrent);
  1314. MDL::IntSplittedGroupData_MDL7 splittedGroupData(sharedData,avOutList[iGroup]);
  1315. MDL::IntGroupData_MDL7 groupData;
  1316. if (groupInfo.pcGroup->numtris && groupInfo.pcGroup->numverts)
  1317. {
  1318. // build output vectors
  1319. const unsigned int iNumVertices = groupInfo.pcGroup->numtris*3;
  1320. groupData.vPositions.resize(iNumVertices);
  1321. groupData.vNormals.resize(iNumVertices);
  1322. if (sharedData.apcOutBones)groupData.aiBones.resize(iNumVertices,0xffffffff);
  1323. // it is also possible that there are 0 UV coordinate sets
  1324. if (groupInfo.pcGroup->num_stpts){
  1325. groupData.vTextureCoords1.resize(iNumVertices,aiVector3D());
  1326. // check whether the triangle data structure is large enough
  1327. // to contain a second UV coodinate set
  1328. if (pcHeader->triangle_stc_size >= AI_MDL7_TRIANGLE_STD_SIZE_TWO_UV)
  1329. {
  1330. groupData.vTextureCoords2.resize(iNumVertices,aiVector3D());
  1331. groupData.bNeed2UV = true;
  1332. }
  1333. }
  1334. groupData.pcFaces = new MDL::IntFace_MDL7[groupInfo.pcGroup->numtris];
  1335. // read all faces into the preallocated arrays
  1336. ReadFaces_3DGS_MDL7(groupInfo, groupData);
  1337. // sort by materials
  1338. SortByMaterials_3DGS_MDL7(groupInfo, groupData,
  1339. splittedGroupData);
  1340. for (unsigned int qq = 0; qq < sharedData.pcMats.size();++qq) {
  1341. if (!splittedGroupData.aiSplit[qq]->empty())
  1342. sharedData.abNeedMaterials[qq] = true;
  1343. }
  1344. }
  1345. else DefaultLogger::get()->warn("[3DGS MDL7] Mesh group consists of 0 "
  1346. "vertices or faces. It will be skipped.");
  1347. // process all frames and generate output meshes
  1348. ProcessFrames_3DGS_MDL7(groupInfo,groupData, sharedData,szCurrent,&szCurrent);
  1349. GenerateOutputMeshes_3DGS_MDL7(groupData,splittedGroupData);
  1350. }
  1351. // generate a nodegraph and subnodes for each group
  1352. pScene->mRootNode = new aiNode();
  1353. // now we need to build a final mesh list
  1354. for (uint32_t i = 0; i < pcHeader->groups_num;++i)
  1355. pScene->mNumMeshes += (unsigned int)avOutList[i].size();
  1356. this->pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  1357. {
  1358. unsigned int p = 0,q = 0;
  1359. for (uint32_t i = 0; i < pcHeader->groups_num;++i)
  1360. {
  1361. for (unsigned int a = 0; a < avOutList[i].size();++a)
  1362. {
  1363. this->pScene->mMeshes[p++] = avOutList[i][a];
  1364. }
  1365. if (!avOutList[i].empty())++pScene->mRootNode->mNumChildren;
  1366. }
  1367. // we will later need an extra node to serve as parent for all bones
  1368. if (sharedData.apcOutBones)++pScene->mRootNode->mNumChildren;
  1369. this->pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
  1370. p = 0;
  1371. for (uint32_t i = 0; i < pcHeader->groups_num;++i) {
  1372. if (avOutList[i].empty())continue;
  1373. aiNode* const pcNode = pScene->mRootNode->mChildren[p] = new aiNode();
  1374. pcNode->mNumMeshes = (unsigned int)avOutList[i].size();
  1375. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  1376. pcNode->mParent = this->pScene->mRootNode;
  1377. for (unsigned int a = 0; a < pcNode->mNumMeshes;++a)
  1378. pcNode->mMeshes[a] = q + a;
  1379. q += (unsigned int)avOutList[i].size();
  1380. // setup the name of the node
  1381. char* const szBuffer = &aszGroupNameBuffer[i*AI_MDL7_MAX_GROUPNAMESIZE];
  1382. if ('\0' == *szBuffer)
  1383. pcNode->mName.length = ::sprintf(szBuffer,"Group_%i",p);
  1384. else pcNode->mName.length = ::strlen(szBuffer);
  1385. ::strcpy(pcNode->mName.data,szBuffer);
  1386. ++p;
  1387. }
  1388. }
  1389. // if there is only one root node with a single child we can optimize it a bit ...
  1390. if (1 == pScene->mRootNode->mNumChildren && !sharedData.apcOutBones)
  1391. {
  1392. aiNode* pcOldRoot = this->pScene->mRootNode;
  1393. pScene->mRootNode = pcOldRoot->mChildren[0];
  1394. pcOldRoot->mChildren[0] = NULL;
  1395. delete pcOldRoot;
  1396. pScene->mRootNode->mParent = NULL;
  1397. }
  1398. else pScene->mRootNode->mName.Set("<mesh_root>");
  1399. delete[] avOutList;
  1400. delete[] aszGroupNameBuffer;
  1401. AI_DEBUG_INVALIDATE_PTR(avOutList);
  1402. AI_DEBUG_INVALIDATE_PTR(aszGroupNameBuffer);
  1403. // build a final material list.
  1404. CopyMaterials_3DGS_MDL7(sharedData);
  1405. HandleMaterialReferences_3DGS_MDL7();
  1406. // generate output bone animations and add all bones to the scenegraph
  1407. if (sharedData.apcOutBones)
  1408. {
  1409. // this step adds empty dummy bones to the nodegraph
  1410. // insert another dummy node to avoid name conflicts
  1411. aiNode* const pc = pScene->mRootNode->mChildren[pScene->mRootNode->mNumChildren-1] = new aiNode();
  1412. pc->mName.Set("<skeleton_root>");
  1413. // add bones to the nodegraph
  1414. AddBonesToNodeGraph_3DGS_MDL7((const Assimp::MDL::IntBone_MDL7 **)
  1415. sharedData.apcOutBones,pc,0xffff);
  1416. // this steps build a valid output animation
  1417. BuildOutputAnims_3DGS_MDL7((const Assimp::MDL::IntBone_MDL7 **)
  1418. sharedData.apcOutBones);
  1419. }
  1420. }
  1421. // ------------------------------------------------------------------------------------------------
  1422. void MDLImporter::CopyMaterials_3DGS_MDL7(MDL::IntSharedData_MDL7 &shared)
  1423. {
  1424. pScene->mNumMaterials = (unsigned int)shared.pcMats.size();
  1425. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  1426. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  1427. pScene->mMaterials[i] = shared.pcMats[i];
  1428. }
  1429. // ------------------------------------------------------------------------------------------------
  1430. void MDLImporter::HandleMaterialReferences_3DGS_MDL7()
  1431. {
  1432. // search for referrer materials
  1433. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  1434. {
  1435. int iIndex = 0;
  1436. if (AI_SUCCESS == aiGetMaterialInteger(pScene->mMaterials[i],
  1437. AI_MDL7_REFERRER_MATERIAL, &iIndex) )
  1438. {
  1439. for (unsigned int a = 0; a < pScene->mNumMeshes;++a)
  1440. {
  1441. aiMesh* const pcMesh = pScene->mMeshes[a];
  1442. if (i == pcMesh->mMaterialIndex)
  1443. pcMesh->mMaterialIndex = iIndex;
  1444. }
  1445. // collapse the rest of the array
  1446. delete pScene->mMaterials[i];
  1447. for (unsigned int pp = i; pp < pScene->mNumMaterials-1;++pp)
  1448. {
  1449. this->pScene->mMaterials[pp] = pScene->mMaterials[pp+1];
  1450. for (unsigned int a = 0; a < pScene->mNumMeshes;++a)
  1451. {
  1452. aiMesh* const pcMesh = pScene->mMeshes[a];
  1453. if (pcMesh->mMaterialIndex > i)--pcMesh->mMaterialIndex;
  1454. }
  1455. }
  1456. --pScene->mNumMaterials;
  1457. }
  1458. }
  1459. }
  1460. // ------------------------------------------------------------------------------------------------
  1461. // Read bone transformation keys
  1462. void MDLImporter::ParseBoneTrafoKeys_3DGS_MDL7(
  1463. const MDL::IntGroupInfo_MDL7& groupInfo,
  1464. IntFrameInfo_MDL7& frame,
  1465. MDL::IntSharedData_MDL7& shared)
  1466. {
  1467. const MDL::Header_MDL7* const pcHeader = (const MDL::Header_MDL7*)this->mBuffer;
  1468. // only the first group contains bone animation keys
  1469. if (frame.pcFrame->transmatrix_count)
  1470. {
  1471. if (!groupInfo.iIndex)
  1472. {
  1473. // skip all frames vertices. We can't support them
  1474. const MDL::BoneTransform_MDL7* pcBoneTransforms = (const MDL::BoneTransform_MDL7*)
  1475. (((const char*)frame.pcFrame) + pcHeader->frame_stc_size +
  1476. frame.pcFrame->vertices_count * pcHeader->framevertex_stc_size);
  1477. // read all transformation matrices
  1478. for (unsigned int iTrafo = 0; iTrafo < frame.pcFrame->transmatrix_count;++iTrafo)
  1479. {
  1480. if(pcBoneTransforms->bone_index >= pcHeader->bones_num) {
  1481. DefaultLogger::get()->warn("Index overflow in frame area. "
  1482. "Unable to parse this bone transformation");
  1483. }
  1484. else {
  1485. AddAnimationBoneTrafoKey_3DGS_MDL7(frame.iIndex,
  1486. pcBoneTransforms,shared.apcOutBones);
  1487. }
  1488. pcBoneTransforms = (const MDL::BoneTransform_MDL7*)(
  1489. (const char*)pcBoneTransforms + pcHeader->bonetrans_stc_size);
  1490. }
  1491. }
  1492. else
  1493. {
  1494. DefaultLogger::get()->warn("Found animation keyframes "
  1495. "in a group that is not the first. They will be igored");
  1496. }
  1497. }
  1498. }
  1499. // ------------------------------------------------------------------------------------------------
  1500. // Attach bones to the output nodegraph
  1501. void MDLImporter::AddBonesToNodeGraph_3DGS_MDL7(const MDL::IntBone_MDL7** apcBones,
  1502. aiNode* pcParent,uint16_t iParentIndex)
  1503. {
  1504. ai_assert(NULL != apcBones && NULL != pcParent);
  1505. // get a pointer to the header ...
  1506. const MDL::Header_MDL7* const pcHeader = (const MDL::Header_MDL7*)this->mBuffer;
  1507. const MDL::IntBone_MDL7** apcBones2 = apcBones;
  1508. for (uint32_t i = 0; i < pcHeader->bones_num;++i)
  1509. {
  1510. const MDL::IntBone_MDL7* const pcBone = *apcBones2++;
  1511. if (pcBone->iParent == iParentIndex)++pcParent->mNumChildren;
  1512. }
  1513. pcParent->mChildren = new aiNode*[pcParent->mNumChildren];
  1514. unsigned int qq = 0;
  1515. for (uint32_t i = 0; i < pcHeader->bones_num;++i)
  1516. {
  1517. const MDL::IntBone_MDL7* const pcBone = *apcBones++;
  1518. if (pcBone->iParent != iParentIndex)continue;
  1519. aiNode* pcNode = pcParent->mChildren[qq++] = new aiNode();
  1520. pcNode->mName = aiString( pcBone->mName );
  1521. this->AddBonesToNodeGraph_3DGS_MDL7(apcBones,pcNode,(uint16_t)i);
  1522. }
  1523. }
  1524. // ------------------------------------------------------------------------------------------------
  1525. // Build output animations
  1526. void MDLImporter::BuildOutputAnims_3DGS_MDL7(
  1527. const MDL::IntBone_MDL7** apcBonesOut)
  1528. {
  1529. ai_assert(NULL != apcBonesOut);
  1530. const MDL::Header_MDL7* const pcHeader = (const MDL::Header_MDL7*)mBuffer;
  1531. // one animation ...
  1532. aiAnimation* pcAnim = new aiAnimation();
  1533. for (uint32_t i = 0; i < pcHeader->bones_num;++i)
  1534. {
  1535. if (!apcBonesOut[i]->pkeyPositions.empty())
  1536. {
  1537. // get the last frame ... (needn't be equal to pcHeader->frames_num)
  1538. for (size_t qq = 0; qq < apcBonesOut[i]->pkeyPositions.size();++qq)
  1539. {
  1540. pcAnim->mDuration = std::max(pcAnim->mDuration, (double)
  1541. apcBonesOut[i]->pkeyPositions[qq].mTime);
  1542. }
  1543. ++pcAnim->mNumChannels;
  1544. }
  1545. }
  1546. if (pcAnim->mDuration)
  1547. {
  1548. pcAnim->mChannels = new aiNodeAnim*[pcAnim->mNumChannels];
  1549. unsigned int iCnt = 0;
  1550. for (uint32_t i = 0; i < pcHeader->bones_num;++i)
  1551. {
  1552. if (!apcBonesOut[i]->pkeyPositions.empty())
  1553. {
  1554. const MDL::IntBone_MDL7* const intBone = apcBonesOut[i];
  1555. aiNodeAnim* const pcNodeAnim = pcAnim->mChannels[iCnt++] = new aiNodeAnim();
  1556. pcNodeAnim->mNodeName = aiString( intBone->mName );
  1557. // allocate enough storage for all keys
  1558. pcNodeAnim->mNumPositionKeys = (unsigned int)intBone->pkeyPositions.size();
  1559. pcNodeAnim->mNumScalingKeys = (unsigned int)intBone->pkeyPositions.size();
  1560. pcNodeAnim->mNumRotationKeys = (unsigned int)intBone->pkeyPositions.size();
  1561. pcNodeAnim->mPositionKeys = new aiVectorKey[pcNodeAnim->mNumPositionKeys];
  1562. pcNodeAnim->mScalingKeys = new aiVectorKey[pcNodeAnim->mNumPositionKeys];
  1563. pcNodeAnim->mRotationKeys = new aiQuatKey[pcNodeAnim->mNumPositionKeys];
  1564. // copy all keys
  1565. for (unsigned int qq = 0; qq < pcNodeAnim->mNumPositionKeys;++qq)
  1566. {
  1567. pcNodeAnim->mPositionKeys[qq] = intBone->pkeyPositions[qq];
  1568. pcNodeAnim->mScalingKeys[qq] = intBone->pkeyScalings[qq];
  1569. pcNodeAnim->mRotationKeys[qq] = intBone->pkeyRotations[qq];
  1570. }
  1571. }
  1572. }
  1573. // store the output animation
  1574. pScene->mNumAnimations = 1;
  1575. pScene->mAnimations = new aiAnimation*[1];
  1576. pScene->mAnimations[0] = pcAnim;
  1577. }
  1578. else delete pcAnim;
  1579. }
  1580. // ------------------------------------------------------------------------------------------------
  1581. void MDLImporter::AddAnimationBoneTrafoKey_3DGS_MDL7(unsigned int iTrafo,
  1582. const MDL::BoneTransform_MDL7* pcBoneTransforms,
  1583. MDL::IntBone_MDL7** apcBonesOut)
  1584. {
  1585. ai_assert(NULL != pcBoneTransforms);
  1586. ai_assert(NULL != apcBonesOut);
  1587. // first .. get the transformation matrix
  1588. aiMatrix4x4 mTransform;
  1589. mTransform.a1 = pcBoneTransforms->m[0];
  1590. mTransform.b1 = pcBoneTransforms->m[1];
  1591. mTransform.c1 = pcBoneTransforms->m[2];
  1592. mTransform.d1 = pcBoneTransforms->m[3];
  1593. mTransform.a2 = pcBoneTransforms->m[4];
  1594. mTransform.b2 = pcBoneTransforms->m[5];
  1595. mTransform.c2 = pcBoneTransforms->m[6];
  1596. mTransform.d2 = pcBoneTransforms->m[7];
  1597. mTransform.a3 = pcBoneTransforms->m[8];
  1598. mTransform.b3 = pcBoneTransforms->m[9];
  1599. mTransform.c3 = pcBoneTransforms->m[10];
  1600. mTransform.d3 = pcBoneTransforms->m[11];
  1601. // now decompose the transformation matrix into separate
  1602. // scaling, rotation and translation
  1603. aiVectorKey vScaling,vPosition;
  1604. aiQuatKey qRotation;
  1605. // FIXME: Decompose will assert in debug builds if the
  1606. // matrix is invalid ...
  1607. mTransform.Decompose(vScaling.mValue,qRotation.mValue,vPosition.mValue);
  1608. // now generate keys
  1609. vScaling.mTime = qRotation.mTime = vPosition.mTime = (double)iTrafo;
  1610. // add the keys to the bone
  1611. MDL::IntBone_MDL7* const pcBoneOut = apcBonesOut[pcBoneTransforms->bone_index];
  1612. pcBoneOut->pkeyPositions.push_back ( vPosition );
  1613. pcBoneOut->pkeyScalings.push_back ( vScaling );
  1614. pcBoneOut->pkeyRotations.push_back ( qRotation );
  1615. }
  1616. // ------------------------------------------------------------------------------------------------
  1617. // Construct output meshes
  1618. void MDLImporter::GenerateOutputMeshes_3DGS_MDL7(
  1619. MDL::IntGroupData_MDL7& groupData,
  1620. MDL::IntSplittedGroupData_MDL7& splittedGroupData)
  1621. {
  1622. const MDL::IntSharedData_MDL7& shared = splittedGroupData.shared;
  1623. // get a pointer to the header ...
  1624. const MDL::Header_MDL7* const pcHeader = (const MDL::Header_MDL7*)this->mBuffer;
  1625. const unsigned int iNumOutBones = pcHeader->bones_num;
  1626. for (std::vector<MaterialHelper*>::size_type i = 0; i < shared.pcMats.size();++i)
  1627. {
  1628. if (!splittedGroupData.aiSplit[i]->empty())
  1629. {
  1630. // allocate the output mesh
  1631. aiMesh* pcMesh = new aiMesh();
  1632. pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  1633. pcMesh->mMaterialIndex = (unsigned int)i;
  1634. // allocate output storage
  1635. pcMesh->mNumFaces = (unsigned int)splittedGroupData.aiSplit[i]->size();
  1636. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  1637. pcMesh->mNumVertices = pcMesh->mNumFaces*3;
  1638. pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
  1639. pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
  1640. if (!groupData.vTextureCoords1.empty())
  1641. {
  1642. pcMesh->mNumUVComponents[0] = 2;
  1643. pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
  1644. if (!groupData.vTextureCoords2.empty())
  1645. {
  1646. pcMesh->mNumUVComponents[1] = 2;
  1647. pcMesh->mTextureCoords[1] = new aiVector3D[pcMesh->mNumVertices];
  1648. }
  1649. }
  1650. // iterate through all faces and build an unique set of vertices
  1651. unsigned int iCurrent = 0;
  1652. for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace)
  1653. {
  1654. pcMesh->mFaces[iFace].mNumIndices = 3;
  1655. pcMesh->mFaces[iFace].mIndices = new unsigned int[3];
  1656. unsigned int iSrcFace = splittedGroupData.aiSplit[i]->operator[](iFace);
  1657. const MDL::IntFace_MDL7& oldFace = groupData.pcFaces[iSrcFace];
  1658. // iterate through all face indices
  1659. for (unsigned int c = 0; c < 3;++c)
  1660. {
  1661. const uint32_t iIndex = oldFace.mIndices[c];
  1662. pcMesh->mVertices[iCurrent] = groupData.vPositions[iIndex];
  1663. pcMesh->mNormals[iCurrent] = groupData.vNormals[iIndex];
  1664. if (!groupData.vTextureCoords1.empty())
  1665. {
  1666. pcMesh->mTextureCoords[0][iCurrent] = groupData.vTextureCoords1[iIndex];
  1667. if (!groupData.vTextureCoords2.empty())
  1668. {
  1669. pcMesh->mTextureCoords[1][iCurrent] = groupData.vTextureCoords2[iIndex];
  1670. }
  1671. }
  1672. pcMesh->mFaces[iFace].mIndices[c] = iCurrent++;
  1673. }
  1674. }
  1675. // if we have bones in the mesh we'll need to generate
  1676. // proper vertex weights for them
  1677. if (!groupData.aiBones.empty())
  1678. {
  1679. std::vector<std::vector<unsigned int> > aaiVWeightList;
  1680. aaiVWeightList.resize(iNumOutBones);
  1681. int iCurrent = 0;
  1682. for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace)
  1683. {
  1684. unsigned int iSrcFace = splittedGroupData.aiSplit[i]->operator[](iFace);
  1685. const MDL::IntFace_MDL7& oldFace = groupData.pcFaces[iSrcFace];
  1686. // iterate through all face indices
  1687. for (unsigned int c = 0; c < 3;++c)
  1688. {
  1689. unsigned int iBone = groupData.aiBones[ oldFace.mIndices[c] ];
  1690. if (0xffffffff != iBone)
  1691. {
  1692. if (iBone >= iNumOutBones)
  1693. {
  1694. DefaultLogger::get()->error("Bone index overflow. "
  1695. "The bone index of a vertex exceeds the allowed range. ");
  1696. iBone = iNumOutBones-1;
  1697. }
  1698. aaiVWeightList[ iBone ].push_back ( iCurrent );
  1699. }
  1700. ++iCurrent;
  1701. }
  1702. }
  1703. // now check which bones are required ...
  1704. for (std::vector<std::vector<unsigned int> >::const_iterator
  1705. kimmi = aaiVWeightList.begin();
  1706. kimmi != aaiVWeightList.end();++kimmi)
  1707. {
  1708. if (!(*kimmi).empty())++pcMesh->mNumBones;
  1709. }
  1710. pcMesh->mBones = new aiBone*[pcMesh->mNumBones];
  1711. iCurrent = 0;
  1712. for (std::vector<std::vector<unsigned int> >::const_iterator
  1713. kimmi = aaiVWeightList.begin();
  1714. kimmi != aaiVWeightList.end();++kimmi,++iCurrent)
  1715. {
  1716. if ((*kimmi).empty())continue;
  1717. // seems we'll need this node
  1718. aiBone* pcBone = pcMesh->mBones[ iCurrent ] = new aiBone();
  1719. pcBone->mName = aiString(shared.apcOutBones[ iCurrent ]->mName);
  1720. pcBone->mOffsetMatrix = shared.apcOutBones[ iCurrent ]->mOffsetMatrix;
  1721. // setup vertex weights
  1722. pcBone->mNumWeights = (unsigned int)(*kimmi).size();
  1723. pcBone->mWeights = new aiVertexWeight[pcBone->mNumWeights];
  1724. for (unsigned int weight = 0; weight < pcBone->mNumWeights;++weight)
  1725. {
  1726. pcBone->mWeights[weight].mVertexId = (*kimmi)[weight];
  1727. pcBone->mWeights[weight].mWeight = 1.0f;
  1728. }
  1729. }
  1730. }
  1731. // add the mesh to the list of output meshes
  1732. splittedGroupData.avOutList.push_back(pcMesh);
  1733. }
  1734. }
  1735. }
  1736. // ------------------------------------------------------------------------------------------------
  1737. // Join to materials
  1738. void MDLImporter::JoinSkins_3DGS_MDL7(
  1739. MaterialHelper* pcMat1,
  1740. MaterialHelper* pcMat2,
  1741. MaterialHelper* pcMatOut)
  1742. {
  1743. ai_assert(NULL != pcMat1 && NULL != pcMat2 && NULL != pcMatOut);
  1744. // first create a full copy of the first skin property set
  1745. // and assign it to the output material
  1746. MaterialHelper::CopyPropertyList(pcMatOut,pcMat1);
  1747. int iVal = 0;
  1748. pcMatOut->AddProperty<int>(&iVal,1,AI_MATKEY_UVWSRC_DIFFUSE(0));
  1749. // then extract the diffuse texture from the second skin,
  1750. // setup 1 as UV source and we have it
  1751. aiString sString;
  1752. if(AI_SUCCESS == aiGetMaterialString ( pcMat2, AI_MATKEY_TEXTURE_DIFFUSE(0),&sString ))
  1753. {
  1754. iVal = 1;
  1755. pcMatOut->AddProperty<int>(&iVal,1,AI_MATKEY_UVWSRC_DIFFUSE(1));
  1756. pcMatOut->AddProperty(&sString,AI_MATKEY_TEXTURE_DIFFUSE(1));
  1757. }
  1758. }
  1759. // ------------------------------------------------------------------------------------------------
  1760. // Read a half-life 2 MDL
  1761. void MDLImporter::InternReadFile_HL2( )
  1762. {
  1763. //const MDL::Header_HL2* pcHeader = (const MDL::Header_HL2*)this->mBuffer;
  1764. }
  1765. #endif // !! ASSIMP_BUILD_NO_MDL_IMPORTER