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