MDLLoader.cpp 84 KB

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