PretransformVertices.cpp 16 KB

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