PretransformVertices.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 "PretransformVertices" post processing step
  35. */
  36. #include "AssimpPCH.h"
  37. #include "PretransformVertices.h"
  38. using namespace Assimp;
  39. // some array offsets
  40. #define AI_PTVS_VERTEX 0x0
  41. #define AI_PTVS_FACE 0x1
  42. // ------------------------------------------------------------------------------------------------
  43. // Constructor to be privately used by Importer
  44. PretransformVertices::PretransformVertices()
  45. {
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. PretransformVertices::~PretransformVertices()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Returns whether the processing step is present in the given flag field.
  55. bool PretransformVertices::IsActive( unsigned int pFlags) const
  56. {
  57. return (pFlags & aiProcess_PreTransformVertices) != 0;
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Count the number of nodes
  61. unsigned int CountNodes( aiNode* pcNode )
  62. {
  63. unsigned int iRet = 1;
  64. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  65. {
  66. iRet += CountNodes(pcNode->mChildren[i]);
  67. }
  68. return iRet;
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Get a bitwise combination identifying the vertex format of a mesh
  72. unsigned int GetMeshVFormat(aiMesh* pcMesh)
  73. {
  74. // the vertex format is stored in aiMesh::mBones for later retrieval.
  75. // there isn't a good reason to compute it a few hundred times
  76. // from scratch. The pointer is unused as animations are lost
  77. // during PretransformVertices.
  78. if (pcMesh->mBones)
  79. return (unsigned int)(unsigned long)pcMesh->mBones;
  80. ai_assert(NULL != pcMesh->mVertices);
  81. // FIX: the hash may never be 0. Otherwise a comparison against
  82. // nullptr could be successful
  83. unsigned int iRet = 1;
  84. // normals
  85. if (pcMesh->HasNormals())iRet |= 0x2;
  86. // tangents and bitangents
  87. if (pcMesh->HasTangentsAndBitangents())iRet |= 0x4;
  88. // texture coordinates
  89. unsigned int p = 0;
  90. ai_assert(8 >= AI_MAX_NUMBER_OF_TEXTURECOORDS);
  91. while (pcMesh->HasTextureCoords(p))
  92. {
  93. iRet |= (0x100 << p);
  94. if (3 == pcMesh->mNumUVComponents[p])
  95. iRet |= (0x10000 << p);
  96. ++p;
  97. }
  98. // vertex colors
  99. p = 0;
  100. ai_assert(8 >= AI_MAX_NUMBER_OF_COLOR_SETS);
  101. while (pcMesh->HasVertexColors(p))iRet |= (0x1000000 << p++);
  102. // store the value for later use
  103. pcMesh->mBones = (aiBone**)(unsigned long)iRet;
  104. return iRet;
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. // Count the number of vertices in the whole scene and a given
  108. // material index
  109. void CountVerticesAndFaces( aiScene* pcScene, aiNode* pcNode, unsigned int iMat,
  110. unsigned int iVFormat, unsigned int* piFaces, unsigned int* piVertices)
  111. {
  112. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  113. {
  114. aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ];
  115. if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh))
  116. {
  117. *piVertices += pcMesh->mNumVertices;
  118. *piFaces += pcMesh->mNumFaces;
  119. }
  120. }
  121. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  122. {
  123. CountVerticesAndFaces(pcScene,pcNode->mChildren[i],iMat,
  124. iVFormat,piFaces,piVertices);
  125. }
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. // Collect vertex/face data
  129. void CollectData( aiScene* pcScene, aiNode* pcNode, unsigned int iMat,
  130. unsigned int iVFormat, aiMesh* pcMeshOut,
  131. unsigned int aiCurrent[2])
  132. {
  133. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  134. {
  135. aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ];
  136. if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh))
  137. {
  138. // copy positions, transform them to worldspace
  139. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  140. {
  141. pcMeshOut->mVertices[aiCurrent[AI_PTVS_VERTEX]+n] =
  142. pcNode->mTransformation * pcMesh->mVertices[n];
  143. }
  144. if (iVFormat & 0x2)
  145. {
  146. aiMatrix4x4 mWorldIT = pcNode->mTransformation;
  147. mWorldIT.Inverse().Transpose();
  148. // TODO: implement Inverse() for aiMatrix3x3
  149. aiMatrix3x3 m = aiMatrix3x3(mWorldIT);
  150. // copy normals, transform them to worldspace
  151. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  152. {
  153. pcMeshOut->mNormals[aiCurrent[AI_PTVS_VERTEX]+n] =
  154. m * pcMesh->mNormals[n];
  155. }
  156. }
  157. if (iVFormat & 0x4)
  158. {
  159. // copy tangents
  160. memcpy(pcMeshOut->mTangents + aiCurrent[AI_PTVS_VERTEX],
  161. pcMesh->mTangents,
  162. pcMesh->mNumVertices * sizeof(aiVector3D));
  163. // copy bitangents
  164. memcpy(pcMeshOut->mBitangents + aiCurrent[AI_PTVS_VERTEX],
  165. pcMesh->mBitangents,
  166. pcMesh->mNumVertices * sizeof(aiVector3D));
  167. }
  168. unsigned int p = 0;
  169. while (iVFormat & (0x100 << p))
  170. {
  171. // copy texture coordinates
  172. memcpy(pcMeshOut->mTextureCoords[p] + aiCurrent[AI_PTVS_VERTEX],
  173. pcMesh->mTextureCoords[p],
  174. pcMesh->mNumVertices * sizeof(aiVector3D));
  175. ++p;
  176. }
  177. p = 0;
  178. while (iVFormat & (0x1000000 << p))
  179. {
  180. // copy vertex colors
  181. memcpy(pcMeshOut->mColors[p] + aiCurrent[AI_PTVS_VERTEX],
  182. pcMesh->mColors[p],
  183. pcMesh->mNumVertices * sizeof(aiColor4D));
  184. ++p;
  185. }
  186. // now we need to copy all faces
  187. // since we will delete the source mesh afterwards,
  188. // we don't need to reallocate the array of indices
  189. for (unsigned int planck = 0;planck<pcMesh->mNumFaces;++planck)
  190. {
  191. pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE]+planck].mNumIndices =
  192. pcMesh->mFaces[planck].mNumIndices;
  193. unsigned int* pi = pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE]+planck].
  194. mIndices = pcMesh->mFaces[planck].mIndices;
  195. // offset all vrtex indices
  196. for (unsigned int hahn = 0; hahn < pcMesh->mFaces[planck].mNumIndices;++hahn)
  197. {
  198. pi[hahn] += aiCurrent[AI_PTVS_VERTEX];
  199. }
  200. // just make sure the array won't be deleted by the
  201. // aiFace destructor ...
  202. pcMesh->mFaces[planck].mIndices = NULL;
  203. // FIX: update the mPrimitiveTypes member of the mesh
  204. switch (pcMesh->mFaces[planck].mNumIndices)
  205. {
  206. case 0x1:
  207. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_POINT;
  208. break;
  209. case 0x2:
  210. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_LINE;
  211. break;
  212. case 0x3:
  213. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  214. break;
  215. default:
  216. pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  217. break;
  218. };
  219. }
  220. aiCurrent[AI_PTVS_VERTEX] += pcMesh->mNumVertices;
  221. aiCurrent[AI_PTVS_FACE] += pcMesh->mNumFaces;
  222. }
  223. }
  224. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  225. {
  226. CollectData(pcScene,pcNode->mChildren[i],iMat,
  227. iVFormat,pcMeshOut,aiCurrent);
  228. }
  229. }
  230. // ------------------------------------------------------------------------------------------------
  231. // Get a list of all vertex formats that occur for a given material index
  232. // The output list contains duplicate elements
  233. void GetVFormatList( aiScene* pcScene, unsigned int iMat,
  234. std::list<unsigned int>& aiOut)
  235. {
  236. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  237. {
  238. aiMesh* pcMesh = pcScene->mMeshes[ i ];
  239. if (iMat == pcMesh->mMaterialIndex)
  240. {
  241. aiOut.push_back(GetMeshVFormat(pcMesh));
  242. }
  243. }
  244. }
  245. // ------------------------------------------------------------------------------------------------
  246. // Compute the absolute transformation matrices of each node
  247. void ComputeAbsoluteTransform( aiNode* pcNode )
  248. {
  249. if (pcNode->mParent)
  250. {
  251. pcNode->mTransformation = pcNode->mParent->mTransformation*pcNode->mTransformation;
  252. }
  253. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  254. {
  255. ComputeAbsoluteTransform(pcNode->mChildren[i]);
  256. }
  257. }
  258. // ------------------------------------------------------------------------------------------------
  259. // Executes the post processing step on the given imported data.
  260. void PretransformVertices::Execute( aiScene* pScene)
  261. {
  262. DefaultLogger::get()->debug("PretransformVerticesProcess begin");
  263. const unsigned int iOldMeshes = pScene->mNumMeshes;
  264. const unsigned int iOldAnimationChannels = pScene->mNumAnimations;
  265. const unsigned int iOldNodes = CountNodes(pScene->mRootNode);
  266. // first compute absolute transformation matrices for all nodes
  267. ComputeAbsoluteTransform(pScene->mRootNode);
  268. // delete aiMesh::mBones for all meshes. The bones are
  269. // removed during this step and we need the pointer as
  270. // temporary storage
  271. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  272. {
  273. aiMesh* mesh = pScene->mMeshes[i];
  274. for (unsigned int a = 0; a < mesh->mNumBones;++a)
  275. delete mesh->mBones[a];
  276. delete[] mesh->mBones;
  277. mesh->mBones = NULL;
  278. }
  279. // now build a list of output meshes
  280. std::vector<aiMesh*> apcOutMeshes;
  281. apcOutMeshes.reserve(pScene->mNumMaterials<<1u);
  282. std::list<unsigned int> aiVFormats;
  283. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  284. {
  285. // get the list of all vertex formats for this material
  286. aiVFormats.clear();
  287. GetVFormatList(pScene,i,aiVFormats);
  288. aiVFormats.sort();
  289. aiVFormats.unique();
  290. for (std::list<unsigned int>::const_iterator
  291. j = aiVFormats.begin();
  292. j != aiVFormats.end();++j)
  293. {
  294. unsigned int iVertices = 0;
  295. unsigned int iFaces = 0;
  296. CountVerticesAndFaces(pScene,pScene->mRootNode,i,*j,&iFaces,&iVertices);
  297. if (iFaces && iVertices)
  298. {
  299. apcOutMeshes.push_back(new aiMesh());
  300. aiMesh* pcMesh = apcOutMeshes.back();
  301. pcMesh->mNumFaces = iFaces;
  302. pcMesh->mNumVertices = iVertices;
  303. pcMesh->mFaces = new aiFace[iFaces];
  304. pcMesh->mVertices = new aiVector3D[iVertices];
  305. pcMesh->mMaterialIndex = i;
  306. if ((*j) & 0x2)pcMesh->mNormals = new aiVector3D[iVertices];
  307. if ((*j) & 0x4)
  308. {
  309. pcMesh->mTangents = new aiVector3D[iVertices];
  310. pcMesh->mBitangents = new aiVector3D[iVertices];
  311. }
  312. iFaces = 0;
  313. while ((*j) & (0x100 << iFaces))
  314. {
  315. pcMesh->mTextureCoords[iFaces] = new aiVector3D[iVertices];
  316. if ((*j) & (0x10000 << iFaces))pcMesh->mNumUVComponents[iFaces] = 3;
  317. else pcMesh->mNumUVComponents[iFaces] = 2;
  318. iFaces++;
  319. }
  320. iFaces = 0;
  321. while ((*j) & (0x1000000 << iFaces))
  322. pcMesh->mColors[iFaces++] = new aiColor4D[iVertices];
  323. // fill the mesh ...
  324. unsigned int aiTemp[2] = {0,0};
  325. CollectData(pScene,pScene->mRootNode,i,*j,pcMesh,aiTemp);
  326. }
  327. }
  328. }
  329. // remove all animations from the scene
  330. for (unsigned int i = 0; i < pScene->mNumAnimations;++i)
  331. delete pScene->mAnimations[i];
  332. delete[] pScene->mAnimations;
  333. pScene->mAnimations = NULL;
  334. pScene->mNumAnimations = 0;
  335. // now delete all meshes in the scene and build a new mesh list
  336. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  337. {
  338. pScene->mMeshes[i]->mBones = NULL;
  339. delete pScene->mMeshes[i];
  340. // invalidate the contents of the old mesh array. We will most
  341. // likely have less output meshes now, so the last entries of
  342. // the mesh array are not overridden. We set them to NULL to
  343. // make sure the developer gets notified when his application
  344. // attempts to access these fields ...
  345. pScene->mMeshes[i] = NULL;
  346. }
  347. // If no meshes are referenced in the node graph it is
  348. // possible that we get no output meshes. However, this
  349. // is OK if we had no input meshes, too
  350. if (apcOutMeshes.empty())
  351. {
  352. if (pScene->mNumMeshes)
  353. {
  354. throw new ImportErrorException("No output meshes: all meshes are orphaned "
  355. "and have no node references");
  356. }
  357. }
  358. else
  359. {
  360. // It is impossible that we have more output meshes than
  361. // input meshes, so we can easily reuse the old mesh array
  362. pScene->mNumMeshes = (unsigned int)apcOutMeshes.size();
  363. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  364. pScene->mMeshes[i] = apcOutMeshes[i];
  365. }
  366. // --- we need to keep all cameras and lights
  367. for (unsigned int i = 0; i < pScene->mNumCameras;++i)
  368. {
  369. aiCamera* cam = pScene->mCameras[i];
  370. const aiNode* nd = pScene->mRootNode->FindNode(cam->mName);
  371. ai_assert(NULL != nd);
  372. // multiply all properties of the camera with the absolute
  373. // transformation of the corresponding node
  374. cam->mPosition = nd->mTransformation * cam->mPosition;
  375. cam->mLookAt = aiMatrix3x3( nd->mTransformation ) * cam->mLookAt;
  376. cam->mUp = aiMatrix3x3( nd->mTransformation ) * cam->mUp;
  377. }
  378. for (unsigned int i = 0; i < pScene->mNumLights;++i)
  379. {
  380. aiLight* l = pScene->mLights[i];
  381. const aiNode* nd = pScene->mRootNode->FindNode(l->mName);
  382. ai_assert(NULL != nd);
  383. // multiply all properties of the camera with the absolute
  384. // transformation of the corresponding node
  385. l->mPosition = nd->mTransformation * l->mPosition;
  386. l->mDirection = aiMatrix3x3( nd->mTransformation ) * l->mDirection;
  387. }
  388. // now delete all nodes in the scene and build a new
  389. // flat node graph with a root node and some level 1 children
  390. delete pScene->mRootNode;
  391. pScene->mRootNode = new aiNode();
  392. pScene->mRootNode->mName.Set("<dummy_root>");
  393. if (1 == pScene->mNumMeshes && !pScene->mNumLights && !pScene->mNumCameras)
  394. {
  395. pScene->mRootNode->mNumMeshes = 1;
  396. pScene->mRootNode->mMeshes = new unsigned int[1];
  397. pScene->mRootNode->mMeshes[0] = 0;
  398. }
  399. else
  400. {
  401. pScene->mRootNode->mNumChildren = pScene->mNumMeshes+pScene->mNumLights+pScene->mNumCameras;
  402. aiNode** nodes = pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
  403. // generate mesh nodes
  404. for (unsigned int i = 0; i < pScene->mNumMeshes;++i,++nodes)
  405. {
  406. aiNode* pcNode = *nodes = new aiNode();
  407. pcNode->mParent = pScene->mRootNode;
  408. pcNode->mName.length = ::sprintf(pcNode->mName.data,"mesh_%i",i);
  409. // setup mesh indices
  410. pcNode->mNumMeshes = 1;
  411. pcNode->mMeshes = new unsigned int[1];
  412. pcNode->mMeshes[0] = i;
  413. }
  414. // generate light nodes
  415. for (unsigned int i = 0; i < pScene->mNumLights;++i,++nodes)
  416. {
  417. aiNode* pcNode = *nodes = new aiNode();
  418. pcNode->mParent = pScene->mRootNode;
  419. pcNode->mName.length = ::sprintf(pcNode->mName.data,"light_%i",i);
  420. pScene->mLights[i]->mName = pcNode->mName;
  421. }
  422. // generate camera nodes
  423. for (unsigned int i = 0; i < pScene->mNumCameras;++i,++nodes)
  424. {
  425. aiNode* pcNode = *nodes = new aiNode();
  426. pcNode->mParent = pScene->mRootNode;
  427. pcNode->mName.length = ::sprintf(pcNode->mName.data,"cam_%i",i);
  428. pScene->mCameras[i]->mName = pcNode->mName;
  429. }
  430. }
  431. // print statistics
  432. if (!DefaultLogger::isNullLogger())
  433. {
  434. char buffer[4096];
  435. DefaultLogger::get()->debug("PretransformVerticesProcess finished");
  436. ::sprintf(buffer,"Removed %i nodes and %i animation channels (%i output nodes)",
  437. iOldNodes,iOldAnimationChannels,CountNodes(pScene->mRootNode));
  438. DefaultLogger::get()->info(buffer);
  439. ::sprintf(buffer,"Kept %i lights and %i cameras",
  440. pScene->mNumLights,pScene->mNumCameras);
  441. DefaultLogger::get()->info(buffer);
  442. ::sprintf(buffer,"Moved %i meshes to WCS (number of output meshes: %i)",
  443. iOldMeshes,pScene->mNumMeshes);
  444. DefaultLogger::get()->info(buffer);
  445. }
  446. return;
  447. }