You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1943 lines
70 KiB

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