MDLLoader.cpp 86 KB

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