shadowVolumeBSP.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 _SHADOWVOLUMEBSP_H_
  23. #define _SHADOWVOLUMEBSP_H_
  24. #ifndef _TVECTOR_H_
  25. #include "core/util/tVector.h"
  26. #endif
  27. #ifndef _MMATH_H_
  28. #include "math/mMath.h"
  29. #endif
  30. #ifndef _DATACHUNKER_H_
  31. #include "core/dataChunker.h"
  32. #endif
  33. #ifndef _LIGHTMANAGER_H_
  34. #include "lighting/lightManager.h"
  35. #endif
  36. /// Used to calculate shadows.
  37. class ShadowVolumeBSP
  38. {
  39. public:
  40. ShadowVolumeBSP();
  41. ~ShadowVolumeBSP();
  42. struct SVNode;
  43. struct SurfaceInfo
  44. {
  45. U32 mSurfaceIndex;
  46. U32 mPlaneIndex;
  47. Vector<U32> mShadowed;
  48. SVNode * mShadowVolume;
  49. };
  50. struct SVNode
  51. {
  52. enum Side
  53. {
  54. Front = 0,
  55. Back = 1,
  56. On = 2,
  57. Split = 3
  58. };
  59. SVNode * mFront;
  60. SVNode * mBack;
  61. U32 mPlaneIndex;
  62. U32 mShadowVolume;
  63. /// Used with shadowed interiors.
  64. SurfaceInfo * mSurfaceInfo;
  65. };
  66. struct SVPoly
  67. {
  68. enum {
  69. MaxWinding = 32
  70. };
  71. U32 mWindingCount;
  72. Point3F mWinding[MaxWinding];
  73. PlaneF mPlane;
  74. SVNode * mTarget;
  75. U32 mShadowVolume;
  76. SVPoly * mNext;
  77. SurfaceInfo * mSurfaceInfo;
  78. };
  79. void insertPoly(SVNode **, SVPoly *);
  80. void insertPolyFront(SVNode **, SVPoly *);
  81. void insertPolyBack(SVNode **, SVPoly *);
  82. void splitPoly(SVPoly *, const PlaneF &, SVPoly **, SVPoly **);
  83. void insertShadowVolume(SVNode **, U32);
  84. void addUniqueVolume(SurfaceInfo *, U32);
  85. SVNode::Side whichSide(SVPoly *, const PlaneF &) const;
  86. //
  87. bool testPoint(SVNode *, const Point3F &);
  88. bool testPoly(SVNode *, SVPoly *);
  89. void addToPolyList(SVPoly **, SVPoly *) const;
  90. void clipPoly(SVNode *, SVPoly **, SVPoly *);
  91. void clipToSelf(SVNode *, SVPoly **, SVPoly *);
  92. F32 getPolySurfaceArea(SVPoly *) const;
  93. F32 getClippedSurfaceArea(SVNode *, SVPoly *);
  94. void movePolyList(SVPoly **, SVPoly *) const;
  95. F32 getLitSurfaceArea(SVPoly *, SurfaceInfo *);
  96. Vector<SurfaceInfo *> mSurfaces;
  97. Chunker<SVNode> mNodeChunker;
  98. Chunker<SVPoly> mPolyChunker;
  99. SVNode * createNode();
  100. void recycleNode(SVNode *);
  101. SVPoly * createPoly();
  102. void recyclePoly(SVPoly *);
  103. U32 insertPlane(const PlaneF &);
  104. const PlaneF & getPlane(U32) const;
  105. //
  106. SVNode * mSVRoot;
  107. Vector<SVNode*> mShadowVolumes;
  108. SVNode * getShadowVolume(U32);
  109. Vector<PlaneF> mPlanes;
  110. SVNode * mNodeStore;
  111. SVPoly * mPolyStore;
  112. // used to remove the last inserted interior from the tree
  113. Vector<SVNode*> mParentNodes;
  114. SVNode * mFirstInteriorNode;
  115. void removeLastInterior();
  116. /// @name Access functions
  117. /// @{
  118. void insertPoly(SVPoly * poly) {insertPoly(&mSVRoot, poly);}
  119. bool testPoint(Point3F & pnt) {return(testPoint(mSVRoot, pnt));}
  120. bool testPoly(SVPoly * poly) {return(testPoly(mSVRoot, poly));}
  121. F32 getClippedSurfaceArea(SVPoly * poly) {return(getClippedSurfaceArea(mSVRoot, poly));}
  122. /// @}
  123. /// @name Helpers
  124. /// @{
  125. void buildPolyVolume(SVPoly *, LightInfo *);
  126. SVPoly * copyPoly(SVPoly *);
  127. /// @}
  128. };
  129. #endif