MDLLoader.cpp 86 KB

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