reflector.h

Engine/source/scene/reflector.h

More...

Classes:

Public Typedefs

ReflectorList 

Detailed Description

Public Typedefs

typedef Vector< ReflectorBase * > ReflectorList 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _REFLECTOR_H_
 25#define _REFLECTOR_H_
 26
 27#ifndef _GFXCUBEMAP_H_
 28#include "gfx/gfxCubemap.h"
 29#endif
 30#ifndef _GFXTARGET_H_
 31#include "gfx/gfxTarget.h"
 32#endif
 33#ifndef _SIMDATABLOCK_H_
 34#include "console/simDatablock.h"
 35#endif
 36#ifndef _MMATH_H_
 37#include "math/mMath.h"
 38#endif
 39#ifndef _MATHUTIL_FRUSTUM_H_
 40#include "math/util/frustum.h"
 41#endif
 42
 43struct CameraQuery;
 44class Point2I;
 45class Frustum;
 46class SceneManager;
 47class SceneObject;
 48class GFXOcclusionQuery;
 49
 50
 51struct ReflectParams
 52{
 53   const CameraQuery *query;
 54   Point2I viewportExtent;
 55   Frustum culler;
 56   U32 startOfUpdateMs;
 57   S8 eyeId;
 58};
 59
 60
 61class ReflectorDesc : public SimDataBlock
 62{
 63   typedef SimDataBlock Parent;
 64
 65public:
 66
 67   ReflectorDesc();
 68   virtual ~ReflectorDesc();
 69
 70   DECLARE_CONOBJECT( ReflectorDesc );
 71
 72   static void initPersistFields();
 73
 74   virtual void packData( BitStream *stream );
 75   virtual void unpackData( BitStream* stream );   
 76   virtual bool preload( bool server, String &errorStr );
 77
 78   U32 texSize;   
 79   F32 nearDist;
 80   F32 farDist;
 81   U32 objectTypeMask;
 82   F32 detailAdjust;
 83   F32 priority;
 84   U32 maxRateMs;
 85   bool useOcclusionQuery;
 86   //U32 lastLodSize;
 87};
 88
 89
 90class ReflectorBase
 91{
 92public:
 93
 94   ReflectorBase();
 95   virtual ~ReflectorBase();
 96
 97   bool isEnabled() const { return mEnabled; }
 98
 99   virtual void unregisterReflector();
100   virtual F32 calcScore( const ReflectParams &params );
101   virtual void updateReflection( const ReflectParams &params ) {}
102
103   GFXOcclusionQuery* getOcclusionQuery() const { return mOcclusionQuery; }
104
105   bool isOccluded() const { return mOccluded; }
106
107   /// Returns true if this reflector is in the process of rendering.
108   bool isRendering() const { return mIsRendering; }   
109
110   /// Signifies that the query has not finished yet and a new query
111   /// does not need to be submitted.
112   bool mQueryPending;
113
114protected:
115
116   bool mEnabled;
117
118   bool mIsRendering;
119
120   GFXOcclusionQuery *mOcclusionQuery;
121
122   bool mOccluded;   
123
124   SceneObject *mObject;
125
126   ReflectorDesc *mDesc;
127
128public:
129
130   // These are public because some of them
131   // are exposed as fields.
132
133   F32 score;
134   U32 lastUpdateMs;
135   
136
137};
138
139typedef Vector<ReflectorBase*> ReflectorList;
140
141
142class CubeReflector : public ReflectorBase
143{
144   typedef ReflectorBase Parent;
145
146public:
147
148   CubeReflector();
149   virtual ~CubeReflector() {}
150
151   void registerReflector( SceneObject *inObject,
152                           ReflectorDesc *inDesc );
153
154   virtual void unregisterReflector();
155   virtual void updateReflection( const ReflectParams &params, Point3F explicitPostion = Point3F::Max);
156
157   GFXCubemap* getCubemap() const { return mCubemap; }
158
159   void updateFace( const ReflectParams &params, U32 faceidx, Point3F explicitPostion = Point3F::Max);
160   F32 calcFaceScore( const ReflectParams &params, U32 faceidx );
161
162protected:
163
164   GFXTexHandle mDepthBuff;
165   GFXTextureTargetRef mRenderTarget;   
166   GFXCubemapHandle  mCubemap;
167   U32 mLastTexSize;
168
169   class CubeFaceReflector : public ReflectorBase
170   {
171      typedef ReflectorBase Parent;
172      friend class CubeReflector;
173
174   public:
175      U32 faceIdx;
176      CubeReflector *cube;
177
178      virtual void updateReflection( const ReflectParams &params ) { cube->updateFace( params, faceIdx ); } 
179      virtual F32 calcScore( const ReflectParams &params );
180   };
181
182   CubeFaceReflector mFaces[6];
183};
184
185
186class PlaneReflector : public ReflectorBase
187{
188   typedef ReflectorBase Parent;
189
190public:
191
192   PlaneReflector() 
193   {
194      refplane.set( Point3F(0,0,0), Point3F(0,0,1) );
195      objectSpace = false;
196      mLastTexSize = Point2I(0,0);
197   }
198
199   virtual ~PlaneReflector() {}
200
201   void registerReflector( SceneObject *inObject,
202                           ReflectorDesc *inDesc );
203
204   virtual F32 calcScore( const ReflectParams &params );
205   virtual void updateReflection( const ReflectParams &params ); 
206
207   /// Set up the GFX matrices
208   void setGFXMatrices( const MatrixF &camTrans );
209
210   /// Set up camera matrix for a reflection on the plane
211   MatrixF getCameraReflection( const MatrixF &camTrans );
212
213   /// Oblique frustum clipping - use near plane of zbuffer as a clip plane
214   MatrixF getFrustumClipProj( MatrixF &modelview );
215
216protected:
217
218   Point2I mLastTexSize;
219
220   // The camera position at the last update.
221   Point3F mLastPos;
222
223   // The camera direction at the last update.
224   VectorF mLastDir;
225
226public:
227
228   GFXTextureTargetRef reflectTarget;
229
230   GFXTexHandle innerReflectTex[2]; /// < Textures we actually render to
231   GFXTexHandle reflectTex; ///< Last texture we rendered to
232   GFXTexHandle depthBuff;
233   PlaneF refplane;
234   bool objectSpace;
235};
236
237#endif // _REFLECTOR_H_
238