clippedPolyList.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CLIPPEDPOLYLIST_H_
  23. #define _CLIPPEDPOLYLIST_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. #ifndef _TVECTORSPEC_H_
  28. #include "core/util/tVectorSpecializations.h"
  29. #endif
  30. #ifndef _ABSTRACTPOLYLIST_H_
  31. #include "collision/abstractPolyList.h"
  32. #endif
  33. #define CLIPPEDPOLYLIST_FLAG_ALLOWCLIPPING 0x01
  34. /// The clipped polylist class takes the geometry passed to it and clips
  35. /// it against the PlaneList set.
  36. ///
  37. /// It also contains helper functions for
  38. /// @see AbstractPolyList
  39. class ClippedPolyList : public AbstractPolyList
  40. {
  41. void memcpy(U32* d, U32* s,U32 size);
  42. public:
  43. struct Vertex {
  44. Point3F point;
  45. U32 mask;
  46. };
  47. struct Poly {
  48. PlaneF plane;
  49. SceneObject* object;
  50. BaseMatInstance* material;
  51. U32 vertexStart;
  52. U32 vertexCount;
  53. U32 surfaceKey;
  54. U32 polyFlags;
  55. };
  56. /// ???
  57. static bool allowClipping;
  58. typedef Vector<PlaneF> PlaneList;
  59. typedef Vector<Vertex> VertexList;
  60. typedef Vector<Poly> PolyList;
  61. typedef FastVector<U32> IndexList;
  62. typedef PlaneList::iterator PlaneListIterator;
  63. typedef VertexList::iterator VertexListIterator;
  64. typedef PolyList::iterator PolyListIterator;
  65. typedef IndexList::iterator IndexListIterator;
  66. // Internal data
  67. PolyList mPolyList;
  68. VertexList mVertexList;
  69. IndexList mIndexList;
  70. // Temporary lists used by triangulate and kept
  71. // here to reduce memory allocations.
  72. PolyList mTempPolyList;
  73. IndexList mTempIndexList;
  74. const static U32 IndexListReserveSize = 128;
  75. /// The per-vertex normals.
  76. /// @see generateNormals()
  77. Vector<VectorF> mNormalList;
  78. PlaneList mPolyPlaneList;
  79. /// The list of planes to clip against.
  80. ///
  81. /// This should be set before filling the polylist.
  82. PlaneList mPlaneList;
  83. /// If non-zero any poly facing away from this
  84. /// normal is removed from the list.
  85. ///
  86. /// This should be set before filling the polylist.
  87. VectorF mNormal;
  88. /// If the dot product result between a poly's normal and mNormal is greater
  89. /// than this value it will be rejected.
  90. /// The default value is 0.
  91. /// 90 degrees = mCos( mDegToRad( 90.0f ) = 0
  92. F32 mNormalTolCosineRadians;
  93. //
  94. ClippedPolyList();
  95. ~ClippedPolyList();
  96. void clear();
  97. // AbstractPolyList
  98. bool isEmpty() const;
  99. U32 addPoint(const Point3F& p);
  100. U32 addPointAndNormal(const Point3F& p, const Point3F& normal);
  101. U32 addPlane(const PlaneF& plane);
  102. void begin(BaseMatInstance* material,U32 surfaceKey);
  103. void plane(U32 v1,U32 v2,U32 v3);
  104. void plane(const PlaneF& p);
  105. void plane(const U32 index);
  106. void vertex(U32 vi);
  107. void end();
  108. /// Often after clipping you'll end up with orphan verticies
  109. /// that are unused by the poly list. This removes these unused
  110. /// verts and updates the index list.
  111. void cullUnusedVerts();
  112. /// This breaks all polys in the polylist into triangles.
  113. void triangulate();
  114. /// Generates averaged normals from the poly normals.
  115. /// @see mNormalList
  116. void generateNormals();
  117. protected:
  118. // AbstractPolyList
  119. const PlaneF& getIndexedPlane(const U32 index);
  120. };
  121. #endif