MDLLoader.cpp 71 KB

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