3DSConverter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
  37. // internal headers
  38. #include "3DSLoader.h"
  39. #include "TargetAnimation.h"
  40. using namespace Assimp;
  41. // ------------------------------------------------------------------------------------------------
  42. // Setup final material indices, generae a default material if necessary
  43. void Discreet3DSImporter::ReplaceDefaultMaterial()
  44. {
  45. //////////////////////////////////////////////////////////////////////////
  46. // Try to find an existing material that matches the
  47. // typical default material setting:
  48. // - no textures
  49. // - diffuse color (in grey!)
  50. // NOTE: This is here to workaround the fact that some
  51. // exporters are writing a default material, too.
  52. //////////////////////////////////////////////////////////////////////////
  53. unsigned int idx = 0xcdcdcdcd;
  54. for (unsigned int i = 0; i < mScene->mMaterials.size();++i)
  55. {
  56. std::string s = mScene->mMaterials[i].mName;
  57. for (std::string::iterator it = s.begin(); it != s.end(); ++it)
  58. *it = ::tolower(*it);
  59. if (std::string::npos == s.find("default"))continue;
  60. if (mScene->mMaterials[i].mDiffuse.r !=
  61. mScene->mMaterials[i].mDiffuse.g ||
  62. mScene->mMaterials[i].mDiffuse.r !=
  63. mScene->mMaterials[i].mDiffuse.b)continue;
  64. if (mScene->mMaterials[i].sTexDiffuse.mMapName.length() != 0 ||
  65. mScene->mMaterials[i].sTexBump.mMapName.length() != 0 ||
  66. mScene->mMaterials[i].sTexOpacity.mMapName.length() != 0 ||
  67. mScene->mMaterials[i].sTexEmissive.mMapName.length() != 0 ||
  68. mScene->mMaterials[i].sTexSpecular.mMapName.length() != 0 ||
  69. mScene->mMaterials[i].sTexShininess.mMapName.length() != 0 )
  70. {
  71. continue;
  72. }
  73. idx = i;
  74. }
  75. if (0xcdcdcdcd == idx)idx = (unsigned int)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 cnt = 0;
  79. for (std::vector<D3DS::Mesh>::iterator
  80. i = mScene->mMeshes.begin();
  81. i != 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) = idx;
  92. ++cnt;
  93. }
  94. else if ( (*a) >= mScene->mMaterials.size())
  95. {
  96. (*a) = idx;
  97. DefaultLogger::get()->warn("Material index overflow in 3DS file. Using default material");
  98. ++cnt;
  99. }
  100. }
  101. }
  102. if (cnt && idx == mScene->mMaterials.size())
  103. {
  104. // We need to create our own default material
  105. D3DS::Material sMat;
  106. sMat.mDiffuse = aiColor3D(0.3f,0.3f,0.3f);
  107. sMat.mName = "%%%DEFAULT";
  108. mScene->mMaterials.push_back(sMat);
  109. DefaultLogger::get()->info("3DS: Generating default material");
  110. }
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Check whether all indices are valid. Otherwise we'd crash before the validation step is reached
  114. void Discreet3DSImporter::CheckIndices(D3DS::Mesh& sMesh)
  115. {
  116. for (std::vector< D3DS::Face >::iterator i = sMesh.mFaces.begin(); i != sMesh.mFaces.end();++i)
  117. {
  118. // check whether all indices are in range
  119. for (unsigned int a = 0; a < 3;++a)
  120. {
  121. if ((*i).mIndices[a] >= sMesh.mPositions.size())
  122. {
  123. DefaultLogger::get()->warn("3DS: Vertex index overflow)");
  124. (*i).mIndices[a] = (uint32_t)sMesh.mPositions.size()-1;
  125. }
  126. if ( !sMesh.mTexCoords.empty() && (*i).mIndices[a] >= sMesh.mTexCoords.size())
  127. {
  128. DefaultLogger::get()->warn("3DS: Texture coordinate index overflow)");
  129. (*i).mIndices[a] = (uint32_t)sMesh.mTexCoords.size()-1;
  130. }
  131. }
  132. }
  133. }
  134. // ------------------------------------------------------------------------------------------------
  135. // Generate out unique verbose format representation
  136. void Discreet3DSImporter::MakeUnique(D3DS::Mesh& sMesh)
  137. {
  138. // TODO: really necessary? I don't think. Just a waste of memory and time
  139. // to do it now in a separate buffer.
  140. // Allocate output storage
  141. std::vector<aiVector3D> vNew (sMesh.mFaces.size() * 3);
  142. std::vector<aiVector3D> vNew2;
  143. if (sMesh.mTexCoords.size())
  144. vNew2.resize(sMesh.mFaces.size() * 3);
  145. for (unsigned int i = 0, base = 0; i < sMesh.mFaces.size();++i)
  146. {
  147. D3DS::Face& face = sMesh.mFaces[i];
  148. // Positions
  149. for (unsigned int a = 0; a < 3;++a,++base)
  150. {
  151. vNew[base] = sMesh.mPositions[face.mIndices[a]];
  152. if (sMesh.mTexCoords.size())
  153. vNew2[base] = sMesh.mTexCoords[face.mIndices[a]];
  154. face.mIndices[a] = base;
  155. }
  156. }
  157. sMesh.mPositions = vNew;
  158. sMesh.mTexCoords = vNew2;
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. // Convert a 3DS texture to texture keys in an aiMaterial
  162. void CopyTexture(MaterialHelper& mat, D3DS::Texture& texture, aiTextureType type)
  163. {
  164. // Setup the texture name
  165. aiString tex;
  166. tex.Set( texture.mMapName);
  167. mat.AddProperty( &tex, AI_MATKEY_TEXTURE(type,0));
  168. // Setup the texture blend factor
  169. if (is_not_qnan(texture.mTextureBlend))
  170. mat.AddProperty<float>( &texture.mTextureBlend, 1, AI_MATKEY_TEXBLEND(type,0));
  171. // Setup the texture mapping mode
  172. mat.AddProperty<int>((int*)&texture.mMapMode,1,AI_MATKEY_MAPPINGMODE_U(type,0));
  173. mat.AddProperty<int>((int*)&texture.mMapMode,1,AI_MATKEY_MAPPINGMODE_V(type,0));
  174. // Mirroring - double the scaling values
  175. // FIXME: this is not really correct ...
  176. if (texture.mMapMode == aiTextureMapMode_Mirror)
  177. {
  178. texture.mScaleU *= 2.f;
  179. texture.mScaleV *= 2.f;
  180. texture.mOffsetU /= 2.f;
  181. texture.mOffsetV /= 2.f;
  182. }
  183. // Setup texture UV transformations
  184. mat.AddProperty<float>(&texture.mOffsetU,5,AI_MATKEY_UVTRANSFORM(type,0));
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. // Convert a 3DS material to an aiMaterial
  188. void Discreet3DSImporter::ConvertMaterial(D3DS::Material& oldMat,
  189. MaterialHelper& mat)
  190. {
  191. // NOTE: Pass the background image to the viewer by bypassing the
  192. // material system. This is an evil hack, never do it again!
  193. if (0 != mBackgroundImage.length() && bHasBG)
  194. {
  195. aiString tex;
  196. tex.Set( mBackgroundImage);
  197. mat.AddProperty( &tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
  198. // Be sure this is only done for the first material
  199. mBackgroundImage = std::string("");
  200. }
  201. // At first add the base ambient color of the scene to the material
  202. oldMat.mAmbient.r += mClrAmbient.r;
  203. oldMat.mAmbient.g += mClrAmbient.g;
  204. oldMat.mAmbient.b += mClrAmbient.b;
  205. aiString name;
  206. name.Set( oldMat.mName);
  207. mat.AddProperty( &name, AI_MATKEY_NAME);
  208. // Material colors
  209. mat.AddProperty( &oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  210. mat.AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  211. mat.AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  212. mat.AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  213. // Phong shininess and shininess strength
  214. if (D3DS::Discreet3DS::Phong == oldMat.mShading ||
  215. D3DS::Discreet3DS::Metal == oldMat.mShading)
  216. {
  217. if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength)
  218. {
  219. oldMat.mShading = D3DS::Discreet3DS::Gouraud;
  220. }
  221. else
  222. {
  223. mat.AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  224. mat.AddProperty( &oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  225. }
  226. }
  227. // Opacity
  228. mat.AddProperty<float>( &oldMat.mTransparency,1,AI_MATKEY_OPACITY);
  229. // Bump height scaling
  230. mat.AddProperty<float>( &oldMat.mBumpHeight,1,AI_MATKEY_BUMPSCALING);
  231. // Two sided rendering?
  232. if (oldMat.mTwoSided)
  233. {
  234. int i = 1;
  235. mat.AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  236. }
  237. // Shading mode
  238. aiShadingMode eShading = aiShadingMode_NoShading;
  239. switch (oldMat.mShading)
  240. {
  241. case D3DS::Discreet3DS::Flat:
  242. eShading = aiShadingMode_Flat; break;
  243. // I don't know what "Wire" shading should be,
  244. // assume it is simple lambertian diffuse shading
  245. case D3DS::Discreet3DS::Wire:
  246. {
  247. // Set the wireframe flag
  248. unsigned int iWire = 1;
  249. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  250. }
  251. case D3DS::Discreet3DS::Gouraud:
  252. eShading = aiShadingMode_Gouraud; break;
  253. // assume cook-torrance shading for metals.
  254. case D3DS::Discreet3DS::Phong :
  255. eShading = aiShadingMode_Phong; break;
  256. case D3DS::Discreet3DS::Metal :
  257. eShading = aiShadingMode_CookTorrance; break;
  258. // FIX to workaround a warning with GCC 4 who complained
  259. // about a missing case Blinn: here - Blinn isn't a valid
  260. // value in the 3DS Loader, it is just needed for ASE
  261. case D3DS::Discreet3DS::Blinn :
  262. eShading = aiShadingMode_Blinn; break;
  263. }
  264. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  265. // DIFFUSE texture
  266. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  267. CopyTexture(mat,oldMat.sTexDiffuse, aiTextureType_DIFFUSE);
  268. // SPECULAR texture
  269. if( oldMat.sTexSpecular.mMapName.length() > 0)
  270. CopyTexture(mat,oldMat.sTexSpecular, aiTextureType_SPECULAR);
  271. // OPACITY texture
  272. if( oldMat.sTexOpacity.mMapName.length() > 0)
  273. CopyTexture(mat,oldMat.sTexOpacity, aiTextureType_OPACITY);
  274. // EMISSIVE texture
  275. if( oldMat.sTexEmissive.mMapName.length() > 0)
  276. CopyTexture(mat,oldMat.sTexEmissive, aiTextureType_EMISSIVE);
  277. // BUMP texture
  278. if( oldMat.sTexBump.mMapName.length() > 0)
  279. CopyTexture(mat,oldMat.sTexBump, aiTextureType_HEIGHT);
  280. // SHININESS texture
  281. if( oldMat.sTexShininess.mMapName.length() > 0)
  282. CopyTexture(mat,oldMat.sTexShininess, aiTextureType_SHININESS);
  283. // REFLECTION texture
  284. if( oldMat.sTexReflective.mMapName.length() > 0)
  285. CopyTexture(mat,oldMat.sTexReflective, aiTextureType_REFLECTION);
  286. // Store the name of the material itself, too
  287. if( oldMat.mName.length()) {
  288. aiString tex;
  289. tex.Set( oldMat.mName);
  290. mat.AddProperty( &tex, AI_MATKEY_NAME);
  291. }
  292. }
  293. // ------------------------------------------------------------------------------------------------
  294. // Split meshes by their materials and generate output aiMesh'es
  295. void Discreet3DSImporter::ConvertMeshes(aiScene* pcOut)
  296. {
  297. std::vector<aiMesh*> avOutMeshes;
  298. avOutMeshes.reserve(mScene->mMeshes.size() * 2);
  299. unsigned int iFaceCnt = 0;
  300. // we need to split all meshes by their materials
  301. for (std::vector<D3DS::Mesh>::iterator i = mScene->mMeshes.begin();
  302. i != mScene->mMeshes.end();++i)
  303. {
  304. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  305. mScene->mMaterials.size()];
  306. unsigned int iNum = 0;
  307. for (std::vector<unsigned int>::const_iterator a = (*i).mFaceMaterials.begin();
  308. a != (*i).mFaceMaterials.end();++a,++iNum)
  309. {
  310. aiSplit[*a].push_back(iNum);
  311. }
  312. // now generate submeshes
  313. for (unsigned int p = 0; p < mScene->mMaterials.size();++p)
  314. {
  315. if (aiSplit[p].size())
  316. {
  317. aiMesh* meshOut = new aiMesh();
  318. meshOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  319. // be sure to setup the correct material index
  320. meshOut->mMaterialIndex = p;
  321. // use the color data as temporary storage
  322. meshOut->mColors[0] = (aiColor4D*)(&*i);
  323. avOutMeshes.push_back(meshOut);
  324. // convert vertices
  325. meshOut->mNumFaces = (unsigned int)aiSplit[p].size();
  326. meshOut->mNumVertices = meshOut->mNumFaces*3;
  327. // allocate enough storage for faces
  328. meshOut->mFaces = new aiFace[meshOut->mNumFaces];
  329. iFaceCnt += meshOut->mNumFaces;
  330. meshOut->mVertices = new aiVector3D[meshOut->mNumVertices];
  331. meshOut->mNormals = new aiVector3D[meshOut->mNumVertices];
  332. if ((*i).mTexCoords.size())
  333. {
  334. meshOut->mTextureCoords[0] = new aiVector3D[meshOut->mNumVertices];
  335. }
  336. for (unsigned int q = 0, base = 0; q < aiSplit[p].size();++q)
  337. {
  338. register unsigned int index = aiSplit[p][q];
  339. aiFace& face = meshOut->mFaces[q];
  340. face.mIndices = new unsigned int[3];
  341. face.mNumIndices = 3;
  342. for (unsigned int a = 0; a < 3;++a,++base)
  343. {
  344. unsigned int idx = (*i).mFaces[index].mIndices[a];
  345. meshOut->mVertices[base] = (*i).mPositions[idx];
  346. meshOut->mNormals [base] = (*i).mNormals[idx];
  347. if ((*i).mTexCoords.size())
  348. meshOut->mTextureCoords[0][base] = (*i).mTexCoords[idx];
  349. face.mIndices[a] = base;
  350. }
  351. }
  352. }
  353. }
  354. delete[] aiSplit;
  355. }
  356. // Copy them to the output array
  357. pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
  358. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  359. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  360. pcOut->mMeshes[a] = avOutMeshes[a];
  361. // We should have at least one face here
  362. if (!iFaceCnt)
  363. throw new ImportErrorException("No faces loaded. The mesh is empty");
  364. }
  365. // ------------------------------------------------------------------------------------------------
  366. // Add a node to the scenegraph and setup its final transformation
  367. void Discreet3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,
  368. D3DS::Node* pcIn, aiMatrix4x4& absTrafo)
  369. {
  370. std::vector<unsigned int> iArray;
  371. iArray.reserve(3);
  372. aiMatrix4x4 abs;
  373. /*if (pcIn->mName == "$$$DUMMY") {
  374. // FIX: Append the "real" name of the dummy to the string
  375. pcIn->mName = "Dummy." + pcIn->mDummyName;
  376. }
  377. else*/ // if (pcIn->mName != "$$$DUMMY")
  378. {
  379. // Find all meshes with the same name as the node
  380. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  381. {
  382. const D3DS::Mesh* pcMesh = (const D3DS::Mesh*)pcSOut->mMeshes[a]->mColors[0];
  383. ai_assert(NULL != pcMesh);
  384. if (pcIn->mName == pcMesh->mName)
  385. iArray.push_back(a);
  386. }
  387. if (!iArray.empty())
  388. {
  389. // The matrix should be identical for all meshes with the
  390. // same name. It HAS to be identical for all meshes .....
  391. aiMatrix4x4 mInv = ((D3DS::Mesh*)pcSOut->mMeshes[iArray[0]]->mColors[0])->mMat;
  392. mInv.Inverse();
  393. const aiVector3D& pivot = pcIn->vPivot;
  394. pcOut->mNumMeshes = (unsigned int)iArray.size();
  395. pcOut->mMeshes = new unsigned int[iArray.size()];
  396. for (unsigned int i = 0;i < iArray.size();++i)
  397. {
  398. const unsigned int iIndex = iArray[i];
  399. aiMesh* const mesh = pcSOut->mMeshes[iIndex];
  400. // Pivot point adjustment
  401. // See: http://www.zfx.info/DisplayThread.php?MID=235690#235690
  402. const aiVector3D* const pvEnd = mesh->mVertices+mesh->mNumVertices;
  403. aiVector3D* pvCurrent = mesh->mVertices;
  404. if(pivot.x || pivot.y || pivot.z)
  405. {
  406. for (;pvCurrent != pvEnd;++pvCurrent)
  407. {
  408. *pvCurrent = mInv * (*pvCurrent);
  409. *pvCurrent -= pivot;
  410. }
  411. }
  412. else
  413. {
  414. for (;pvCurrent != pvEnd;++pvCurrent)
  415. *pvCurrent = mInv * (*pvCurrent);
  416. }
  417. // Setup the mesh index
  418. pcOut->mMeshes[i] = iIndex;
  419. }
  420. }
  421. }
  422. // Setup the name of the node
  423. pcOut->mName.Set(pcIn->mName);
  424. // Now build the transformation matrix of the node
  425. // ROTATION
  426. if (pcIn->aRotationKeys.size()){
  427. pcOut->mTransformation = aiMatrix4x4( pcIn->aRotationKeys[0].mValue.GetMatrix() );
  428. }
  429. else if (pcIn->aCameraRollKeys.size())
  430. {
  431. aiMatrix4x4::RotationZ(AI_DEG_TO_RAD(- pcIn->aCameraRollKeys[0].mValue),
  432. pcOut->mTransformation);
  433. }
  434. // SCALING
  435. aiMatrix4x4& m = pcOut->mTransformation;
  436. if (pcIn->aScalingKeys.size())
  437. {
  438. const aiVector3D& v = pcIn->aScalingKeys[0].mValue;
  439. m.a1 *= v.x; m.b1 *= v.x; m.c1 *= v.x;
  440. m.a2 *= v.y; m.b2 *= v.y; m.c2 *= v.y;
  441. m.a3 *= v.z; m.b3 *= v.z; m.c3 *= v.z;
  442. }
  443. // TRANSLATION
  444. if (pcIn->aPositionKeys.size())
  445. {
  446. const aiVector3D& v = pcIn->aPositionKeys[0].mValue;
  447. m.a4 += v.x;
  448. m.b4 += v.y;
  449. m.c4 += v.z;
  450. }
  451. // Generate animation channels for the node
  452. if (pcIn->aPositionKeys.size() > 1 || pcIn->aRotationKeys.size() > 1 ||
  453. pcIn->aScalingKeys.size() > 1 || pcIn->aCameraRollKeys.size() > 1 ||
  454. pcIn->aTargetPositionKeys.size() > 1)
  455. {
  456. aiAnimation* anim = pcSOut->mAnimations[0];
  457. ai_assert(NULL != anim);
  458. if (pcIn->aCameraRollKeys.size() > 1)
  459. {
  460. DefaultLogger::get()->debug("3DS: Converting camera roll track ...");
  461. // Camera roll keys - in fact they're just rotations
  462. // around the camera's z axis. The angles are given
  463. // in degrees (and they're clockwise).
  464. pcIn->aRotationKeys.resize(pcIn->aCameraRollKeys.size());
  465. for (unsigned int i = 0; i < pcIn->aCameraRollKeys.size();++i)
  466. {
  467. aiQuatKey& q = pcIn->aRotationKeys[i];
  468. aiFloatKey& f = pcIn->aCameraRollKeys[i];
  469. q.mTime = f.mTime;
  470. q.mValue = aiQuaternion(0.f,0.f,AI_DEG_TO_RAD(- f.mValue));
  471. }
  472. }
  473. #if 0
  474. if (pcIn->aTargetPositionKeys.size() > 1)
  475. {
  476. DefaultLogger::get()->debug("3DS: Converting target track ...");
  477. // Camera or spot light - need to convert the separate
  478. // target position channel to our representation
  479. TargetAnimationHelper helper;
  480. if (pcIn->aPositionKeys.empty())
  481. {
  482. // We can just pass zero here ...
  483. helper.SetFixedMainAnimationChannel(aiVector3D());
  484. }
  485. else helper.SetMainAnimationChannel(&pcIn->aPositionKeys);
  486. helper.SetTargetAnimationChannel(&pcIn->aTargetPositionKeys);
  487. // Do the conversion
  488. std::vector<aiVectorKey> distanceTrack;
  489. helper.Process(&distanceTrack);
  490. // Now add a new node as child, name it <ourName>.Target
  491. // and assign the distance track to it. This is that the
  492. // information where the target is and how it moves is
  493. // not lost
  494. D3DS::Node* nd = new D3DS::Node();
  495. pcIn->push_back(nd);
  496. nd->mName = pcIn->mName + ".Target";
  497. aiNodeAnim* nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
  498. nda->mNodeName.Set(nd->mName);
  499. nda->mNumPositionKeys = (unsigned int)distanceTrack.size();
  500. nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
  501. ::memcpy(nda->mPositionKeys,&distanceTrack[0],
  502. sizeof(aiVectorKey)*nda->mNumPositionKeys);
  503. // The target animation is now encoded in the local transformation
  504. // matrix of the camera, so we must clear the corresponding
  505. // fields in aiCamera or aiLight.
  506. for (unsigned int n = 0; n < pcSOut->mNumCameras;++n) {
  507. if (pcSOut->mCameras[n]->mName == pcOut->mName) {
  508. pcSOut->mCameras[n]->mLookAt = aiVector3D(0.f,0.f,1.f);
  509. }
  510. }
  511. for (unsigned int n = 0; n < pcSOut->mNumLights;++n) {
  512. if (pcSOut->mLights[n]->mName == pcOut->mName) {
  513. pcSOut->mLights[n]->mDirection = aiVector3D(0.f,0.f,1.f);
  514. }
  515. }
  516. }
  517. #endif
  518. // Allocate a new node anim and setup its name
  519. aiNodeAnim* nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
  520. nda->mNodeName.Set(pcIn->mName);
  521. // POSITION keys
  522. if (pcIn->aPositionKeys.size() > 0)
  523. {
  524. nda->mNumPositionKeys = (unsigned int)pcIn->aPositionKeys.size();
  525. nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
  526. ::memcpy(nda->mPositionKeys,&pcIn->aPositionKeys[0],
  527. sizeof(aiVectorKey)*nda->mNumPositionKeys);
  528. }
  529. // ROTATION keys
  530. if (pcIn->aRotationKeys.size() > 0)
  531. {
  532. nda->mNumRotationKeys = (unsigned int)pcIn->aRotationKeys.size();
  533. nda->mRotationKeys = new aiQuatKey[nda->mNumRotationKeys];
  534. // Rotations are quaternion offsets
  535. aiQuaternion abs;
  536. for (unsigned int n = 0; n < nda->mNumRotationKeys;++n)
  537. {
  538. const aiQuatKey& q = pcIn->aRotationKeys[n];
  539. abs = (n ? abs * q.mValue : q.mValue);
  540. nda->mRotationKeys[n].mTime = q.mTime;
  541. nda->mRotationKeys[n].mValue = abs.Normalize();
  542. }
  543. }
  544. // SCALING keys
  545. if (pcIn->aScalingKeys.size() > 0)
  546. {
  547. nda->mNumScalingKeys = (unsigned int)pcIn->aScalingKeys.size();
  548. nda->mScalingKeys = new aiVectorKey[nda->mNumScalingKeys];
  549. ::memcpy(nda->mScalingKeys,&pcIn->aScalingKeys[0],
  550. sizeof(aiVectorKey)*nda->mNumScalingKeys);
  551. }
  552. }
  553. // Allocate storage for children
  554. pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
  555. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  556. // Recursively process all children
  557. const unsigned int size = pcIn->mChildren.size();
  558. for (unsigned int i = 0; i < size;++i)
  559. {
  560. pcOut->mChildren[i] = new aiNode();
  561. pcOut->mChildren[i]->mParent = pcOut;
  562. AddNodeToGraph(pcSOut,pcOut->mChildren[i],pcIn->mChildren[i],abs);
  563. }
  564. }
  565. // ------------------------------------------------------------------------------------------------
  566. // Find out how many node animation channels we'll have finally
  567. void CountTracks(D3DS::Node* node, unsigned int& cnt)
  568. {
  569. //////////////////////////////////////////////////////////////////////////////
  570. // We will never generate more than one channel for a node, so
  571. // this is rather easy here.
  572. if (node->aPositionKeys.size() > 1 || node->aRotationKeys.size() > 1 ||
  573. node->aScalingKeys.size() > 1 || node->aCameraRollKeys.size() > 1 ||
  574. node->aTargetPositionKeys.size() > 1)
  575. {
  576. ++cnt;
  577. // account for the additional channel for the camera/spotlight target position
  578. if (node->aTargetPositionKeys.size() > 1)++cnt;
  579. }
  580. // Recursively process all children
  581. for (unsigned int i = 0; i < node->mChildren.size();++i)
  582. CountTracks(node->mChildren[i],cnt);
  583. }
  584. // ------------------------------------------------------------------------------------------------
  585. // Generate the output node graph
  586. void Discreet3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  587. {
  588. pcOut->mRootNode = new aiNode();
  589. if (0 == mRootNode->mChildren.size())
  590. {
  591. //////////////////////////////////////////////////////////////////////////////
  592. // It seems the file is so fucked up that it has not even a hierarchy.
  593. // generate a flat hiearachy which looks like this:
  594. //
  595. // ROOT_NODE
  596. // |
  597. // ----------------------------------------
  598. // | | | | |
  599. // MESH_0 MESH_1 MESH_2 ... MESH_N CAMERA_0 ....
  600. //
  601. DefaultLogger::get()->warn("No hierarchy information has been found in the file. ");
  602. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes +
  603. mScene->mCameras.size() + mScene->mLights.size();
  604. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mRootNode->mNumChildren ];
  605. pcOut->mRootNode->mName.Set("<3DSDummyRoot>");
  606. // Build dummy nodes for all meshes
  607. unsigned int a = 0;
  608. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i,++a)
  609. {
  610. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  611. pcNode->mParent = pcOut->mRootNode;
  612. pcNode->mMeshes = new unsigned int[1];
  613. pcNode->mMeshes[0] = i;
  614. pcNode->mNumMeshes = 1;
  615. // Build a name for the node
  616. pcNode->mName.length = sprintf(pcNode->mName.data,"3DSMesh_%i",i);
  617. }
  618. // Build dummy nodes for all cameras
  619. for (unsigned int i = 0; i < (unsigned int )mScene->mCameras.size();++i,++a)
  620. {
  621. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  622. pcNode->mParent = pcOut->mRootNode;
  623. // Build a name for the node
  624. pcNode->mName = mScene->mCameras[i]->mName;
  625. }
  626. // Build dummy nodes for all lights
  627. for (unsigned int i = 0; i < (unsigned int )mScene->mLights.size();++i,++a)
  628. {
  629. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  630. pcNode->mParent = pcOut->mRootNode;
  631. // Build a name for the node
  632. pcNode->mName = mScene->mLights[i]->mName;
  633. }
  634. }
  635. else
  636. {
  637. // First of all: find out how many scaling, rotation and translation
  638. // animation tracks we'll have afterwards
  639. unsigned int numChannel = 0;
  640. CountTracks(mRootNode,numChannel);
  641. if (numChannel)
  642. {
  643. // Allocate a primary animation channel
  644. pcOut->mNumAnimations = 1;
  645. pcOut->mAnimations = new aiAnimation*[1];
  646. aiAnimation* anim = pcOut->mAnimations[0] = new aiAnimation();
  647. anim->mName.Set("3DSMasterAnim");
  648. // Allocate enough storage for all node animation channels,
  649. // but don't set the mNumChannels member - we'll use it to
  650. // index into the array
  651. anim->mChannels = new aiNodeAnim*[numChannel];
  652. }
  653. aiMatrix4x4 m;
  654. AddNodeToGraph(pcOut, pcOut->mRootNode, mRootNode,m);
  655. }
  656. // We used the first vertex color set to store some
  657. // temporary values so we need to cleanup here
  658. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  659. pcOut->mMeshes[a]->mColors[0] = NULL;
  660. // if the root node has only one child ... set the child as root node
  661. if (1 == pcOut->mRootNode->mNumChildren)
  662. {
  663. aiNode* pcOld = pcOut->mRootNode;
  664. pcOut->mRootNode = pcOut->mRootNode->mChildren[0];
  665. pcOut->mRootNode->mParent = NULL;
  666. pcOld->mChildren[0] = NULL;
  667. delete pcOld;
  668. }
  669. // If the root node is unnamed name it "<3DSRoot>"
  670. if (::strstr( pcOut->mRootNode->mName.data, "UNNAMED" ) ||
  671. pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')
  672. {
  673. pcOut->mRootNode->mName.Set("<3DSRoot>");
  674. }
  675. }
  676. // ------------------------------------------------------------------------------------------------
  677. // Convert all meshes in the scene and generate the final output scene.
  678. void Discreet3DSImporter::ConvertScene(aiScene* pcOut)
  679. {
  680. // Allocate enough storage for all output materials
  681. pcOut->mNumMaterials = (unsigned int)mScene->mMaterials.size();
  682. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  683. // ... and convert the 3DS materials to aiMaterial's
  684. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  685. {
  686. MaterialHelper* pcNew = new MaterialHelper();
  687. ConvertMaterial(mScene->mMaterials[i],*pcNew);
  688. pcOut->mMaterials[i] = pcNew;
  689. }
  690. // Generate the output mesh list
  691. ConvertMeshes(pcOut);
  692. // Now copy all light sources to the output scene
  693. pcOut->mNumLights = (unsigned int)mScene->mLights.size();
  694. if (pcOut->mNumLights)
  695. {
  696. pcOut->mLights = new aiLight*[pcOut->mNumLights];
  697. ::memcpy(pcOut->mLights,&mScene->mLights[0],sizeof(void*)*pcOut->mNumLights);
  698. }
  699. // Now copy all cameras to the output scene
  700. pcOut->mNumCameras = (unsigned int)mScene->mCameras.size();
  701. if (pcOut->mNumCameras)
  702. {
  703. pcOut->mCameras = new aiCamera*[pcOut->mNumCameras];
  704. ::memcpy(pcOut->mCameras,&mScene->mCameras[0],sizeof(void*)*pcOut->mNumCameras);
  705. }
  706. }
  707. #endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER