You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp 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 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 Generation of normal vectors basing on smoothing groups */
  35. #ifndef AI_SMOOTHINGGROUPS_INL_INCLUDED
  36. #define AI_SMOOTHINGGROUPS_INL_INCLUDED
  37. // internal headers
  38. #include "SGSpatialSort.h"
  39. // CRT header
  40. #include <algorithm>
  41. using namespace Assimp;
  42. // ------------------------------------------------------------------------------------------------
  43. template <class T>
  44. void ComputeNormalsWithSmoothingsGroups(MeshWithSmoothingGroups<T>& sMesh)
  45. {
  46. // First generate face normals
  47. sMesh.mNormals.resize(sMesh.mPositions.size(),aiVector3D());
  48. for( unsigned int a = 0; a < sMesh.mFaces.size(); a++)
  49. {
  50. T& face = sMesh.mFaces[a];
  51. aiVector3D* pV1 = &sMesh.mPositions[face.mIndices[0]];
  52. aiVector3D* pV2 = &sMesh.mPositions[face.mIndices[1]];
  53. aiVector3D* pV3 = &sMesh.mPositions[face.mIndices[2]];
  54. aiVector3D pDelta1 = *pV2 - *pV1;
  55. aiVector3D pDelta2 = *pV3 - *pV1;
  56. aiVector3D vNor = pDelta1 ^ pDelta2;
  57. for (unsigned int c = 0; c < 3;++c)
  58. sMesh.mNormals[face.mIndices[c]] = vNor;
  59. }
  60. // calculate the position bounds so we have a reliable epsilon to check position differences against
  61. aiVector3D minVec( 1e10f, 1e10f, 1e10f), maxVec( -1e10f, -1e10f, -1e10f);
  62. for( unsigned int a = 0; a < sMesh.mPositions.size(); a++)
  63. {
  64. minVec.x = std::min( minVec.x, sMesh.mPositions[a].x);
  65. minVec.y = std::min( minVec.y, sMesh.mPositions[a].y);
  66. minVec.z = std::min( minVec.z, sMesh.mPositions[a].z);
  67. maxVec.x = std::max( maxVec.x, sMesh.mPositions[a].x);
  68. maxVec.y = std::max( maxVec.y, sMesh.mPositions[a].y);
  69. maxVec.z = std::max( maxVec.z, sMesh.mPositions[a].z);
  70. }
  71. const float posEpsilon = (maxVec - minVec).Length() * 1e-5f;
  72. std::vector<aiVector3D> avNormals;
  73. avNormals.resize(sMesh.mNormals.size());
  74. // now generate the spatial sort tree
  75. SGSpatialSort sSort;
  76. for( typename std::vector<T>::iterator i = sMesh.mFaces.begin();
  77. i != sMesh.mFaces.end();++i)
  78. {
  79. for (unsigned int c = 0; c < 3;++c)
  80. sSort.Add(sMesh.mPositions[(*i).mIndices[c]],(*i).mIndices[c],(*i).iSmoothGroup);
  81. }
  82. sSort.Prepare();
  83. std::vector<bool> vertexDone(sMesh.mPositions.size(),false);
  84. for( typename std::vector<T>::iterator i = sMesh.mFaces.begin();
  85. i != sMesh.mFaces.end();++i)
  86. {
  87. std::vector<unsigned int> poResult;
  88. for (unsigned int c = 0; c < 3;++c)
  89. {
  90. register unsigned int idx = (*i).mIndices[c];
  91. if (vertexDone[idx])continue;
  92. sSort.FindPositions(sMesh.mPositions[idx],(*i).iSmoothGroup,
  93. posEpsilon,poResult);
  94. aiVector3D vNormals;
  95. for (std::vector<unsigned int>::const_iterator
  96. a = poResult.begin();
  97. a != poResult.end();++a)
  98. {
  99. vNormals += sMesh.mNormals[(*a)];
  100. }
  101. vNormals.Normalize();
  102. // write back into all affected normals
  103. for (std::vector<unsigned int>::const_iterator
  104. a = poResult.begin();
  105. a != poResult.end();++a)
  106. {
  107. idx = *a;
  108. avNormals [idx] = vNormals;
  109. vertexDone[idx] = true;
  110. }
  111. }
  112. }
  113. sMesh.mNormals = avNormals;
  114. }
  115. #endif // !! AI_SMOOTHINGGROUPS_INL_INCLUDED