reflector.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. S8 eyeId;
  52. };
  53. class ReflectorDesc : public SimDataBlock
  54. {
  55. typedef SimDataBlock Parent;
  56. public:
  57. ReflectorDesc();
  58. virtual ~ReflectorDesc();
  59. DECLARE_CONOBJECT( ReflectorDesc );
  60. static void initPersistFields();
  61. virtual void packData( BitStream *stream );
  62. virtual void unpackData( BitStream* stream );
  63. virtual bool preload( bool server, String &errorStr );
  64. U32 texSize;
  65. F32 nearDist;
  66. F32 farDist;
  67. U32 objectTypeMask;
  68. F32 detailAdjust;
  69. F32 priority;
  70. U32 maxRateMs;
  71. bool useOcclusionQuery;
  72. //U32 lastLodSize;
  73. };
  74. class ReflectorBase
  75. {
  76. public:
  77. ReflectorBase();
  78. virtual ~ReflectorBase();
  79. bool isEnabled() const { return mEnabled; }
  80. virtual void unregisterReflector();
  81. virtual F32 calcScore( const ReflectParams &params );
  82. virtual void updateReflection( const ReflectParams &params ) {}
  83. GFXOcclusionQuery* getOcclusionQuery() const { return mOcclusionQuery; }
  84. bool isOccluded() const { return mOccluded; }
  85. /// Returns true if this reflector is in the process of rendering.
  86. bool isRendering() const { return mIsRendering; }
  87. /// Signifies that the query has not finished yet and a new query
  88. /// does not need to be submitted.
  89. bool mQueryPending;
  90. protected:
  91. bool mEnabled;
  92. bool mIsRendering;
  93. GFXOcclusionQuery *mOcclusionQuery;
  94. bool mOccluded;
  95. SceneObject *mObject;
  96. ReflectorDesc *mDesc;
  97. public:
  98. // These are public because some of them
  99. // are exposed as fields.
  100. F32 score;
  101. U32 lastUpdateMs;
  102. };
  103. typedef Vector<ReflectorBase*> ReflectorList;
  104. class CubeReflector : public ReflectorBase
  105. {
  106. typedef ReflectorBase Parent;
  107. public:
  108. CubeReflector();
  109. virtual ~CubeReflector() {}
  110. void registerReflector( SceneObject *inObject,
  111. ReflectorDesc *inDesc );
  112. virtual void unregisterReflector();
  113. virtual void updateReflection( const ReflectParams &params, Point3F explicitPostion = Point3F::Max);
  114. GFXCubemap* getCubemap() const { return mCubemap; }
  115. void updateFace( const ReflectParams &params, U32 faceidx, Point3F explicitPostion = Point3F::Max);
  116. F32 calcFaceScore( const ReflectParams &params, U32 faceidx );
  117. protected:
  118. GFXTexHandle mDepthBuff;
  119. GFXTextureTargetRef mRenderTarget;
  120. GFXCubemapHandle mCubemap;
  121. U32 mLastTexSize;
  122. class CubeFaceReflector : public ReflectorBase
  123. {
  124. typedef ReflectorBase Parent;
  125. friend class CubeReflector;
  126. public:
  127. U32 faceIdx;
  128. CubeReflector *cube;
  129. virtual void updateReflection( const ReflectParams &params ) { cube->updateFace( params, faceIdx ); }
  130. virtual F32 calcScore( const ReflectParams &params );
  131. };
  132. CubeFaceReflector mFaces[6];
  133. };
  134. class PlaneReflector : public ReflectorBase
  135. {
  136. typedef ReflectorBase Parent;
  137. public:
  138. PlaneReflector()
  139. {
  140. refplane.set( Point3F(0,0,0), Point3F(0,0,1) );
  141. objectSpace = false;
  142. mLastTexSize = Point2I(0,0);
  143. }
  144. virtual ~PlaneReflector() {}
  145. void registerReflector( SceneObject *inObject,
  146. ReflectorDesc *inDesc );
  147. virtual F32 calcScore( const ReflectParams &params );
  148. virtual void updateReflection( const ReflectParams &params );
  149. /// Set up the GFX matrices
  150. void setGFXMatrices( const MatrixF &camTrans );
  151. /// Set up camera matrix for a reflection on the plane
  152. MatrixF getCameraReflection( const MatrixF &camTrans );
  153. /// Oblique frustum clipping - use near plane of zbuffer as a clip plane
  154. MatrixF getFrustumClipProj( MatrixF &modelview );
  155. protected:
  156. Point2I mLastTexSize;
  157. // The camera position at the last update.
  158. Point3F mLastPos;
  159. // The camera direction at the last update.
  160. VectorF mLastDir;
  161. public:
  162. GFXTextureTargetRef reflectTarget;
  163. GFXTexHandle innerReflectTex[2]; /// < Textures we actually render to
  164. GFXTexHandle reflectTex; ///< Last texture we rendered to
  165. GFXTexHandle depthBuff;
  166. PlaneF refplane;
  167. bool objectSpace;
  168. };
  169. #endif // _REFLECTOR_H_