MDLLoader.cpp 82 KB

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