ASELoader.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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. if (pcParent->mNumChildren)
  479. {
  480. pcParent->mChildren = new aiNode*[apcNodes.size()];
  481. // now build all nodes for our nice new children
  482. for (unsigned int p = 0; p < apcNodes.size();++p)
  483. {
  484. pcParent->mChildren[p] = apcNodes[p];
  485. }
  486. }
  487. return;
  488. }
  489. // ------------------------------------------------------------------------------------------------
  490. void ASEImporter::BuildNodes()
  491. {
  492. ai_assert(NULL != pcScene);
  493. // allocate the one and only root node
  494. pcScene->mRootNode = new aiNode();
  495. pcScene->mRootNode->mNumMeshes = 0;
  496. pcScene->mRootNode->mMeshes = 0;
  497. pcScene->mRootNode->mName.Set("<root>");
  498. // Setup the coordinate system transformation
  499. //pcScene->mRootNode->mTransformation.c3 *= -1.f;
  500. pcScene->mRootNode->mNumChildren = 1;
  501. pcScene->mRootNode->mChildren = new aiNode*[1];
  502. pcScene->mRootNode->mChildren[0] = new aiNode();
  503. // Change the transformation matrix of all nodes
  504. for (std::vector<BaseNode*>::iterator it = nodes.begin(), end = nodes.end();
  505. it != end; ++it)
  506. {
  507. aiMatrix4x4& m = (*it)->mTransform;
  508. m.Transpose(); // row-order vs column-order
  509. }
  510. // add all nodes
  511. AddNodes(nodes,pcScene->mRootNode->mChildren[0],NULL);
  512. // now iterate through al nodes and find those that have not yet
  513. // been added to the nodegraph (= their parent could not be recognized)
  514. std::vector<const BaseNode*> aiList;
  515. for (std::vector<BaseNode*>::iterator it = nodes.begin(), end = nodes.end();
  516. it != end; ++it)
  517. {
  518. if ((*it)->mProcessed)continue;
  519. // check whether our parent is known
  520. bool bKnowParent = false;
  521. // research the list, beginning from now and try to find out whether
  522. // there is a node that references *us* as a parent
  523. for (std::vector<BaseNode*>::const_iterator it2 = nodes.begin();
  524. it2 != end; ++it2)
  525. {
  526. if (it2 == it)continue;
  527. if ((*it2)->mName == (*it)->mParent)
  528. {
  529. bKnowParent = true;
  530. break;
  531. }
  532. }
  533. if (!bKnowParent)
  534. {
  535. aiList.push_back(*it);
  536. }
  537. }
  538. // Are there ane orphaned nodes?
  539. if (!aiList.empty())
  540. {
  541. std::vector<aiNode*> apcNodes;
  542. apcNodes.reserve(aiList.size() + pcScene->mRootNode->mNumChildren);
  543. for (unsigned int i = 0; i < pcScene->mRootNode->mNumChildren;++i)
  544. apcNodes.push_back(pcScene->mRootNode->mChildren[i]);
  545. delete[] pcScene->mRootNode->mChildren;
  546. for (std::vector<const BaseNode*>::/*const_*/iterator
  547. i = aiList.begin();
  548. i != aiList.end();++i)
  549. {
  550. const ASE::BaseNode* src = *i;
  551. // the parent is not known, so we can assume that we must add
  552. // this node to the root node of the whole scene
  553. aiNode* pcNode = new aiNode();
  554. pcNode->mParent = pcScene->mRootNode;
  555. pcNode->mName.Set(src->mName);
  556. AddMeshes(src,pcNode);
  557. AddNodes(nodes,pcNode,pcNode->mName.data);
  558. apcNodes.push_back(pcNode);
  559. }
  560. // Regenerate our output array
  561. pcScene->mRootNode->mChildren = new aiNode*[apcNodes.size()];
  562. for (unsigned int i = 0; i < apcNodes.size();++i)
  563. pcScene->mRootNode->mChildren[i] = apcNodes[i];
  564. pcScene->mRootNode->mNumChildren = (unsigned int)apcNodes.size();
  565. }
  566. // Reset the third color set to NULL - we used this field to
  567. // store a temporary pointer
  568. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  569. pcScene->mMeshes[i]->mColors[2] = NULL;
  570. // if there is only one subnode, set it as root node
  571. // FIX: The sub node may not have animations assigned
  572. if (1 == pcScene->mRootNode->mNumChildren && !pcScene->mNumAnimations)
  573. {
  574. aiNode* cc = pcScene->mRootNode->mChildren[0];
  575. aiNode* pc = pcScene->mRootNode;
  576. pcScene->mRootNode = cc;
  577. pcScene->mRootNode->mParent = NULL;
  578. cc->mTransformation = pc->mTransformation * cc->mTransformation;
  579. // make sure the destructor won't delete us ...
  580. delete[] pc->mChildren;
  581. pc->mChildren = NULL;
  582. pc->mNumChildren = 0;
  583. delete pc;
  584. }
  585. // The root node should not have at least one child or the file is invalid
  586. else if (!pcScene->mRootNode->mNumChildren)
  587. {
  588. throw new ImportErrorException("No nodes loaded. The ASE/ASK file is either empty or corrupt");
  589. }
  590. return;
  591. }
  592. // ------------------------------------------------------------------------------------------------
  593. void ASEImporter::BuildUniqueRepresentation(ASE::Mesh& mesh)
  594. {
  595. // allocate output storage
  596. std::vector<aiVector3D> mPositions;
  597. std::vector<aiVector3D> amTexCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS];
  598. std::vector<aiColor4D> mVertexColors;
  599. std::vector<aiVector3D> mNormals;
  600. std::vector<BoneVertex> mBoneVertices;
  601. unsigned int iSize = (unsigned int)mesh.mFaces.size() * 3;
  602. mPositions.resize(iSize);
  603. // optional texture coordinates
  604. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS;++i)
  605. {
  606. if (!mesh.amTexCoords[i].empty())
  607. {
  608. amTexCoords[i].resize(iSize);
  609. }
  610. }
  611. // optional vertex colors
  612. if (!mesh.mVertexColors.empty())
  613. {
  614. mVertexColors.resize(iSize);
  615. }
  616. // optional vertex normals (vertex normals can simply be copied)
  617. if (!mesh.mNormals.empty())
  618. {
  619. mNormals.resize(iSize);
  620. }
  621. // bone vertices. There is no need to change the bone list
  622. if (!mesh.mBoneVertices.empty())
  623. {
  624. mBoneVertices.resize(iSize);
  625. }
  626. // iterate through all faces in the mesh
  627. unsigned int iCurrent = 0, fi = 0;
  628. for (std::vector<ASE::Face>::iterator
  629. i = mesh.mFaces.begin();
  630. i != mesh.mFaces.end();++i,++fi)
  631. {
  632. for (unsigned int n = 0; n < 3;++n,++iCurrent)
  633. {
  634. mPositions[iCurrent] = mesh.mPositions[(*i).mIndices[n]];
  635. //std::swap((float&)mPositions[iCurrent].z,(float&)mPositions[iCurrent].y); // DX-to-OGL
  636. //mPositions[iCurrent].y *= -1.f;
  637. // add texture coordinates
  638. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  639. {
  640. if (!mesh.amTexCoords[c].empty())
  641. {
  642. amTexCoords[c][iCurrent] = mesh.amTexCoords[c][(*i).amUVIndices[c][n]];
  643. // amTexCoords[c][iCurrent].y = 1.f- amTexCoords[c][iCurrent].y; // DX-to-OGL
  644. }
  645. }
  646. // add vertex colors
  647. if (!mesh.mVertexColors.empty())
  648. {
  649. mVertexColors[iCurrent] = mesh.mVertexColors[(*i).mColorIndices[n]];
  650. }
  651. // add normal vectors
  652. if (!mesh.mNormals.empty())
  653. {
  654. mNormals[iCurrent] = mesh.mNormals[fi*3+n];
  655. mNormals[iCurrent].Normalize();
  656. //std::swap((float&)mNormals[iCurrent].z,(float&)mNormals[iCurrent].y); // DX-to-OGL
  657. //mNormals[iCurrent].y *= -1.0f;
  658. }
  659. // handle bone vertices
  660. if ((*i).mIndices[n] < mesh.mBoneVertices.size())
  661. {
  662. // (sometimes this will cause bone verts to be duplicated
  663. // however, I' quite sure Schrompf' JoinVerticesStep
  664. // will fix that again ...)
  665. mBoneVertices[iCurrent] = mesh.mBoneVertices[(*i).mIndices[n]];
  666. }
  667. }
  668. // we need to flip the order of the indices
  669. (*i).mIndices[0] = iCurrent-1;
  670. (*i).mIndices[1] = iCurrent-2;
  671. (*i).mIndices[2] = iCurrent-3;
  672. }
  673. // replace the old arrays
  674. mesh.mNormals = mNormals;
  675. mesh.mPositions = mPositions;
  676. mesh.mVertexColors = mVertexColors;
  677. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  678. mesh.amTexCoords[c] = amTexCoords[c];
  679. return;
  680. }
  681. // ------------------------------------------------------------------------------------------------
  682. void ASEImporter::ConvertMaterial(ASE::Material& mat)
  683. {
  684. // allocate the output material
  685. mat.pcInstance = new MaterialHelper();
  686. // At first add the base ambient color of the
  687. // scene to the material
  688. mat.mAmbient.r += this->mParser->m_clrAmbient.r;
  689. mat.mAmbient.g += this->mParser->m_clrAmbient.g;
  690. mat.mAmbient.b += this->mParser->m_clrAmbient.b;
  691. aiString name;
  692. name.Set( mat.mName);
  693. mat.pcInstance->AddProperty( &name, AI_MATKEY_NAME);
  694. // material colors
  695. mat.pcInstance->AddProperty( &mat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  696. mat.pcInstance->AddProperty( &mat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  697. mat.pcInstance->AddProperty( &mat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  698. mat.pcInstance->AddProperty( &mat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  699. // shininess
  700. if (0.0f != mat.mSpecularExponent && 0.0f != mat.mShininessStrength)
  701. {
  702. mat.pcInstance->AddProperty( &mat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  703. mat.pcInstance->AddProperty( &mat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  704. }
  705. // if there is no shininess, we can disable phong lighting
  706. else if (D3DS::Discreet3DS::Metal == mat.mShading ||
  707. D3DS::Discreet3DS::Phong == mat.mShading ||
  708. D3DS::Discreet3DS::Blinn == mat.mShading)
  709. {
  710. mat.mShading = D3DS::Discreet3DS::Gouraud;
  711. }
  712. // opacity
  713. mat.pcInstance->AddProperty<float>( &mat.mTransparency,1,AI_MATKEY_OPACITY);
  714. // shading mode
  715. aiShadingMode eShading = aiShadingMode_NoShading;
  716. switch (mat.mShading)
  717. {
  718. case D3DS::Discreet3DS::Flat:
  719. eShading = aiShadingMode_Flat; break;
  720. case D3DS::Discreet3DS::Phong :
  721. eShading = aiShadingMode_Phong; break;
  722. case D3DS::Discreet3DS::Blinn :
  723. eShading = aiShadingMode_Blinn; break;
  724. // I don't know what "Wire" shading should be,
  725. // assume it is simple lambertian diffuse (L dot N) shading
  726. case D3DS::Discreet3DS::Wire:
  727. case D3DS::Discreet3DS::Gouraud:
  728. eShading = aiShadingMode_Gouraud; break;
  729. case D3DS::Discreet3DS::Metal :
  730. eShading = aiShadingMode_CookTorrance; break;
  731. }
  732. mat.pcInstance->AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  733. if (D3DS::Discreet3DS::Wire == mat.mShading)
  734. {
  735. // set the wireframe flag
  736. unsigned int iWire = 1;
  737. mat.pcInstance->AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  738. }
  739. // texture, if there is one
  740. if( mat.sTexDiffuse.mMapName.length() > 0)
  741. {
  742. aiString tex;
  743. tex.Set( mat.sTexDiffuse.mMapName);
  744. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  745. if (is_not_qnan(mat.sTexDiffuse.mTextureBlend))
  746. mat.pcInstance->AddProperty<float>( &mat.sTexDiffuse.mTextureBlend, 1,
  747. AI_MATKEY_TEXBLEND_DIFFUSE(0));
  748. }
  749. if( mat.sTexSpecular.mMapName.length() > 0)
  750. {
  751. aiString tex;
  752. tex.Set( mat.sTexSpecular.mMapName);
  753. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  754. if (is_not_qnan(mat.sTexSpecular.mTextureBlend))
  755. mat.pcInstance->AddProperty<float>( &mat.sTexSpecular.mTextureBlend, 1,
  756. AI_MATKEY_TEXBLEND_SPECULAR(0));
  757. }
  758. if( mat.sTexOpacity.mMapName.length() > 0)
  759. {
  760. aiString tex;
  761. tex.Set( mat.sTexOpacity.mMapName);
  762. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  763. if (is_not_qnan(mat.sTexOpacity.mTextureBlend))
  764. mat.pcInstance->AddProperty<float>( &mat.sTexOpacity.mTextureBlend, 1,
  765. AI_MATKEY_TEXBLEND_OPACITY(0));
  766. }
  767. if( mat.sTexEmissive.mMapName.length() > 0)
  768. {
  769. aiString tex;
  770. tex.Set( mat.sTexEmissive.mMapName);
  771. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  772. if (is_not_qnan(mat.sTexEmissive.mTextureBlend))
  773. mat.pcInstance->AddProperty<float>( &mat.sTexEmissive.mTextureBlend, 1,
  774. AI_MATKEY_TEXBLEND_EMISSIVE(0));
  775. }
  776. if( mat.sTexAmbient.mMapName.length() > 0)
  777. {
  778. aiString tex;
  779. tex.Set( mat.sTexAmbient.mMapName);
  780. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_AMBIENT(0));
  781. if (is_not_qnan(mat.sTexAmbient.mTextureBlend))
  782. mat.pcInstance->AddProperty<float>( &mat.sTexAmbient.mTextureBlend, 1,
  783. AI_MATKEY_TEXBLEND_AMBIENT(0));
  784. }
  785. if( mat.sTexBump.mMapName.length() > 0)
  786. {
  787. aiString tex;
  788. tex.Set( mat.sTexBump.mMapName);
  789. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  790. if (is_not_qnan(mat.sTexBump.mTextureBlend))
  791. mat.pcInstance->AddProperty<float>( &mat.sTexBump.mTextureBlend, 1,
  792. AI_MATKEY_TEXBLEND_HEIGHT(0));
  793. }
  794. if( mat.sTexShininess.mMapName.length() > 0)
  795. {
  796. aiString tex;
  797. tex.Set( mat.sTexShininess.mMapName);
  798. mat.pcInstance->AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  799. if (is_not_qnan(mat.sTexShininess.mTextureBlend))
  800. mat.pcInstance->AddProperty<float>( &mat.sTexBump.mTextureBlend, 1,
  801. AI_MATKEY_TEXBLEND_SHININESS(0));
  802. }
  803. // store the name of the material itself, too
  804. if( mat.mName.length() > 0)
  805. {
  806. aiString tex;
  807. tex.Set( mat.mName);
  808. mat.pcInstance->AddProperty( &tex, AI_MATKEY_NAME);
  809. }
  810. return;
  811. }
  812. // ------------------------------------------------------------------------------------------------
  813. void ASEImporter::ConvertMeshes(ASE::Mesh& mesh, std::vector<aiMesh*>& avOutMeshes)
  814. {
  815. // validate the material index of the mesh
  816. if (mesh.iMaterialIndex >= mParser->m_vMaterials.size())
  817. {
  818. mesh.iMaterialIndex = (unsigned int)mParser->m_vMaterials.size()-1;
  819. DefaultLogger::get()->warn("Material index is out of range");
  820. }
  821. // if the material the mesh is assigned to is consisting of submeshes
  822. // we'll need to split it ... Quak.
  823. if (!mParser->m_vMaterials[mesh.iMaterialIndex].avSubMaterials.empty())
  824. {
  825. std::vector<ASE::Material> vSubMaterials = mParser->
  826. m_vMaterials[mesh.iMaterialIndex].avSubMaterials;
  827. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  828. vSubMaterials.size()];
  829. // build a list of all faces per submaterial
  830. for (unsigned int i = 0; i < mesh.mFaces.size();++i)
  831. {
  832. // check range
  833. if (mesh.mFaces[i].iMaterial >= vSubMaterials.size())
  834. {
  835. DefaultLogger::get()->warn("Submaterial index is out of range");
  836. // use the last material instead
  837. aiSplit[vSubMaterials.size()-1].push_back(i);
  838. }
  839. else aiSplit[mesh.mFaces[i].iMaterial].push_back(i);
  840. }
  841. // now generate submeshes
  842. for (unsigned int p = 0; p < vSubMaterials.size();++p)
  843. {
  844. if (!aiSplit[p].empty())
  845. {
  846. aiMesh* p_pcOut = new aiMesh();
  847. p_pcOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  848. // let the sub material index
  849. p_pcOut->mMaterialIndex = p;
  850. // we will need this material
  851. mParser->m_vMaterials[mesh.iMaterialIndex].avSubMaterials[p].bNeed = true;
  852. // store the real index here ... color channel 3
  853. p_pcOut->mColors[3] = (aiColor4D*)(uintptr_t)mesh.iMaterialIndex;
  854. // store a pointer to the mesh in color channel 2
  855. p_pcOut->mColors[2] = (aiColor4D*) &mesh;
  856. avOutMeshes.push_back(p_pcOut);
  857. // convert vertices
  858. p_pcOut->mNumVertices = (unsigned int)aiSplit[p].size()*3;
  859. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  860. // receive output vertex weights
  861. std::vector<std::pair<unsigned int, float> >* avOutputBones;
  862. if (!mesh.mBones.empty())
  863. {
  864. avOutputBones = new std::vector<std::pair<unsigned int, float> >[mesh.mBones.size()];
  865. }
  866. // allocate enough storage for faces
  867. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  868. unsigned int iBase = 0,iIndex;
  869. if (p_pcOut->mNumVertices)
  870. {
  871. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  872. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  873. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  874. {
  875. iIndex = aiSplit[p][q];
  876. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  877. p_pcOut->mFaces[q].mNumIndices = 3;
  878. for (unsigned int t = 0; t < 3;++t)
  879. {
  880. const uint32_t iIndex2 = mesh.mFaces[iIndex].mIndices[t];
  881. p_pcOut->mVertices[iBase] = mesh.mPositions [iIndex2];
  882. p_pcOut->mNormals [iBase] = mesh.mNormals [iIndex2];
  883. // convert bones, if existing
  884. if (!mesh.mBones.empty())
  885. {
  886. // check whether there is a vertex weight that is using
  887. // this vertex index ...
  888. if (iIndex2 < mesh.mBoneVertices.size())
  889. {
  890. for (std::vector<std::pair<int,float> >::const_iterator
  891. blubb = mesh.mBoneVertices[iIndex2].mBoneWeights.begin();
  892. blubb != mesh.mBoneVertices[iIndex2].mBoneWeights.end();++blubb)
  893. {
  894. // NOTE: illegal cases have already been filtered out
  895. avOutputBones[(*blubb).first].push_back(std::pair<unsigned int, float>(
  896. iBase,(*blubb).second));
  897. }
  898. }
  899. }
  900. ++iBase;
  901. }
  902. // Flip the face order
  903. p_pcOut->mFaces[q].mIndices[0] = iBase-3;
  904. p_pcOut->mFaces[q].mIndices[1] = iBase-2;
  905. p_pcOut->mFaces[q].mIndices[2] = iBase-1;
  906. }
  907. }
  908. // convert texture coordinates (up to AI_MAX_NUMBER_OF_TEXTURECOORDS sets supported)
  909. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  910. {
  911. if (!mesh.amTexCoords[c].empty())
  912. {
  913. p_pcOut->mTextureCoords[c] = new aiVector3D[p_pcOut->mNumVertices];
  914. iBase = 0;
  915. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  916. {
  917. iIndex = aiSplit[p][q];
  918. for (unsigned int t = 0; t < 3;++t)
  919. {
  920. p_pcOut->mTextureCoords[c][iBase++] = mesh.amTexCoords[c][mesh.mFaces[iIndex].mIndices[t]];
  921. }
  922. }
  923. // setup the number of valid vertex components
  924. p_pcOut->mNumUVComponents[c] = mesh.mNumUVComponents[c];
  925. }
  926. }
  927. // convert vertex colors (only one set supported)
  928. if (!mesh.mVertexColors.empty())
  929. {
  930. p_pcOut->mColors[0] = new aiColor4D[p_pcOut->mNumVertices];
  931. iBase = 0;
  932. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  933. {
  934. iIndex = aiSplit[p][q];
  935. for (unsigned int t = 0; t < 3;++t)
  936. {
  937. p_pcOut->mColors[0][iBase++] = mesh.mVertexColors[mesh.mFaces[iIndex].mIndices[t]];
  938. }
  939. }
  940. }
  941. if (!mesh.mBones.empty())
  942. {
  943. p_pcOut->mNumBones = 0;
  944. for (unsigned int mrspock = 0; mrspock < mesh.mBones.size();++mrspock)
  945. if (!avOutputBones[mrspock].empty())p_pcOut->mNumBones++;
  946. p_pcOut->mBones = new aiBone* [ p_pcOut->mNumBones ];
  947. aiBone** pcBone = p_pcOut->mBones;
  948. for (unsigned int mrspock = 0; mrspock < mesh.mBones.size();++mrspock)
  949. {
  950. if (!avOutputBones[mrspock].empty())
  951. {
  952. // we will need this bone. add it to the output mesh and
  953. // add all per-vertex weights
  954. aiBone* pc = *pcBone = new aiBone();
  955. pc->mName.Set(mesh.mBones[mrspock].mName);
  956. pc->mNumWeights = (unsigned int)avOutputBones[mrspock].size();
  957. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  958. for (unsigned int captainkirk = 0; captainkirk < pc->mNumWeights;++captainkirk)
  959. {
  960. const std::pair<unsigned int,float>& ref = avOutputBones[mrspock][captainkirk];
  961. pc->mWeights[captainkirk].mVertexId = ref.first;
  962. pc->mWeights[captainkirk].mWeight = ref.second;
  963. }
  964. ++pcBone;
  965. }
  966. }
  967. // delete allocated storage
  968. delete[] avOutputBones;
  969. }
  970. }
  971. }
  972. // delete storage
  973. delete[] aiSplit;
  974. }
  975. else
  976. {
  977. // Otherwise we can simply copy the data to one output mesh
  978. // This codepath needs less memory and uses fast memcpy()s
  979. // to do the actual copying. So I think it is worth the
  980. // effort here.
  981. aiMesh* p_pcOut = new aiMesh();
  982. p_pcOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  983. // set an empty sub material index
  984. p_pcOut->mMaterialIndex = ASE::Face::DEFAULT_MATINDEX;
  985. mParser->m_vMaterials[mesh.iMaterialIndex].bNeed = true;
  986. // store the real index here ... in color channel 3
  987. p_pcOut->mColors[3] = (aiColor4D*)(uintptr_t)mesh.iMaterialIndex;
  988. // store a pointer to the mesh in color channel 2
  989. p_pcOut->mColors[2] = (aiColor4D*) &mesh;
  990. avOutMeshes.push_back(p_pcOut);
  991. // if the mesh hasn't faces or vertices, there are two cases
  992. // possible: 1. the model is invalid. 2. This is a dummy
  993. // helper object which we are going to remove later ...
  994. if (mesh.mFaces.empty() || mesh.mPositions.empty())
  995. {
  996. return;
  997. }
  998. // convert vertices
  999. p_pcOut->mNumVertices = (unsigned int)mesh.mPositions.size();
  1000. p_pcOut->mNumFaces = (unsigned int)mesh.mFaces.size();
  1001. // allocate enough storage for faces
  1002. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  1003. // copy vertices
  1004. p_pcOut->mVertices = new aiVector3D[mesh.mPositions.size()];
  1005. memcpy(p_pcOut->mVertices,&mesh.mPositions[0],
  1006. mesh.mPositions.size() * sizeof(aiVector3D));
  1007. // copy normals
  1008. p_pcOut->mNormals = new aiVector3D[mesh.mNormals.size()];
  1009. memcpy(p_pcOut->mNormals,&mesh.mNormals[0],
  1010. mesh.mNormals.size() * sizeof(aiVector3D));
  1011. // copy texture coordinates
  1012. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c)
  1013. {
  1014. if (!mesh.amTexCoords[c].empty())
  1015. {
  1016. p_pcOut->mTextureCoords[c] = new aiVector3D[mesh.amTexCoords[c].size()];
  1017. memcpy(p_pcOut->mTextureCoords[c],&mesh.amTexCoords[c][0],
  1018. mesh.amTexCoords[c].size() * sizeof(aiVector3D));
  1019. // setup the number of valid vertex components
  1020. p_pcOut->mNumUVComponents[c] = mesh.mNumUVComponents[c];
  1021. }
  1022. }
  1023. // copy vertex colors
  1024. if (!mesh.mVertexColors.empty())
  1025. {
  1026. p_pcOut->mColors[0] = new aiColor4D[mesh.mVertexColors.size()];
  1027. memcpy(p_pcOut->mColors[0],&mesh.mVertexColors[0],
  1028. mesh.mVertexColors.size() * sizeof(aiColor4D));
  1029. }
  1030. // copy faces
  1031. for (unsigned int iFace = 0; iFace < p_pcOut->mNumFaces;++iFace)
  1032. {
  1033. p_pcOut->mFaces[iFace].mNumIndices = 3;
  1034. p_pcOut->mFaces[iFace].mIndices = new unsigned int[3];
  1035. // copy indices (flip the face order, too)
  1036. p_pcOut->mFaces[iFace].mIndices[0] = mesh.mFaces[iFace].mIndices[2];
  1037. p_pcOut->mFaces[iFace].mIndices[1] = mesh.mFaces[iFace].mIndices[1];
  1038. p_pcOut->mFaces[iFace].mIndices[2] = mesh.mFaces[iFace].mIndices[0];
  1039. }
  1040. // copy vertex bones
  1041. if (!mesh.mBones.empty() && !mesh.mBoneVertices.empty())
  1042. {
  1043. std::vector<aiVertexWeight>* avBonesOut = new
  1044. std::vector<aiVertexWeight>[mesh.mBones.size()];
  1045. // find all vertex weights for this bone
  1046. unsigned int quak = 0;
  1047. for (std::vector<BoneVertex>::const_iterator
  1048. harrypotter = mesh.mBoneVertices.begin();
  1049. harrypotter != mesh.mBoneVertices.end();++harrypotter,++quak)
  1050. {
  1051. for (std::vector<std::pair<int,float> >::const_iterator
  1052. ronaldweasley = (*harrypotter).mBoneWeights.begin();
  1053. ronaldweasley != (*harrypotter).mBoneWeights.end();++ronaldweasley)
  1054. {
  1055. aiVertexWeight weight;
  1056. weight.mVertexId = quak;
  1057. weight.mWeight = (*ronaldweasley).second;
  1058. avBonesOut[(*ronaldweasley).first].push_back(weight);
  1059. }
  1060. }
  1061. // now build a final bone list
  1062. p_pcOut->mNumBones = 0;
  1063. for (unsigned int jfkennedy = 0; jfkennedy < mesh.mBones.size();++jfkennedy)
  1064. if (!avBonesOut[jfkennedy].empty())p_pcOut->mNumBones++;
  1065. p_pcOut->mBones = new aiBone*[p_pcOut->mNumBones];
  1066. aiBone** pcBone = p_pcOut->mBones;
  1067. for (unsigned int jfkennedy = 0; jfkennedy < mesh.mBones.size();++jfkennedy)
  1068. {
  1069. if (!avBonesOut[jfkennedy].empty())
  1070. {
  1071. aiBone* pc = *pcBone = new aiBone();
  1072. pc->mName.Set(mesh.mBones[jfkennedy].mName);
  1073. pc->mNumWeights = (unsigned int)avBonesOut[jfkennedy].size();
  1074. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  1075. ::memcpy(pc->mWeights,&avBonesOut[jfkennedy][0],
  1076. sizeof(aiVertexWeight) * pc->mNumWeights);
  1077. ++pcBone;
  1078. }
  1079. }
  1080. // delete allocated storage
  1081. delete[] avBonesOut;
  1082. }
  1083. }
  1084. }
  1085. // ------------------------------------------------------------------------------------------------
  1086. void ASEImporter::BuildMaterialIndices()
  1087. {
  1088. ai_assert(NULL != pcScene);
  1089. // iterate through all materials and check whether we need them
  1090. for (unsigned int iMat = 0; iMat < mParser->m_vMaterials.size();++iMat)
  1091. {
  1092. if (mParser->m_vMaterials[iMat].bNeed)
  1093. {
  1094. // convert it to the aiMaterial layout
  1095. ASE::Material& mat = mParser->m_vMaterials[iMat];
  1096. ConvertMaterial(mat);
  1097. TextureTransform::ApplyScaleNOffset(mat);
  1098. ++pcScene->mNumMaterials;
  1099. }
  1100. for (unsigned int iSubMat = 0; iSubMat < mParser->m_vMaterials[
  1101. iMat].avSubMaterials.size();++iSubMat)
  1102. {
  1103. if (mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].bNeed)
  1104. {
  1105. // convert it to the aiMaterial layout
  1106. ASE::Material& mat = mParser->m_vMaterials[iMat].avSubMaterials[iSubMat];
  1107. ConvertMaterial(mat);
  1108. TextureTransform::ApplyScaleNOffset(mat);
  1109. ++pcScene->mNumMaterials;
  1110. }
  1111. }
  1112. }
  1113. // allocate the output material array
  1114. pcScene->mMaterials = new aiMaterial*[pcScene->mNumMaterials];
  1115. D3DS::Material** pcIntMaterials = new D3DS::Material*[pcScene->mNumMaterials];
  1116. unsigned int iNum = 0;
  1117. for (unsigned int iMat = 0; iMat < mParser->m_vMaterials.size();++iMat)
  1118. {
  1119. if (mParser->m_vMaterials[iMat].bNeed)
  1120. {
  1121. ai_assert(NULL != mParser->m_vMaterials[iMat].pcInstance);
  1122. pcScene->mMaterials[iNum] = mParser->m_vMaterials[iMat].pcInstance;
  1123. // store the internal material, too
  1124. pcIntMaterials[iNum] = &mParser->m_vMaterials[iMat];
  1125. // iterate through all meshes and search for one which is using
  1126. // this top-level material index
  1127. for (unsigned int iMesh = 0; iMesh < pcScene->mNumMeshes;++iMesh)
  1128. {
  1129. if (ASE::Face::DEFAULT_MATINDEX == pcScene->mMeshes[iMesh]->mMaterialIndex &&
  1130. iMat == (uintptr_t)pcScene->mMeshes[iMesh]->mColors[3])
  1131. {
  1132. pcScene->mMeshes[iMesh]->mMaterialIndex = iNum;
  1133. pcScene->mMeshes[iMesh]->mColors[3] = NULL;
  1134. }
  1135. }
  1136. iNum++;
  1137. }
  1138. for (unsigned int iSubMat = 0; iSubMat < mParser->m_vMaterials[iMat].avSubMaterials.size();++iSubMat)
  1139. {
  1140. if (mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].bNeed)
  1141. {
  1142. ai_assert(NULL != mParser->m_vMaterials[iMat].avSubMaterials[iSubMat].pcInstance);
  1143. pcScene->mMaterials[iNum] = mParser->m_vMaterials[iMat].
  1144. avSubMaterials[iSubMat].pcInstance;
  1145. // store the internal material, too
  1146. pcIntMaterials[iNum] = &mParser->m_vMaterials[iMat].avSubMaterials[iSubMat];
  1147. // iterate through all meshes and search for one which is using
  1148. // this sub-level material index
  1149. for (unsigned int iMesh = 0; iMesh < pcScene->mNumMeshes;++iMesh)
  1150. {
  1151. if (iSubMat == pcScene->mMeshes[iMesh]->mMaterialIndex &&
  1152. iMat == (uintptr_t)pcScene->mMeshes[iMesh]->mColors[3])
  1153. {
  1154. pcScene->mMeshes[iMesh]->mMaterialIndex = iNum;
  1155. pcScene->mMeshes[iMesh]->mColors[3] = NULL;
  1156. }
  1157. }
  1158. iNum++;
  1159. }
  1160. }
  1161. }
  1162. // prepare for the next step
  1163. for (unsigned int hans = 0; hans < mParser->m_vMaterials.size();++hans)
  1164. TextureTransform::ApplyScaleNOffset(mParser->m_vMaterials[hans]);
  1165. // now we need to iterate through all meshes,
  1166. // generating correct texture coordinates and material uv indices
  1167. for (unsigned int curie = 0; curie < pcScene->mNumMeshes;++curie)
  1168. {
  1169. aiMesh* pcMesh = pcScene->mMeshes[curie];
  1170. // apply texture coordinate transformations
  1171. TextureTransform::BakeScaleNOffset(pcMesh,pcIntMaterials[pcMesh->mMaterialIndex]);
  1172. }
  1173. for (unsigned int hans = 0; hans < pcScene->mNumMaterials;++hans)
  1174. {
  1175. // setup the correct UV indices for each material
  1176. TextureTransform::SetupMatUVSrc(pcScene->mMaterials[hans],
  1177. pcIntMaterials[hans]);
  1178. }
  1179. delete[] pcIntMaterials;
  1180. // finished!
  1181. return;
  1182. }
  1183. // ------------------------------------------------------------------------------------------------
  1184. // Generate normal vectors basing on smoothing groups
  1185. bool ASEImporter::GenerateNormals(ASE::Mesh& mesh)
  1186. {
  1187. if (!mesh.mNormals.empty() && !configRecomputeNormals)
  1188. {
  1189. // check whether there are only uninitialized normals. If there are
  1190. // some, skip all normals from the file and compute them on our own
  1191. for (std::vector<aiVector3D>::const_iterator
  1192. qq = mesh.mNormals.begin();
  1193. qq != mesh.mNormals.end();++qq)
  1194. {
  1195. if ((*qq).x || (*qq).y || (*qq).z)
  1196. {
  1197. return true;
  1198. }
  1199. }
  1200. }
  1201. // The array will be reused
  1202. ComputeNormalsWithSmoothingsGroups<ASE::Face>(mesh);
  1203. return false;
  1204. }