ASELoader.cpp 43 KB

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