ASELoader.cpp 53 KB

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