25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

440 lines
14 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 UnrealLoader.cpp
  35. * @brief Implementation of the UNREAL (*.3D) importer class
  36. *
  37. * Sources:
  38. * http://local.wasp.uwa.edu.au/~pbourke/dataformats/unreal/
  39. */
  40. #include "AssimpPCH.h"
  41. #ifndef ASSIMP_BUILD_NO_3D_IMPORTER
  42. #include "UnrealLoader.h"
  43. #include "StreamReader.h"
  44. #include "ParsingUtils.h"
  45. #include "fast_atof.h"
  46. #include "ConvertToLHProcess.h"
  47. using namespace Assimp;
  48. static const aiImporterDesc desc = {
  49. "Unreal Mesh Importer",
  50. "",
  51. "",
  52. "",
  53. aiImporterFlags_SupportTextFlavour,
  54. 0,
  55. 0,
  56. 0,
  57. 0,
  58. "3d uc"
  59. };
  60. // ------------------------------------------------------------------------------------------------
  61. // Constructor to be privately used by Importer
  62. UnrealImporter::UnrealImporter()
  63. : configFrameID (0)
  64. , configHandleFlags (true)
  65. {}
  66. // ------------------------------------------------------------------------------------------------
  67. // Destructor, private as well
  68. UnrealImporter::~UnrealImporter()
  69. {}
  70. // ------------------------------------------------------------------------------------------------
  71. // Returns whether the class can handle the format of the given file.
  72. bool UnrealImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const
  73. {
  74. return SimpleExtensionCheck(pFile,"3d","uc");
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Build a string of all file extensions supported
  78. const aiImporterDesc* UnrealImporter::GetInfo () const
  79. {
  80. return &desc;
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Setup configuration properties for the loader
  84. void UnrealImporter::SetupProperties(const Importer* pImp)
  85. {
  86. // The
  87. // AI_CONFIG_IMPORT_UNREAL_KEYFRAME option overrides the
  88. // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
  89. configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_UNREAL_KEYFRAME,-1);
  90. if(static_cast<unsigned int>(-1) == configFrameID) {
  91. configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
  92. }
  93. // AI_CONFIG_IMPORT_UNREAL_HANDLE_FLAGS, default is true
  94. configHandleFlags = (0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_UNREAL_HANDLE_FLAGS,1));
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Imports the given file into the given scene structure.
  98. void UnrealImporter::InternReadFile( const std::string& pFile,
  99. aiScene* pScene, IOSystem* pIOHandler)
  100. {
  101. // For any of the 3 files being passed get the three correct paths
  102. // First of all, determine file extension
  103. std::string::size_type pos = pFile.find_last_of('.');
  104. std::string extension = GetExtension(pFile);
  105. std::string d_path,a_path,uc_path;
  106. if (extension == "3d") {
  107. // jjjj_d.3d
  108. // jjjj_a.3d
  109. pos = pFile.find_last_of('_');
  110. if (std::string::npos == pos) {
  111. throw DeadlyImportError("UNREAL: Unexpected naming scheme");
  112. }
  113. extension = pFile.substr(0,pos);
  114. }
  115. else {
  116. extension = pFile.substr(0,pos);
  117. }
  118. // build proper paths
  119. d_path = extension+"_d.3d";
  120. a_path = extension+"_a.3d";
  121. uc_path = extension+".uc";
  122. DefaultLogger::get()->debug("UNREAL: data file is " + d_path);
  123. DefaultLogger::get()->debug("UNREAL: aniv file is " + a_path);
  124. DefaultLogger::get()->debug("UNREAL: uc file is " + uc_path);
  125. // and open the files ... we can't live without them
  126. IOStream* p = pIOHandler->Open(d_path);
  127. if (!p)
  128. throw DeadlyImportError("UNREAL: Unable to open _d file");
  129. StreamReaderLE d_reader(pIOHandler->Open(d_path));
  130. const uint16_t numTris = d_reader.GetI2();
  131. const uint16_t numVert = d_reader.GetI2();
  132. d_reader.IncPtr(44);
  133. if (!numTris || numVert < 3)
  134. throw DeadlyImportError("UNREAL: Invalid number of vertices/triangles");
  135. // maximum texture index
  136. unsigned int maxTexIdx = 0;
  137. // collect triangles
  138. std::vector<Unreal::Triangle> triangles(numTris);
  139. for (std::vector<Unreal::Triangle>::iterator it = triangles.begin(), end = triangles.end();it != end; ++it) {
  140. Unreal::Triangle& tri = *it;
  141. for (unsigned int i = 0; i < 3;++i) {
  142. tri.mVertex[i] = d_reader.GetI2();
  143. if (tri.mVertex[i] >= numTris) {
  144. DefaultLogger::get()->warn("UNREAL: vertex index out of range");
  145. tri.mVertex[i] = 0;
  146. }
  147. }
  148. tri.mType = d_reader.GetI1();
  149. // handle mesh flagss?
  150. if (configHandleFlags)
  151. tri.mType = Unreal::MF_NORMAL_OS;
  152. else {
  153. // ignore MOD and MASKED for the moment, treat them as two-sided
  154. if (tri.mType == Unreal::MF_NORMAL_MOD_TS || tri.mType == Unreal::MF_NORMAL_MASKED_TS)
  155. tri.mType = Unreal::MF_NORMAL_TS;
  156. }
  157. d_reader.IncPtr(1);
  158. for (unsigned int i = 0; i < 3;++i)
  159. for (unsigned int i2 = 0; i2 < 2;++i2)
  160. tri.mTex[i][i2] = d_reader.GetI1();
  161. tri.mTextureNum = d_reader.GetI1();
  162. maxTexIdx = std::max(maxTexIdx,(unsigned int)tri.mTextureNum);
  163. d_reader.IncPtr(1);
  164. }
  165. p = pIOHandler->Open(a_path);
  166. if (!p)
  167. throw DeadlyImportError("UNREAL: Unable to open _a file");
  168. StreamReaderLE a_reader(pIOHandler->Open(a_path));
  169. // read number of frames
  170. const uint32_t numFrames = a_reader.GetI2();
  171. if (configFrameID >= numFrames)
  172. throw DeadlyImportError("UNREAL: The requested frame does not exist");
  173. uint32_t st = a_reader.GetI2();
  174. if (st != numVert*4)
  175. throw DeadlyImportError("UNREAL: Unexpected aniv file length");
  176. // skip to our frame
  177. a_reader.IncPtr(configFrameID *numVert*4);
  178. // collect vertices
  179. std::vector<aiVector3D> vertices(numVert);
  180. for (std::vector<aiVector3D>::iterator it = vertices.begin(), end = vertices.end(); it != end; ++it) {
  181. int32_t val = a_reader.GetI4();
  182. Unreal::DecompressVertex(*it,val);
  183. }
  184. // list of textures.
  185. std::vector< std::pair<unsigned int, std::string> > textures;
  186. // allocate the output scene
  187. aiNode* nd = pScene->mRootNode = new aiNode();
  188. nd->mName.Set("<UnrealRoot>");
  189. // we can live without the uc file if necessary
  190. boost::scoped_ptr<IOStream> pb (pIOHandler->Open(uc_path));
  191. if (pb.get()) {
  192. std::vector<char> _data;
  193. TextFileToBuffer(pb.get(),_data);
  194. const char* data = &_data[0];
  195. std::vector< std::pair< std::string,std::string > > tempTextures;
  196. // do a quick search in the UC file for some known, usually texture-related, tags
  197. for (;*data;++data) {
  198. if (TokenMatchI(data,"#exec",5)) {
  199. SkipSpacesAndLineEnd(&data);
  200. // #exec TEXTURE IMPORT [...] NAME=jjjjj [...] FILE=jjjj.pcx [...]
  201. if (TokenMatchI(data,"TEXTURE",7)) {
  202. SkipSpacesAndLineEnd(&data);
  203. if (TokenMatchI(data,"IMPORT",6)) {
  204. tempTextures.push_back(std::pair< std::string,std::string >());
  205. std::pair< std::string,std::string >& me = tempTextures.back();
  206. for (;!IsLineEnd(*data);++data) {
  207. if (!::ASSIMP_strincmp(data,"NAME=",5)) {
  208. const char *d = data+=5;
  209. for (;!IsSpaceOrNewLine(*data);++data);
  210. me.first = std::string(d,(size_t)(data-d));
  211. }
  212. else if (!::ASSIMP_strincmp(data,"FILE=",5)) {
  213. const char *d = data+=5;
  214. for (;!IsSpaceOrNewLine(*data);++data);
  215. me.second = std::string(d,(size_t)(data-d));
  216. }
  217. }
  218. if (!me.first.length() || !me.second.length())
  219. tempTextures.pop_back();
  220. }
  221. }
  222. // #exec MESHMAP SETTEXTURE MESHMAP=box NUM=1 TEXTURE=Jtex1
  223. // #exec MESHMAP SCALE MESHMAP=box X=0.1 Y=0.1 Z=0.2
  224. else if (TokenMatchI(data,"MESHMAP",7)) {
  225. SkipSpacesAndLineEnd(&data);
  226. if (TokenMatchI(data,"SETTEXTURE",10)) {
  227. textures.push_back(std::pair<unsigned int, std::string>());
  228. std::pair<unsigned int, std::string>& me = textures.back();
  229. for (;!IsLineEnd(*data);++data) {
  230. if (!::ASSIMP_strincmp(data,"NUM=",4)) {
  231. data += 4;
  232. me.first = strtoul10(data,&data);
  233. }
  234. else if (!::ASSIMP_strincmp(data,"TEXTURE=",8)) {
  235. data += 8;
  236. const char *d = data;
  237. for (;!IsSpaceOrNewLine(*data);++data);
  238. me.second = std::string(d,(size_t)(data-d));
  239. // try to find matching path names, doesn't care if we don't find them
  240. for (std::vector< std::pair< std::string,std::string > >::const_iterator it = tempTextures.begin();
  241. it != tempTextures.end(); ++it) {
  242. if ((*it).first == me.second) {
  243. me.second = (*it).second;
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. }
  250. else if (TokenMatchI(data,"SCALE",5)) {
  251. for (;!IsLineEnd(*data);++data) {
  252. if (data[0] == 'X' && data[1] == '=') {
  253. data = fast_atoreal_move<float>(data+2,(float&)nd->mTransformation.a1);
  254. }
  255. else if (data[0] == 'Y' && data[1] == '=') {
  256. data = fast_atoreal_move<float>(data+2,(float&)nd->mTransformation.b2);
  257. }
  258. else if (data[0] == 'Z' && data[1] == '=') {
  259. data = fast_atoreal_move<float>(data+2,(float&)nd->mTransformation.c3);
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. else {
  268. DefaultLogger::get()->error("Unable to open .uc file");
  269. }
  270. std::vector<Unreal::TempMat> materials;
  271. materials.reserve(textures.size()*2+5);
  272. // find out how many output meshes and materials we'll have and build material indices
  273. for (std::vector<Unreal::Triangle>::iterator it = triangles.begin(), end = triangles.end();it != end; ++it) {
  274. Unreal::Triangle& tri = *it;
  275. Unreal::TempMat mat(tri);
  276. std::vector<Unreal::TempMat>::iterator nt = std::find(materials.begin(),materials.end(),mat);
  277. if (nt == materials.end()) {
  278. // add material
  279. tri.matIndex = materials.size();
  280. mat.numFaces = 1;
  281. materials.push_back(mat);
  282. ++pScene->mNumMeshes;
  283. }
  284. else {
  285. tri.matIndex = static_cast<unsigned int>(nt-materials.begin());
  286. ++nt->numFaces;
  287. }
  288. }
  289. if (!pScene->mNumMeshes) {
  290. throw DeadlyImportError("UNREAL: Unable to find valid mesh data");
  291. }
  292. // allocate meshes and bind them to the node graph
  293. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  294. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = pScene->mNumMeshes];
  295. nd->mNumMeshes = pScene->mNumMeshes;
  296. nd->mMeshes = new unsigned int[nd->mNumMeshes];
  297. for (unsigned int i = 0; i < pScene->mNumMeshes;++i) {
  298. aiMesh* m = pScene->mMeshes[i] = new aiMesh();
  299. m->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  300. const unsigned int num = materials[i].numFaces;
  301. m->mFaces = new aiFace [num];
  302. m->mVertices = new aiVector3D [num*3];
  303. m->mTextureCoords[0] = new aiVector3D [num*3];
  304. nd->mMeshes[i] = i;
  305. // create materials, too
  306. aiMaterial* mat = new aiMaterial();
  307. pScene->mMaterials[i] = mat;
  308. // all white by default - texture rulez
  309. aiColor3D color(1.f,1.f,1.f);
  310. aiString s;
  311. ::sprintf(s.data,"mat%i_tx%i_",i,materials[i].tex);
  312. // set the two-sided flag
  313. if (materials[i].type == Unreal::MF_NORMAL_TS) {
  314. const int twosided = 1;
  315. mat->AddProperty(&twosided,1,AI_MATKEY_TWOSIDED);
  316. ::strcat(s.data,"ts_");
  317. }
  318. else ::strcat(s.data,"os_");
  319. // make TRANS faces 90% opaque that RemRedundantMaterials won't catch us
  320. if (materials[i].type == Unreal::MF_NORMAL_TRANS_TS) {
  321. const float opac = 0.9f;
  322. mat->AddProperty(&opac,1,AI_MATKEY_OPACITY);
  323. ::strcat(s.data,"tran_");
  324. }
  325. else ::strcat(s.data,"opaq_");
  326. // a special name for the weapon attachment point
  327. if (materials[i].type == Unreal::MF_WEAPON_PLACEHOLDER) {
  328. s.length = ::sprintf(s.data,"$WeaponTag$");
  329. color = aiColor3D(0.f,0.f,0.f);
  330. }
  331. // set color and name
  332. mat->AddProperty(&color,1,AI_MATKEY_COLOR_DIFFUSE);
  333. s.length = ::strlen(s.data);
  334. mat->AddProperty(&s,AI_MATKEY_NAME);
  335. // set texture, if any
  336. const unsigned int tex = materials[i].tex;
  337. for (std::vector< std::pair< unsigned int, std::string > >::const_iterator it = textures.begin();it != textures.end();++it) {
  338. if ((*it).first == tex) {
  339. s.Set((*it).second);
  340. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  341. break;
  342. }
  343. }
  344. }
  345. // fill them.
  346. for (std::vector<Unreal::Triangle>::iterator it = triangles.begin(), end = triangles.end();it != end; ++it) {
  347. Unreal::Triangle& tri = *it;
  348. Unreal::TempMat mat(tri);
  349. std::vector<Unreal::TempMat>::iterator nt = std::find(materials.begin(),materials.end(),mat);
  350. aiMesh* mesh = pScene->mMeshes[nt-materials.begin()];
  351. aiFace& f = mesh->mFaces[mesh->mNumFaces++];
  352. f.mIndices = new unsigned int[f.mNumIndices = 3];
  353. for (unsigned int i = 0; i < 3;++i,mesh->mNumVertices++) {
  354. f.mIndices[i] = mesh->mNumVertices;
  355. mesh->mVertices[mesh->mNumVertices] = vertices[ tri.mVertex[i] ];
  356. mesh->mTextureCoords[0][mesh->mNumVertices] = aiVector3D( tri.mTex[i][0] / 255.f, 1.f - tri.mTex[i][1] / 255.f, 0.f);
  357. }
  358. }
  359. // convert to RH
  360. MakeLeftHandedProcess hero;
  361. hero.Execute(pScene);
  362. FlipWindingOrderProcess flipper;
  363. flipper.Execute(pScene);
  364. }
  365. #endif // !! ASSIMP_BUILD_NO_3D_IMPORTER