ASELoader.cpp 37 KB

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