ASELoader.cpp 46 KB

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