MDLLoader.cpp 86 KB

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