3DSConverter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 3ds importer class */
  35. // internal headers
  36. #include "3DSLoader.h"
  37. #include "MaterialSystem.h"
  38. #include "TextureTransform.h"
  39. #include "StringComparison.h"
  40. #include "qnan.h"
  41. // public ASSIMP headers
  42. #include "../include/DefaultLogger.h"
  43. #include "../include/IOStream.h"
  44. #include "../include/IOSystem.h"
  45. #include "../include/aiMesh.h"
  46. #include "../include/aiScene.h"
  47. #include "../include/aiAssert.h"
  48. using namespace Assimp;
  49. #ifdef _MSC_VER
  50. # define sprintf sprintf_s
  51. #endif
  52. // ------------------------------------------------------------------------------------------------
  53. void Dot3DSImporter::ReplaceDefaultMaterial()
  54. {
  55. // try to find an existing material that matches the
  56. // typical default material setting:
  57. // - no textures
  58. // - diffuse color (in grey!)
  59. // NOTE: This is here to workaround the fact that some
  60. // exporters are writing a default material, too.
  61. unsigned int iIndex = 0xcdcdcdcd;
  62. for (unsigned int i = 0; i < this->mScene->mMaterials.size();++i)
  63. {
  64. if (std::string::npos == this->mScene->mMaterials[i].mName.find("default") &&
  65. std::string::npos == this->mScene->mMaterials[i].mName.find("DEFAULT"))continue;
  66. if (this->mScene->mMaterials[i].mDiffuse.r !=
  67. this->mScene->mMaterials[i].mDiffuse.g ||
  68. this->mScene->mMaterials[i].mDiffuse.r !=
  69. this->mScene->mMaterials[i].mDiffuse.b)continue;
  70. if (this->mScene->mMaterials[i].sTexDiffuse.mMapName.length() != 0 ||
  71. this->mScene->mMaterials[i].sTexBump.mMapName.length()!= 0 ||
  72. this->mScene->mMaterials[i].sTexOpacity.mMapName.length() != 0 ||
  73. this->mScene->mMaterials[i].sTexEmissive.mMapName.length() != 0 ||
  74. this->mScene->mMaterials[i].sTexSpecular.mMapName.length() != 0 ||
  75. this->mScene->mMaterials[i].sTexShininess.mMapName.length() != 0 )continue;
  76. iIndex = i;
  77. }
  78. if (0xcdcdcdcd == iIndex)iIndex = (unsigned int)this->mScene->mMaterials.size();
  79. // now iterate through all meshes and through all faces and
  80. // find all faces that are using the default material
  81. unsigned int iCnt = 0;
  82. for (std::vector<Dot3DS::Mesh>::iterator
  83. i = this->mScene->mMeshes.begin();
  84. i != this->mScene->mMeshes.end();++i)
  85. {
  86. for (std::vector<unsigned int>::iterator
  87. a = (*i).mFaceMaterials.begin();
  88. a != (*i).mFaceMaterials.end();++a)
  89. {
  90. // NOTE: The additional check seems to be necessary,
  91. // some exporters seem to generate invalid data here
  92. if (0xcdcdcdcd == (*a))
  93. {
  94. (*a) = iIndex;
  95. ++iCnt;
  96. }
  97. else if ( (*a) >= this->mScene->mMaterials.size())
  98. {
  99. (*a) = iIndex;
  100. ++iCnt;
  101. DefaultLogger::get()->warn("Material index overflow in 3DS file. Using default material");
  102. }
  103. }
  104. }
  105. if (iCnt && iIndex == this->mScene->mMaterials.size())
  106. {
  107. // we need to create our own default material
  108. Dot3DS::Material sMat;
  109. sMat.mDiffuse = aiColor3D(0.3f,0.3f,0.3f);
  110. sMat.mName = "%%%DEFAULT";
  111. this->mScene->mMaterials.push_back(sMat);
  112. }
  113. return;
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. void Dot3DSImporter::CheckIndices(Dot3DS::Mesh* sMesh)
  117. {
  118. for (std::vector< Dot3DS::Face >::iterator
  119. i = sMesh->mFaces.begin();
  120. i != sMesh->mFaces.end();++i)
  121. {
  122. // check whether all indices are in range
  123. if ((*i).mIndices[0] >= sMesh->mPositions.size())
  124. {
  125. DefaultLogger::get()->warn("Face index overflow in 3DS file (#1)");
  126. (*i).mIndices[0] = (uint32_t)sMesh->mPositions.size()-1;
  127. }
  128. if ((*i).mIndices[1] >= sMesh->mPositions.size())
  129. {
  130. DefaultLogger::get()->warn("Face index overflow in 3DS file (#2)");
  131. (*i).mIndices[1] = (uint32_t)sMesh->mPositions.size()-1;
  132. }
  133. if ((*i).mIndices[2] >= sMesh->mPositions.size())
  134. {
  135. DefaultLogger::get()->warn("Face index overflow in 3DS file (#3)");
  136. (*i).mIndices[2] = (uint32_t)sMesh->mPositions.size()-1;
  137. }
  138. }
  139. return;
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. void Dot3DSImporter::MakeUnique(Dot3DS::Mesh* sMesh)
  143. {
  144. unsigned int iBase = 0;
  145. std::vector<aiVector3D> vNew;
  146. std::vector<aiVector2D> vNew2;
  147. vNew.resize(sMesh->mFaces.size() * 3);
  148. if (sMesh->mTexCoords.size())vNew2.resize(sMesh->mFaces.size() * 3);
  149. for (unsigned int i = 0; i < sMesh->mFaces.size();++i)
  150. {
  151. uint32_t iTemp1,iTemp2;
  152. // positions
  153. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[2]];
  154. iTemp1 = iBase++;
  155. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[1]];
  156. iTemp2 = iBase++;
  157. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[0]];
  158. // texture coordinates
  159. if (sMesh->mTexCoords.size())
  160. {
  161. vNew2[iTemp1] = sMesh->mTexCoords[sMesh->mFaces[i].mIndices[2]];
  162. vNew2[iTemp2] = sMesh->mTexCoords[sMesh->mFaces[i].mIndices[1]];
  163. vNew2[iBase] = sMesh->mTexCoords[sMesh->mFaces[i].mIndices[0]];
  164. }
  165. sMesh->mFaces[i].mIndices[2] = iBase++;
  166. sMesh->mFaces[i].mIndices[0] = iTemp1;
  167. sMesh->mFaces[i].mIndices[1] = iTemp2;
  168. }
  169. sMesh->mPositions = vNew;
  170. sMesh->mTexCoords = vNew2;
  171. return;
  172. }
  173. // ------------------------------------------------------------------------------------------------
  174. void Dot3DSImporter::ConvertMaterial(Dot3DS::Material& oldMat,
  175. MaterialHelper& mat)
  176. {
  177. // NOTE: Pass the background image to the viewer by bypassing the
  178. // material system. This is an evil hack, never do it again!
  179. if (0 != this->mBackgroundImage.length() && this->bHasBG)
  180. {
  181. aiString tex;
  182. tex.Set( this->mBackgroundImage);
  183. mat.AddProperty( &tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
  184. // be sure this is only done for the first material
  185. this->mBackgroundImage = std::string("");
  186. }
  187. // At first add the base ambient color of the
  188. // scene to the material
  189. oldMat.mAmbient.r += this->mClrAmbient.r;
  190. oldMat.mAmbient.g += this->mClrAmbient.g;
  191. oldMat.mAmbient.b += this->mClrAmbient.b;
  192. aiString name;
  193. name.Set( oldMat.mName);
  194. mat.AddProperty( &name, AI_MATKEY_NAME);
  195. // material colors
  196. mat.AddProperty( &oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  197. mat.AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  198. mat.AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  199. mat.AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  200. // phong shininess and shininess strength
  201. if (Dot3DS::Dot3DSFile::Phong == oldMat.mShading ||
  202. Dot3DS::Dot3DSFile::Metal == oldMat.mShading)
  203. {
  204. if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength)
  205. {
  206. oldMat.mShading = Dot3DS::Dot3DSFile::Gouraud;
  207. }
  208. else
  209. {
  210. mat.AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  211. mat.AddProperty( &oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  212. }
  213. }
  214. // opacity
  215. mat.AddProperty<float>( &oldMat.mTransparency,1,AI_MATKEY_OPACITY);
  216. // bump height scaling
  217. mat.AddProperty<float>( &oldMat.mBumpHeight,1,AI_MATKEY_BUMPSCALING);
  218. // two sided rendering?
  219. if (oldMat.mTwoSided)
  220. {
  221. int i = 0;
  222. mat.AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  223. }
  224. // shading mode
  225. aiShadingMode eShading = aiShadingMode_NoShading;
  226. switch (oldMat.mShading)
  227. {
  228. case Dot3DS::Dot3DSFile::Flat:
  229. eShading = aiShadingMode_Flat; break;
  230. // I don't know what "Wire" shading should be,
  231. // assume it is simple lambertian diffuse (L dot N) shading
  232. case Dot3DS::Dot3DSFile::Wire:
  233. case Dot3DS::Dot3DSFile::Gouraud:
  234. eShading = aiShadingMode_Gouraud; break;
  235. // assume cook-torrance shading for metals.
  236. // NOTE: I assume the real shader inside 3ds max is an anisotropic
  237. // Phong-Blinn shader, but this is a good approximation too
  238. case Dot3DS::Dot3DSFile::Phong :
  239. eShading = aiShadingMode_Phong; break;
  240. case Dot3DS::Dot3DSFile::Metal :
  241. eShading = aiShadingMode_CookTorrance; break;
  242. }
  243. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  244. if (Dot3DS::Dot3DSFile::Wire == oldMat.mShading)
  245. {
  246. // set the wireframe flag
  247. unsigned int iWire = 1;
  248. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  249. }
  250. // texture, if there is one
  251. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  252. {
  253. aiString tex;
  254. tex.Set( oldMat.sTexDiffuse.mMapName);
  255. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  256. if (is_not_qnan(oldMat.sTexDiffuse.mTextureBlend))
  257. mat.AddProperty<float>( &oldMat.sTexDiffuse.mTextureBlend, 1, AI_MATKEY_TEXBLEND_DIFFUSE(0));
  258. if (aiTextureMapMode_Clamp != oldMat.sTexDiffuse.mMapMode)
  259. {
  260. int i = (int)oldMat.sTexSpecular.mMapMode;
  261. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  262. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  263. }
  264. }
  265. if( oldMat.sTexSpecular.mMapName.length() > 0)
  266. {
  267. aiString tex;
  268. tex.Set( oldMat.sTexSpecular.mMapName);
  269. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  270. if (is_not_qnan(oldMat.sTexSpecular.mTextureBlend))
  271. mat.AddProperty<float>( &oldMat.sTexSpecular.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SPECULAR(0));
  272. if (aiTextureMapMode_Clamp != oldMat.sTexSpecular.mMapMode)
  273. {
  274. int i = (int)oldMat.sTexSpecular.mMapMode;
  275. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
  276. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SPECULAR(0));
  277. }
  278. }
  279. if( oldMat.sTexOpacity.mMapName.length() > 0)
  280. {
  281. aiString tex;
  282. tex.Set( oldMat.sTexOpacity.mMapName);
  283. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  284. if (is_not_qnan(oldMat.sTexOpacity.mTextureBlend))
  285. mat.AddProperty<float>( &oldMat.sTexOpacity.mTextureBlend, 1,AI_MATKEY_TEXBLEND_OPACITY(0));
  286. if (aiTextureMapMode_Clamp != oldMat.sTexOpacity.mMapMode)
  287. {
  288. int i = (int)oldMat.sTexOpacity.mMapMode;
  289. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
  290. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_OPACITY(0));
  291. }
  292. }
  293. if( oldMat.sTexEmissive.mMapName.length() > 0)
  294. {
  295. aiString tex;
  296. tex.Set( oldMat.sTexEmissive.mMapName);
  297. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  298. if (is_not_qnan(oldMat.sTexEmissive.mTextureBlend))
  299. mat.AddProperty<float>( &oldMat.sTexEmissive.mTextureBlend, 1, AI_MATKEY_TEXBLEND_EMISSIVE(0));
  300. if (aiTextureMapMode_Clamp != oldMat.sTexEmissive.mMapMode)
  301. {
  302. int i = (int)oldMat.sTexEmissive.mMapMode;
  303. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_EMISSIVE(0));
  304. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_EMISSIVE(0));
  305. }
  306. }
  307. if( oldMat.sTexBump.mMapName.length() > 0)
  308. {
  309. aiString tex;
  310. tex.Set( oldMat.sTexBump.mMapName);
  311. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  312. if (is_not_qnan(oldMat.sTexBump.mTextureBlend))
  313. mat.AddProperty<float>( &oldMat.sTexBump.mTextureBlend, 1, AI_MATKEY_TEXBLEND_HEIGHT(0));
  314. if (aiTextureMapMode_Clamp != oldMat.sTexBump.mMapMode)
  315. {
  316. int i = (int)oldMat.sTexBump.mMapMode;
  317. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
  318. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_HEIGHT(0));
  319. }
  320. }
  321. if( oldMat.sTexShininess.mMapName.length() > 0)
  322. {
  323. aiString tex;
  324. tex.Set( oldMat.sTexShininess.mMapName);
  325. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  326. if (is_not_qnan(oldMat.sTexShininess.mTextureBlend))
  327. mat.AddProperty<float>( &oldMat.sTexShininess.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SHININESS(0));
  328. if (aiTextureMapMode_Clamp != oldMat.sTexShininess.mMapMode)
  329. {
  330. int i = (int)oldMat.sTexShininess.mMapMode;
  331. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
  332. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
  333. }
  334. }
  335. // store the name of the material itself, too
  336. if( oldMat.mName.length())
  337. {
  338. aiString tex;
  339. tex.Set( oldMat.mName);
  340. mat.AddProperty( &tex, AI_MATKEY_NAME);
  341. }
  342. return;
  343. }
  344. // ------------------------------------------------------------------------------------------------
  345. void Dot3DSImporter::ConvertMeshes(aiScene* pcOut)
  346. {
  347. std::vector<aiMesh*> avOutMeshes;
  348. avOutMeshes.reserve(this->mScene->mMeshes.size() * 2);
  349. unsigned int iFaceCnt = 0;
  350. // we need to split all meshes by their materials
  351. for (std::vector<Dot3DS::Mesh>::iterator
  352. i = this->mScene->mMeshes.begin();
  353. i != this->mScene->mMeshes.end();++i)
  354. {
  355. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  356. this->mScene->mMaterials.size()];
  357. unsigned int iNum = 0;
  358. for (std::vector<unsigned int>::const_iterator
  359. a = (*i).mFaceMaterials.begin();
  360. a != (*i).mFaceMaterials.end();++a,++iNum)
  361. {
  362. // check range
  363. // FIX: shouldn't be necessary anymore, has been moved to ReplaceDefaultMaterial()
  364. #if 0
  365. if ((*a) >= this->mScene->mMaterials.size())
  366. {
  367. DefaultLogger::get()->error("3DS face material index is out of range");
  368. // use the last material instead
  369. // TODO: assign the default material index
  370. aiSplit[this->mScene->mMaterials.size()-1].push_back(iNum);
  371. }
  372. else
  373. #endif
  374. aiSplit[*a].push_back(iNum);
  375. }
  376. // now generate submeshes
  377. bool bFirst = true;
  378. for (unsigned int p = 0; p < this->mScene->mMaterials.size();++p)
  379. {
  380. if (aiSplit[p].size() != 0)
  381. {
  382. aiMesh* p_pcOut = new aiMesh();
  383. // be sure to setup the correct material index
  384. p_pcOut->mMaterialIndex = p;
  385. // use the color data as temporary storage
  386. p_pcOut->mColors[0] = (aiColor4D*)(&*i);
  387. avOutMeshes.push_back(p_pcOut);
  388. // convert vertices
  389. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  390. p_pcOut->mNumVertices = p_pcOut->mNumFaces*3;
  391. // allocate enough storage for faces
  392. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  393. iFaceCnt += p_pcOut->mNumFaces;
  394. if (p_pcOut->mNumVertices)
  395. {
  396. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  397. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  398. unsigned int iBase = 0;
  399. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  400. {
  401. unsigned int iIndex = aiSplit[p][q];
  402. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  403. p_pcOut->mFaces[q].mNumIndices = 3;
  404. p_pcOut->mFaces[q].mIndices[2] = iBase;
  405. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[0]];
  406. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[0]];
  407. p_pcOut->mFaces[q].mIndices[1] = iBase;
  408. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[1]];
  409. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[1]];
  410. p_pcOut->mFaces[q].mIndices[0] = iBase;
  411. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[2]];
  412. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[2]];
  413. }
  414. }
  415. // convert texture coordinates
  416. if ((*i).mTexCoords.size())
  417. {
  418. p_pcOut->mTextureCoords[0] = new aiVector3D[p_pcOut->mNumVertices];
  419. unsigned int iBase = 0;
  420. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  421. {
  422. unsigned int iIndex2 = aiSplit[p][q];
  423. unsigned int iIndex = (*i).mFaces[iIndex2].mIndices[0];
  424. aiVector2D& pc = (*i).mTexCoords[iIndex];
  425. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  426. iIndex = (*i).mFaces[iIndex2].mIndices[1];
  427. pc = (*i).mTexCoords[iIndex];
  428. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  429. iIndex = (*i).mFaces[iIndex2].mIndices[2];
  430. pc = (*i).mTexCoords[iIndex];
  431. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  432. }
  433. // apply texture coordinate scalings
  434. TextureTransform::BakeScaleNOffset ( p_pcOut, &this->mScene->mMaterials[
  435. p_pcOut->mMaterialIndex] );
  436. }
  437. }
  438. }
  439. delete[] aiSplit;
  440. }
  441. pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
  442. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  443. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  444. {
  445. pcOut->mMeshes[a] = avOutMeshes[a];
  446. }
  447. if (!iFaceCnt)
  448. throw new ImportErrorException("No faces loaded. The mesh is empty");
  449. // for each material in the scene we need to setup the UV source
  450. // set for each texture
  451. for (unsigned int a = 0; a < pcOut->mNumMaterials;++a)
  452. TextureTransform::SetupMatUVSrc( pcOut->mMaterials[a], &this->mScene->mMaterials[a] );
  453. return;
  454. }
  455. // ------------------------------------------------------------------------------------------------
  456. void Dot3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,Dot3DS::Node* pcIn)
  457. {
  458. std::vector<unsigned int> iArray;
  459. iArray.reserve(3);
  460. if (pcIn->mName != "$$$DUMMY")
  461. {
  462. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  463. {
  464. const Dot3DS::Mesh* pcMesh = (const Dot3DS::Mesh*)pcSOut->mMeshes[a]->mColors[0];
  465. ai_assert(NULL != pcMesh);
  466. // do case independent comparisons here, just for safety
  467. if (pcIn->mName.length() == pcMesh->mName.length() &&
  468. !ASSIMP_stricmp(pcIn->mName.c_str(),pcMesh->mName.c_str()))
  469. {
  470. iArray.push_back(a);
  471. }
  472. }
  473. if (!iArray.empty())
  474. {
  475. aiMatrix4x4& mTrafo = ((Dot3DS::Mesh*)pcSOut->mMeshes[iArray[0]]->mColors[0])->mMat;
  476. aiMatrix4x4 mInv = mTrafo;
  477. if (!this->configSkipPivot)
  478. mInv.Inverse();
  479. pcOut->mName.Set(pcIn->mName);
  480. pcOut->mNumMeshes = (unsigned int)iArray.size();
  481. pcOut->mMeshes = new unsigned int[iArray.size()];
  482. for (unsigned int i = 0;i < iArray.size();++i)
  483. {
  484. const unsigned int iIndex = iArray[i];
  485. aiMesh* const mesh = pcSOut->mMeshes[iIndex];
  486. // http://www.zfx.info/DisplayThread.php?MID=235690#235690
  487. const aiVector3D& pivot = pcIn->vPivot;
  488. const aiVector3D* const pvEnd = mesh->mVertices+mesh->mNumVertices;
  489. aiVector3D* pvCurrent = mesh->mVertices;
  490. if(pivot.x || pivot.y || pivot.z && !this->configSkipPivot)
  491. {
  492. while (pvCurrent != pvEnd)
  493. {
  494. *pvCurrent = mInv * (*pvCurrent);
  495. pvCurrent->x -= pivot.x;
  496. pvCurrent->y -= pivot.y;
  497. pvCurrent->z -= pivot.z;
  498. *pvCurrent = mTrafo * (*pvCurrent);
  499. std::swap( pvCurrent->y, pvCurrent->z );
  500. ++pvCurrent;
  501. }
  502. }
  503. else
  504. {
  505. while (pvCurrent != pvEnd)
  506. {
  507. std::swap( pvCurrent->y, pvCurrent->z );
  508. ++pvCurrent;
  509. }
  510. }
  511. pcOut->mMeshes[i] = iIndex;
  512. }
  513. }
  514. }
  515. pcOut->mTransformation = aiMatrix4x4();
  516. pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
  517. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  518. for (unsigned int i = 0; i < pcIn->mChildren.size();++i)
  519. {
  520. pcOut->mChildren[i] = new aiNode();
  521. pcOut->mChildren[i]->mParent = pcOut;
  522. AddNodeToGraph(pcSOut,pcOut->mChildren[i],
  523. pcIn->mChildren[i]);
  524. }
  525. return;
  526. }
  527. // ------------------------------------------------------------------------------------------------
  528. void Dot3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  529. {
  530. pcOut->mRootNode = new aiNode();
  531. if (0 == this->mRootNode->mChildren.size())
  532. {
  533. // seems the file has not even a hierarchy.
  534. // generate a flat hiearachy which looks like this:
  535. //
  536. // ROOT_NODE
  537. // |
  538. // ----------------------------------------
  539. // | | | |
  540. // MESH_0 MESH_1 MESH_2 ... MESH_N
  541. //
  542. unsigned int iCnt = 0;
  543. DefaultLogger::get()->warn("No hierarchy information has been found in the file. ");
  544. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes;
  545. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mNumMeshes ];
  546. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i)
  547. {
  548. aiNode* pcNode = new aiNode();
  549. pcNode->mParent = pcOut->mRootNode;
  550. pcNode->mNumChildren = 0;
  551. pcNode->mChildren = 0;
  552. pcNode->mMeshes = new unsigned int[1];
  553. pcNode->mMeshes[0] = i;
  554. pcNode->mNumMeshes = 1;
  555. char szBuffer[128];
  556. int iLen;
  557. iLen = sprintf(szBuffer,"UNNAMED_%i",i);
  558. ai_assert(0 < iLen);
  559. ::memcpy(pcNode->mName.data,szBuffer,iLen);
  560. pcNode->mName.data[iLen] = '\0';
  561. pcNode->mName.length = iLen;
  562. // add the new child to the parent node
  563. pcOut->mRootNode->mChildren[i] = pcNode;
  564. }
  565. }
  566. else this->AddNodeToGraph(pcOut, pcOut->mRootNode, this->mRootNode);
  567. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  568. pcOut->mMeshes[a]->mColors[0] = NULL;
  569. // if the root node has only one child ... set the child as root node
  570. if (1 == pcOut->mRootNode->mNumChildren)
  571. {
  572. aiNode* pcOld = pcOut->mRootNode;
  573. pcOut->mRootNode = pcOut->mRootNode->mChildren[0];
  574. pcOut->mRootNode->mParent = NULL;
  575. pcOld->mChildren[0] = NULL;
  576. delete pcOld;
  577. }
  578. // if the root node is a default node setup a name for it
  579. if (pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')
  580. {
  581. pcOut->mRootNode->mName.Set("<root>");
  582. }
  583. }
  584. // ------------------------------------------------------------------------------------------------
  585. void Dot3DSImporter::ConvertScene(aiScene* pcOut)
  586. {
  587. pcOut->mNumMaterials = (unsigned int)this->mScene->mMaterials.size();
  588. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  589. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  590. {
  591. MaterialHelper* pcNew = new MaterialHelper();
  592. this->ConvertMaterial(this->mScene->mMaterials[i],*pcNew);
  593. pcOut->mMaterials[i] = pcNew;
  594. }
  595. this->ConvertMeshes(pcOut);
  596. return;
  597. }