ASELoader.cpp 38 KB

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