reflector.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 _REFLECTOR_H_
  23. #define _REFLECTOR_H_
  24. #ifndef _GFXCUBEMAP_H_
  25. #include "gfx/gfxCubemap.h"
  26. #endif
  27. #ifndef _GFXTARGET_H_
  28. #include "gfx/gfxTarget.h"
  29. #endif
  30. #ifndef _SIMDATABLOCK_H_
  31. #include "console/simDatablock.h"
  32. #endif
  33. #ifndef _MMATH_H_
  34. #include "math/mMath.h"
  35. #endif
  36. #ifndef _MATHUTIL_FRUSTUM_H_
  37. #include "math/util/frustum.h"
  38. #endif
  39. struct CameraQuery;
  40. class Point2I;
  41. class Frustum;
  42. class SceneManager;
  43. class SceneObject;
  44. class GFXOcclusionQuery;
  45. struct ReflectParams
  46. {
  47. const CameraQuery *query;
  48. Point2I viewportExtent;
  49. Frustum culler;
  50. U32 startOfUpdateMs;
  51. };
  52. class ReflectorDesc : public SimDataBlock
  53. {
  54. typedef SimDataBlock Parent;
  55. public:
  56. ReflectorDesc();
  57. virtual ~ReflectorDesc();
  58. DECLARE_CONOBJECT( ReflectorDesc );
  59. static void initPersistFields();
  60. virtual void packData( BitStream *stream );
  61. virtual void unpackData( BitStream* stream );
  62. virtual bool preload( bool server, String &errorStr );
  63. U32 texSize;
  64. F32 nearDist;
  65. F32 farDist;
  66. U32 objectTypeMask;
  67. F32 detailAdjust;
  68. F32 priority;
  69. U32 maxRateMs;
  70. bool useOcclusionQuery;
  71. //U32 lastLodSize;
  72. };
  73. class ReflectorBase
  74. {
  75. public:
  76. ReflectorBase();
  77. virtual ~ReflectorBase();
  78. bool isEnabled() const { return mEnabled; }
  79. virtual void unregisterReflector();
  80. virtual F32 calcScore( const ReflectParams &params );
  81. virtual void updateReflection( const ReflectParams &params ) {}
  82. GFXOcclusionQuery* getOcclusionQuery() const { return mOcclusionQuery; }
  83. bool isOccluded() const { return mOccluded; }
  84. /// Returns true if this reflector is in the process of rendering.
  85. bool isRendering() const { return mIsRendering; }
  86. /// Signifies that the query has not finished yet and a new query
  87. /// does not need to be submitted.
  88. bool mQueryPending;
  89. protected:
  90. bool mEnabled;
  91. bool mIsRendering;
  92. GFXOcclusionQuery *mOcclusionQuery;
  93. bool mOccluded;
  94. SceneObject *mObject;
  95. ReflectorDesc *mDesc;
  96. public:
  97. // These are public because some of them
  98. // are exposed as fields.
  99. F32 score;
  100. U32 lastUpdateMs;
  101. };
  102. typedef Vector<ReflectorBase*> ReflectorList;
  103. class CubeReflector : public ReflectorBase
  104. {
  105. typedef ReflectorBase Parent;
  106. public:
  107. CubeReflector();
  108. virtual ~CubeReflector() {}
  109. void registerReflector( SceneObject *inObject,
  110. ReflectorDesc *inDesc );
  111. virtual void unregisterReflector();
  112. virtual void updateReflection( const ReflectParams &params );
  113. GFXCubemap* getCubemap() const { return cubemap; }
  114. void updateFace( const ReflectParams &params, U32 faceidx );
  115. F32 calcFaceScore( const ReflectParams &params, U32 faceidx );
  116. protected:
  117. GFXTexHandle depthBuff;
  118. GFXTextureTargetRef renderTarget;
  119. GFXCubemapHandle cubemap;
  120. U32 mLastTexSize;
  121. class CubeFaceReflector : public ReflectorBase
  122. {
  123. typedef ReflectorBase Parent;
  124. friend class CubeReflector;
  125. public:
  126. U32 faceIdx;
  127. CubeReflector *cube;
  128. virtual void updateReflection( const ReflectParams &params ) { cube->updateFace( params, faceIdx ); }
  129. virtual F32 calcScore( const ReflectParams &params );
  130. };
  131. CubeFaceReflector mFaces[6];
  132. };
  133. class PlaneReflector : public ReflectorBase
  134. {
  135. typedef ReflectorBase Parent;
  136. public:
  137. PlaneReflector()
  138. {
  139. refplane.set( Point3F(0,0,0), Point3F(0,0,1) );
  140. objectSpace = false;
  141. mLastTexSize = 0;
  142. }
  143. virtual ~PlaneReflector() {}
  144. void registerReflector( SceneObject *inObject,
  145. ReflectorDesc *inDesc );
  146. virtual F32 calcScore( const ReflectParams &params );
  147. virtual void updateReflection( const ReflectParams &params );
  148. /// Set up the GFX matrices
  149. void setGFXMatrices( const MatrixF &camTrans );
  150. /// Set up camera matrix for a reflection on the plane
  151. MatrixF getCameraReflection( const MatrixF &camTrans );
  152. /// Oblique frustum clipping - use near plane of zbuffer as a clip plane
  153. MatrixF getFrustumClipProj( MatrixF &modelview );
  154. protected:
  155. U32 mLastTexSize;
  156. // The camera position at the last update.
  157. Point3F mLastPos;
  158. // The camera direction at the last update.
  159. VectorF mLastDir;
  160. public:
  161. GFXTextureTargetRef reflectTarget;
  162. GFXTexHandle reflectTex;
  163. GFXTexHandle depthBuff;
  164. PlaneF refplane;
  165. bool objectSpace;
  166. };
  167. #endif // _REFLECTOR_H_