ASELoader.cpp 34 KB

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