3DSConverter.cpp 23 KB

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