3DSConverter.cpp 25 KB

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