3DSConverter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. }
  231. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  232. if (Dot3DS::Dot3DSFile::Wire == oldMat.mShading)
  233. {
  234. // set the wireframe flag
  235. unsigned int iWire = 1;
  236. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  237. }
  238. // texture, if there is one
  239. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  240. {
  241. aiString tex;
  242. tex.Set( oldMat.sTexDiffuse.mMapName);
  243. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  244. if (is_not_qnan(oldMat.sTexDiffuse.mTextureBlend))
  245. mat.AddProperty<float>( &oldMat.sTexDiffuse.mTextureBlend, 1, AI_MATKEY_TEXBLEND_DIFFUSE(0));
  246. if (aiTextureMapMode_Clamp != oldMat.sTexDiffuse.mMapMode)
  247. {
  248. int i = (int)oldMat.sTexSpecular.mMapMode;
  249. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  250. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  251. }
  252. }
  253. if( oldMat.sTexSpecular.mMapName.length() > 0)
  254. {
  255. aiString tex;
  256. tex.Set( oldMat.sTexSpecular.mMapName);
  257. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  258. if (is_not_qnan(oldMat.sTexSpecular.mTextureBlend))
  259. mat.AddProperty<float>( &oldMat.sTexSpecular.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SPECULAR(0));
  260. if (aiTextureMapMode_Clamp != oldMat.sTexSpecular.mMapMode)
  261. {
  262. int i = (int)oldMat.sTexSpecular.mMapMode;
  263. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
  264. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SPECULAR(0));
  265. }
  266. }
  267. if( oldMat.sTexOpacity.mMapName.length() > 0)
  268. {
  269. aiString tex;
  270. tex.Set( oldMat.sTexOpacity.mMapName);
  271. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  272. if (is_not_qnan(oldMat.sTexOpacity.mTextureBlend))
  273. mat.AddProperty<float>( &oldMat.sTexOpacity.mTextureBlend, 1,AI_MATKEY_TEXBLEND_OPACITY(0));
  274. if (aiTextureMapMode_Clamp != oldMat.sTexOpacity.mMapMode)
  275. {
  276. int i = (int)oldMat.sTexOpacity.mMapMode;
  277. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
  278. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_OPACITY(0));
  279. }
  280. }
  281. if( oldMat.sTexEmissive.mMapName.length() > 0)
  282. {
  283. aiString tex;
  284. tex.Set( oldMat.sTexEmissive.mMapName);
  285. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  286. if (is_not_qnan(oldMat.sTexEmissive.mTextureBlend))
  287. mat.AddProperty<float>( &oldMat.sTexEmissive.mTextureBlend, 1, AI_MATKEY_TEXBLEND_EMISSIVE(0));
  288. if (aiTextureMapMode_Clamp != oldMat.sTexEmissive.mMapMode)
  289. {
  290. int i = (int)oldMat.sTexEmissive.mMapMode;
  291. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_EMISSIVE(0));
  292. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_EMISSIVE(0));
  293. }
  294. }
  295. if( oldMat.sTexBump.mMapName.length() > 0)
  296. {
  297. aiString tex;
  298. tex.Set( oldMat.sTexBump.mMapName);
  299. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  300. if (is_not_qnan(oldMat.sTexBump.mTextureBlend))
  301. mat.AddProperty<float>( &oldMat.sTexBump.mTextureBlend, 1, AI_MATKEY_TEXBLEND_HEIGHT(0));
  302. if (aiTextureMapMode_Clamp != oldMat.sTexBump.mMapMode)
  303. {
  304. int i = (int)oldMat.sTexBump.mMapMode;
  305. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
  306. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_HEIGHT(0));
  307. }
  308. }
  309. if( oldMat.sTexShininess.mMapName.length() > 0)
  310. {
  311. aiString tex;
  312. tex.Set( oldMat.sTexShininess.mMapName);
  313. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  314. if (is_not_qnan(oldMat.sTexShininess.mTextureBlend))
  315. mat.AddProperty<float>( &oldMat.sTexShininess.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SHININESS(0));
  316. if (aiTextureMapMode_Clamp != oldMat.sTexShininess.mMapMode)
  317. {
  318. int i = (int)oldMat.sTexShininess.mMapMode;
  319. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
  320. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
  321. }
  322. }
  323. // store the name of the material itself, too
  324. if( oldMat.mName.length())
  325. {
  326. aiString tex;
  327. tex.Set( oldMat.mName);
  328. mat.AddProperty( &tex, AI_MATKEY_NAME);
  329. }
  330. return;
  331. }
  332. // ------------------------------------------------------------------------------------------------
  333. void Dot3DSImporter::ConvertMeshes(aiScene* pcOut)
  334. {
  335. std::vector<aiMesh*> avOutMeshes;
  336. avOutMeshes.reserve(this->mScene->mMeshes.size() * 2);
  337. unsigned int iFaceCnt = 0;
  338. // we need to split all meshes by their materials
  339. for (std::vector<Dot3DS::Mesh>::iterator
  340. i = this->mScene->mMeshes.begin();
  341. i != this->mScene->mMeshes.end();++i)
  342. {
  343. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  344. this->mScene->mMaterials.size()];
  345. unsigned int iNum = 0;
  346. for (std::vector<unsigned int>::const_iterator
  347. a = (*i).mFaceMaterials.begin();
  348. a != (*i).mFaceMaterials.end();++a,++iNum)
  349. {
  350. aiSplit[*a].push_back(iNum);
  351. }
  352. // now generate submeshes
  353. bool bFirst = true;
  354. for (unsigned int p = 0; p < this->mScene->mMaterials.size();++p)
  355. {
  356. if (aiSplit[p].size() != 0)
  357. {
  358. aiMesh* p_pcOut = new aiMesh();
  359. // be sure to setup the correct material index
  360. p_pcOut->mMaterialIndex = p;
  361. // use the color data as temporary storage
  362. p_pcOut->mColors[0] = (aiColor4D*)(&*i);
  363. avOutMeshes.push_back(p_pcOut);
  364. // convert vertices
  365. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  366. p_pcOut->mNumVertices = p_pcOut->mNumFaces*3;
  367. // allocate enough storage for faces
  368. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  369. iFaceCnt += p_pcOut->mNumFaces;
  370. if (p_pcOut->mNumVertices)
  371. {
  372. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  373. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  374. unsigned int iBase = 0;
  375. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  376. {
  377. unsigned int iIndex = aiSplit[p][q];
  378. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  379. p_pcOut->mFaces[q].mNumIndices = 3;
  380. p_pcOut->mFaces[q].mIndices[2] = iBase;
  381. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[0]];
  382. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[0]];
  383. p_pcOut->mFaces[q].mIndices[1] = iBase;
  384. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[1]];
  385. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[1]];
  386. p_pcOut->mFaces[q].mIndices[0] = iBase;
  387. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[2]];
  388. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[2]];
  389. }
  390. }
  391. // convert texture coordinates
  392. if ((*i).mTexCoords.size())
  393. {
  394. p_pcOut->mTextureCoords[0] = new aiVector3D[p_pcOut->mNumVertices];
  395. unsigned int iBase = 0;
  396. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  397. {
  398. unsigned int iIndex2 = aiSplit[p][q];
  399. unsigned int iIndex = (*i).mFaces[iIndex2].mIndices[0];
  400. aiVector2D& pc = (*i).mTexCoords[iIndex];
  401. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  402. iIndex = (*i).mFaces[iIndex2].mIndices[1];
  403. pc = (*i).mTexCoords[iIndex];
  404. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  405. iIndex = (*i).mFaces[iIndex2].mIndices[2];
  406. pc = (*i).mTexCoords[iIndex];
  407. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  408. }
  409. // apply texture coordinate scalings
  410. TextureTransform::BakeScaleNOffset ( p_pcOut, &this->mScene->mMaterials[
  411. p_pcOut->mMaterialIndex] );
  412. }
  413. }
  414. }
  415. delete[] aiSplit;
  416. }
  417. pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
  418. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  419. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  420. pcOut->mMeshes[a] = avOutMeshes[a];
  421. if (!iFaceCnt)throw new ImportErrorException("No faces loaded. The mesh is empty");
  422. // for each material in the scene we need to setup the UV source
  423. // set for each texture
  424. for (unsigned int a = 0; a < pcOut->mNumMaterials;++a)
  425. TextureTransform::SetupMatUVSrc( pcOut->mMaterials[a], &this->mScene->mMaterials[a] );
  426. return;
  427. }
  428. // ------------------------------------------------------------------------------------------------
  429. void Dot3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,Dot3DS::Node* pcIn)
  430. {
  431. std::vector<unsigned int> iArray;
  432. iArray.reserve(3);
  433. if (pcIn->mName != "$$$DUMMY")
  434. {
  435. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  436. {
  437. const Dot3DS::Mesh* pcMesh = (const Dot3DS::Mesh*)pcSOut->mMeshes[a]->mColors[0];
  438. ai_assert(NULL != pcMesh);
  439. // do case independent comparisons here, just for safety
  440. if (!ASSIMP_stricmp(pcIn->mName,pcMesh->mName))
  441. iArray.push_back(a);
  442. }
  443. if (!iArray.empty())
  444. {
  445. aiMatrix4x4& mTrafo = ((Dot3DS::Mesh*)pcSOut->mMeshes[iArray[0]]->mColors[0])->mMat;
  446. aiMatrix4x4 mInv = mTrafo;
  447. if (!this->configSkipPivot)
  448. mInv.Inverse();
  449. pcOut->mName.Set(pcIn->mName);
  450. pcOut->mNumMeshes = (unsigned int)iArray.size();
  451. pcOut->mMeshes = new unsigned int[iArray.size()];
  452. for (unsigned int i = 0;i < iArray.size();++i)
  453. {
  454. const unsigned int iIndex = iArray[i];
  455. aiMesh* const mesh = pcSOut->mMeshes[iIndex];
  456. // http://www.zfx.info/DisplayThread.php?MID=235690#235690
  457. const aiVector3D& pivot = pcIn->vPivot;
  458. const aiVector3D* const pvEnd = mesh->mVertices+mesh->mNumVertices;
  459. aiVector3D* pvCurrent = mesh->mVertices;
  460. if(pivot.x || pivot.y || pivot.z && !this->configSkipPivot)
  461. {
  462. while (pvCurrent != pvEnd)
  463. {
  464. *pvCurrent = mInv * (*pvCurrent);
  465. pvCurrent->x -= pivot.x;
  466. pvCurrent->y -= pivot.y;
  467. pvCurrent->z -= pivot.z;
  468. *pvCurrent = mTrafo * (*pvCurrent);
  469. //std::swap( pvCurrent->y, pvCurrent->z );
  470. ++pvCurrent;
  471. }
  472. }
  473. #if 0
  474. else
  475. {
  476. while (pvCurrent != pvEnd)
  477. {
  478. std::swap( pvCurrent->y, pvCurrent->z );
  479. ++pvCurrent;
  480. }
  481. }
  482. #endif
  483. pcOut->mMeshes[i] = iIndex;
  484. }
  485. }
  486. }
  487. pcOut->mTransformation = aiMatrix4x4();
  488. pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
  489. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  490. for (unsigned int i = 0; i < pcIn->mChildren.size();++i)
  491. {
  492. pcOut->mChildren[i] = new aiNode();
  493. pcOut->mChildren[i]->mParent = pcOut;
  494. AddNodeToGraph(pcSOut,pcOut->mChildren[i],
  495. pcIn->mChildren[i]);
  496. }
  497. return;
  498. }
  499. // ------------------------------------------------------------------------------------------------
  500. void Dot3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  501. {
  502. pcOut->mRootNode = new aiNode();
  503. if (0 == this->mRootNode->mChildren.size())
  504. {
  505. // seems the file has not even a hierarchy.
  506. // generate a flat hiearachy which looks like this:
  507. //
  508. // ROOT_NODE
  509. // |
  510. // ----------------------------------------
  511. // | | | |
  512. // MESH_0 MESH_1 MESH_2 ... MESH_N
  513. //
  514. unsigned int iCnt = 0;
  515. DefaultLogger::get()->warn("No hierarchy information has been found in the file. ");
  516. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes;
  517. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mNumMeshes ];
  518. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i)
  519. {
  520. aiNode* pcNode = new aiNode();
  521. pcNode->mParent = pcOut->mRootNode;
  522. pcNode->mNumChildren = 0;
  523. pcNode->mChildren = 0;
  524. pcNode->mMeshes = new unsigned int[1];
  525. pcNode->mMeshes[0] = i;
  526. pcNode->mNumMeshes = 1;
  527. char szBuffer[128];
  528. int iLen;
  529. iLen = sprintf(szBuffer,"UNNAMED_%i",i);
  530. ai_assert(0 < iLen);
  531. ::memcpy(pcNode->mName.data,szBuffer,iLen);
  532. pcNode->mName.data[iLen] = '\0';
  533. pcNode->mName.length = iLen;
  534. // add the new child to the parent node
  535. pcOut->mRootNode->mChildren[i] = pcNode;
  536. }
  537. }
  538. else this->AddNodeToGraph(pcOut, pcOut->mRootNode, this->mRootNode);
  539. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  540. pcOut->mMeshes[a]->mColors[0] = NULL;
  541. // if the root node has only one child ... set the child as root node
  542. if (1 == pcOut->mRootNode->mNumChildren)
  543. {
  544. aiNode* pcOld = pcOut->mRootNode;
  545. pcOut->mRootNode = pcOut->mRootNode->mChildren[0];
  546. pcOut->mRootNode->mParent = NULL;
  547. pcOld->mChildren[0] = NULL;
  548. delete pcOld;
  549. }
  550. // if the root node is a default node setup a name for it
  551. if (pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')
  552. pcOut->mRootNode->mName.Set("<root>");
  553. }
  554. // ------------------------------------------------------------------------------------------------
  555. void Dot3DSImporter::ConvertScene(aiScene* pcOut)
  556. {
  557. pcOut->mNumMaterials = (unsigned int)this->mScene->mMaterials.size();
  558. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  559. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  560. {
  561. MaterialHelper* pcNew = new MaterialHelper();
  562. this->ConvertMaterial(this->mScene->mMaterials[i],*pcNew);
  563. pcOut->mMaterials[i] = pcNew;
  564. }
  565. this->ConvertMeshes(pcOut);
  566. return;
  567. }