2
0

ScenePreprocessor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "ScenePreprocessor.h"
  34. #include <assimp/ai_assert.h>
  35. #include <assimp/scene.h>
  36. #include <assimp/DefaultLogger.hpp>
  37. using namespace Assimp;
  38. // ---------------------------------------------------------------------------------------------
  39. void ScenePreprocessor::ProcessScene() {
  40. ai_assert(scene != nullptr);
  41. // Process all meshes
  42. for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
  43. ProcessMesh(scene->mMeshes[i]);
  44. // - nothing to do for nodes for the moment
  45. // - nothing to do for textures for the moment
  46. // - nothing to do for lights for the moment
  47. // - nothing to do for cameras for the moment
  48. // Process all animations
  49. for (unsigned int i = 0; i < scene->mNumAnimations; ++i)
  50. ProcessAnimation(scene->mAnimations[i]);
  51. // Generate a default material if none was specified
  52. if (!scene->mNumMaterials && scene->mNumMeshes) {
  53. scene->mMaterials = new aiMaterial *[2];
  54. aiMaterial *helper;
  55. aiString name;
  56. scene->mMaterials[scene->mNumMaterials] = helper = new aiMaterial();
  57. aiColor3D clr(0.6f, 0.6f, 0.6f);
  58. helper->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
  59. // setup the default name to make this material identifiable
  60. name.Set(AI_DEFAULT_MATERIAL_NAME);
  61. helper->AddProperty(&name, AI_MATKEY_NAME);
  62. ASSIMP_LOG_DEBUG("ScenePreprocessor: Adding default material \'" AI_DEFAULT_MATERIAL_NAME "\'");
  63. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  64. scene->mMeshes[i]->mMaterialIndex = scene->mNumMaterials;
  65. }
  66. scene->mNumMaterials++;
  67. }
  68. }
  69. // ---------------------------------------------------------------------------------------------
  70. void ScenePreprocessor::ProcessMesh(aiMesh *mesh) {
  71. // If aiMesh::mNumUVComponents is *not* set assign the default value of 2
  72. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  73. if (!mesh->mTextureCoords[i]) {
  74. mesh->mNumUVComponents[i] = 0;
  75. } else {
  76. if (!mesh->mNumUVComponents[i]) {
  77. mesh->mNumUVComponents[i] = 2;
  78. }
  79. aiVector3D *p = mesh->mTextureCoords[i], *end = p + mesh->mNumVertices;
  80. // Ensure unused components are zeroed. This will make 1D texture channels work
  81. // as if they were 2D channels .. just in case an application doesn't handle
  82. // this case
  83. if (2 == mesh->mNumUVComponents[i]) {
  84. for (; p != end; ++p) {
  85. p->z = 0.f;
  86. }
  87. } else if (1 == mesh->mNumUVComponents[i]) {
  88. for (; p != end; ++p) {
  89. p->z = p->y = 0.f;
  90. }
  91. } else if (3 == mesh->mNumUVComponents[i]) {
  92. // Really 3D coordinates? Check whether the third coordinate is != 0 for at least one element
  93. for (; p != end; ++p) {
  94. if (p->z != 0) {
  95. break;
  96. }
  97. }
  98. if (p == end) {
  99. ASSIMP_LOG_WARN("ScenePreprocessor: UVs are declared to be 3D but they're obviously not. Reverting to 2D.");
  100. mesh->mNumUVComponents[i] = 2;
  101. }
  102. }
  103. }
  104. }
  105. // If the information which primitive types are there in the
  106. // mesh is currently not available, compute it.
  107. if (!mesh->mPrimitiveTypes) {
  108. for (unsigned int a = 0; a < mesh->mNumFaces; ++a) {
  109. aiFace &face = mesh->mFaces[a];
  110. switch (face.mNumIndices) {
  111. case 3u:
  112. mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  113. break;
  114. case 2u:
  115. mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  116. break;
  117. case 1u:
  118. mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  119. break;
  120. default:
  121. mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  122. break;
  123. }
  124. }
  125. }
  126. // If tangents and normals are given but no bitangents compute them
  127. if (mesh->mTangents && mesh->mNormals && !mesh->mBitangents) {
  128. mesh->mBitangents = new aiVector3D[mesh->mNumVertices];
  129. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  130. mesh->mBitangents[i] = mesh->mNormals[i] ^ mesh->mTangents[i];
  131. }
  132. }
  133. }
  134. // ---------------------------------------------------------------------------------------------
  135. void ScenePreprocessor::ProcessAnimation(aiAnimation *anim) {
  136. double first = 10e10, last = -10e10;
  137. for (unsigned int i = 0; i < anim->mNumChannels; ++i) {
  138. aiNodeAnim *channel = anim->mChannels[i];
  139. // If the exact duration of the animation is not given
  140. // compute it now.
  141. if (anim->mDuration == -1.) {
  142. // Position keys
  143. for (unsigned int j = 0; j < channel->mNumPositionKeys; ++j) {
  144. aiVectorKey &key = channel->mPositionKeys[j];
  145. first = std::min(first, key.mTime);
  146. last = std::max(last, key.mTime);
  147. }
  148. // Scaling keys
  149. for (unsigned int j = 0; j < channel->mNumScalingKeys; ++j) {
  150. aiVectorKey &key = channel->mScalingKeys[j];
  151. first = std::min(first, key.mTime);
  152. last = std::max(last, key.mTime);
  153. }
  154. // Rotation keys
  155. for (unsigned int j = 0; j < channel->mNumRotationKeys; ++j) {
  156. aiQuatKey &key = channel->mRotationKeys[j];
  157. first = std::min(first, key.mTime);
  158. last = std::max(last, key.mTime);
  159. }
  160. }
  161. // Check whether the animation channel has no rotation
  162. // or position tracks. In this case we generate a dummy
  163. // track from the information we have in the transformation
  164. // matrix of the corresponding node.
  165. if (!channel->mNumRotationKeys || !channel->mNumPositionKeys || !channel->mNumScalingKeys) {
  166. // Find the node that belongs to this animation
  167. aiNode *node = scene->mRootNode->FindNode(channel->mNodeName);
  168. if (node) // ValidateDS will complain later if 'node' is nullptr
  169. {
  170. // Decompose the transformation matrix of the node
  171. aiVector3D scaling, position;
  172. aiQuaternion rotation;
  173. node->mTransformation.Decompose(scaling, rotation, position);
  174. // No rotation keys? Generate a dummy track
  175. if (!channel->mNumRotationKeys) {
  176. if (channel->mRotationKeys) {
  177. delete[] channel->mRotationKeys;
  178. channel->mRotationKeys = nullptr;
  179. }
  180. ai_assert(!channel->mRotationKeys);
  181. channel->mNumRotationKeys = 1;
  182. channel->mRotationKeys = new aiQuatKey[1];
  183. aiQuatKey &q = channel->mRotationKeys[0];
  184. q.mTime = 0.;
  185. q.mValue = rotation;
  186. ASSIMP_LOG_VERBOSE_DEBUG("ScenePreprocessor: Dummy rotation track has been generated");
  187. } else {
  188. ai_assert(channel->mRotationKeys);
  189. }
  190. // No scaling keys? Generate a dummy track
  191. if (!channel->mNumScalingKeys) {
  192. if (channel->mScalingKeys) {
  193. delete[] channel->mScalingKeys;
  194. channel->mScalingKeys = nullptr;
  195. }
  196. ai_assert(!channel->mScalingKeys);
  197. channel->mNumScalingKeys = 1;
  198. channel->mScalingKeys = new aiVectorKey[1];
  199. aiVectorKey &q = channel->mScalingKeys[0];
  200. q.mTime = 0.;
  201. q.mValue = scaling;
  202. ASSIMP_LOG_VERBOSE_DEBUG("ScenePreprocessor: Dummy scaling track has been generated");
  203. } else {
  204. ai_assert(channel->mScalingKeys);
  205. }
  206. // No position keys? Generate a dummy track
  207. if (!channel->mNumPositionKeys) {
  208. if (channel->mPositionKeys) {
  209. delete[] channel->mPositionKeys;
  210. channel->mPositionKeys = nullptr;
  211. }
  212. ai_assert(!channel->mPositionKeys);
  213. channel->mNumPositionKeys = 1;
  214. channel->mPositionKeys = new aiVectorKey[1];
  215. aiVectorKey &q = channel->mPositionKeys[0];
  216. q.mTime = 0.;
  217. q.mValue = position;
  218. ASSIMP_LOG_VERBOSE_DEBUG("ScenePreprocessor: Dummy position track has been generated");
  219. } else {
  220. ai_assert(channel->mPositionKeys);
  221. }
  222. }
  223. }
  224. }
  225. if (anim->mDuration == -1.) {
  226. ASSIMP_LOG_VERBOSE_DEBUG("ScenePreprocessor: Setting animation duration");
  227. anim->mDuration = last - std::min(first, 0.);
  228. }
  229. }