MDLLoader.cpp 87 KB

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