MDLLoader.cpp 71 KB

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