reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofShadow.h
Go to the documentation of this file.
1//
2// ofShadow.h
3// openFrameworksLib
4//
5// Created by Nick Hardeman on 10/3/22.
6//
7
8#pragma once
9
10#include "ofShader.h"
11#include "ofNode.h"
12
20
21class ofLight;
23
24class ofShadow {
25public:
26
27 struct GLData {
28 GLuint fboId = 0;
29 GLuint texId = 0;
30 bool bAllocated = false;
31 bool bFboAllocated = false;
32
33#if defined(TARGET_OPENGLES)
34 int width = 512;
35 int height = 512;
36#else
37 int width = 1024;
38 int height = 1024;
39#endif
40
41 int totalShadows = 0;
42 };
43
44 class Data{
45 public:
46 // position, direction, up and right are in world space
47 glm::vec3 position = {0,0,0};
48 glm::vec3 direction = {1,0,0};
49
50 glm::vec3 up = {0,1,0};
51 glm::vec3 right = {1,0,0};
52
53 int lightType = 0;
54#if defined(TARGET_OPENGLES)
56#else
58#endif
59
60 glm::mat4 shadowMatrix;
61 float strength = 0.5;
62
63 int texIndex = 0;
64
65 bool isEnabled = false;
66 float bias = 0.005f;
67 float normalBias = 0.0;
68
70
71 int index = -1;
72 float nearClip = 1;
73 float farClip = 1500;
74 float sampleRadius = 1.f;
75 };
76
77 static GLenum getTextureTarget( int aLightType );
78 static int getNumTotalPossibleShadows( int aLightType );
79
80 static void setDepthMapResolution( int aLightType, int ares );
81 static void setDepthMapResolution( int aLightType, int awidth, int aheight );
82
83 static int getDepthMapWidth(int aLightType);
84 static int getDepthMapHeight(int aLightType);
85
86 static GLuint getPointTexId();
87 static GLuint getDirectionalTexId();
88 static GLuint getSpotTexId();
89 static GLuint getAreaTexId();
90
91 static bool hasActiveShadows();
92 static std::string getShadowTypeAsString( ofShadowType atype );
93
94 static void enableAllShadows();
95 static void disableAllShadows();
96 static void setAllShadowTypes( ofShadowType atype );
97 static void setAllShadowDepthResolutions( int awidth, int aheight );
98 static void setAllShadowBias( float bias );
99 static void setAllShadowNormalBias( float normalBias );
100 static void setAllShadowSampleRadius( float sampleRadius );
101 static std::string getShaderDefinesAsString();
102
103 static bool areShadowsSupported();
104
105 ofShadow();
106 ~ofShadow();
107
108 void setLightType( int atype );
109
110 void update( const ofLight& alight );
111
112 bool beginDepth();
113 bool endDepth();
114
115 bool beginDepth(GLenum aCubeFace);
116 bool endDepth(GLenum aCubeFace);
117
118 void clear();
119
120 const std::shared_ptr<ofShadow::Data>& getData() const { return data; }
121 bool getIsEnabled() { return data->isEnabled; }
122 void setEnabled( bool ab );
123
124 float getNearClip() { return data->nearClip; }
125 float getFarClip() { return data->farClip; }
126 void setNearClip( float anear ) { data->nearClip = anear; }
127 void setFarClip( float afar ) { data->farClip = afar; }
128
129 const bool isMultiCubeFacePass() const;
130 const bool isSingleOmniPass() const;
131 const int getNumShadowDepthPasses() const;
132 void setSingleOmniPass( bool ab );
133 int getDepthMapWidth();
134 int getDepthMapHeight();
135
136 void setDirectionalBounds( float aWorldWidth, float aWorldHeight );
137
138 void setShadowType(ofShadowType atype) {data->shadowType = atype;}
139
144
145 GLuint getDepthMapFboId();
146 GLuint getDepthMapTexId();
147
148 const float& getStrength() { return data->strength; }
149 void setStrength(float astrength) { data->strength = astrength; }
150
151 const float getBias() { return data->bias; }
152 void setBias( float abias ) { data->bias = abias; }
153
154 const float getNormalBias() { return data->normalBias; }
155 void setNormalBias( float abias ) { data->normalBias = abias; }
156
157 const float getSampleRadius() { return data->sampleRadius; }
158 void setSampleRadius( float aradius ) { data->sampleRadius = aradius; }
159
160 void setAreaLightSize( float awidth, float aheight ) {mAreaLightWidth=awidth;mAreaLightHeight=aheight;};
161
162 void drawFrustum();
163 std::vector<glm::vec3> getFrustumCorners( const glm::vec3& aup, const glm::vec3& aright, const glm::vec3& afwd );
164
165 std::string getShadowTypeAsString();
166
167 const ofShader & getDepthShader(ofGLProgrammableRenderer & renderer) const;
168 bool setupShadowDepthShader(ofShader& ashader, const std::string aShaderMain);
169 bool setupShadowDepthShader(ofShader& ashader, int aLightType, const std::string aShaderMain);
170 void updateDepth(const ofShader & shader,ofGLProgrammableRenderer & renderer) const;
171 void updateDepth(const ofShader & shader,GLenum aCubeFace,ofGLProgrammableRenderer & renderer) const;
172
173protected:
174
175 static void _updateTexDataIds();
176
177 void _drawFrustum( const glm::vec3& aup, const glm::vec3& aright, const glm::vec3& afwd );
178
179 void _allocate();
180
181 void _checkSetup();
182 void _allocateFbo();
183 void _checkFbos();
184 void _updateNumShadows();
185
186 std::shared_ptr<ofShadow::Data> data;
187
188 bool mBSinglePass = true;
189
190 glm::mat4 mShadowProjection = glm::mat4(1.0);
191
192 std::vector<glm::mat4> mLookAtMats;
193 std::vector<glm::mat4> mViewProjMats;
194
195 float mOrthoScaleX = 1.0;
196 float mOrthoScaleY = 1.0;
199
200 float mAreaLightWidth = 100;
201 float mAreaLightHeight = 50.0;
202
203 float mFov = 90;
204
205 bool mBEnableCulling = true;
207
208 const glm::mat4 biasMatrix = glm::mat4(
209 0.5, 0.0, 0.0, 0.0,
210 0.0, 0.5, 0.0, 0.0,
211 0.0, 0.0, 0.5, 0.0,
212 0.5, 0.5, 0.5, 1.0
213 );
214
215private:
216 struct Shaders{
217 ofShader depth;
218 ofShader depthCube;
219 ofShader depthCubeMultiPass;
220 };
221 void initShaders(ofGLProgrammableRenderer & renderer) const;
222
223 mutable std::map<ofGLProgrammableRenderer*,std::shared_ptr<Shaders>> shaders;
224};
225
226std::vector<std::weak_ptr<ofShadow::Data> > & ofShadowsData();
Definition ofGLProgrammableRenderer.h:17
Definition ofLight.h:41
Definition ofShader.h:35
Definition ofShadow.h:44
int numDepthPasses
Definition ofShadow.h:69
bool isEnabled
Definition ofShadow.h:65
int index
Definition ofShadow.h:71
glm::mat4 shadowMatrix
Definition ofShadow.h:60
glm::vec3 right
Definition ofShadow.h:51
float nearClip
Definition ofShadow.h:72
float bias
Definition ofShadow.h:66
float farClip
Definition ofShadow.h:73
glm::vec3 position
Definition ofShadow.h:47
glm::vec3 direction
Definition ofShadow.h:48
int texIndex
Definition ofShadow.h:63
ofShadowType shadowType
Definition ofShadow.h:57
float normalBias
Definition ofShadow.h:67
float sampleRadius
Definition ofShadow.h:74
float strength
Definition ofShadow.h:61
int lightType
Definition ofShadow.h:53
glm::vec3 up
Definition ofShadow.h:50
Definition ofShadow.h:24
float mOrthoScaleX
Definition ofShadow.h:195
static void setDepthMapResolution(int aLightType, int ares)
Definition ofShadow.cpp:124
std::vector< glm::vec3 > getFrustumCorners(const glm::vec3 &aup, const glm::vec3 &aright, const glm::vec3 &afwd)
Definition ofShadow.cpp:798
void setSampleRadius(float aradius)
Definition ofShadow.h:158
static void enableAllShadows()
Definition ofShadow.cpp:208
~ofShadow()
Definition ofShadow.cpp:368
void _allocateFbo()
Definition ofShadow.cpp:965
float mOrthoScaleY
Definition ofShadow.h:196
float mDirectionalBoundsWidth
Definition ofShadow.h:197
void setStrength(float astrength)
Definition ofShadow.h:149
void setBias(float abias)
Definition ofShadow.h:152
static void setAllShadowDepthResolutions(int awidth, int aheight)
Definition ofShadow.cpp:250
bool isGlCullingEnabled()
Definition ofShadow.h:140
static std::string getShaderDefinesAsString()
Definition ofShadow.cpp:307
float mFov
Definition ofShadow.h:203
static void disableAllShadows()
Definition ofShadow.cpp:224
float mAreaLightWidth
Definition ofShadow.h:200
static void _updateTexDataIds()
Definition ofShadow.cpp:338
GLenum getFrontFaceWindingOrder()
Definition ofShadow.h:142
void setNormalBias(float abias)
Definition ofShadow.h:155
static void setAllShadowTypes(ofShadowType atype)
Definition ofShadow.cpp:235
void setDirectionalBounds(float aWorldWidth, float aWorldHeight)
Definition ofShadow.cpp:701
GLenum mGlFrontFaceWindingOrder
Definition ofShadow.h:206
static void setAllShadowSampleRadius(float sampleRadius)
Definition ofShadow.cpp:292
glm::mat4 mShadowProjection
Definition ofShadow.h:190
void setEnabled(bool ab)
Definition ofShadow.cpp:637
std::shared_ptr< ofShadow::Data > data
Definition ofShadow.h:186
GLuint getDepthMapFboId()
Definition ofShadow.cpp:707
const bool isSingleOmniPass() const
Definition ofShadow.cpp:658
std::vector< glm::mat4 > mLookAtMats
Definition ofShadow.h:192
void drawFrustum()
Definition ofShadow.cpp:717
int getDepthMapHeight()
Definition ofShadow.cpp:696
static GLuint getAreaTexId()
Definition ofShadow.cpp:173
bool beginDepth()
Definition ofShadow.cpp:477
void _drawFrustum(const glm::vec3 &aup, const glm::vec3 &aright, const glm::vec3 &afwd)
Definition ofShadow.cpp:729
bool mBEnableCulling
Definition ofShadow.h:205
static GLuint getDirectionalTexId()
Definition ofShadow.cpp:161
void setFrontFaceWindingOrder(GLenum aw)
Definition ofShadow.h:143
static GLenum getTextureTarget(int aLightType)
Definition ofShadow.cpp:92
const float & getStrength()
Definition ofShadow.h:148
float mDirectionalBoundsHeight
Definition ofShadow.h:198
void setNearClip(float anear)
Definition ofShadow.h:126
static bool areShadowsSupported()
Definition ofShadow.cpp:326
const float getBias()
Definition ofShadow.h:151
GLuint getDepthMapTexId()
Definition ofShadow.cpp:712
void clear()
Definition ofShadow.cpp:628
void _checkFbos()
Definition ofShadow.cpp:1098
const bool isMultiCubeFacePass() const
Definition ofShadow.cpp:650
std::string getShadowTypeAsString()
Definition ofShadow.cpp:875
static GLuint getSpotTexId()
Definition ofShadow.cpp:167
static int getNumTotalPossibleShadows(int aLightType)
Definition ofShadow.cpp:78
float getNearClip()
Definition ofShadow.h:124
const float getSampleRadius()
Definition ofShadow.h:157
ofShadow()
Definition ofShadow.cpp:357
const std::shared_ptr< ofShadow::Data > & getData() const
Definition ofShadow.h:120
const ofShader & getDepthShader(ofGLProgrammableRenderer &renderer) const
Definition ofShadow.cpp:880
void _checkSetup()
Definition ofShadow.cpp:935
void update(const ofLight &alight)
Definition ofShadow.cpp:387
bool mBSinglePass
Definition ofShadow.h:188
static void setAllShadowBias(float bias)
Definition ofShadow.cpp:262
static bool hasActiveShadows()
Definition ofShadow.cpp:196
float mAreaLightHeight
Definition ofShadow.h:201
void setShadowType(ofShadowType atype)
Definition ofShadow.h:138
bool getIsEnabled()
Definition ofShadow.h:121
const glm::mat4 biasMatrix
Definition ofShadow.h:208
void _allocate()
Definition ofShadow.cpp:959
const int getNumShadowDepthPasses() const
Definition ofShadow.cpp:663
const float getNormalBias()
Definition ofShadow.h:154
bool setupShadowDepthShader(ofShader &ashader, const std::string aShaderMain)
Definition ofShadow.cpp:1143
void setFarClip(float afar)
Definition ofShadow.h:127
bool endDepth()
Definition ofShadow.cpp:544
float getFarClip()
Definition ofShadow.h:125
int getDepthMapWidth()
Definition ofShadow.cpp:691
void setGlCullingEnabled(bool ab)
Definition ofShadow.h:141
static void setAllShadowNormalBias(float normalBias)
Definition ofShadow.cpp:277
static GLuint getPointTexId()
Definition ofShadow.cpp:155
void setSingleOmniPass(bool ab)
Definition ofShadow.cpp:671
void _updateNumShadows()
Definition ofShadow.cpp:1122
void setAreaLightSize(float awidth, float aheight)
Definition ofShadow.h:160
void updateDepth(const ofShader &shader, ofGLProgrammableRenderer &renderer) const
Definition ofShadow.cpp:900
void setLightType(int atype)
Definition ofShadow.cpp:373
std::vector< glm::mat4 > mViewProjMats
Definition ofShadow.h:193
std::vector< std::weak_ptr< ofShadow::Data > > & ofShadowsData()
Definition ofShadow.cpp:26
ofShadowType
Definition ofShadow.h:13
@ OF_SHADOW_TYPE_TOTAL
Definition ofShadow.h:18
@ OF_SHADOW_TYPE_PCF_MED
Definition ofShadow.h:16
@ OF_SHADOW_TYPE_PCF_LOW
Definition ofShadow.h:15
@ OF_SHADOW_TYPE_HARD
Definition ofShadow.h:14
@ OF_SHADOW_TYPE_PCF_HIGH
Definition ofShadow.h:17
Definition ofShadow.h:27
GLuint texId
Definition ofShadow.h:29
int totalShadows
Definition ofShadow.h:41
bool bAllocated
Definition ofShadow.h:30
bool bFboAllocated
Definition ofShadow.h:31
int height
Definition ofShadow.h:38
int width
Definition ofShadow.h:37
GLuint fboId
Definition ofShadow.h:28