GeometryUtils.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "GeometryUtils.h"
  25. #include "Plane.h"
  26. inline void ClipEdge(float* output, float* v0, float* v1, float d0, float d1, unsigned components)
  27. {
  28. float t = d0 / (d0 - d1);
  29. for (unsigned i = 0; i < components; ++i)
  30. output[i] = v0[i] + t * (v1[i] - v0[i]);
  31. }
  32. inline void CopyVertex(float* output, float* input, unsigned components)
  33. {
  34. for (unsigned i = 0; i < components; ++i)
  35. output[i] = input[i];
  36. }
  37. unsigned ClipPolygon(float* input, float* output, unsigned vertexCount, unsigned vertexSize, const Plane& plane, float* clip, unsigned* clipVertexCount)
  38. {
  39. unsigned components = vertexSize / sizeof(float);
  40. unsigned outVertexCount = 0;
  41. float* lastVertex = input;
  42. float lastDistance = 0.0f;
  43. if (clipVertexCount)
  44. *clipVertexCount = 0;
  45. for (unsigned i = 0; i < vertexCount; ++i)
  46. {
  47. float* vertex = input + i * components;
  48. const Vector3& position = *((Vector3*)vertex);
  49. float distance = plane.Distance(position);
  50. if (distance >= 0.0f)
  51. {
  52. if (lastDistance < 0.0f)
  53. {
  54. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components);
  55. if (clip && clipVertexCount)
  56. {
  57. CopyVertex(clip, output, components);
  58. clip += components;
  59. ++(*clipVertexCount);
  60. }
  61. output += components;
  62. ++outVertexCount;
  63. }
  64. for (unsigned j = 0; j < components; ++j)
  65. output[j] = vertex[j];
  66. output += components;
  67. ++outVertexCount;
  68. }
  69. else
  70. {
  71. if (lastDistance >= 0.0f && i != 0)
  72. {
  73. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components);
  74. if (clip && clipVertexCount)
  75. {
  76. CopyVertex(clip, output, components);
  77. clip += components;
  78. ++(*clipVertexCount);
  79. }
  80. output += components;
  81. ++outVertexCount;
  82. }
  83. }
  84. lastVertex = vertex;
  85. lastDistance = distance;
  86. }
  87. // Recheck the distances of the last and first vertices and add the final clipped vertex if applicable
  88. {
  89. float* vertex = input;
  90. const Vector3& position = *((Vector3*)vertex);
  91. float distance = plane.Distance(position);
  92. if ((lastDistance < 0.0f && distance >= 0.0f) || (lastDistance >= 0.0f && distance < 0.0f))
  93. {
  94. ClipEdge(output, lastVertex, vertex, lastDistance, distance, components);
  95. if (clip && clipVertexCount)
  96. {
  97. CopyVertex(clip, output, components);
  98. clip += components;
  99. ++(*clipVertexCount);
  100. }
  101. output += components;
  102. ++outVertexCount;
  103. }
  104. }
  105. return outVertexCount;
  106. }