MDLLoader.cpp 73 KB

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