ASELoader.cpp 42 KB

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