ASELoader.cpp 41 KB

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