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.
 
 
 
 
 
 

175 lines
5.1 KiB

  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. #include "stdafx.h"
  35. #include "assimp_view.h"
  36. // note: these are no longer part of the public API, but they are
  37. // exported on Windows to keep AssimpView alive.
  38. #include "GenFaceNormalsProcess.h"
  39. #include "GenVertexNormalsProcess.h"
  40. #include "JoinVerticesProcess.h"
  41. #include "CalcTangentsProcess.h"
  42. #include "MakeVerboseFormat.h"
  43. namespace AssimpView {
  44. bool g_bWasFlipped = false;
  45. float g_smoothAngle = 80.f;
  46. //-------------------------------------------------------------------------------
  47. // Flip all normal vectors
  48. //-------------------------------------------------------------------------------
  49. void AssetHelper::FlipNormalsInt()
  50. {
  51. // invert all normal vectors
  52. for (unsigned int i = 0; i < this->pcScene->mNumMeshes;++i)
  53. {
  54. aiMesh* pcMesh = this->pcScene->mMeshes[i];
  55. if (!pcMesh->mNormals)
  56. continue;
  57. for (unsigned int a = 0; a < pcMesh->mNumVertices;++a){
  58. pcMesh->mNormals[a] *= -1.0f;
  59. }
  60. }
  61. }
  62. //-------------------------------------------------------------------------------
  63. void AssetHelper::FlipNormals()
  64. {
  65. FlipNormalsInt();
  66. // recreate native data
  67. DeleteAssetData(true);
  68. CreateAssetData();
  69. g_bWasFlipped = ! g_bWasFlipped;
  70. }
  71. //-------------------------------------------------------------------------------
  72. // Set the normal set of the scene
  73. //-------------------------------------------------------------------------------
  74. void AssetHelper::SetNormalSet(unsigned int iSet)
  75. {
  76. // we need to build an unique set of vertices for this ...
  77. {
  78. MakeVerboseFormatProcess* pcProcess = new MakeVerboseFormatProcess();
  79. pcProcess->Execute(pcScene);
  80. delete pcProcess;
  81. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  82. {
  83. if (!apcMeshes[i]->pvOriginalNormals)
  84. {
  85. apcMeshes[i]->pvOriginalNormals = new aiVector3D[pcScene->mMeshes[i]->mNumVertices];
  86. memcpy( apcMeshes[i]->pvOriginalNormals,pcScene->mMeshes[i]->mNormals,
  87. pcScene->mMeshes[i]->mNumVertices * sizeof(aiVector3D));
  88. }
  89. delete[] pcScene->mMeshes[i]->mNormals;
  90. pcScene->mMeshes[i]->mNormals = NULL;
  91. }
  92. }
  93. // now we can start to calculate a new set of normals
  94. if (HARD == iSet)
  95. {
  96. GenFaceNormalsProcess* pcProcess = new GenFaceNormalsProcess();
  97. pcProcess->Execute(pcScene);
  98. FlipNormalsInt();
  99. delete pcProcess;
  100. }
  101. else if (SMOOTH == iSet)
  102. {
  103. GenVertexNormalsProcess* pcProcess = new GenVertexNormalsProcess();
  104. pcProcess->SetMaxSmoothAngle((float)AI_DEG_TO_RAD(g_smoothAngle));
  105. pcProcess->Execute(pcScene);
  106. FlipNormalsInt();
  107. delete pcProcess;
  108. }
  109. else if (ORIGINAL == iSet)
  110. {
  111. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  112. {
  113. if (apcMeshes[i]->pvOriginalNormals)
  114. {
  115. delete[] pcScene->mMeshes[i]->mNormals;
  116. pcScene->mMeshes[i]->mNormals = apcMeshes[i]->pvOriginalNormals;
  117. apcMeshes[i]->pvOriginalNormals = NULL;
  118. }
  119. }
  120. }
  121. // recalculate tangents and bitangents
  122. Assimp::BaseProcess* pcProcess = new CalcTangentsProcess();
  123. pcProcess->Execute(pcScene);
  124. delete pcProcess;
  125. // join the mesh vertices again
  126. pcProcess = new JoinVerticesProcess();
  127. pcProcess->Execute(pcScene);
  128. delete pcProcess;
  129. iNormalSet = iSet;
  130. if (g_bWasFlipped)
  131. {
  132. // invert all normal vectors
  133. for (unsigned int i = 0; i < pcScene->mNumMeshes;++i)
  134. {
  135. aiMesh* pcMesh = pcScene->mMeshes[i];
  136. for (unsigned int a = 0; a < pcMesh->mNumVertices;++a)
  137. {
  138. pcMesh->mNormals[a] *= -1.0f;
  139. }
  140. }
  141. }
  142. // recreate native data
  143. DeleteAssetData(true);
  144. CreateAssetData();
  145. return;
  146. }
  147. };