HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_ShadowMap.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_ShadowMap.h ( RE Library, C++)
7  *
8  * COMMENTS:
9  * Contains the texture map and the code for generating a shadow map from
10  * a light.
11  */
12 #ifndef RE_ShadowMap_h
13 #define RE_ShadowMap_h
14 
15 #include "RE_API.h"
16 #include "RE_Light.h"
17 #include "RE_Texture.h"
18 
19 #include <UT/UT_NonCopyable.h>
20 #include <UT/UT_Rect.h>
21 #include <UT/UT_VectorTypes.h>
22 #include <SYS/SYS_Types.h>
23 
24 class RE_OGLFramebuffer;
25 class RE_OGLTexture;
26 class RE_Render;
27 class RE_Shader;
28 class RE_Texture2DMap;
29 class RE_TextureCubeMap;
30 class RE_Uniform;
31 
32 #define RE_SHADOW_MAX_MAPS \
33  (RE_SHADOW_MAX_AREA_MAPS > RE_SHADOW_NUM_CUBE_MAPS ? \
34  RE_SHADOW_MAX_AREA_MAPS : RE_SHADOW_NUM_CUBE_MAPS)
35 
37 {
38 public:
39  RE_ShadowMap(const RE_Light *light, int mapsize);
40  ~RE_ShadowMap();
41 
43 
44  // If true, this shadow map contains multiple maps for doing blurry
45  // shadows.
46  void setMultiMap(bool m);
47  bool isMultiMap() const;
48 
49 
50  // If true, this shadow map renders to an 32b R32F texture, without
51  // the non-linear clamping depth to [0,1]. OpenGL3 hardware required.
52  void useLinearDepthMap(bool ld);
53  bool usesLinearDepthMap() const;
54 
55 
56  // Multiplies the current GL matrix by the transformation that maps a
57  // world space point to a light space point. If 'mat' is supplied, the
58  // transform is assigned to it rather than affecting the current GL matrix.
59  void getShadowMapTransform(RE_Render *r, bool bias,
60  UT_Matrix4F *mat = nullptr,
61  bool cube=false,
64  int area_index = 0,
65  int xform_mask = 0x3);
66 
67  // Sets up GL to render the light's shadow map. The proper transform
68  // will be set for you. If this returns false, shadow maps are not
69  // supported and no further shadow map calls should be made (including
70  // finishShadowMapRender). If this returns true, the shadow map is
71  // ready to be rendered to and finishShadowMapRender must be called
72  // after rendering to it. When rendering a point light, the scene must be
73  // rendered 6x for each face of the cube map, so 'face' is used to indicate
74  // which cube face is currently being rendered. 'samples' allows for
75  // multisampled shadow maps, providing better edge antialiaing.
76  bool prepShadowMapRender(RE_Render *r,
78  int area_index = 0);
79 
80  // When you are done rendering from the light's view, call this
81  // to clean up your GL state and return you to the main viewport.
82  void finishShadowMapRender(RE_Render *r,
84  int area_index = 0);
85 
86 
87  void setupCascadeMap(RE_Render *r,
88  RE_Shader *sh,
89  RE_UniformBlock *block);
90 
91  // Sets up a distribution of point cube maps to approximate the area shape.
92  void setAreaLightShape(RE_LightAreaShape sh) { myAreaShape=sh; }
93  RE_LightAreaShape getAreaLightShape() const { return myAreaShape; }
94 
95  // Returns the number of area maps for a given light shape.
96  int getNumAreaMaps() const;
97 
98  // View frustum points (near plane TL, TR, BL, BR; then ditto for far plane)
99  // Will swap out the contents of pnts.
100  void setViewFrustum(UT_Vector3FArray &pnts, UT_Vector3F &campos);
101  void setSceneBounds(const UT_BoundingBox &scene_bounds);
102 
103  // Single point lights have one shadow map, while area lights have 4.
104  RE_Texture *getShadowMap(int area_index = 0);
105 
106  // Allows for versioned updates to shadow maps.
107  void setVersion(int64 version) { myVersion = version; }
108  int64 getVersion() const { return myVersion; }
109 
110  void setFrameTime(fpreal t) { myTime = t; }
111  fpreal getFrameTime() const { return myTime; }
112 
113  // Only valid after the shadowmap has been created.
114  void getZClipRange(fpreal &n, fpreal &f) { n = myZNear; f=myZFar; }
115 
116  // generic method which can be used to rotate a view toward a specific cube
117  // face.
118  static UT_Matrix4D computeCubeTransform(RE_TextureCubeFace face,
121 private:
122  bool prepShadowCubeRender(RE_Render *r, int area_index);
123  void finishShadowCubeRender(RE_Render *r, int area_index);
124 
125  bool prepCascadeMapRender(RE_Render *r);
126  void finishCascadeMapRender(RE_Render *r);
127 
128  void clearMaps(bool keep_first_map);
129 
130  RE_OGLFramebuffer *getShadowFrameBuffer(RE_Render *r,
131  RE_OGLTexture *&tex,
132  bool cube,
133  RE_TextureCubeFace face,
134  int area_index);
135 
136  static bool shTexDeleted(RE_Texture *tex, void *shmapobj);
137 
138  UT_Vector4 getAreaLinePos(int index);
139  UT_Vector4 getAreaGridPos(int index);
140  UT_Vector4 getAreaDiskPos(int index);
141  UT_Vector4 getAreaCubePos(int index);
142  UT_Vector4 getAreaSpherePos(int index);
143 
144  const RE_Light *myLight;
145  int myShadowMapSize;
146  RE_OGLFramebuffer *myShadowMapFBO;
147  RE_OGLFramebuffer *myShadowCubeFBO;
148  RE_OGLFramebuffer *myShadowCascadeFBO;
149  RE_Texture2DMap *myShadowHandle[RE_SHADOW_MAX_MAPS];
150  RE_OGLTexture *myShadowMap[RE_SHADOW_MAX_MAPS];
151  RE_TextureCubeMap *myShadowCubeHandle[RE_SHADOW_MAX_AREA_MAPS];
152  RE_OGLTexture *myShadowCube[RE_SHADOW_MAX_AREA_MAPS];
153  RE_OGLTexture *myCascadeMap;
154  RE_OGLTexture *myShadowCubeZ;
155  RE_Uniform *myViewMatrix;
156  RE_Uniform *myProjMatrix;
157  int64 myVersion;
158  fpreal myTime;
159  bool myIsMultiMap;
160  bool myLinearDepthMap;
161  UT_DimRect myPrevViewport;
162  RE_LightAreaShape myAreaShape;
163  fpreal myZNear, myZFar;
164  UT_Vector3FArray myViewFrustum;
165  UT_Vector3F myViewCamPos;
166  UT_BoundingBox mySceneBounds;
167  UT_Vector4F myCascadeBias;
168  UT_Matrix4FArray myCascadeTransforms;
169 };
170 
171 #endif
void setVersion(int64 version)
Definition: RE_ShadowMap.h:107
#define RE_SHADOW_MAX_AREA_MAPS
Definition: RE_Light.h:37
RE_TextureCubeFace
#define RE_API
Definition: RE_API.h:10
#define RE_SHADOW_MAX_MAPS
Definition: RE_ShadowMap.h:32
int64 getVersion() const
Definition: RE_ShadowMap.h:108
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
void setAreaLightShape(RE_LightAreaShape sh)
Definition: RE_ShadowMap.h:92
fpreal getFrameTime() const
Definition: RE_ShadowMap.h:111
void getZClipRange(fpreal &n, fpreal &f)
Definition: RE_ShadowMap.h:114
void setFrameTime(fpreal t)
Definition: RE_ShadowMap.h:110
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
GA_API const UT_StringHolder transform
GLdouble t
Definition: glad.h:2397
GT_API const UT_StringHolder version
RE_LightAreaShape
Definition: RE_Light.h:41
fpreal64 fpreal
Definition: SYS_Types.h:278
RE_LightAreaShape getAreaLightShape() const
Definition: RE_ShadowMap.h:93
GLuint index
Definition: glcorearb.h:786
SIM_API const UT_StringHolder position
GLsizei mapsize
Definition: glad.h:2748
GLboolean r
Definition: glcorearb.h:1222