ASELoader.cpp 46 KB

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