25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScenePreprocessor.cpp 8.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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 "AssimpPCH.h"
  34. #include "ScenePreprocessor.h"
  35. using namespace Assimp;
  36. // ---------------------------------------------------------------------------------------------
  37. void ScenePreprocessor::ProcessScene ()
  38. {
  39. ai_assert(scene != NULL);
  40. // Process all meshes
  41. for (unsigned int i = 0; i < scene->mNumMeshes;++i)
  42. ProcessMesh(scene->mMeshes[i]);
  43. // - nothing to do for nodes for the moment
  44. // - nothing to do for textures for the moment
  45. // - nothing to do for lights for the moment
  46. // - nothing to do for cameras for the moment
  47. // Process all animations
  48. for (unsigned int i = 0; i < scene->mNumAnimations;++i)
  49. ProcessAnimation(scene->mAnimations[i]);
  50. // Generate a default material if none was specified
  51. if (!scene->mNumMaterials && scene->mNumMeshes) {
  52. scene->mMaterials = new aiMaterial*[2];
  53. aiMaterial* helper;
  54. aiString name;
  55. scene->mMaterials[scene->mNumMaterials] = helper = new aiMaterial();
  56. aiColor3D clr(0.6f,0.6f,0.6f);
  57. helper->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  58. // setup the default name to make this material identifiable
  59. name.Set(AI_DEFAULT_MATERIAL_NAME);
  60. helper->AddProperty(&name,AI_MATKEY_NAME);
  61. DefaultLogger::get()->debug("ScenePreprocessor: Adding default material \'" AI_DEFAULT_MATERIAL_NAME "\'");
  62. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  63. scene->mMeshes[i]->mMaterialIndex = scene->mNumMaterials;
  64. }
  65. scene->mNumMaterials++;
  66. }
  67. }
  68. // ---------------------------------------------------------------------------------------------
  69. void ScenePreprocessor::ProcessMesh (aiMesh* mesh)
  70. {
  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. aiVector3D* p = mesh->mTextureCoords[i], *end = p+mesh->mNumVertices;
  79. // Ensure unsued components are zeroed. This will make 1D texture channels work
  80. // as if they were 2D channels .. just in case an application doesn't handle
  81. // this case
  82. if (2 == mesh->mNumUVComponents[i]) {
  83. for (; p != end; ++p)
  84. p->z = 0.f;
  85. }
  86. else if (1 == mesh->mNumUVComponents[i]) {
  87. for (; p != end; ++p)
  88. p->z = p->y = 0.f;
  89. }
  90. else if (3 == mesh->mNumUVComponents[i]) {
  91. // Really 3D coordinates? Check whether the third coordinate is != 0 for at least one element
  92. for (; p != end; ++p) {
  93. if (p->z != 0)
  94. break;
  95. }
  96. if (p == end) {
  97. DefaultLogger::get()->warn("ScenePreprocessor: UVs are declared to be 3D but they're obviously not. Reverting to 2D.");
  98. mesh->mNumUVComponents[i] = 2;
  99. }
  100. }
  101. }
  102. }
  103. // If the information which primitive types are there in the
  104. // mesh is currently not available, compute it.
  105. if (!mesh->mPrimitiveTypes) {
  106. for (unsigned int a = 0; a < mesh->mNumFaces; ++a) {
  107. aiFace& face = mesh->mFaces[a];
  108. switch (face.mNumIndices)
  109. {
  110. case 3u:
  111. mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  112. break;
  113. case 2u:
  114. mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  115. break;
  116. case 1u:
  117. mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  118. break;
  119. default:
  120. mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  121. break;
  122. }
  123. }
  124. }
  125. // If tangents and normals are given but no bitangents compute them
  126. if (mesh->mTangents && mesh->mNormals && !mesh->mBitangents) {
  127. mesh->mBitangents = new aiVector3D[mesh->mNumVertices];
  128. for (unsigned int i = 0; i < mesh->mNumVertices;++i) {
  129. mesh->mBitangents[i] = mesh->mNormals[i] ^ mesh->mTangents[i];
  130. }
  131. }
  132. }
  133. // ---------------------------------------------------------------------------------------------
  134. void ScenePreprocessor::ProcessAnimation (aiAnimation* anim)
  135. {
  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. */
  142. if (anim->mDuration == -1.) {
  143. // Position keys
  144. for (unsigned int i = 0; i < channel->mNumPositionKeys;++i) {
  145. aiVectorKey& key = channel->mPositionKeys[i];
  146. first = std::min (first, key.mTime);
  147. last = std::max (last, key.mTime);
  148. }
  149. // Scaling keys
  150. for (unsigned int i = 0; i < channel->mNumScalingKeys;++i) {
  151. aiVectorKey& key = channel->mScalingKeys[i];
  152. first = std::min (first, key.mTime);
  153. last = std::max (last, key.mTime);
  154. }
  155. // Rotation keys
  156. for (unsigned int i = 0; i < channel->mNumRotationKeys;++i) {
  157. aiQuatKey& key = channel->mRotationKeys[i];
  158. first = std::min (first, key.mTime);
  159. last = std::max (last, key.mTime);
  160. }
  161. }
  162. /* Check whether the animation channel has no rotation
  163. * or position tracks. In this case we generate a dummy
  164. * track from the information we have in the transformation
  165. * matrix of the corresponding node.
  166. */
  167. if (!channel->mNumRotationKeys || !channel->mNumPositionKeys || !channel->mNumScalingKeys) {
  168. // Find the node that belongs to this animation
  169. aiNode* node = scene->mRootNode->FindNode(channel->mNodeName);
  170. if (node) // ValidateDS will complain later if 'node' is NULL
  171. {
  172. // Decompose the transformation matrix of the node
  173. aiVector3D scaling, position;
  174. aiQuaternion rotation;
  175. node->mTransformation.Decompose(scaling, rotation,position);
  176. // No rotation keys? Generate a dummy track
  177. if (!channel->mNumRotationKeys) {
  178. channel->mNumRotationKeys = 1;
  179. channel->mRotationKeys = new aiQuatKey[1];
  180. aiQuatKey& q = channel->mRotationKeys[0];
  181. q.mTime = 0.;
  182. q.mValue = rotation;
  183. DefaultLogger::get()->debug("ScenePreprocessor: Dummy rotation track has been generated");
  184. }
  185. // No scaling keys? Generate a dummy track
  186. if (!channel->mNumScalingKeys) {
  187. channel->mNumScalingKeys = 1;
  188. channel->mScalingKeys = new aiVectorKey[1];
  189. aiVectorKey& q = channel->mScalingKeys[0];
  190. q.mTime = 0.;
  191. q.mValue = scaling;
  192. DefaultLogger::get()->debug("ScenePreprocessor: Dummy scaling track has been generated");
  193. }
  194. // No position keys? Generate a dummy track
  195. if (!channel->mNumPositionKeys) {
  196. channel->mNumPositionKeys = 1;
  197. channel->mPositionKeys = new aiVectorKey[1];
  198. aiVectorKey& q = channel->mPositionKeys[0];
  199. q.mTime = 0.;
  200. q.mValue = position;
  201. DefaultLogger::get()->debug("ScenePreprocessor: Dummy position track has been generated");
  202. }
  203. }
  204. }
  205. }
  206. if (anim->mDuration == -1.) {
  207. DefaultLogger::get()->debug("ScenePreprocessor: Setting animation duration");
  208. anim->mDuration = last - std::min( first, 0. );
  209. }
  210. }