ASELoader.cpp 42 KB

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