MDLLoader.cpp 71 KB

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