MDLLoader.cpp 82 KB

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