MDLLoader.cpp 73 KB

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