MDLLoader.cpp 71 KB

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