3DSConverter.cpp 22 KB

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