MDLLoader.cpp 82 KB

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