MDLLoader.cpp 72 KB

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