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.
 
 
 
 
 
 

483 lines
17 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 Implementation of the MDC importer class */
  35. #include "AssimpPCH.h"
  36. #ifndef ASSIMP_BUILD_NO_MDC_IMPORTER
  37. // internal headers
  38. #include "MDCLoader.h"
  39. #include "MD3FileData.h"
  40. #include "MDCNormalTable.h" // shouldn't be included by other units
  41. using namespace Assimp;
  42. using namespace Assimp::MDC;
  43. static const aiImporterDesc desc = {
  44. "Return To Castle Wolfenstein Mesh Importer",
  45. "",
  46. "",
  47. "",
  48. aiImporterFlags_SupportBinaryFlavour,
  49. 0,
  50. 0,
  51. 0,
  52. 0,
  53. "mdc"
  54. };
  55. // ------------------------------------------------------------------------------------------------
  56. void MDC::BuildVertex(const Frame& frame,
  57. const BaseVertex& bvert,
  58. const CompressedVertex& cvert,
  59. aiVector3D& vXYZOut,
  60. aiVector3D& vNorOut)
  61. {
  62. // compute the position
  63. const float xd = (cvert.xd - AI_MDC_CVERT_BIAS) * AI_MDC_DELTA_SCALING;
  64. const float yd = (cvert.yd - AI_MDC_CVERT_BIAS) * AI_MDC_DELTA_SCALING;
  65. const float zd = (cvert.zd - AI_MDC_CVERT_BIAS) * AI_MDC_DELTA_SCALING;
  66. vXYZOut.x = frame.localOrigin.x + AI_MDC_BASE_SCALING * (bvert.x + xd);
  67. vXYZOut.y = frame.localOrigin.y + AI_MDC_BASE_SCALING * (bvert.y + yd);
  68. vXYZOut.z = frame.localOrigin.z + AI_MDC_BASE_SCALING * (bvert.z + zd);
  69. // compute the normal vector .. ehm ... lookup it in the table :-)
  70. vNorOut.x = mdcNormals[cvert.nd][0];
  71. vNorOut.y = mdcNormals[cvert.nd][1];
  72. vNorOut.z = mdcNormals[cvert.nd][2];
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Constructor to be privately used by Importer
  76. MDCImporter::MDCImporter()
  77. {
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Destructor, private as well
  81. MDCImporter::~MDCImporter()
  82. {
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // Returns whether the class can handle the format of the given file.
  86. bool MDCImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  87. {
  88. const std::string extension = GetExtension(pFile);
  89. if (extension == "mdc")
  90. return true;
  91. // if check for extension is not enough, check for the magic tokens
  92. if (!extension.length() || checkSig) {
  93. uint32_t tokens[1];
  94. tokens[0] = AI_MDC_MAGIC_NUMBER_LE;
  95. return CheckMagicToken(pIOHandler,pFile,tokens,1);
  96. }
  97. return false;
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. const aiImporterDesc* MDCImporter::GetInfo () const
  101. {
  102. return &desc;
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // Validate the header of the given MDC file
  106. void MDCImporter::ValidateHeader()
  107. {
  108. AI_SWAP4( this->pcHeader->ulVersion );
  109. AI_SWAP4( this->pcHeader->ulFlags );
  110. AI_SWAP4( this->pcHeader->ulNumFrames );
  111. AI_SWAP4( this->pcHeader->ulNumTags );
  112. AI_SWAP4( this->pcHeader->ulNumSurfaces );
  113. AI_SWAP4( this->pcHeader->ulNumSkins );
  114. AI_SWAP4( this->pcHeader->ulOffsetBorderFrames );
  115. if (pcHeader->ulIdent != AI_MDC_MAGIC_NUMBER_BE &&
  116. pcHeader->ulIdent != AI_MDC_MAGIC_NUMBER_LE)
  117. {
  118. char szBuffer[5];
  119. szBuffer[0] = ((char*)&pcHeader->ulIdent)[0];
  120. szBuffer[1] = ((char*)&pcHeader->ulIdent)[1];
  121. szBuffer[2] = ((char*)&pcHeader->ulIdent)[2];
  122. szBuffer[3] = ((char*)&pcHeader->ulIdent)[3];
  123. szBuffer[4] = '\0';
  124. throw DeadlyImportError("Invalid MDC magic word: should be IDPC, the "
  125. "magic word found is " + std::string( szBuffer ));
  126. }
  127. if (pcHeader->ulVersion != AI_MDC_VERSION)
  128. DefaultLogger::get()->warn("Unsupported MDC file version (2 (AI_MDC_VERSION) was expected)");
  129. if (pcHeader->ulOffsetBorderFrames + pcHeader->ulNumFrames * sizeof(MDC::Frame) > this->fileSize ||
  130. pcHeader->ulOffsetSurfaces + pcHeader->ulNumSurfaces * sizeof(MDC::Surface) > this->fileSize)
  131. {
  132. throw DeadlyImportError("Some of the offset values in the MDC header are invalid "
  133. "and point to something behind the file.");
  134. }
  135. if (this->configFrameID >= this->pcHeader->ulNumFrames)
  136. throw DeadlyImportError("The requested frame is not available");
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. // Validate the header of a given MDC file surface
  140. void MDCImporter::ValidateSurfaceHeader(BE_NCONST MDC::Surface* pcSurf)
  141. {
  142. AI_SWAP4(pcSurf->ulFlags);
  143. AI_SWAP4(pcSurf->ulNumCompFrames);
  144. AI_SWAP4(pcSurf->ulNumBaseFrames);
  145. AI_SWAP4(pcSurf->ulNumShaders);
  146. AI_SWAP4(pcSurf->ulNumVertices);
  147. AI_SWAP4(pcSurf->ulNumTriangles);
  148. AI_SWAP4(pcSurf->ulOffsetTriangles);
  149. AI_SWAP4(pcSurf->ulOffsetTexCoords);
  150. AI_SWAP4(pcSurf->ulOffsetBaseVerts);
  151. AI_SWAP4(pcSurf->ulOffsetCompVerts);
  152. AI_SWAP4(pcSurf->ulOffsetFrameBaseFrames);
  153. AI_SWAP4(pcSurf->ulOffsetFrameCompFrames);
  154. AI_SWAP4(pcSurf->ulOffsetEnd);
  155. const unsigned int iMax = this->fileSize - (unsigned int)((int8_t*)pcSurf-(int8_t*)pcHeader);
  156. if (pcSurf->ulOffsetBaseVerts + pcSurf->ulNumVertices * sizeof(MDC::BaseVertex) > iMax ||
  157. (pcSurf->ulNumCompFrames && pcSurf->ulOffsetCompVerts + pcSurf->ulNumVertices * sizeof(MDC::CompressedVertex) > iMax) ||
  158. pcSurf->ulOffsetTriangles + pcSurf->ulNumTriangles * sizeof(MDC::Triangle) > iMax ||
  159. pcSurf->ulOffsetTexCoords + pcSurf->ulNumVertices * sizeof(MDC::TexturCoord) > iMax ||
  160. pcSurf->ulOffsetShaders + pcSurf->ulNumShaders * sizeof(MDC::Shader) > iMax ||
  161. pcSurf->ulOffsetFrameBaseFrames + pcSurf->ulNumBaseFrames * 2 > iMax ||
  162. (pcSurf->ulNumCompFrames && pcSurf->ulOffsetFrameCompFrames + pcSurf->ulNumCompFrames * 2 > iMax))
  163. {
  164. throw DeadlyImportError("Some of the offset values in the MDC surface header "
  165. "are invalid and point somewhere behind the file.");
  166. }
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. // Setup configuration properties
  170. void MDCImporter::SetupProperties(const Importer* pImp)
  171. {
  172. // The AI_CONFIG_IMPORT_MDC_KEYFRAME option overrides the
  173. // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
  174. if(static_cast<unsigned int>(-1) == (configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_MDC_KEYFRAME,-1))){
  175. configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
  176. }
  177. }
  178. // ------------------------------------------------------------------------------------------------
  179. // Imports the given file into the given scene structure.
  180. void MDCImporter::InternReadFile(
  181. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  182. {
  183. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  184. // Check whether we can read from the file
  185. if( file.get() == NULL)
  186. throw DeadlyImportError( "Failed to open MDC file " + pFile + ".");
  187. // check whether the mdc file is large enough to contain the file header
  188. fileSize = (unsigned int)file->FileSize();
  189. if( fileSize < sizeof(MDC::Header))
  190. throw DeadlyImportError( "MDC File is too small.");
  191. std::vector<unsigned char> mBuffer2(fileSize);
  192. file->Read( &mBuffer2[0], 1, fileSize);
  193. mBuffer = &mBuffer2[0];
  194. // validate the file header
  195. this->pcHeader = (BE_NCONST MDC::Header*)this->mBuffer;
  196. this->ValidateHeader();
  197. std::vector<std::string> aszShaders;
  198. // get a pointer to the frame we want to read
  199. BE_NCONST MDC::Frame* pcFrame = (BE_NCONST MDC::Frame*)(this->mBuffer+
  200. this->pcHeader->ulOffsetBorderFrames);
  201. // no need to swap the other members, we won't need them
  202. pcFrame += configFrameID;
  203. AI_SWAP4( pcFrame->localOrigin[0] );
  204. AI_SWAP4( pcFrame->localOrigin[1] );
  205. AI_SWAP4( pcFrame->localOrigin[2] );
  206. // get the number of valid surfaces
  207. BE_NCONST MDC::Surface* pcSurface, *pcSurface2;
  208. pcSurface = pcSurface2 = new (mBuffer + pcHeader->ulOffsetSurfaces) MDC::Surface;
  209. unsigned int iNumShaders = 0;
  210. for (unsigned int i = 0; i < pcHeader->ulNumSurfaces;++i)
  211. {
  212. // validate the surface header
  213. this->ValidateSurfaceHeader(pcSurface2);
  214. if (pcSurface2->ulNumVertices && pcSurface2->ulNumTriangles)++pScene->mNumMeshes;
  215. iNumShaders += pcSurface2->ulNumShaders;
  216. pcSurface2 = new ((int8_t*)pcSurface2 + pcSurface2->ulOffsetEnd) MDC::Surface;
  217. }
  218. aszShaders.reserve(iNumShaders);
  219. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  220. // necessary that we don't crash if an exception occurs
  221. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  222. pScene->mMeshes[i] = NULL;
  223. // now read all surfaces
  224. unsigned int iDefaultMatIndex = UINT_MAX;
  225. for (unsigned int i = 0, iNum = 0; i < pcHeader->ulNumSurfaces;++i)
  226. {
  227. if (!pcSurface->ulNumVertices || !pcSurface->ulNumTriangles)continue;
  228. aiMesh* pcMesh = pScene->mMeshes[iNum++] = new aiMesh();
  229. pcMesh->mNumFaces = pcSurface->ulNumTriangles;
  230. pcMesh->mNumVertices = pcMesh->mNumFaces * 3;
  231. // store the name of the surface for use as node name.
  232. // FIX: make sure there is a 0 termination
  233. const_cast<char&>(pcSurface->ucName[AI_MDC_MAXQPATH-1]) = '\0';
  234. pcMesh->mTextureCoords[3] = (aiVector3D*)pcSurface->ucName;
  235. // go to the first shader in the file. ignore the others.
  236. if (pcSurface->ulNumShaders)
  237. {
  238. const MDC::Shader* pcShader = (const MDC::Shader*)((int8_t*)pcSurface + pcSurface->ulOffsetShaders);
  239. pcMesh->mMaterialIndex = (unsigned int)aszShaders.size();
  240. // create a new shader
  241. aszShaders.push_back(std::string( pcShader->ucName, std::min(
  242. ::strlen(pcShader->ucName),sizeof(pcShader->ucName)) ));
  243. }
  244. // need to create a default material
  245. else if (UINT_MAX == iDefaultMatIndex)
  246. {
  247. pcMesh->mMaterialIndex = iDefaultMatIndex = (unsigned int)aszShaders.size();
  248. aszShaders.push_back(std::string());
  249. }
  250. // otherwise assign a reference to the default material
  251. else pcMesh->mMaterialIndex = iDefaultMatIndex;
  252. // allocate output storage for the mesh
  253. aiVector3D* pcVertCur = pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
  254. aiVector3D* pcNorCur = pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
  255. aiVector3D* pcUVCur = pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
  256. aiFace* pcFaceCur = pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  257. // create all vertices/faces
  258. BE_NCONST MDC::Triangle* pcTriangle = (BE_NCONST MDC::Triangle*)
  259. ((int8_t*)pcSurface+pcSurface->ulOffsetTriangles);
  260. BE_NCONST MDC::TexturCoord* const pcUVs = (BE_NCONST MDC::TexturCoord*)
  261. ((int8_t*)pcSurface+pcSurface->ulOffsetTexCoords);
  262. // get a pointer to the uncompressed vertices
  263. int16_t iOfs = *((int16_t*) ((int8_t*) pcSurface +
  264. pcSurface->ulOffsetFrameBaseFrames) + this->configFrameID);
  265. AI_SWAP2(iOfs);
  266. BE_NCONST MDC::BaseVertex* const pcVerts = (BE_NCONST MDC::BaseVertex*)
  267. ((int8_t*)pcSurface+pcSurface->ulOffsetBaseVerts) +
  268. ((int)iOfs * pcSurface->ulNumVertices * 4);
  269. // do the main swapping stuff ...
  270. #if (defined AI_BUILD_BIG_ENDIAN)
  271. // swap all triangles
  272. for (unsigned int i = 0; i < pcSurface->ulNumTriangles;++i)
  273. {
  274. AI_SWAP4( pcTriangle[i].aiIndices[0] );
  275. AI_SWAP4( pcTriangle[i].aiIndices[1] );
  276. AI_SWAP4( pcTriangle[i].aiIndices[2] );
  277. }
  278. // swap all vertices
  279. for (unsigned int i = 0; i < pcSurface->ulNumVertices*pcSurface->ulNumBaseFrames;++i)
  280. {
  281. AI_SWAP2( pcVerts->normal );
  282. AI_SWAP2( pcVerts->x );
  283. AI_SWAP2( pcVerts->y );
  284. AI_SWAP2( pcVerts->z );
  285. }
  286. // swap all texture coordinates
  287. for (unsigned int i = 0; i < pcSurface->ulNumVertices;++i)
  288. {
  289. AI_SWAP4( pcUVs->v );
  290. AI_SWAP4( pcUVs->v );
  291. }
  292. #endif
  293. const MDC::CompressedVertex* pcCVerts = NULL;
  294. int16_t* mdcCompVert = NULL;
  295. // access compressed frames for large frame numbers, but never for the first
  296. if( this->configFrameID && pcSurface->ulNumCompFrames > 0 )
  297. {
  298. mdcCompVert = (int16_t*) ((int8_t*)pcSurface+pcSurface->ulOffsetFrameCompFrames) + this->configFrameID;
  299. AI_SWAP2P(mdcCompVert);
  300. if( *mdcCompVert >= 0 )
  301. {
  302. pcCVerts = (const MDC::CompressedVertex*)((int8_t*)pcSurface +
  303. pcSurface->ulOffsetCompVerts) + *mdcCompVert * pcSurface->ulNumVertices;
  304. }
  305. else mdcCompVert = NULL;
  306. }
  307. // copy all faces
  308. for (unsigned int iFace = 0; iFace < pcSurface->ulNumTriangles;++iFace,
  309. ++pcTriangle,++pcFaceCur)
  310. {
  311. const unsigned int iOutIndex = iFace*3;
  312. pcFaceCur->mNumIndices = 3;
  313. pcFaceCur->mIndices = new unsigned int[3];
  314. for (unsigned int iIndex = 0; iIndex < 3;++iIndex,
  315. ++pcVertCur,++pcUVCur,++pcNorCur)
  316. {
  317. uint32_t quak = pcTriangle->aiIndices[iIndex];
  318. if (quak >= pcSurface->ulNumVertices)
  319. {
  320. DefaultLogger::get()->error("MDC vertex index is out of range");
  321. quak = pcSurface->ulNumVertices-1;
  322. }
  323. // compressed vertices?
  324. if (mdcCompVert)
  325. {
  326. MDC::BuildVertex(*pcFrame,pcVerts[quak],pcCVerts[quak],
  327. *pcVertCur,*pcNorCur);
  328. }
  329. else
  330. {
  331. // copy position
  332. pcVertCur->x = pcVerts[quak].x * AI_MDC_BASE_SCALING;
  333. pcVertCur->y = pcVerts[quak].y * AI_MDC_BASE_SCALING;
  334. pcVertCur->z = pcVerts[quak].z * AI_MDC_BASE_SCALING;
  335. // copy normals
  336. MD3::LatLngNormalToVec3( pcVerts[quak].normal, &pcNorCur->x );
  337. // copy texture coordinates
  338. pcUVCur->x = pcUVs[quak].u;
  339. pcUVCur->y = 1.0f-pcUVs[quak].v; // DX to OGL
  340. }
  341. pcVertCur->x += pcFrame->localOrigin[0] ;
  342. pcVertCur->y += pcFrame->localOrigin[1] ;
  343. pcVertCur->z += pcFrame->localOrigin[2] ;
  344. }
  345. // swap the face order - DX to OGL
  346. pcFaceCur->mIndices[0] = iOutIndex + 2;
  347. pcFaceCur->mIndices[1] = iOutIndex + 1;
  348. pcFaceCur->mIndices[2] = iOutIndex + 0;
  349. }
  350. pcSurface = new ((int8_t*)pcSurface + pcSurface->ulOffsetEnd) MDC::Surface;
  351. }
  352. // create a flat node graph with a root node and one child for each surface
  353. if (!pScene->mNumMeshes)
  354. throw DeadlyImportError( "Invalid MDC file: File contains no valid mesh");
  355. else if (1 == pScene->mNumMeshes)
  356. {
  357. pScene->mRootNode = new aiNode();
  358. pScene->mRootNode->mName.Set(std::string((const char*)pScene->mMeshes[0]->mTextureCoords[3]));
  359. pScene->mRootNode->mNumMeshes = 1;
  360. pScene->mRootNode->mMeshes = new unsigned int[1];
  361. pScene->mRootNode->mMeshes[0] = 0;
  362. }
  363. else
  364. {
  365. pScene->mRootNode = new aiNode();
  366. pScene->mRootNode->mNumChildren = pScene->mNumMeshes;
  367. pScene->mRootNode->mChildren = new aiNode*[pScene->mNumMeshes];
  368. pScene->mRootNode->mName.Set("<root>");
  369. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  370. {
  371. aiNode* pcNode = pScene->mRootNode->mChildren[i] = new aiNode();
  372. pcNode->mParent = pScene->mRootNode;
  373. pcNode->mName.Set(std::string((const char*)pScene->mMeshes[i]->mTextureCoords[3]));
  374. pcNode->mNumMeshes = 1;
  375. pcNode->mMeshes = new unsigned int[1];
  376. pcNode->mMeshes[0] = i;
  377. }
  378. }
  379. // make sure we invalidate the pointer to the mesh name
  380. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  381. pScene->mMeshes[i]->mTextureCoords[3] = NULL;
  382. // create materials
  383. pScene->mNumMaterials = (unsigned int)aszShaders.size();
  384. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  385. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  386. {
  387. aiMaterial* pcMat = new aiMaterial();
  388. pScene->mMaterials[i] = pcMat;
  389. const std::string& name = aszShaders[i];
  390. int iMode = (int)aiShadingMode_Gouraud;
  391. pcMat->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  392. // add a small ambient color value - RtCW seems to have one
  393. aiColor3D clr;
  394. clr.b = clr.g = clr.r = 0.05f;
  395. pcMat->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
  396. if (name.length())clr.b = clr.g = clr.r = 1.0f;
  397. else clr.b = clr.g = clr.r = 0.6f;
  398. pcMat->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
  399. pcMat->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
  400. if (name.length())
  401. {
  402. aiString path;
  403. path.Set(name);
  404. pcMat->AddProperty(&path,AI_MATKEY_TEXTURE_DIFFUSE(0));
  405. }
  406. }
  407. }
  408. #endif // !! ASSIMP_BUILD_NO_MDC_IMPORTER