MDLLoader.cpp 72 KB

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