ASELoader.cpp 42 KB

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