clippedPolyList.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 = 0;
  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. Poly() :object(NULL), material(NULL), vertexStart(0), vertexCount(0), surfaceKey(0), polyFlags(0) {}
  56. ~Poly() {}
  57. };
  58. /// ???
  59. static bool allowClipping;
  60. typedef Vector<PlaneF> PlaneList;
  61. typedef Vector<Vertex> VertexList;
  62. typedef Vector<Poly> PolyList;
  63. typedef FastVector<U32> IndexList;
  64. typedef PlaneList::iterator PlaneListIterator;
  65. typedef VertexList::iterator VertexListIterator;
  66. typedef PolyList::iterator PolyListIterator;
  67. typedef IndexList::iterator IndexListIterator;
  68. // Internal data
  69. PolyList mPolyList;
  70. VertexList mVertexList;
  71. IndexList mIndexList;
  72. // Temporary lists used by triangulate and kept
  73. // here to reduce memory allocations.
  74. PolyList mTempPolyList;
  75. IndexList mTempIndexList;
  76. const static U32 IndexListReserveSize = 128;
  77. /// The per-vertex normals.
  78. /// @see generateNormals()
  79. Vector<VectorF> mNormalList;
  80. PlaneList mPolyPlaneList;
  81. /// The list of planes to clip against.
  82. ///
  83. /// This should be set before filling the polylist.
  84. PlaneList mPlaneList;
  85. /// If non-zero any poly facing away from this
  86. /// normal is removed from the list.
  87. ///
  88. /// This should be set before filling the polylist.
  89. VectorF mNormal;
  90. /// If the dot product result between a poly's normal and mNormal is greater
  91. /// than this value it will be rejected.
  92. /// The default value is 0.
  93. /// 90 degrees = mCos( mDegToRad( 90.0f ) = 0
  94. F32 mNormalTolCosineRadians;
  95. //
  96. ClippedPolyList();
  97. ~ClippedPolyList();
  98. void clear();
  99. // AbstractPolyList
  100. bool isEmpty() const;
  101. U32 addPoint(const Point3F& p);
  102. U32 addPointAndNormal(const Point3F& p, const Point3F& normal);
  103. U32 addPlane(const PlaneF& plane);
  104. void begin(BaseMatInstance* material,U32 surfaceKey);
  105. void plane(U32 v1,U32 v2,U32 v3);
  106. void plane(const PlaneF& p);
  107. void plane(const U32 index);
  108. void vertex(U32 vi);
  109. void end();
  110. /// Often after clipping you'll end up with orphan verticies
  111. /// that are unused by the poly list. This removes these unused
  112. /// verts and updates the index list.
  113. void cullUnusedVerts();
  114. /// This breaks all polys in the polylist into triangles.
  115. void triangulate();
  116. /// Generates averaged normals from the poly normals.
  117. /// @see mNormalList
  118. void generateNormals();
  119. protected:
  120. // AbstractPolyList
  121. const PlaneF& getIndexedPlane(const U32 index);
  122. };
  123. #endif