SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLShadowMap.h
Go to the documentation of this file.
1 /**
2  * \file SLShadowMap.h
3  * \date May 2020
4  * \remarks Please use clangformat to format the code. See more code style on
5  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
6  * \authors Michael Schertenleib, Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #ifndef SLSHADOWMAP_H
11 #define SLSHADOWMAP_H
12 
13 #include <SL.h>
14 #include <SLEnums.h>
15 #include <SLPlane.h>
16 #include <SLMat4.h>
17 #include <SLNode.h>
18 #include <SLGLDepthBuffer.h>
19 
20 class SLGLVertexArrayExt;
21 class SLLight;
22 class SLLightDirect;
23 class SLLightSpot;
24 class SLMaterial;
25 class SLMaterial;
26 class SLSceneView;
27 class SLCamera;
28 //-----------------------------------------------------------------------------
29 //! Class for standard and cascaded shadow mapping
30 /*! Shadow mapping is a technique to render shadows. The scene gets rendered
31  * from the point of view of the lights which cast shadows. The resulting
32  * depth-map of that render-pass can be used to determine which fragments are
33  * affected by which lights. The standard fixed size shadow maps can be used
34  * with all light types. The auto sized shadow maps get automatically sized
35  * to a specified camera. At the moment only directional light get supported
36  * with multiple cascaded shadow maps.
37  */
39 {
40 public:
41  //! Ctor for standard fixed sized shadow mapping without cascades
42  SLShadowMap(SLLight* light,
43  const SLfloat lightClipNear = 0.1f,
44  const SLfloat lightClipFar = 20.0f,
45  const SLVec2f& size = SLVec2f(8, 8),
46  const SLVec2i& texSize = SLVec2i(1024, 1024));
47 
48  //! Ctor for standard auto sized shadow mapping with cascades
49  SLShadowMap(SLLight* light,
51  const SLVec2i& texSize = SLVec2i(1024, 1024),
52  const SLint numCascades = 4);
53 
54  ~SLShadowMap();
55 
56  // Public methods
57  void renderShadows(SLSceneView* sv, SLNode* root);
58  void drawFrustum();
59  void drawRays();
60 
61  // Setters
66  void size(const SLVec2f& size)
67  {
68  _size.set(size);
69  _halfSize.set(size / 2);
70  }
73  void cascadesFactor(float factor) { _cascadesFactor = factor; }
74 
75  // Getters
77  SLbool useCubemap() const { return _useCubemap; }
78  SLbool useCascaded() const { return _useCascaded; }
82  SLVec2i rayCount() { return _rayCount; }
85  SLVec2f size() { return _size; }
87  int numCascades() { return _numCascades; }
88  int maxCascades() { return _maxCascades; }
89  float cascadesFactor() { return _cascadesFactor; }
90  SLCamera* camera() { return _camera; }
91 
92  static SLuint drawCalls; //!< NO. of draw calls for shadow mapping
93 
94 private:
95  void updateLightSpaces();
98  float camClipNear,
99  float camClipFar);
101  SLSceneView* sv,
102  SLMat4f& lightView);
103  void lightCullingAdaptiveRec(SLNode* node,
104  SLMat4f& lightProj,
105  SLMat4f& lightView,
106  SLPlane* lightFrustumPlanes,
107  SLVNode& visibleNodes);
108  void drawNodesDirectionalCulling(SLVNode visibleNodes,
109  SLSceneView* sv,
110  SLMat4f& lightView);
111 
112 private:
113  SLLight* _light; //!< The light which uses this shadow map
114  SLProjType _projection; //!< Projection to use to create shadow map
115  SLbool _useCubemap; //!< Flag if cubemap should be used for perspective projections
116  SLbool _useCascaded; //!< Flag if cascaded shadow maps should be used
117  SLint _numCascades; //!< Number of cascades for directional light shadow mapping
118  SLint _maxCascades; //!< Max. number of cascades for for which the shader gets generated
119  SLfloat _cascadesFactor; //!< Factor that determines the cascades distribution
120  SLMat4f _lightView[6]; //!< Light view matrices
121  SLMat4f _lightProj[6]; //!< Light projection matrices
122  SLMat4f _lightSpace[6]; //!< Light space matrices (= _lightProj * _lightView)
123  SLGLVDepthBuffer _depthBuffers; //!< Vector of framebuffers with texture
124  SLGLVertexArrayExt* _frustumVAO; //!< Visualization of light-space-frustum
125  SLVec2i _rayCount; //!< Amount of rays drawn by drawRays()
126  SLMaterial* _material; //!< Material used to render the shadow map
127  SLfloat _lightClipNear; //!< Light frustum near clipping plane
128  SLfloat _lightClipFar; //!< Light frustum far clipping plane
129  SLVec2f _size; //!< Height and width of the frustum (only for SLLightDirect non cascaded)
130  SLVec2f _halfSize; //!< _size divided by two (only for SLLightDirect non cascaded)
131  SLVec2i _textureSize; //!< Size of the shadow map texture
132  SLCamera* _camera; //!< Camera to witch the light frustums are adapted
133 };
134 //-----------------------------------------------------------------------------
135 #endif // SLSHADOWMAP_H
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
int SLint
Definition: SL.h:170
SLProjType
Enumeration for different camera projections.
Definition: SLEnums.h:134
Uses an OpenGL framebuffer object as a depth-buffer.
std::vector< SLGLDepthBuffer * > SLGLVDepthBuffer
deque< SLNode * > SLVNode
SLVNode typedef for a vector of SLNodes.
Definition: SLNode.h:26
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
Active or visible camera node class.
Definition: SLCamera.h:54
SLGLVertexArray adds Helper Functions for quick Line & Point Drawing.
SLLightDirect class for a directional light source.
Definition: SLLightDirect.h:40
Abstract Light class for OpenGL light sources.
Definition: SLLight.h:61
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
Defines a plane in 3D space with the equation ax + by + cy + d = 0.
Definition: SLPlane.h:25
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
Class for standard and cascaded shadow mapping.
Definition: SLShadowMap.h:39
void numCascades(int numCascades)
Definition: SLShadowMap.h:72
SLGLVDepthBuffer _depthBuffers
Vector of framebuffers with texture.
Definition: SLShadowMap.h:123
SLShadowMap(SLLight *light, const SLfloat lightClipNear=0.1f, const SLfloat lightClipFar=20.0f, const SLVec2f &size=SLVec2f(8, 8), const SLVec2i &texSize=SLVec2i(1024, 1024))
Ctor for standard fixed sized shadow mapping without cascades.
Definition: SLShadowMap.cpp:38
SLProjType _projection
Projection to use to create shadow map.
Definition: SLShadowMap.h:114
int numCascades()
Definition: SLShadowMap.h:87
int maxCascades()
Definition: SLShadowMap.h:88
SLbool useCascaded() const
Definition: SLShadowMap.h:78
SLbool _useCascaded
Flag if cascaded shadow maps should be used.
Definition: SLShadowMap.h:116
void renderDirectionalLightCascaded(SLSceneView *sv, SLNode *root)
SLMat4f _lightView[6]
Light view matrices.
Definition: SLShadowMap.h:120
SLGLVDepthBuffer depthBuffers()
Definition: SLShadowMap.h:81
SLVec2i textureSize()
Definition: SLShadowMap.h:86
SLGLDepthBuffer * depthBuffer()
Definition: SLShadowMap.h:80
SLint _numCascades
Number of cascades for directional light shadow mapping.
Definition: SLShadowMap.h:117
SLVec2i _rayCount
Amount of rays drawn by drawRays()
Definition: SLShadowMap.h:125
SLProjType projection()
Definition: SLShadowMap.h:76
SLCamera * _camera
Camera to witch the light frustums are adapted.
Definition: SLShadowMap.h:132
void drawNodesIntoDepthBufferRec(SLNode *node, SLSceneView *sv, SLMat4f &lightView)
SLMaterial * _material
Material used to render the shadow map.
Definition: SLShadowMap.h:126
void updateLightSpaces()
SLShadowMap::updateLightSpaces updates a light view projection matrix.
SLint _maxCascades
Max. number of cascades for for which the shader gets generated.
Definition: SLShadowMap.h:118
void rayCount(const SLVec2i &rayCount)
Definition: SLShadowMap.h:63
static SLuint drawCalls
NO. of draw calls for shadow mapping.
Definition: SLShadowMap.h:92
void useCubemap(SLbool useCubemap)
Definition: SLShadowMap.h:62
SLMat4f _lightProj[6]
Light projection matrices.
Definition: SLShadowMap.h:121
SLbool useCubemap() const
Definition: SLShadowMap.h:77
SLGLVertexArrayExt * _frustumVAO
Visualization of light-space-frustum.
Definition: SLShadowMap.h:124
void drawFrustum()
SLShadowMap::drawFrustum draws the volume affected by the shadow map.
SLfloat _lightClipNear
Light frustum near clipping plane.
Definition: SLShadowMap.h:127
SLVec2f _halfSize
_size divided by two (only for SLLightDirect non cascaded)
Definition: SLShadowMap.h:130
SLVec2i _textureSize
Size of the shadow map texture.
Definition: SLShadowMap.h:131
void clipNear(SLfloat clipNear)
Definition: SLShadowMap.h:64
SLVec2i rayCount()
Definition: SLShadowMap.h:82
SLMat4f _lightSpace[6]
Light space matrices (= _lightProj * _lightView)
Definition: SLShadowMap.h:122
SLfloat _cascadesFactor
Factor that determines the cascades distribution.
Definition: SLShadowMap.h:119
void size(const SLVec2f &size)
Definition: SLShadowMap.h:66
SLfloat lightClipNear()
Definition: SLShadowMap.h:83
void textureSize(const SLVec2i &textureSize)
Definition: SLShadowMap.h:71
void clipFar(SLfloat clipFar)
Definition: SLShadowMap.h:65
SLfloat lightClipFar()
Definition: SLShadowMap.h:84
SLVec2f _size
Height and width of the frustum (only for SLLightDirect non cascaded)
Definition: SLShadowMap.h:129
void cascadesFactor(float factor)
Definition: SLShadowMap.h:73
void lightCullingAdaptiveRec(SLNode *node, SLMat4f &lightProj, SLMat4f &lightView, SLPlane *lightFrustumPlanes, SLVNode &visibleNodes)
void renderShadows(SLSceneView *sv, SLNode *root)
float cascadesFactor()
Definition: SLShadowMap.h:89
SLLight * _light
The light which uses this shadow map.
Definition: SLShadowMap.h:113
void drawRays()
SLfloat _lightClipFar
Light frustum far clipping plane.
Definition: SLShadowMap.h:128
SLVVec2f getShadowMapCascades(int numCascades, float camClipNear, float camClipFar)
void drawNodesDirectionalCulling(SLVNode visibleNodes, SLSceneView *sv, SLMat4f &lightView)
SLVec2f size()
Definition: SLShadowMap.h:85
SLCamera * camera()
Definition: SLShadowMap.h:90
SLbool _useCubemap
Flag if cubemap should be used for perspective projections.
Definition: SLShadowMap.h:115
SLMat4f * lightSpace()
Definition: SLShadowMap.h:79
void set(const T X, const T Y)
Definition: SLVec2.h:40