MDLLoader.cpp 84 KB

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