MDLLoader.cpp 73 KB

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