ASELoader.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the ASE importer class */
  35. // internal headers
  36. #include "ASELoader.h"
  37. #include "MaterialSystem.h"
  38. #include "StringComparison.h"
  39. #include "TextureTransform.h"
  40. // utilities
  41. #include "fast_atof.h"
  42. #include "qnan.h"
  43. // ASSIMP public headers
  44. #include "../include/IOStream.h"
  45. #include "../include/IOSystem.h"
  46. #include "../include/aiMesh.h"
  47. #include "../include/aiScene.h"
  48. #include "../include/aiAssert.h"
  49. #include "../include/DefaultLogger.h"
  50. #include <boost/scoped_ptr.hpp>
  51. using namespace Assimp;
  52. using namespace Assimp::ASE;
  53. // ------------------------------------------------------------------------------------------------
  54. // Constructor to be privately used by Importer
  55. ASEImporter::ASEImporter()
  56. {
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // Destructor, private as well
  60. ASEImporter::~ASEImporter()
  61. {
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Returns whether the class can handle the format of the given file.
  65. bool ASEImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  66. {
  67. // simple check of file extension is enough for the moment
  68. std::string::size_type pos = pFile.find_last_of('.');
  69. // no file extension - can't read
  70. if( pos == std::string::npos)
  71. return false;
  72. std::string extension = pFile.substr( pos);
  73. if (extension.length() < 4)return false;
  74. if (extension[0] != '.')return false;
  75. if (extension[1] != 'a' && extension[1] != 'A')return false;
  76. if (extension[2] != 's' && extension[2] != 'S')return false;
  77. // NOTE: Sometimes the extension .ASK is also used
  78. // however, it often contains static animation skeletons
  79. // only (without real animations).
  80. if (extension[3] != 'e' && extension[3] != 'E' &&
  81. extension[3] != 'k' && extension[3] != 'K')return false;
  82. return true;
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // Imports the given file into the given scene structure.
  86. void ASEImporter::InternReadFile(
  87. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  88. {
  89. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  90. // Check whether we can read from the file
  91. if( file.get() == NULL)
  92. throw new ImportErrorException( "Failed to open ASE file " + pFile + ".");
  93. size_t fileSize = file->FileSize();
  94. // allocate storage and copy the contents of the file to a memory buffer
  95. // (terminate it with zero)
  96. std::vector<char> mBuffer2(fileSize+1);
  97. file->Read( &mBuffer2[0], 1, fileSize);
  98. mBuffer2[fileSize] = '\0';
  99. this->mBuffer = &mBuffer2[0];
  100. this->pcScene = pScene;
  101. // construct an ASE parser and parse the file
  102. // TODO: clean this up, mParser should be a reference, not a pointer ...
  103. ASE::Parser parser(this->mBuffer);
  104. this->mParser = &parser;
  105. this->mParser->Parse();
  106. // if absolutely no material has been loaded from the file
  107. // we need to generate a default material
  108. this->GenerateDefaultMaterial();
  109. // process all meshes
  110. std::vector<aiMesh*> avOutMeshes;
  111. avOutMeshes.reserve(this->mParser->m_vMeshes.size()*2);
  112. for (std::vector<ASE::Mesh>::iterator
  113. i = this->mParser->m_vMeshes.begin();
  114. i != this->mParser->m_vMeshes.end();++i)
  115. {
  116. if ((*i).bSkip)continue;
  117. this->TransformVertices(*i);
  118. // now we need to create proper meshes from the import we need to
  119. // split them by materials, build valid vertex/face lists ...
  120. this->BuildUniqueRepresentation(*i);
  121. // need to generate proper vertex normals if necessary
  122. this->GenerateNormals(*i);
  123. // convert all meshes to aiMesh objects
  124. this->ConvertMeshes(*i,avOutMeshes);
  125. }
  126. // now build the output mesh list. remove dummies
  127. pScene->mNumMeshes = (unsigned int)avOutMeshes.size();
  128. aiMesh** pp = pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  129. for (std::vector<aiMesh*>::const_iterator
  130. i = avOutMeshes.begin();
  131. i != avOutMeshes.end();++i)
  132. {
  133. if (!(*i)->mNumFaces)continue;
  134. *pp++ = *i;
  135. }
  136. pScene->mNumMeshes = (unsigned int)(pp - pScene->mMeshes);
  137. // buil final material indices (remove submaterials and make the final list)
  138. this->BuildMaterialIndices();
  139. // build the final node graph
  140. this->BuildNodes();
  141. // build output animations
  142. this->BuildAnimations();
  143. return;
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. void ASEImporter::GenerateDefaultMaterial()
  147. {
  148. ai_assert(NULL != this->mParser);
  149. bool bHas = false;
  150. for (std::vector<ASE::Mesh>::iterator
  151. i = this->mParser->m_vMeshes.begin();
  152. i != this->mParser->m_vMeshes.end();++i)
  153. {
  154. if ((*i).bSkip)continue;
  155. if (ASE::Face::DEFAULT_MATINDEX == (*i).iMaterialIndex)
  156. {
  157. (*i).iMaterialIndex = (unsigned int)this->mParser->m_vMaterials.size();
  158. bHas = true;
  159. }
  160. }
  161. if (bHas || this->mParser->m_vMaterials.empty())
  162. {
  163. // add a simple material without sub materials to the parser's list
  164. this->mParser->m_vMaterials.push_back ( ASE::Material() );
  165. ASE::Material& mat = this->mParser->m_vMaterials.back();
  166. mat.mDiffuse = aiColor3D(0.6f,0.6f,0.6f);
  167. mat.mSpecular = aiColor3D(1.0f,1.0f,1.0f);
  168. mat.mAmbient = aiColor3D(0.05f,0.05f,0.05f);
  169. mat.mShading = Dot3DSFile::Gouraud;
  170. mat.mName = AI_DEFAULT_MATERIAL_NAME;
  171. }
  172. }
  173. // ------------------------------------------------------------------------------------------------
  174. void ASEImporter::BuildAnimations()
  175. {
  176. // check whether we have at least one mesh which has animations
  177. std::vector<ASE::Mesh>::iterator i = this->mParser->m_vMeshes.begin();
  178. unsigned int iNum = 0;
  179. for (;i != this->mParser->m_vMeshes.end();++i)
  180. {
  181. if ((*i).bSkip)continue;
  182. if ((*i).mAnim.akeyPositions.size() > 1 || (*i).mAnim.akeyRotations.size() > 1)
  183. ++iNum;
  184. }
  185. if (iNum)
  186. {
  187. this->pcScene->mNumAnimations = 1;
  188. this->pcScene->mAnimations = new aiAnimation*[1];
  189. aiAnimation* pcAnim = this->pcScene->mAnimations[0] = new aiAnimation();
  190. pcAnim->mNumBones = iNum;
  191. pcAnim->mBones = new aiBoneAnim*[iNum];
  192. pcAnim->mTicksPerSecond = this->mParser->iFrameSpeed * this->mParser->iTicksPerFrame;
  193. iNum = 0;
  194. i = this->mParser->m_vMeshes.begin();
  195. for (;i != this->mParser->m_vMeshes.end();++i)
  196. {
  197. if ((*i).bSkip)continue;
  198. if ((*i).mAnim.akeyPositions.size() > 1 || (*i).mAnim.akeyRotations.size() > 1)
  199. {
  200. aiBoneAnim* pcBoneAnim = pcAnim->mBones[iNum++] = new aiBoneAnim();
  201. pcBoneAnim->mBoneName.Set((*i).mName);
  202. // copy position keys
  203. if ((*i).mAnim.akeyPositions.size() > 1 )
  204. {
  205. pcBoneAnim->mNumPositionKeys = (unsigned int) (*i).mAnim.akeyPositions.size();
  206. pcBoneAnim->mPositionKeys = new aiVectorKey[pcBoneAnim->mNumPositionKeys];
  207. ::memcpy(pcBoneAnim->mPositionKeys,&(*i).mAnim.akeyPositions[0],
  208. pcBoneAnim->mNumPositionKeys * sizeof(aiVectorKey));
  209. for (unsigned int qq = 0; qq < pcBoneAnim->mNumPositionKeys;++qq)
  210. {
  211. double dTime = pcBoneAnim->mPositionKeys[qq].mTime;
  212. pcAnim->mDuration = std::max(pcAnim->mDuration,dTime);
  213. }
  214. }
  215. // copy rotation keys
  216. if ((*i).mAnim.akeyRotations.size() > 1 )
  217. {
  218. pcBoneAnim->mNumRotationKeys = (unsigned int) (*i).mAnim.akeyPositions.size();
  219. pcBoneAnim->mRotationKeys = new aiQuatKey[pcBoneAnim->mNumPositionKeys];
  220. ::memcpy(pcBoneAnim->mRotationKeys,&(*i).mAnim.akeyRotations[0],
  221. pcBoneAnim->mNumRotationKeys * sizeof(aiQuatKey));
  222. for (unsigned int qq = 0; qq < pcBoneAnim->mNumRotationKeys;++qq)
  223. {
  224. double dTime = pcBoneAnim->mRotationKeys[qq].mTime;
  225. pcAnim->mDuration = std::max(pcAnim->mDuration,dTime);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. // ------------------------------------------------------------------------------------------------
  233. void ASEImporter::AddNodes(aiNode* pcParent,const char* szName)
  234. {
  235. aiMatrix4x4 m;
  236. ASE::DecompTransform dec(m);
  237. this->AddNodes(pcParent,szName,dec);
  238. }
  239. // ------------------------------------------------------------------------------------------------
  240. void ASEImporter::AddNodes(aiNode* pcParent,const char* szName,
  241. const ASE::DecompTransform& decompTrafo)
  242. {
  243. const size_t len = szName ? strlen(szName) : 0;
  244. ai_assert(4 <= AI_MAX_NUMBER_OF_COLOR_SETS);
  245. std::vector<aiNode*> apcNodes;
  246. aiMesh** pcMeshes = pcScene->mMeshes;
  247. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  248. {
  249. // get the name of the mesh
  250. aiMesh* pcMesh = *pcMeshes++;
  251. const ASE::Mesh& mesh = *((const ASE::Mesh*)pcMesh->mColors[2]);
  252. // TODO: experimental quick'n'dirty, clean this up ...
  253. std::string szMyName[2] = {mesh.mName,mesh.mParent} ;
  254. if (!szMyName)
  255. {
  256. continue;
  257. }
  258. if (szName)
  259. {
  260. if( len != szMyName[1].length() ||
  261. 0 != ASSIMP_stricmp ( szName, szMyName[1].c_str() ))
  262. {
  263. continue;
  264. }
  265. }
  266. else if ('\0' != szMyName[1].c_str()[0])continue;
  267. apcNodes.push_back(new aiNode());
  268. aiNode* node = apcNodes.back();
  269. node->mName.Set(szMyName[0]);
  270. node->mNumMeshes = 1;
  271. node->mMeshes = new unsigned int[1];
  272. node->mMeshes[0] = i;
  273. node->mParent = pcParent;
  274. aiMatrix4x4 mParentAdjust = decompTrafo.mMatrix;
  275. mParentAdjust.Inverse();
  276. node->mTransformation = mParentAdjust*mesh.mTransform;
  277. // Transform all vertices of the mesh back into their local space ->
  278. // at the moment they are pretransformed
  279. aiMatrix4x4 mInverse = mesh.mTransform;
  280. mInverse.Inverse();
  281. aiVector3D* pvCurPtr = pcMesh->mVertices;
  282. const aiVector3D* const pvEndPtr = pcMesh->mVertices + pcMesh->mNumVertices;
  283. while (pvCurPtr != pvEndPtr)
  284. {
  285. *pvCurPtr = mInverse * (*pvCurPtr);
  286. pvCurPtr++;
  287. }
  288. // add sub nodes
  289. aiMatrix4x4 mNewAbs = decompTrafo.mMatrix * node->mTransformation;
  290. ASE::DecompTransform dec( mNewAbs);
  291. this->AddNodes(node,node->mName.data,dec);
  292. }
  293. // allocate enough space for the child nodes
  294. pcParent->mNumChildren = (unsigned int)apcNodes.size();
  295. pcParent->mChildren = new aiNode*[apcNodes.size()];
  296. // now build all nodes
  297. for (unsigned int p = 0; p < apcNodes.size();++p)
  298. {
  299. pcParent->mChildren[p] = apcNodes[p];
  300. }
  301. return;
  302. }
  303. // ------------------------------------------------------------------------------------------------
  304. void ASEImporter::BuildNodes()
  305. {
  306. ai_assert(NULL != pcScene);
  307. // allocate the root node
  308. pcScene->mRootNode = new aiNode();
  309. pcScene->mRootNode->mNumMeshes = 0;
  310. pcScene->mRootNode->mMeshes = 0;
  311. pcScene->mRootNode->mName.Set("<root>");
  312. // add all nodes
  313. this->AddNodes(pcScene->mRootNode,NULL);
  314. // now iterate through al meshes and find those that have not yet
  315. // been added to the nodegraph (= their parent could not be recognized)
  316. std::vector<unsigned int> aiList;
  317. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  318. {
  319. // get the name of the mesh
  320. const ASE::Mesh& mesh = *((const ASE::Mesh*)pcScene->mMeshes[i]->mColors[2]);
  321. // TODO: experimental quick'n'dirty, clean this up ...
  322. std::string szMyName[2] = {mesh.mName,mesh.mParent} ;
  323. if (!szMyName)
  324. {
  325. continue;
  326. }
  327. // check whether our parent is known
  328. bool bKnowParent = false;
  329. for (unsigned int i2 = 0; i2 < pcScene->mNumMeshes;++i2)
  330. {
  331. if (i2 == i)continue;
  332. const ASE::Mesh& mesh2 = *((const ASE::Mesh*)pcScene->mMeshes[i2]->mColors[2]);
  333. // TODO: experimental quick'n'dirty, clean this up ...
  334. std::string szMyName2[2] = {mesh2.mName,mesh2.mParent} ;
  335. if (!szMyName2)
  336. {
  337. continue;
  338. }
  339. if (!ASSIMP_stricmp ( szMyName[1], szMyName2[0]))
  340. {
  341. bKnowParent = true;
  342. break;
  343. }
  344. // check if there is another mesh with the same unknown parent
  345. // that has already been handled and added to the list
  346. if (i2 < i)
  347. {
  348. if (ASSIMP_stricmp ( szMyName[1], szMyName2[1]))
  349. {
  350. bKnowParent = true;
  351. break;
  352. }
  353. }
  354. }
  355. if (!bKnowParent)
  356. {
  357. aiList.push_back(i);
  358. }
  359. }
  360. if (!aiList.empty())
  361. {
  362. std::vector<aiNode*> apcNodes;
  363. apcNodes.reserve(aiList.size() + pcScene->mRootNode->mNumChildren);
  364. for (unsigned int i = 0; i < pcScene->mRootNode->mNumChildren;++i)
  365. apcNodes.push_back(pcScene->mRootNode->mChildren[i]);
  366. delete[] pcScene->mRootNode->mChildren;
  367. for (std::vector<unsigned int>::/*const_*/iterator
  368. i = aiList.begin();
  369. i != aiList.end();++i)
  370. {
  371. std::string* szMyName = (std::string*)pcScene->mMeshes[*i]->mColors[1];
  372. if (!szMyName)continue;
  373. DefaultLogger::get()->info("Generating dummy node: " + szMyName[1] + ". "
  374. "This node is not defined in the ASE file, but referenced as "
  375. "parent node.");
  376. // the parent is not known, so we can assume that we must add
  377. // this node to the root node of the whole scene
  378. aiNode* pcNode = new aiNode();
  379. pcNode->mParent = pcScene->mRootNode;
  380. pcNode->mName.Set(szMyName[1]);
  381. this->AddNodes(pcNode,pcNode->mName.data);
  382. apcNodes.push_back(pcNode);
  383. }
  384. pcScene->mRootNode->mChildren = new aiNode*[apcNodes.size()];
  385. for (unsigned int i = 0; i < apcNodes.size();++i)
  386. pcScene->mRootNode->mChildren[i] = apcNodes[i];
  387. pcScene->mRootNode->mNumChildren = (unsigned int)apcNodes.size();
  388. }
  389. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  390. pcScene->mMeshes[i]->mColors[2] = NULL;
  391. // if there is only one subnode, set it as root node
  392. if (1 == pcScene->mRootNode->mNumChildren)
  393. {
  394. aiNode* pc = pcScene->mRootNode;
  395. pcScene->mRootNode = pcScene->mRootNode->mChildren[0];
  396. pcScene->mRootNode->mParent = NULL;
  397. // make sure the destructor won't delete us ...
  398. delete[] pc->mChildren;
  399. pc->mChildren = NULL;
  400. pc->mNumChildren = 0;
  401. delete pc;
  402. }
  403. else if (!pcScene->mRootNode->mNumChildren)
  404. {
  405. throw new ImportErrorException("No nodes loaded. The ASE/ASK file is either empty or corrupt");
  406. }
  407. return;
  408. }
  409. // ------------------------------------------------------------------------------------------------
  410. void ASEImporter::TransformVertices(ASE::Mesh& mesh)
  411. {
  412. // the matrix data is stored in column-major format,
  413. // but we need row major
  414. mesh.mTransform.Transpose();
  415. }
  416. // ------------------------------------------------------------------------------------------------
  417. void ASEImporter::BuildUniqueRepresentation(ASE::Mesh& mesh)
  418. {
  419. // allocate output storage
  420. std::vector<aiVector3D> mPositions;
  421. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  422. std::vector<aiColor4D> mVertexColors;
  423. std::vector<aiVector3D> mNormals;
  424. std::vector<BoneVertex> mBoneVertices;
  425. unsigned int iSize = (unsigned int)mesh.mFaces.size() * 3;
  426. mPositions.resize(iSize);
  427. // optional texture coordinates
  428. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  429. {
  430. if (!mesh.amTexCoords[i].empty())
  431. {
  432. amTexCoords[i].resize(iSize);
  433. }
  434. }
  435. // optional vertex colors
  436. if (!mesh.mVertexColors.empty())
  437. {
  438. mVertexColors.resize(iSize);
  439. }
  440. // optional vertex normals (vertex normals can simply be copied)
  441. if (!mesh.mNormals.empty())
  442. {
  443. mNormals.resize(iSize);
  444. }
  445. // bone vertices. There is no need to change the bone list
  446. if (!mesh.mBoneVertices.empty())
  447. {
  448. mBoneVertices.resize(iSize);
  449. }
  450. // iterate through all faces in the mesh
  451. unsigned int iCurrent = 0;
  452. for (std::vector<ASE::Face>::iterator
  453. i = mesh.mFaces.begin();
  454. i != mesh.mFaces.end();++i)
  455. {
  456. for (unsigned int n = 0; n < 3;++n,++iCurrent)
  457. {
  458. mPositions[iCurrent] = mesh.mPositions[(*i).mIndices[n]];
  459. std::swap(mPositions[iCurrent].z,mPositions[iCurrent].y); // DX-to-OGL
  460. // add texture coordinates
  461. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  462. {
  463. if (!mesh.amTexCoords[c].empty())
  464. {
  465. amTexCoords[c][iCurrent] = mesh.amTexCoords[c][(*i).amUVIndices[c][n]];
  466. amTexCoords[c][iCurrent].y = 1.0f - amTexCoords[c][iCurrent].y; // DX-to-OGL
  467. }
  468. }
  469. // add vertex colors
  470. if (!mesh.mVertexColors.empty())
  471. {
  472. mVertexColors[iCurrent] = mesh.mVertexColors[(*i).mColorIndices[n]];
  473. }
  474. // add normal vectors
  475. if (!mesh.mNormals.empty())
  476. {
  477. mNormals[iCurrent] = mesh.mNormals[(*i).mIndices[n]];
  478. std::swap(mNormals[iCurrent].z,mNormals[iCurrent].y); // DX-to-OGL
  479. }
  480. // handle bone vertices
  481. if ((*i).mIndices[n] < mesh.mBoneVertices.size())
  482. {
  483. // (sometimes this will cause bone verts to be duplicated
  484. // however, I' quite sure Schrompf' JoinVerticesStep
  485. // will fix that again ...)
  486. mBoneVertices[iCurrent] = mesh.mBoneVertices[(*i).mIndices[n]];
  487. }
  488. }
  489. // we need to flip the order of the indices
  490. (*i).mIndices[0] = iCurrent-1;
  491. (*i).mIndices[1] = iCurrent-2;
  492. (*i).mIndices[2] = iCurrent-3;
  493. }
  494. // replace the old arrays
  495. mesh.mNormals = mNormals;
  496. mesh.mPositions = mPositions;
  497. mesh.mVertexColors = mVertexColors;
  498. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  499. mesh.amTexCoords[c] = amTexCoords[c];
  500. return;
  501. }
  502. // ------------------------------------------------------------------------------------------------
  503. void ASEImporter::ConvertMaterial(ASE::Material& mat)
  504. {
  505. // allocate the output material
  506. mat.pcInstance = new MaterialHelper();
  507. // At first add the base ambient color of the
  508. // scene to the material
  509. mat.mAmbient.r += this->mParser->m_clrAmbient.r;
  510. mat.mAmbient.g += this->mParser->m_clrAmbient.g;
  511. mat.mAmbient.b += this->mParser->m_clrAmbient.b;
  512. aiString name;
  513. name.Set( mat.mName);
  514. mat.pcInstance->AddProperty( &name, AI_MATKEY_NAME);
  515. // material colors
  516. mat.pcInstance->AddProperty( &mat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  517. mat.pcInstance->AddProperty( &mat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  518. mat.pcInstance->AddProperty( &mat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  519. mat.pcInstance->AddProperty( &mat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  520. // shininess
  521. if (0.0f != mat.mSpecularExponent && 0.0f != mat.mShininessStrength)
  522. {
  523. mat.pcInstance->AddProperty( &mat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  524. mat.pcInstance->AddProperty( &mat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  525. }
  526. // if there is no shininess, we can disable phong lighting
  527. else if (Dot3DS::Dot3DSFile::Metal == mat.mShading ||
  528. Dot3DS::Dot3DSFile::Phong == mat.mShading ||
  529. Dot3DS::Dot3DSFile::Blinn == mat.mShading)
  530. {
  531. mat.mShading = Dot3DS::Dot3DSFile::Gouraud;
  532. }
  533. // opacity
  534. mat.pcInstance->AddProperty<float>( &mat.mTransparency,1,AI_MATKEY_OPACITY);
  535. // shading mode
  536. aiShadingMode eShading = aiShadingMode_NoShading;
  537. switch (mat.mShading)
  538. {
  539. case Dot3DS::Dot3DSFile::Flat:
  540. eShading = aiShadingMode_Flat; break;
  541. case Dot3DS::Dot3DSFile::Phong :
  542. eShading = aiShadingMode_Phong; break;
  543. case Dot3DS::Dot3DSFile::Blinn :
  544. eShading = aiShadingMode_Blinn; break;
  545. // I don't know what "Wire" shading should be,
  546. // assume it is simple lambertian diffuse (L dot N) shading
  547. case Dot3DS::Dot3DSFile::Wire:
  548. case Dot3DS::Dot3DSFile::Gouraud:
  549. eShading = aiShadingMode_Gouraud; break;
  550. case Dot3DS::Dot3DSFile::Metal :
  551. eShading = aiShadingMode_CookTorrance; break;
  552. }
  553. mat.pcInstance->AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  554. if (Dot3DS::Dot3DSFile::Wire == mat.mShading)
  555. {
  556. // set the wireframe flag
  557. unsigned int iWire = 1;
  558. mat.pcInstance->AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  559. }
  560. // texture, if there is one
  561. if( mat.sTexDiffuse.mMapName.length() > 0)
  562. {
  563. aiString tex;
  564. tex.Set( mat.sTexDiffuse.mMapName);
  565. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  566. if (is_not_qnan(mat.sTexDiffuse.mTextureBlend))
  567. mat.pcInstance->AddProperty<float>( &mat.sTexDiffuse.mTextureBlend, 1,
  568. AI_MATKEY_TEXBLEND_DIFFUSE(0));
  569. }
  570. if( mat.sTexSpecular.mMapName.length() > 0)
  571. {
  572. aiString tex;
  573. tex.Set( mat.sTexSpecular.mMapName);
  574. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  575. if (is_not_qnan(mat.sTexSpecular.mTextureBlend))
  576. mat.pcInstance->AddProperty<float>( &mat.sTexSpecular.mTextureBlend, 1,
  577. AI_MATKEY_TEXBLEND_SPECULAR(0));
  578. }
  579. if( mat.sTexOpacity.mMapName.length() > 0)
  580. {
  581. aiString tex;
  582. tex.Set( mat.sTexOpacity.mMapName);
  583. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  584. if (is_not_qnan(mat.sTexOpacity.mTextureBlend))
  585. mat.pcInstance->AddProperty<float>( &mat.sTexOpacity.mTextureBlend, 1,
  586. AI_MATKEY_TEXBLEND_OPACITY(0));
  587. }
  588. if( mat.sTexEmissive.mMapName.length() > 0)
  589. {
  590. aiString tex;
  591. tex.Set( mat.sTexEmissive.mMapName);
  592. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  593. if (is_not_qnan(mat.sTexEmissive.mTextureBlend))
  594. mat.pcInstance->AddProperty<float>( &mat.sTexEmissive.mTextureBlend, 1,
  595. AI_MATKEY_TEXBLEND_EMISSIVE(0));
  596. }
  597. if( mat.sTexAmbient.mMapName.length() > 0)
  598. {
  599. aiString tex;
  600. tex.Set( mat.sTexAmbient.mMapName);
  601. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_AMBIENT(0));
  602. if (is_not_qnan(mat.sTexAmbient.mTextureBlend))
  603. mat.pcInstance->AddProperty<float>( &mat.sTexAmbient.mTextureBlend, 1,
  604. AI_MATKEY_TEXBLEND_AMBIENT(0));
  605. }
  606. if( mat.sTexBump.mMapName.length() > 0)
  607. {
  608. aiString tex;
  609. tex.Set( mat.sTexBump.mMapName);
  610. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  611. if (is_not_qnan(mat.sTexBump.mTextureBlend))
  612. mat.pcInstance->AddProperty<float>( &mat.sTexBump.mTextureBlend, 1,
  613. AI_MATKEY_TEXBLEND_HEIGHT(0));
  614. }
  615. if( mat.sTexShininess.mMapName.length() > 0)
  616. {
  617. aiString tex;
  618. tex.Set( mat.sTexShininess.mMapName);
  619. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  620. if (is_not_qnan(mat.sTexShininess.mTextureBlend))
  621. mat.pcInstance->AddProperty<float>( &mat.sTexBump.mTextureBlend, 1,
  622. AI_MATKEY_TEXBLEND_SHININESS(0));
  623. }
  624. // store the name of the material itself, too
  625. if( mat.mName.length() > 0)
  626. {
  627. aiString tex;
  628. tex.Set( mat.mName);
  629. mat.pcInstance->AddProperty( &tex, AI_MATKEY_NAME);
  630. }
  631. return;
  632. }
  633. // ------------------------------------------------------------------------------------------------
  634. void ASEImporter::ConvertMeshes(ASE::Mesh& mesh, std::vector<aiMesh*>& avOutMeshes)
  635. {
  636. // validate the material index of the mesh
  637. if (mesh.iMaterialIndex >= this->mParser->m_vMaterials.size())
  638. {
  639. mesh.iMaterialIndex = (unsigned int)this->mParser->m_vMaterials.size()-1;
  640. DefaultLogger::get()->warn("Material index is out of range");
  641. }
  642. // if the material the mesh is assigned to is consisting of submeshes
  643. // we'll need to split it ... Quak.
  644. if (!this->mParser->m_vMaterials[mesh.iMaterialIndex].avSubMaterials.empty())
  645. {
  646. std::vector<ASE::Material> vSubMaterials = this->mParser->
  647. m_vMaterials[mesh.iMaterialIndex].avSubMaterials;
  648. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  649. vSubMaterials.size()];
  650. // build a list of all faces per submaterial
  651. unsigned int iNum = 0;
  652. for (unsigned int i = 0; i < mesh.mFaces.size();++i)
  653. {
  654. // check range
  655. if (mesh.mFaces[i].iMaterial >= vSubMaterials.size())
  656. {
  657. DefaultLogger::get()->warn("Submaterial index is out of range");
  658. // use the last material instead
  659. aiSplit[vSubMaterials.size()-1].push_back(i);
  660. }
  661. else aiSplit[mesh.mFaces[i].iMaterial].push_back(i);
  662. }
  663. // now generate submeshes
  664. for (unsigned int p = 0; p < vSubMaterials.size();++p)
  665. {
  666. if (aiSplit[p].size() != 0)
  667. {
  668. aiMesh* p_pcOut = new aiMesh();
  669. // let the sub material index
  670. p_pcOut->mMaterialIndex = p;
  671. // we will need this material
  672. this->mParser->m_vMaterials[mesh.iMaterialIndex].avSubMaterials[p].bNeed = true;
  673. // store the real index here ... color channel 3
  674. p_pcOut->mColors[3] = (aiColor4D*)(uintptr_t)mesh.iMaterialIndex;
  675. // store a pointer to the mesh in color channel 2
  676. p_pcOut->mColors[2] = (aiColor4D*) &mesh;
  677. avOutMeshes.push_back(p_pcOut);
  678. // convert vertices
  679. p_pcOut->mNumVertices = (unsigned int)aiSplit[p].size()*3;
  680. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  681. // receive output vertex weights
  682. std::vector<std::pair<unsigned int, float> >* avOutputBones;
  683. if (!mesh.mBones.empty())
  684. {
  685. avOutputBones = new std::vector<std::pair<unsigned int, float> >[mesh.mBones.size()];
  686. }
  687. // allocate enough storage for faces
  688. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  689. if (p_pcOut->mNumVertices != 0)
  690. {
  691. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  692. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  693. unsigned int iBase = 0;
  694. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  695. {
  696. unsigned int iIndex = aiSplit[p][q];
  697. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  698. p_pcOut->mFaces[q].mNumIndices = 3;
  699. for (unsigned int t = 0; t < 3;++t)
  700. {
  701. const uint32_t iIndex2 = mesh.mFaces[iIndex].mIndices[t];
  702. p_pcOut->mVertices[iBase] = mesh.mPositions[iIndex2];
  703. p_pcOut->mNormals[iBase] = mesh.mNormals[iIndex2];
  704. // convert bones, if existing
  705. if (!mesh.mBones.empty())
  706. {
  707. // check whether there is a vertex weight that is using
  708. // this vertex index ...
  709. if (iIndex2 < mesh.mBoneVertices.size())
  710. {
  711. for (std::vector<std::pair<int,float> >::const_iterator
  712. blubb = mesh.mBoneVertices[iIndex2].mBoneWeights.begin();
  713. blubb != mesh.mBoneVertices[iIndex2].mBoneWeights.end();++blubb)
  714. {
  715. // NOTE: illegal cases have already been filtered out
  716. avOutputBones[(*blubb).first].push_back(std::pair<unsigned int, float>(
  717. iBase,(*blubb).second));
  718. }
  719. }
  720. }
  721. ++iBase;
  722. }
  723. p_pcOut->mFaces[q].mIndices[0] = iBase-3;
  724. p_pcOut->mFaces[q].mIndices[1] = iBase-2;
  725. p_pcOut->mFaces[q].mIndices[2] = iBase-1;
  726. }
  727. }
  728. // convert texture coordinates
  729. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  730. {
  731. if (!mesh.amTexCoords[c].empty())
  732. {
  733. p_pcOut->mTextureCoords[c] = new aiVector3D[p_pcOut->mNumVertices];
  734. unsigned int iBase = 0;
  735. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  736. {
  737. unsigned int iIndex = aiSplit[p][q];
  738. for (unsigned int t = 0; t < 3;++t)
  739. {
  740. p_pcOut->mTextureCoords[c][iBase++] = mesh.amTexCoords[c][mesh.mFaces[iIndex].mIndices[t]];
  741. }
  742. }
  743. // setup the number of valid vertex components
  744. p_pcOut->mNumUVComponents[c] = mesh.mNumUVComponents[c];
  745. }
  746. }
  747. // convert vertex colors (only one set supported)
  748. if (!mesh.mVertexColors.empty())
  749. {
  750. p_pcOut->mColors[0] = new aiColor4D[p_pcOut->mNumVertices];
  751. unsigned int iBase = 0;
  752. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  753. {
  754. unsigned int iIndex = aiSplit[p][q];
  755. for (unsigned int t = 0; t < 3;++t)
  756. {
  757. p_pcOut->mColors[0][iBase++] = mesh.mVertexColors[mesh.mFaces[iIndex].mIndices[t]];
  758. }
  759. }
  760. }
  761. if (!mesh.mBones.empty())
  762. {
  763. p_pcOut->mNumBones = 0;
  764. for (unsigned int mrspock = 0; mrspock < mesh.mBones.size();++mrspock)
  765. if (!avOutputBones[mrspock].empty())p_pcOut->mNumBones++;
  766. p_pcOut->mBones = new aiBone* [ p_pcOut->mNumBones ];
  767. aiBone** pcBone = p_pcOut->mBones;
  768. for (unsigned int mrspock = 0; mrspock < mesh.mBones.size();++mrspock)
  769. {
  770. if (!avOutputBones[mrspock].empty())
  771. {
  772. // we will need this bone. add it to the output mesh and
  773. // add all per-vertex weights
  774. aiBone* pc = *pcBone = new aiBone();
  775. pc->mName.Set(mesh.mBones[mrspock].mName);
  776. pc->mNumWeights = (unsigned int)avOutputBones[mrspock].size();
  777. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  778. for (unsigned int captainkirk = 0; captainkirk < pc->mNumWeights;++captainkirk)
  779. {
  780. const std::pair<unsigned int,float>& ref = avOutputBones[mrspock][captainkirk];
  781. pc->mWeights[captainkirk].mVertexId = ref.first;
  782. pc->mWeights[captainkirk].mWeight = ref.second;
  783. }
  784. ++pcBone;
  785. }
  786. }
  787. // delete allocated storage
  788. delete[] avOutputBones;
  789. }
  790. }
  791. }
  792. // delete storage
  793. delete[] aiSplit;
  794. }
  795. else
  796. {
  797. // otherwise we can simply copy the data to one output mesh
  798. aiMesh* p_pcOut = new aiMesh();
  799. // set an empty sub material index
  800. p_pcOut->mMaterialIndex = ASE::Face::DEFAULT_MATINDEX;
  801. this->mParser->m_vMaterials[mesh.iMaterialIndex].bNeed = true;
  802. // store the real index here ... in color channel 3
  803. p_pcOut->mColors[3] = (aiColor4D*)(uintptr_t)mesh.iMaterialIndex;
  804. // store a pointer to the mesh in color channel 2
  805. p_pcOut->mColors[2] = (aiColor4D*) &mesh;
  806. avOutMeshes.push_back(p_pcOut);
  807. // if the mesh hasn't faces or vertices, there are two cases
  808. // possible: 1. the model is invalid. 2. This is a dummy
  809. // helper object which we are going to remove later ...
  810. if (mesh.mFaces.empty() || mesh.mPositions.empty())
  811. {
  812. return;
  813. }
  814. // convert vertices
  815. p_pcOut->mNumVertices = (unsigned int)mesh.mPositions.size();
  816. p_pcOut->mNumFaces = (unsigned int)mesh.mFaces.size();
  817. // allocate enough storage for faces
  818. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  819. // copy vertices
  820. p_pcOut->mVertices = new aiVector3D[mesh.mPositions.size()];
  821. memcpy(p_pcOut->mVertices,&mesh.mPositions[0],
  822. mesh.mPositions.size() * sizeof(aiVector3D));
  823. // copy normals
  824. p_pcOut->mNormals = new aiVector3D[mesh.mNormals.size()];
  825. memcpy(p_pcOut->mNormals,&mesh.mNormals[0],
  826. mesh.mNormals.size() * sizeof(aiVector3D));
  827. // copy texture coordinates
  828. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  829. {
  830. if (!mesh.amTexCoords[c].empty())
  831. {
  832. p_pcOut->mTextureCoords[c] = new aiVector3D[mesh.amTexCoords[c].size()];
  833. memcpy(p_pcOut->mTextureCoords[c],&mesh.amTexCoords[c][0],
  834. mesh.amTexCoords[c].size() * sizeof(aiVector3D));
  835. // setup the number of valid vertex components
  836. p_pcOut->mNumUVComponents[c] = mesh.mNumUVComponents[c];
  837. }
  838. }
  839. // copy vertex colors
  840. if (!mesh.mVertexColors.empty())
  841. {
  842. p_pcOut->mColors[0] = new aiColor4D[mesh.mVertexColors.size()];
  843. memcpy(p_pcOut->mColors[0],&mesh.mVertexColors[0],
  844. mesh.mVertexColors.size() * sizeof(aiColor4D));
  845. }
  846. // copy faces
  847. for (unsigned int iFace = 0; iFace < p_pcOut->mNumFaces;++iFace)
  848. {
  849. p_pcOut->mFaces[iFace].mNumIndices = 3;
  850. p_pcOut->mFaces[iFace].mIndices = new unsigned int[3];
  851. // copy indices
  852. p_pcOut->mFaces[iFace].mIndices[0] = mesh.mFaces[iFace].mIndices[0];
  853. p_pcOut->mFaces[iFace].mIndices[1] = mesh.mFaces[iFace].mIndices[1];
  854. p_pcOut->mFaces[iFace].mIndices[2] = mesh.mFaces[iFace].mIndices[2];
  855. }
  856. // copy vertex bones
  857. if (!mesh.mBones.empty() && !mesh.mBoneVertices.empty())
  858. {
  859. std::vector<aiVertexWeight>* avBonesOut = new
  860. std::vector<aiVertexWeight>[mesh.mBones.size()];
  861. // find all vertex weights for this bone
  862. unsigned int quak = 0;
  863. for (std::vector<BoneVertex>::const_iterator
  864. harrypotter = mesh.mBoneVertices.begin();
  865. harrypotter != mesh.mBoneVertices.end();++harrypotter,++quak)
  866. {
  867. for (std::vector<std::pair<int,float> >::const_iterator
  868. ronaldweasley = (*harrypotter).mBoneWeights.begin();
  869. ronaldweasley != (*harrypotter).mBoneWeights.end();++ronaldweasley)
  870. {
  871. aiVertexWeight weight;
  872. weight.mVertexId = quak;
  873. weight.mWeight = (*ronaldweasley).second;
  874. avBonesOut[(*ronaldweasley).first].push_back(weight);
  875. }
  876. }
  877. // now build a final bone list
  878. p_pcOut->mNumBones = 0;
  879. for (unsigned int jfkennedy = 0; jfkennedy < mesh.mBones.size();++jfkennedy)
  880. if (!avBonesOut[jfkennedy].empty())p_pcOut->mNumBones++;
  881. p_pcOut->mBones = new aiBone*[p_pcOut->mNumBones];
  882. aiBone** pcBone = p_pcOut->mBones;
  883. for (unsigned int jfkennedy = 0; jfkennedy < mesh.mBones.size();++jfkennedy)
  884. {
  885. if (!avBonesOut[jfkennedy].empty())
  886. {
  887. aiBone* pc = *pcBone = new aiBone();
  888. pc->mName.Set(mesh.mBones[jfkennedy].mName);
  889. pc->mNumWeights = (unsigned int)avBonesOut[jfkennedy].size();
  890. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  891. ::memcpy(pc->mWeights,&avBonesOut[jfkennedy][0],
  892. sizeof(aiVertexWeight) * pc->mNumWeights);
  893. ++pcBone;
  894. }
  895. }
  896. }
  897. }
  898. return;
  899. }
  900. // ------------------------------------------------------------------------------------------------
  901. void ComputeBounds(ASE::Mesh& mesh,aiVector3D& minVec, aiVector3D& maxVec,
  902. aiMatrix4x4& matrix)
  903. {
  904. minVec = aiVector3D( 1e10f, 1e10f, 1e10f);
  905. maxVec = aiVector3D( -1e10f, -1e10f, -1e10f);
  906. for( std::vector<aiVector3D>::const_iterator
  907. i = mesh.mPositions.begin();
  908. i != mesh.mPositions.end();++i)
  909. {
  910. aiVector3D v = matrix*(*i);
  911. minVec.x = std::min( minVec.x, v.x);
  912. minVec.y = std::min( minVec.y, v.y);
  913. minVec.z = std::min( minVec.z, v.z);
  914. maxVec.x = std::max( maxVec.x, v.x);
  915. maxVec.y = std::max( maxVec.y, v.y);
  916. maxVec.z = std::max( maxVec.z, v.z);
  917. }
  918. return;
  919. }
  920. // ------------------------------------------------------------------------------------------------
  921. void ASEImporter::BuildMaterialIndices()
  922. {
  923. ai_assert(NULL != pcScene);
  924. // iterate through all materials and check whether we need them
  925. for (unsigned int iMat = 0; iMat < this->mParser->m_vMaterials.size();++iMat)
  926. {
  927. if (this->mParser->m_vMaterials[iMat].bNeed)
  928. {
  929. // convert it to the aiMaterial layout
  930. ASE::Material& mat = this->mParser->m_vMaterials[iMat];
  931. this->ConvertMaterial(mat);
  932. TextureTransform::ApplyScaleNOffset(mat);
  933. ++pcScene->mNumMaterials;
  934. }
  935. for (unsigned int iSubMat = 0; iSubMat < this->mParser->m_vMaterials[
  936. iMat].avSubMaterials.size();++iSubMat)
  937. {
  938. if (this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].bNeed)
  939. {
  940. // convert it to the aiMaterial layout
  941. ASE::Material& mat = this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat];
  942. this->ConvertMaterial(mat);
  943. TextureTransform::ApplyScaleNOffset(mat);
  944. ++pcScene->mNumMaterials;
  945. }
  946. }
  947. }
  948. // allocate the output material array
  949. pcScene->mMaterials = new aiMaterial*[pcScene->mNumMaterials];
  950. Dot3DS::Material** pcIntMaterials = new Dot3DS::Material*[pcScene->mNumMaterials];
  951. unsigned int iNum = 0;
  952. for (unsigned int iMat = 0; iMat < this->mParser->m_vMaterials.size();++iMat)
  953. {
  954. if (this->mParser->m_vMaterials[iMat].bNeed)
  955. {
  956. ai_assert(NULL != this->mParser->m_vMaterials[iMat].pcInstance);
  957. pcScene->mMaterials[iNum] = this->mParser->m_vMaterials[iMat].pcInstance;
  958. // store the internal material, too
  959. pcIntMaterials[iNum] = &this->mParser->m_vMaterials[iMat];
  960. // iterate through all meshes and search for one which is using
  961. // this top-level material index
  962. for (unsigned int iMesh = 0; iMesh < pcScene->mNumMeshes;++iMesh)
  963. {
  964. if (ASE::Face::DEFAULT_MATINDEX == pcScene->mMeshes[iMesh]->mMaterialIndex &&
  965. iMat == (uintptr_t)pcScene->mMeshes[iMesh]->mColors[3])
  966. {
  967. pcScene->mMeshes[iMesh]->mMaterialIndex = iNum;
  968. pcScene->mMeshes[iMesh]->mColors[3] = NULL;
  969. }
  970. }
  971. iNum++;
  972. }
  973. for (unsigned int iSubMat = 0; iSubMat < this->mParser->m_vMaterials[iMat].avSubMaterials.size();++iSubMat)
  974. {
  975. if (this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].bNeed)
  976. {
  977. ai_assert(NULL != this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].pcInstance);
  978. pcScene->mMaterials[iNum] = this->mParser->m_vMaterials[iMat].
  979. avSubMaterials[iSubMat].pcInstance;
  980. // store the internal material, too
  981. pcIntMaterials[iNum] = &this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat];
  982. // iterate through all meshes and search for one which is using
  983. // this sub-level material index
  984. for (unsigned int iMesh = 0; iMesh < pcScene->mNumMeshes;++iMesh)
  985. {
  986. if (iSubMat == pcScene->mMeshes[iMesh]->mMaterialIndex &&
  987. iMat == (uintptr_t)pcScene->mMeshes[iMesh]->mColors[3])
  988. {
  989. pcScene->mMeshes[iMesh]->mMaterialIndex = iNum;
  990. pcScene->mMeshes[iMesh]->mColors[3] = NULL;
  991. }
  992. }
  993. iNum++;
  994. }
  995. }
  996. }
  997. // prepare for the next step
  998. for (unsigned int hans = 0; hans < this->mParser->m_vMaterials.size();++hans)
  999. TextureTransform::ApplyScaleNOffset(this->mParser->m_vMaterials[hans]);
  1000. // now we need to iterate through all meshes,
  1001. // generating correct texture coordinates and material uv indices
  1002. for (unsigned int curie = 0; curie < pcScene->mNumMeshes;++curie)
  1003. {
  1004. aiMesh* pcMesh = pcScene->mMeshes[curie];
  1005. // apply texture coordinate transformations
  1006. TextureTransform::BakeScaleNOffset(pcMesh,pcIntMaterials[pcMesh->mMaterialIndex]);
  1007. }
  1008. for (unsigned int hans = 0; hans < pcScene->mNumMaterials;++hans)
  1009. {
  1010. // setup the correct UV indices for each material
  1011. TextureTransform::SetupMatUVSrc(pcScene->mMaterials[hans],
  1012. pcIntMaterials[hans]);
  1013. }
  1014. delete[] pcIntMaterials;
  1015. // finished!
  1016. return;
  1017. }
  1018. // ------------------------------------------------------------------------------------------------
  1019. // Generate normal vectors basing on smoothing groups
  1020. void ASEImporter::GenerateNormals(ASE::Mesh& mesh)
  1021. {
  1022. if (!mesh.mNormals.empty())
  1023. {
  1024. // check whether there are uninitialized normals. If there are
  1025. // some, skip all normals from the file and compute them on our own
  1026. for (std::vector<aiVector3D>::const_iterator
  1027. qq = mesh.mNormals.begin();
  1028. qq != mesh.mNormals.end();++qq)
  1029. {
  1030. if (is_qnan((*qq).x))
  1031. {
  1032. DefaultLogger::get()->warn("Normals were specified in the file, "
  1033. "but not all vertices seem to have normals assigned. The "
  1034. "whole normal set will be recomputed.");
  1035. mesh.mNormals.clear();
  1036. break;
  1037. }
  1038. }
  1039. }
  1040. if (mesh.mNormals.empty())
  1041. ComputeNormalsWithSmoothingsGroups<ASE::Face>(mesh);
  1042. return;
  1043. }