ASELoader.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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 (szName)
  255. {
  256. if( len != szMyName[1].length() ||
  257. 0 != ASSIMP_stricmp ( szName, szMyName[1].c_str() ))
  258. {
  259. continue;
  260. }
  261. }
  262. else if ('\0' != szMyName[1].c_str()[0])continue;
  263. apcNodes.push_back(new aiNode());
  264. aiNode* node = apcNodes.back();
  265. node->mName.Set(szMyName[0]);
  266. node->mNumMeshes = 1;
  267. node->mMeshes = new unsigned int[1];
  268. node->mMeshes[0] = i;
  269. node->mParent = pcParent;
  270. aiMatrix4x4 mParentAdjust = decompTrafo.mMatrix;
  271. mParentAdjust.Inverse();
  272. node->mTransformation = mParentAdjust*mesh.mTransform;
  273. // Transform all vertices of the mesh back into their local space ->
  274. // at the moment they are pretransformed
  275. aiMatrix4x4 mInverse = mesh.mTransform;
  276. mInverse.Inverse();
  277. aiVector3D* pvCurPtr = pcMesh->mVertices;
  278. const aiVector3D* const pvEndPtr = pcMesh->mVertices + pcMesh->mNumVertices;
  279. while (pvCurPtr != pvEndPtr)
  280. {
  281. *pvCurPtr = mInverse * (*pvCurPtr);
  282. pvCurPtr++;
  283. }
  284. // add sub nodes
  285. aiMatrix4x4 mNewAbs = decompTrafo.mMatrix * node->mTransformation;
  286. ASE::DecompTransform dec( mNewAbs);
  287. this->AddNodes(node,node->mName.data,dec);
  288. }
  289. // allocate enough space for the child nodes
  290. pcParent->mNumChildren = (unsigned int)apcNodes.size();
  291. pcParent->mChildren = new aiNode*[apcNodes.size()];
  292. // now build all nodes
  293. for (unsigned int p = 0; p < apcNodes.size();++p)
  294. {
  295. pcParent->mChildren[p] = apcNodes[p];
  296. }
  297. return;
  298. }
  299. // ------------------------------------------------------------------------------------------------
  300. void ASEImporter::BuildNodes()
  301. {
  302. ai_assert(NULL != pcScene);
  303. // allocate the root node
  304. pcScene->mRootNode = new aiNode();
  305. pcScene->mRootNode->mNumMeshes = 0;
  306. pcScene->mRootNode->mMeshes = 0;
  307. pcScene->mRootNode->mName.Set("<root>");
  308. // add all nodes
  309. this->AddNodes(pcScene->mRootNode,NULL);
  310. // now iterate through al meshes and find those that have not yet
  311. // been added to the nodegraph (= their parent could not be recognized)
  312. std::vector<unsigned int> aiList;
  313. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  314. {
  315. // get the name of the mesh
  316. const ASE::Mesh& mesh = *((const ASE::Mesh*)pcScene->mMeshes[i]->mColors[2]);
  317. // TODO: experimental quick'n'dirty, clean this up ...
  318. std::string szMyName[2] = {mesh.mName,mesh.mParent} ;
  319. // check whether our parent is known
  320. bool bKnowParent = false;
  321. for (unsigned int i2 = 0; i2 < pcScene->mNumMeshes;++i2)
  322. {
  323. if (i2 == i)continue;
  324. const ASE::Mesh& mesh2 = *((const ASE::Mesh*)pcScene->mMeshes[i2]->mColors[2]);
  325. // TODO: experimental quick'n'dirty, clean this up ...
  326. std::string szMyName2[2] = {mesh2.mName,mesh2.mParent} ;
  327. if (!ASSIMP_stricmp ( szMyName[1], szMyName2[0]))
  328. {
  329. bKnowParent = true;
  330. break;
  331. }
  332. // check if there is another mesh with the same unknown parent
  333. // that has already been handled and added to the list
  334. if (i2 < i)
  335. {
  336. if (ASSIMP_stricmp ( szMyName[1], szMyName2[1]))
  337. {
  338. bKnowParent = true;
  339. break;
  340. }
  341. }
  342. }
  343. if (!bKnowParent)
  344. {
  345. aiList.push_back(i);
  346. }
  347. }
  348. if (!aiList.empty())
  349. {
  350. std::vector<aiNode*> apcNodes;
  351. apcNodes.reserve(aiList.size() + pcScene->mRootNode->mNumChildren);
  352. for (unsigned int i = 0; i < pcScene->mRootNode->mNumChildren;++i)
  353. apcNodes.push_back(pcScene->mRootNode->mChildren[i]);
  354. delete[] pcScene->mRootNode->mChildren;
  355. for (std::vector<unsigned int>::/*const_*/iterator
  356. i = aiList.begin();
  357. i != aiList.end();++i)
  358. {
  359. std::string* szMyName = (std::string*)pcScene->mMeshes[*i]->mColors[1];
  360. if (!szMyName)continue;
  361. DefaultLogger::get()->info("Generating dummy node: " + szMyName[1] + ". "
  362. "This node is not defined in the ASE file, but referenced as "
  363. "parent node.");
  364. // the parent is not known, so we can assume that we must add
  365. // this node to the root node of the whole scene
  366. aiNode* pcNode = new aiNode();
  367. pcNode->mParent = pcScene->mRootNode;
  368. pcNode->mName.Set(szMyName[1]);
  369. this->AddNodes(pcNode,pcNode->mName.data);
  370. apcNodes.push_back(pcNode);
  371. }
  372. pcScene->mRootNode->mChildren = new aiNode*[apcNodes.size()];
  373. for (unsigned int i = 0; i < apcNodes.size();++i)
  374. pcScene->mRootNode->mChildren[i] = apcNodes[i];
  375. pcScene->mRootNode->mNumChildren = (unsigned int)apcNodes.size();
  376. }
  377. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  378. pcScene->mMeshes[i]->mColors[2] = NULL;
  379. // if there is only one subnode, set it as root node
  380. if (1 == pcScene->mRootNode->mNumChildren)
  381. {
  382. aiNode* pc = pcScene->mRootNode;
  383. pcScene->mRootNode = pcScene->mRootNode->mChildren[0];
  384. pcScene->mRootNode->mParent = NULL;
  385. // make sure the destructor won't delete us ...
  386. delete[] pc->mChildren;
  387. pc->mChildren = NULL;
  388. pc->mNumChildren = 0;
  389. delete pc;
  390. }
  391. else if (!pcScene->mRootNode->mNumChildren)
  392. {
  393. throw new ImportErrorException("No nodes loaded. The ASE/ASK file is either empty or corrupt");
  394. }
  395. return;
  396. }
  397. // ------------------------------------------------------------------------------------------------
  398. void ASEImporter::TransformVertices(ASE::Mesh& mesh)
  399. {
  400. // the matrix data is stored in column-major format,
  401. // but we need row major
  402. mesh.mTransform.Transpose();
  403. }
  404. // ------------------------------------------------------------------------------------------------
  405. void ASEImporter::BuildUniqueRepresentation(ASE::Mesh& mesh)
  406. {
  407. // allocate output storage
  408. std::vector<aiVector3D> mPositions;
  409. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  410. std::vector<aiColor4D> mVertexColors;
  411. std::vector<aiVector3D> mNormals;
  412. std::vector<BoneVertex> mBoneVertices;
  413. unsigned int iSize = (unsigned int)mesh.mFaces.size() * 3;
  414. mPositions.resize(iSize);
  415. // optional texture coordinates
  416. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  417. {
  418. if (!mesh.amTexCoords[i].empty())
  419. {
  420. amTexCoords[i].resize(iSize);
  421. }
  422. }
  423. // optional vertex colors
  424. if (!mesh.mVertexColors.empty())
  425. {
  426. mVertexColors.resize(iSize);
  427. }
  428. // optional vertex normals (vertex normals can simply be copied)
  429. if (!mesh.mNormals.empty())
  430. {
  431. mNormals.resize(iSize);
  432. }
  433. // bone vertices. There is no need to change the bone list
  434. if (!mesh.mBoneVertices.empty())
  435. {
  436. mBoneVertices.resize(iSize);
  437. }
  438. // iterate through all faces in the mesh
  439. unsigned int iCurrent = 0;
  440. for (std::vector<ASE::Face>::iterator
  441. i = mesh.mFaces.begin();
  442. i != mesh.mFaces.end();++i)
  443. {
  444. for (unsigned int n = 0; n < 3;++n,++iCurrent)
  445. {
  446. mPositions[iCurrent] = mesh.mPositions[(*i).mIndices[n]];
  447. std::swap((float&)mPositions[iCurrent].z,(float&)mPositions[iCurrent].y); // DX-to-OGL
  448. // add texture coordinates
  449. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  450. {
  451. if (!mesh.amTexCoords[c].empty())
  452. {
  453. amTexCoords[c][iCurrent] = mesh.amTexCoords[c][(*i).amUVIndices[c][n]];
  454. amTexCoords[c][iCurrent].y = 1.0f - amTexCoords[c][iCurrent].y; // DX-to-OGL
  455. }
  456. }
  457. // add vertex colors
  458. if (!mesh.mVertexColors.empty())
  459. {
  460. mVertexColors[iCurrent] = mesh.mVertexColors[(*i).mColorIndices[n]];
  461. }
  462. // add normal vectors
  463. if (!mesh.mNormals.empty())
  464. {
  465. mNormals[iCurrent] = mesh.mNormals[(*i).mIndices[n]];
  466. std::swap((float&)mNormals[iCurrent].z,(float&)mNormals[iCurrent].y); // DX-to-OGL
  467. }
  468. // handle bone vertices
  469. if ((*i).mIndices[n] < mesh.mBoneVertices.size())
  470. {
  471. // (sometimes this will cause bone verts to be duplicated
  472. // however, I' quite sure Schrompf' JoinVerticesStep
  473. // will fix that again ...)
  474. mBoneVertices[iCurrent] = mesh.mBoneVertices[(*i).mIndices[n]];
  475. }
  476. }
  477. // we need to flip the order of the indices
  478. (*i).mIndices[0] = iCurrent-1;
  479. (*i).mIndices[1] = iCurrent-2;
  480. (*i).mIndices[2] = iCurrent-3;
  481. }
  482. // replace the old arrays
  483. mesh.mNormals = mNormals;
  484. mesh.mPositions = mPositions;
  485. mesh.mVertexColors = mVertexColors;
  486. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  487. mesh.amTexCoords[c] = amTexCoords[c];
  488. return;
  489. }
  490. // ------------------------------------------------------------------------------------------------
  491. void ASEImporter::ConvertMaterial(ASE::Material& mat)
  492. {
  493. // allocate the output material
  494. mat.pcInstance = new MaterialHelper();
  495. // At first add the base ambient color of the
  496. // scene to the material
  497. mat.mAmbient.r += this->mParser->m_clrAmbient.r;
  498. mat.mAmbient.g += this->mParser->m_clrAmbient.g;
  499. mat.mAmbient.b += this->mParser->m_clrAmbient.b;
  500. aiString name;
  501. name.Set( mat.mName);
  502. mat.pcInstance->AddProperty( &name, AI_MATKEY_NAME);
  503. // material colors
  504. mat.pcInstance->AddProperty( &mat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  505. mat.pcInstance->AddProperty( &mat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  506. mat.pcInstance->AddProperty( &mat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  507. mat.pcInstance->AddProperty( &mat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  508. // shininess
  509. if (0.0f != mat.mSpecularExponent && 0.0f != mat.mShininessStrength)
  510. {
  511. mat.pcInstance->AddProperty( &mat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  512. mat.pcInstance->AddProperty( &mat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  513. }
  514. // if there is no shininess, we can disable phong lighting
  515. else if (Dot3DS::Dot3DSFile::Metal == mat.mShading ||
  516. Dot3DS::Dot3DSFile::Phong == mat.mShading ||
  517. Dot3DS::Dot3DSFile::Blinn == mat.mShading)
  518. {
  519. mat.mShading = Dot3DS::Dot3DSFile::Gouraud;
  520. }
  521. // opacity
  522. mat.pcInstance->AddProperty<float>( &mat.mTransparency,1,AI_MATKEY_OPACITY);
  523. // shading mode
  524. aiShadingMode eShading = aiShadingMode_NoShading;
  525. switch (mat.mShading)
  526. {
  527. case Dot3DS::Dot3DSFile::Flat:
  528. eShading = aiShadingMode_Flat; break;
  529. case Dot3DS::Dot3DSFile::Phong :
  530. eShading = aiShadingMode_Phong; break;
  531. case Dot3DS::Dot3DSFile::Blinn :
  532. eShading = aiShadingMode_Blinn; break;
  533. // I don't know what "Wire" shading should be,
  534. // assume it is simple lambertian diffuse (L dot N) shading
  535. case Dot3DS::Dot3DSFile::Wire:
  536. case Dot3DS::Dot3DSFile::Gouraud:
  537. eShading = aiShadingMode_Gouraud; break;
  538. case Dot3DS::Dot3DSFile::Metal :
  539. eShading = aiShadingMode_CookTorrance; break;
  540. }
  541. mat.pcInstance->AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  542. if (Dot3DS::Dot3DSFile::Wire == mat.mShading)
  543. {
  544. // set the wireframe flag
  545. unsigned int iWire = 1;
  546. mat.pcInstance->AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  547. }
  548. // texture, if there is one
  549. if( mat.sTexDiffuse.mMapName.length() > 0)
  550. {
  551. aiString tex;
  552. tex.Set( mat.sTexDiffuse.mMapName);
  553. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  554. if (is_not_qnan(mat.sTexDiffuse.mTextureBlend))
  555. mat.pcInstance->AddProperty<float>( &mat.sTexDiffuse.mTextureBlend, 1,
  556. AI_MATKEY_TEXBLEND_DIFFUSE(0));
  557. }
  558. if( mat.sTexSpecular.mMapName.length() > 0)
  559. {
  560. aiString tex;
  561. tex.Set( mat.sTexSpecular.mMapName);
  562. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  563. if (is_not_qnan(mat.sTexSpecular.mTextureBlend))
  564. mat.pcInstance->AddProperty<float>( &mat.sTexSpecular.mTextureBlend, 1,
  565. AI_MATKEY_TEXBLEND_SPECULAR(0));
  566. }
  567. if( mat.sTexOpacity.mMapName.length() > 0)
  568. {
  569. aiString tex;
  570. tex.Set( mat.sTexOpacity.mMapName);
  571. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  572. if (is_not_qnan(mat.sTexOpacity.mTextureBlend))
  573. mat.pcInstance->AddProperty<float>( &mat.sTexOpacity.mTextureBlend, 1,
  574. AI_MATKEY_TEXBLEND_OPACITY(0));
  575. }
  576. if( mat.sTexEmissive.mMapName.length() > 0)
  577. {
  578. aiString tex;
  579. tex.Set( mat.sTexEmissive.mMapName);
  580. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  581. if (is_not_qnan(mat.sTexEmissive.mTextureBlend))
  582. mat.pcInstance->AddProperty<float>( &mat.sTexEmissive.mTextureBlend, 1,
  583. AI_MATKEY_TEXBLEND_EMISSIVE(0));
  584. }
  585. if( mat.sTexAmbient.mMapName.length() > 0)
  586. {
  587. aiString tex;
  588. tex.Set( mat.sTexAmbient.mMapName);
  589. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_AMBIENT(0));
  590. if (is_not_qnan(mat.sTexAmbient.mTextureBlend))
  591. mat.pcInstance->AddProperty<float>( &mat.sTexAmbient.mTextureBlend, 1,
  592. AI_MATKEY_TEXBLEND_AMBIENT(0));
  593. }
  594. if( mat.sTexBump.mMapName.length() > 0)
  595. {
  596. aiString tex;
  597. tex.Set( mat.sTexBump.mMapName);
  598. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  599. if (is_not_qnan(mat.sTexBump.mTextureBlend))
  600. mat.pcInstance->AddProperty<float>( &mat.sTexBump.mTextureBlend, 1,
  601. AI_MATKEY_TEXBLEND_HEIGHT(0));
  602. }
  603. if( mat.sTexShininess.mMapName.length() > 0)
  604. {
  605. aiString tex;
  606. tex.Set( mat.sTexShininess.mMapName);
  607. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  608. if (is_not_qnan(mat.sTexShininess.mTextureBlend))
  609. mat.pcInstance->AddProperty<float>( &mat.sTexBump.mTextureBlend, 1,
  610. AI_MATKEY_TEXBLEND_SHININESS(0));
  611. }
  612. // store the name of the material itself, too
  613. if( mat.mName.length() > 0)
  614. {
  615. aiString tex;
  616. tex.Set( mat.mName);
  617. mat.pcInstance->AddProperty( &tex, AI_MATKEY_NAME);
  618. }
  619. return;
  620. }
  621. // ------------------------------------------------------------------------------------------------
  622. void ASEImporter::ConvertMeshes(ASE::Mesh& mesh, std::vector<aiMesh*>& avOutMeshes)
  623. {
  624. // validate the material index of the mesh
  625. if (mesh.iMaterialIndex >= this->mParser->m_vMaterials.size())
  626. {
  627. mesh.iMaterialIndex = (unsigned int)this->mParser->m_vMaterials.size()-1;
  628. DefaultLogger::get()->warn("Material index is out of range");
  629. }
  630. // if the material the mesh is assigned to is consisting of submeshes
  631. // we'll need to split it ... Quak.
  632. if (!this->mParser->m_vMaterials[mesh.iMaterialIndex].avSubMaterials.empty())
  633. {
  634. std::vector<ASE::Material> vSubMaterials = this->mParser->
  635. m_vMaterials[mesh.iMaterialIndex].avSubMaterials;
  636. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  637. vSubMaterials.size()];
  638. // build a list of all faces per submaterial
  639. for (unsigned int i = 0; i < mesh.mFaces.size();++i)
  640. {
  641. // check range
  642. if (mesh.mFaces[i].iMaterial >= vSubMaterials.size())
  643. {
  644. DefaultLogger::get()->warn("Submaterial index is out of range");
  645. // use the last material instead
  646. aiSplit[vSubMaterials.size()-1].push_back(i);
  647. }
  648. else aiSplit[mesh.mFaces[i].iMaterial].push_back(i);
  649. }
  650. // now generate submeshes
  651. for (unsigned int p = 0; p < vSubMaterials.size();++p)
  652. {
  653. if (aiSplit[p].size() != 0)
  654. {
  655. aiMesh* p_pcOut = new aiMesh();
  656. // let the sub material index
  657. p_pcOut->mMaterialIndex = p;
  658. // we will need this material
  659. this->mParser->m_vMaterials[mesh.iMaterialIndex].avSubMaterials[p].bNeed = true;
  660. // store the real index here ... color channel 3
  661. p_pcOut->mColors[3] = (aiColor4D*)(uintptr_t)mesh.iMaterialIndex;
  662. // store a pointer to the mesh in color channel 2
  663. p_pcOut->mColors[2] = (aiColor4D*) &mesh;
  664. avOutMeshes.push_back(p_pcOut);
  665. // convert vertices
  666. p_pcOut->mNumVertices = (unsigned int)aiSplit[p].size()*3;
  667. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  668. // receive output vertex weights
  669. std::vector<std::pair<unsigned int, float> >* avOutputBones;
  670. if (!mesh.mBones.empty())
  671. {
  672. avOutputBones = new std::vector<std::pair<unsigned int, float> >[mesh.mBones.size()];
  673. }
  674. // allocate enough storage for faces
  675. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  676. if (p_pcOut->mNumVertices != 0)
  677. {
  678. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  679. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  680. unsigned int iBase = 0;
  681. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  682. {
  683. unsigned int iIndex = aiSplit[p][q];
  684. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  685. p_pcOut->mFaces[q].mNumIndices = 3;
  686. for (unsigned int t = 0; t < 3;++t)
  687. {
  688. const uint32_t iIndex2 = mesh.mFaces[iIndex].mIndices[t];
  689. p_pcOut->mVertices[iBase] = mesh.mPositions[iIndex2];
  690. p_pcOut->mNormals[iBase] = mesh.mNormals[iIndex2];
  691. // convert bones, if existing
  692. if (!mesh.mBones.empty())
  693. {
  694. // check whether there is a vertex weight that is using
  695. // this vertex index ...
  696. if (iIndex2 < mesh.mBoneVertices.size())
  697. {
  698. for (std::vector<std::pair<int,float> >::const_iterator
  699. blubb = mesh.mBoneVertices[iIndex2].mBoneWeights.begin();
  700. blubb != mesh.mBoneVertices[iIndex2].mBoneWeights.end();++blubb)
  701. {
  702. // NOTE: illegal cases have already been filtered out
  703. avOutputBones[(*blubb).first].push_back(std::pair<unsigned int, float>(
  704. iBase,(*blubb).second));
  705. }
  706. }
  707. }
  708. ++iBase;
  709. }
  710. p_pcOut->mFaces[q].mIndices[0] = iBase-3;
  711. p_pcOut->mFaces[q].mIndices[1] = iBase-2;
  712. p_pcOut->mFaces[q].mIndices[2] = iBase-1;
  713. }
  714. }
  715. // convert texture coordinates
  716. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  717. {
  718. if (!mesh.amTexCoords[c].empty())
  719. {
  720. p_pcOut->mTextureCoords[c] = new aiVector3D[p_pcOut->mNumVertices];
  721. unsigned int iBase = 0;
  722. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  723. {
  724. unsigned int iIndex = aiSplit[p][q];
  725. for (unsigned int t = 0; t < 3;++t)
  726. {
  727. p_pcOut->mTextureCoords[c][iBase++] = mesh.amTexCoords[c][mesh.mFaces[iIndex].mIndices[t]];
  728. }
  729. }
  730. // setup the number of valid vertex components
  731. p_pcOut->mNumUVComponents[c] = mesh.mNumUVComponents[c];
  732. }
  733. }
  734. // convert vertex colors (only one set supported)
  735. if (!mesh.mVertexColors.empty())
  736. {
  737. p_pcOut->mColors[0] = new aiColor4D[p_pcOut->mNumVertices];
  738. unsigned int iBase = 0;
  739. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  740. {
  741. unsigned int iIndex = aiSplit[p][q];
  742. for (unsigned int t = 0; t < 3;++t)
  743. {
  744. p_pcOut->mColors[0][iBase++] = mesh.mVertexColors[mesh.mFaces[iIndex].mIndices[t]];
  745. }
  746. }
  747. }
  748. if (!mesh.mBones.empty())
  749. {
  750. p_pcOut->mNumBones = 0;
  751. for (unsigned int mrspock = 0; mrspock < mesh.mBones.size();++mrspock)
  752. if (!avOutputBones[mrspock].empty())p_pcOut->mNumBones++;
  753. p_pcOut->mBones = new aiBone* [ p_pcOut->mNumBones ];
  754. aiBone** pcBone = p_pcOut->mBones;
  755. for (unsigned int mrspock = 0; mrspock < mesh.mBones.size();++mrspock)
  756. {
  757. if (!avOutputBones[mrspock].empty())
  758. {
  759. // we will need this bone. add it to the output mesh and
  760. // add all per-vertex weights
  761. aiBone* pc = *pcBone = new aiBone();
  762. pc->mName.Set(mesh.mBones[mrspock].mName);
  763. pc->mNumWeights = (unsigned int)avOutputBones[mrspock].size();
  764. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  765. for (unsigned int captainkirk = 0; captainkirk < pc->mNumWeights;++captainkirk)
  766. {
  767. const std::pair<unsigned int,float>& ref = avOutputBones[mrspock][captainkirk];
  768. pc->mWeights[captainkirk].mVertexId = ref.first;
  769. pc->mWeights[captainkirk].mWeight = ref.second;
  770. }
  771. ++pcBone;
  772. }
  773. }
  774. // delete allocated storage
  775. delete[] avOutputBones;
  776. }
  777. }
  778. }
  779. // delete storage
  780. delete[] aiSplit;
  781. }
  782. else
  783. {
  784. // otherwise we can simply copy the data to one output mesh
  785. aiMesh* p_pcOut = new aiMesh();
  786. // set an empty sub material index
  787. p_pcOut->mMaterialIndex = ASE::Face::DEFAULT_MATINDEX;
  788. this->mParser->m_vMaterials[mesh.iMaterialIndex].bNeed = true;
  789. // store the real index here ... in color channel 3
  790. p_pcOut->mColors[3] = (aiColor4D*)(uintptr_t)mesh.iMaterialIndex;
  791. // store a pointer to the mesh in color channel 2
  792. p_pcOut->mColors[2] = (aiColor4D*) &mesh;
  793. avOutMeshes.push_back(p_pcOut);
  794. // if the mesh hasn't faces or vertices, there are two cases
  795. // possible: 1. the model is invalid. 2. This is a dummy
  796. // helper object which we are going to remove later ...
  797. if (mesh.mFaces.empty() || mesh.mPositions.empty())
  798. {
  799. return;
  800. }
  801. // convert vertices
  802. p_pcOut->mNumVertices = (unsigned int)mesh.mPositions.size();
  803. p_pcOut->mNumFaces = (unsigned int)mesh.mFaces.size();
  804. // allocate enough storage for faces
  805. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  806. // copy vertices
  807. p_pcOut->mVertices = new aiVector3D[mesh.mPositions.size()];
  808. memcpy(p_pcOut->mVertices,&mesh.mPositions[0],
  809. mesh.mPositions.size() * sizeof(aiVector3D));
  810. // copy normals
  811. p_pcOut->mNormals = new aiVector3D[mesh.mNormals.size()];
  812. memcpy(p_pcOut->mNormals,&mesh.mNormals[0],
  813. mesh.mNormals.size() * sizeof(aiVector3D));
  814. // copy texture coordinates
  815. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  816. {
  817. if (!mesh.amTexCoords[c].empty())
  818. {
  819. p_pcOut->mTextureCoords[c] = new aiVector3D[mesh.amTexCoords[c].size()];
  820. memcpy(p_pcOut->mTextureCoords[c],&mesh.amTexCoords[c][0],
  821. mesh.amTexCoords[c].size() * sizeof(aiVector3D));
  822. // setup the number of valid vertex components
  823. p_pcOut->mNumUVComponents[c] = mesh.mNumUVComponents[c];
  824. }
  825. }
  826. // copy vertex colors
  827. if (!mesh.mVertexColors.empty())
  828. {
  829. p_pcOut->mColors[0] = new aiColor4D[mesh.mVertexColors.size()];
  830. memcpy(p_pcOut->mColors[0],&mesh.mVertexColors[0],
  831. mesh.mVertexColors.size() * sizeof(aiColor4D));
  832. }
  833. // copy faces
  834. for (unsigned int iFace = 0; iFace < p_pcOut->mNumFaces;++iFace)
  835. {
  836. p_pcOut->mFaces[iFace].mNumIndices = 3;
  837. p_pcOut->mFaces[iFace].mIndices = new unsigned int[3];
  838. // copy indices
  839. p_pcOut->mFaces[iFace].mIndices[0] = mesh.mFaces[iFace].mIndices[0];
  840. p_pcOut->mFaces[iFace].mIndices[1] = mesh.mFaces[iFace].mIndices[1];
  841. p_pcOut->mFaces[iFace].mIndices[2] = mesh.mFaces[iFace].mIndices[2];
  842. }
  843. // copy vertex bones
  844. if (!mesh.mBones.empty() && !mesh.mBoneVertices.empty())
  845. {
  846. std::vector<aiVertexWeight>* avBonesOut = new
  847. std::vector<aiVertexWeight>[mesh.mBones.size()];
  848. // find all vertex weights for this bone
  849. unsigned int quak = 0;
  850. for (std::vector<BoneVertex>::const_iterator
  851. harrypotter = mesh.mBoneVertices.begin();
  852. harrypotter != mesh.mBoneVertices.end();++harrypotter,++quak)
  853. {
  854. for (std::vector<std::pair<int,float> >::const_iterator
  855. ronaldweasley = (*harrypotter).mBoneWeights.begin();
  856. ronaldweasley != (*harrypotter).mBoneWeights.end();++ronaldweasley)
  857. {
  858. aiVertexWeight weight;
  859. weight.mVertexId = quak;
  860. weight.mWeight = (*ronaldweasley).second;
  861. avBonesOut[(*ronaldweasley).first].push_back(weight);
  862. }
  863. }
  864. // now build a final bone list
  865. p_pcOut->mNumBones = 0;
  866. for (unsigned int jfkennedy = 0; jfkennedy < mesh.mBones.size();++jfkennedy)
  867. if (!avBonesOut[jfkennedy].empty())p_pcOut->mNumBones++;
  868. p_pcOut->mBones = new aiBone*[p_pcOut->mNumBones];
  869. aiBone** pcBone = p_pcOut->mBones;
  870. for (unsigned int jfkennedy = 0; jfkennedy < mesh.mBones.size();++jfkennedy)
  871. {
  872. if (!avBonesOut[jfkennedy].empty())
  873. {
  874. aiBone* pc = *pcBone = new aiBone();
  875. pc->mName.Set(mesh.mBones[jfkennedy].mName);
  876. pc->mNumWeights = (unsigned int)avBonesOut[jfkennedy].size();
  877. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  878. ::memcpy(pc->mWeights,&avBonesOut[jfkennedy][0],
  879. sizeof(aiVertexWeight) * pc->mNumWeights);
  880. ++pcBone;
  881. }
  882. }
  883. }
  884. }
  885. return;
  886. }
  887. // ------------------------------------------------------------------------------------------------
  888. void ComputeBounds(ASE::Mesh& mesh,aiVector3D& minVec, aiVector3D& maxVec,
  889. aiMatrix4x4& matrix)
  890. {
  891. minVec = aiVector3D( 1e10f, 1e10f, 1e10f);
  892. maxVec = aiVector3D( -1e10f, -1e10f, -1e10f);
  893. for( std::vector<aiVector3D>::const_iterator
  894. i = mesh.mPositions.begin();
  895. i != mesh.mPositions.end();++i)
  896. {
  897. aiVector3D v = matrix*(*i);
  898. minVec.x = std::min( minVec.x, v.x);
  899. minVec.y = std::min( minVec.y, v.y);
  900. minVec.z = std::min( minVec.z, v.z);
  901. maxVec.x = std::max( maxVec.x, v.x);
  902. maxVec.y = std::max( maxVec.y, v.y);
  903. maxVec.z = std::max( maxVec.z, v.z);
  904. }
  905. return;
  906. }
  907. // ------------------------------------------------------------------------------------------------
  908. void ASEImporter::BuildMaterialIndices()
  909. {
  910. ai_assert(NULL != pcScene);
  911. // iterate through all materials and check whether we need them
  912. for (unsigned int iMat = 0; iMat < this->mParser->m_vMaterials.size();++iMat)
  913. {
  914. if (this->mParser->m_vMaterials[iMat].bNeed)
  915. {
  916. // convert it to the aiMaterial layout
  917. ASE::Material& mat = this->mParser->m_vMaterials[iMat];
  918. this->ConvertMaterial(mat);
  919. TextureTransform::ApplyScaleNOffset(mat);
  920. ++pcScene->mNumMaterials;
  921. }
  922. for (unsigned int iSubMat = 0; iSubMat < this->mParser->m_vMaterials[
  923. iMat].avSubMaterials.size();++iSubMat)
  924. {
  925. if (this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].bNeed)
  926. {
  927. // convert it to the aiMaterial layout
  928. ASE::Material& mat = this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat];
  929. this->ConvertMaterial(mat);
  930. TextureTransform::ApplyScaleNOffset(mat);
  931. ++pcScene->mNumMaterials;
  932. }
  933. }
  934. }
  935. // allocate the output material array
  936. pcScene->mMaterials = new aiMaterial*[pcScene->mNumMaterials];
  937. Dot3DS::Material** pcIntMaterials = new Dot3DS::Material*[pcScene->mNumMaterials];
  938. unsigned int iNum = 0;
  939. for (unsigned int iMat = 0; iMat < this->mParser->m_vMaterials.size();++iMat)
  940. {
  941. if (this->mParser->m_vMaterials[iMat].bNeed)
  942. {
  943. ai_assert(NULL != this->mParser->m_vMaterials[iMat].pcInstance);
  944. pcScene->mMaterials[iNum] = this->mParser->m_vMaterials[iMat].pcInstance;
  945. // store the internal material, too
  946. pcIntMaterials[iNum] = &this->mParser->m_vMaterials[iMat];
  947. // iterate through all meshes and search for one which is using
  948. // this top-level material index
  949. for (unsigned int iMesh = 0; iMesh < pcScene->mNumMeshes;++iMesh)
  950. {
  951. if (ASE::Face::DEFAULT_MATINDEX == pcScene->mMeshes[iMesh]->mMaterialIndex &&
  952. iMat == (uintptr_t)pcScene->mMeshes[iMesh]->mColors[3])
  953. {
  954. pcScene->mMeshes[iMesh]->mMaterialIndex = iNum;
  955. pcScene->mMeshes[iMesh]->mColors[3] = NULL;
  956. }
  957. }
  958. iNum++;
  959. }
  960. for (unsigned int iSubMat = 0; iSubMat < this->mParser->m_vMaterials[iMat].avSubMaterials.size();++iSubMat)
  961. {
  962. if (this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].bNeed)
  963. {
  964. ai_assert(NULL != this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].pcInstance);
  965. pcScene->mMaterials[iNum] = this->mParser->m_vMaterials[iMat].
  966. avSubMaterials[iSubMat].pcInstance;
  967. // store the internal material, too
  968. pcIntMaterials[iNum] = &this->mParser->m_vMaterials[iMat].avSubMaterials[iSubMat];
  969. // iterate through all meshes and search for one which is using
  970. // this sub-level material index
  971. for (unsigned int iMesh = 0; iMesh < pcScene->mNumMeshes;++iMesh)
  972. {
  973. if (iSubMat == pcScene->mMeshes[iMesh]->mMaterialIndex &&
  974. iMat == (uintptr_t)pcScene->mMeshes[iMesh]->mColors[3])
  975. {
  976. pcScene->mMeshes[iMesh]->mMaterialIndex = iNum;
  977. pcScene->mMeshes[iMesh]->mColors[3] = NULL;
  978. }
  979. }
  980. iNum++;
  981. }
  982. }
  983. }
  984. // prepare for the next step
  985. for (unsigned int hans = 0; hans < this->mParser->m_vMaterials.size();++hans)
  986. TextureTransform::ApplyScaleNOffset(this->mParser->m_vMaterials[hans]);
  987. // now we need to iterate through all meshes,
  988. // generating correct texture coordinates and material uv indices
  989. for (unsigned int curie = 0; curie < pcScene->mNumMeshes;++curie)
  990. {
  991. aiMesh* pcMesh = pcScene->mMeshes[curie];
  992. // apply texture coordinate transformations
  993. TextureTransform::BakeScaleNOffset(pcMesh,pcIntMaterials[pcMesh->mMaterialIndex]);
  994. }
  995. for (unsigned int hans = 0; hans < pcScene->mNumMaterials;++hans)
  996. {
  997. // setup the correct UV indices for each material
  998. TextureTransform::SetupMatUVSrc(pcScene->mMaterials[hans],
  999. pcIntMaterials[hans]);
  1000. }
  1001. delete[] pcIntMaterials;
  1002. // finished!
  1003. return;
  1004. }
  1005. // ------------------------------------------------------------------------------------------------
  1006. // Generate normal vectors basing on smoothing groups
  1007. void ASEImporter::GenerateNormals(ASE::Mesh& mesh)
  1008. {
  1009. if (!mesh.mNormals.empty())
  1010. {
  1011. // check whether there are uninitialized normals. If there are
  1012. // some, skip all normals from the file and compute them on our own
  1013. for (std::vector<aiVector3D>::const_iterator
  1014. qq = mesh.mNormals.begin();
  1015. qq != mesh.mNormals.end();++qq)
  1016. {
  1017. if (is_qnan((*qq).x))
  1018. {
  1019. DefaultLogger::get()->warn("Normals were specified in the file, "
  1020. "but not all vertices seem to have normals assigned. The "
  1021. "whole normal set will be recomputed.");
  1022. mesh.mNormals.clear();
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. if (mesh.mNormals.empty())
  1028. ComputeNormalsWithSmoothingsGroups<ASE::Face>(mesh);
  1029. return;
  1030. }