SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLLight.h
Go to the documentation of this file.
1 /**
2  * \file SLLight.h
3  * \authors Marcus Hudritsch
4  * \date July 2014
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifndef SLLIGHTGL_H
12 #define SLLIGHTGL_H
13 
14 #include <SL.h>
15 #include <SLVec4.h>
16 #include <SLShadowMap.h>
17 
18 #ifdef SL_HAS_OPTIX
19 # include <SLOptixDefinitions.h>
20 # include <SLOptixHelper.h>
21 //#include <SLOptixVectorMath.h>
22 #endif
23 
24 class SLRay;
25 class SLNode;
26 class SLSceneView;
27 
28 //-----------------------------------------------------------------------------
29 //! Struct for uniform buffer with std140 layout
31 {
32  SLint isOn; // 1
33  SLVec4f posWS; // 2, 3, 4, 5
34  SLVec4f posVS; // 6, 7, 8, 9
35  SLVec4f ambient; // 10,11,12,13
36  SLVec4f diffuse; // 14,15,16,17
37  SLVec4f specular; // 18,19,20,21
38  SLVec3f spotDirWS; // 22,23,24
40  SLVec3f spotDirVS; // 26,27,28
45  SLVec3f attentuation; // 33,34,35
49  SLint doesPCF; // 39
52  SLMat4f space[6]; // 42-133 (92=4x4x6)
53 };
54 //-----------------------------------------------------------------------------
55 //! Abstract Light class for OpenGL light sources.
56 /*! The abstract SLLight class encapsulates an invisible light source according
57 to the OpenGL specification. The derivatives SLLightSpot and SLLightRect will
58 also derive from SLNode and can therefore be freely placed in space.
59 */
60 class SLLight
61 {
62 public:
63  explicit SLLight(SLfloat ambiPower = 0.1f,
64  SLfloat diffPower = 1.0f,
65  SLfloat specPower = 1.0f,
66  SLint id = -1);
67  virtual ~SLLight() = default;
68 
69  // Setters
70  void id(const SLint id) { _id = id; }
71  void isOn(const SLbool on) { _isOn = on; }
72 
73  //! Sets the ambient, diffuse and specular powers all with the same color
74  void powers(SLfloat ambiPow,
75  SLfloat diffPow,
76  SLfloat specPow,
77  const SLCol4f& ambiDiffSpecCol = SLCol4f::WHITE)
78  {
79  _ambientColor = ambiDiffSpecCol;
80  _diffuseColor = ambiDiffSpecCol;
81  _specularColor = ambiDiffSpecCol;
82  _ambientPower = ambiPow;
83  _diffusePower = diffPow;
84  _specularPower = specPow;
85  }
86 
87  //! Sets the ambient and diffuse powers with the same color
88  void ambiDiffPowers(SLfloat ambiPow,
89  SLfloat diffPow,
90  const SLCol4f& ambiDiffCol = SLCol4f::WHITE)
91  {
92  _ambientColor = ambiDiffCol;
93  _diffuseColor = ambiDiffCol;
94  _ambientPower = ambiPow;
95  _diffusePower = diffPow;
96  }
97 
98  //! Sets the same color to the ambient and diffuse colors
99  void ambiDiffColor(const SLCol4f& ambiDiffCol)
100  {
101  _ambientColor = ambiDiffCol;
102  _diffuseColor = ambiDiffCol;
103  }
104 
105  void ambientColor(const SLCol4f& ambi) { _ambientColor = ambi; }
106  void ambientPower(const SLfloat ambPow) { _ambientPower = ambPow; }
107  void diffuseColor(const SLCol4f& diff) { _diffuseColor = diff; }
108  void diffusePower(const SLfloat diffPow) { _diffusePower = diffPow; }
109  void specularColor(const SLCol4f& spec) { _specularColor = spec; }
110  void specularPower(const SLfloat specPow) { _specularPower = specPow; }
111  void spotExponent(const SLfloat exp) { _spotExponent = exp; }
112  void spotCutOffDEG(SLfloat cutOffAngleDEG);
113  void kc(SLfloat kc);
114  void kl(SLfloat kl);
115  void kq(SLfloat kq);
116  void attenuation(const SLfloat kConstant,
117  const SLfloat kLinear,
118  const SLfloat kQuadratic)
119  {
120  kc(kConstant);
121  kl(kLinear);
122  kq(kQuadratic);
123  }
126  void doSmoothShadows(SLbool doSS) { _doSoftShadows = doSS; }
127  void smoothShadowLevel(SLuint ssLevel) { _softShadowLevel = ssLevel; }
128  void shadowMinBias(SLfloat minBias) { _shadowMinBias = minBias; }
129  void shadowMaxBias(SLfloat maxBias) { _shadowMaxBias = maxBias; }
130 
131  // Getters
132  SLint id() const { return _id; }
133  SLbool isOn() const { return _isOn; }
135  SLfloat ambientPower() const { return _ambientPower; }
137  SLfloat diffusePower() const { return _diffusePower; }
142  SLfloat spotExponent() const { return _spotExponent; }
143  SLfloat kc() const { return _kc; }
144  SLfloat kl() const { return _kl; }
145  SLfloat kq() const { return _kq; }
146  SLbool isAttenuated() const { return _isAttenuated; }
147  SLfloat attenuation(SLfloat dist) const { return std::min(1.0f / (_kc + _kl * dist + _kq * dist * dist), 1.0f); }
150  SLbool doSoftShadows() const { return _doSoftShadows; }
154  virtual SLbool doCascadedShadows() const { return false; }
155 
156 #ifdef SL_HAS_OPTIX
157  virtual ortLight optixLight(bool)
158  {
159  return {
160  make_float4(diffuse()),
161  make_float4(ambient()),
162  make_float4(specular()),
163  make_float3({positionWS().x, positionWS().y, positionWS().z}),
164  spotCutOffDEG(),
165  spotExponent(),
166  spotCosCut(),
167  make_float3(spotDirWS()),
168  kc(),
169  kl(),
170  kq(),
171  {1, 1},
172  0.0f};
173  }
174 #endif
175 
176  // Virtual functions to be implemented by the inherited
177  virtual SLCol4f ambient() = 0; //!< Return normally _ambientColor * _ambientPower
178  virtual SLCol4f diffuse() = 0; //!< Returns normally _diffuseColor * _diffusePower
179  virtual SLCol4f specular() = 0; //!< Returns normally _specularColor * _specularPower
180  virtual SLVec4f positionWS() const = 0;
181  virtual SLVec3f spotDirWS() = 0;
182  virtual SLfloat shadowTest(SLRay* ray,
183  const SLVec3f& L,
184  SLfloat lightDist,
185  SLNode* root3D) = 0;
186  virtual SLfloat shadowTestMC(SLRay* ray,
187  const SLVec3f& L,
188  SLfloat lightDist,
189  SLNode* root3D) = 0;
190 
191  // Shadow Mapping functions
192  virtual void createShadowMap(float lightClipNear = 0.1f,
193  float lightClipFar = 20.0f,
194  SLVec2f size = SLVec2f(8, 8),
195  SLVec2i texSize = SLVec2i(1024, 1024)) = 0;
196  virtual void createShadowMapAutoSize(SLCamera* camera,
197  SLVec2i texSize = SLVec2i(1024, 1024),
198  int numCascades = 0) = 0;
199  virtual void renderShadowMap(SLSceneView* sv, SLNode* root);
200 
201  // statics valid for overall lighting
202  static SLCol4f globalAmbient; //!< static global ambient light intensity
203  static SLfloat oneOverGamma() { return 1.0f / gamma; }
204  static SLfloat gamma; //!< final output gamma value
205  static SLbool doColoredShadows; //!< flag if shadows should be displayed with colors for debugging
206 
207 protected:
208  SLint _id; //!< OpenGL light number (0-7)
209  SLbool _isOn; //!< Flag if light is on or off
210  SLCol4f _ambientColor; //!< Ambient light color (RGB 0-1)
211  SLfloat _ambientPower; //!< Ambient light power (0-N)
212  SLCol4f _diffuseColor; //!< Diffuse light color (RGB 0-1)
213  SLfloat _diffusePower; //!< Diffuse light power (0-N)
214  SLCol4f _specularColor; //!< Specular light color (RGB 0-1)
215  SLfloat _specularPower; //!< Specular light power (0-N)
216  SLfloat _spotCutOffDEG; //!< Half the spot cone angle
217  SLfloat _spotCosCutOffRAD; //!< cosine of spotCutoff angle
218  SLfloat _spotExponent; //!< Spot attenuation from center to edge of cone
219  SLfloat _kc; //!< Constant light attenuation
220  SLfloat _kl; //!< Linear light attenuation
221  SLfloat _kq; //!< Quadratic light attenuation
222  SLbool _isAttenuated; //!< fast attenuation flag for ray tracing
223  SLbool _createsShadows; //!< flag if light creates shadows or not
224  SLShadowMap* _shadowMap; //!< Used for shadow mapping
225  SLbool _doSoftShadows; //!< flag if percentage-closer filtering for smooth shadows is enabled
226  SLuint _softShadowLevel; //!< Radius to smoothing (1 = 3 * 3; 2 = 5 * 5; ...)
227  SLfloat _shadowMinBias; //!< Min. bias at 0 deg. to use to prevent shadow acne
228  SLfloat _shadowMaxBias; //!< Max. bias at 90 deg. to use to prevent shadow acne
229 };
230 //-----------------------------------------------------------------------------
231 //! STL vector of light pointers
232 typedef vector<SLLight*> SLVLight;
233 //-----------------------------------------------------------------------------
234 #endif
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
vector< SLLight * > SLVLight
STL vector of light pointers.
Definition: SLLight.h:232
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
Active or visible camera node class.
Definition: SLCamera.h:54
Abstract Light class for OpenGL light sources.
Definition: SLLight.h:61
SLfloat kc() const
Definition: SLLight.h:143
SLbool isOn() const
Definition: SLLight.h:133
void ambientColor(const SLCol4f &ambi)
Definition: SLLight.h:105
virtual SLVec3f spotDirWS()=0
virtual SLfloat shadowTest(SLRay *ray, const SLVec3f &L, SLfloat lightDist, SLNode *root3D)=0
SLint _id
OpenGL light number (0-7)
Definition: SLLight.h:208
SLCol4f _ambientColor
Ambient light color (RGB 0-1)
Definition: SLLight.h:210
SLuint _softShadowLevel
Radius to smoothing (1 = 3 * 3; 2 = 5 * 5; ...)
Definition: SLLight.h:226
SLfloat _kc
Constant light attenuation.
Definition: SLLight.h:219
SLfloat shadowMinBias() const
Definition: SLLight.h:152
SLfloat _shadowMaxBias
Max. bias at 90 deg. to use to prevent shadow acne.
Definition: SLLight.h:228
SLfloat attenuation(SLfloat dist) const
Definition: SLLight.h:147
SLCol4f ambientColor()
Definition: SLLight.h:134
SLbool createsShadows() const
Definition: SLLight.h:148
virtual SLbool doCascadedShadows() const
Definition: SLLight.h:154
SLShadowMap * _shadowMap
Used for shadow mapping.
Definition: SLLight.h:224
static SLfloat gamma
final output gamma value
Definition: SLLight.h:204
SLfloat kq() const
Definition: SLLight.h:145
SLfloat diffusePower() const
Definition: SLLight.h:137
virtual ~SLLight()=default
void shadowMaxBias(SLfloat maxBias)
Definition: SLLight.h:129
SLbool doSoftShadows() const
Definition: SLLight.h:150
void shadowMap(SLShadowMap *shadowMap)
Definition: SLLight.h:125
virtual SLCol4f specular()=0
Returns normally _specularColor * _specularPower.
SLfloat _diffusePower
Diffuse light power (0-N)
Definition: SLLight.h:213
SLbool _isAttenuated
fast attenuation flag for ray tracing
Definition: SLLight.h:222
SLfloat spotCutOffDEG() const
Definition: SLLight.h:140
SLbool isAttenuated() const
Definition: SLLight.h:146
SLbool _isOn
Flag if light is on or off.
Definition: SLLight.h:209
virtual SLVec4f positionWS() const =0
SLfloat ambientPower() const
Definition: SLLight.h:135
static SLbool doColoredShadows
flag if shadows should be displayed with colors for debugging
Definition: SLLight.h:205
static SLCol4f globalAmbient
static global ambient light intensity
Definition: SLLight.h:202
SLfloat _kq
Quadratic light attenuation.
Definition: SLLight.h:221
void ambientPower(const SLfloat ambPow)
Definition: SLLight.h:106
void ambiDiffPowers(SLfloat ambiPow, SLfloat diffPow, const SLCol4f &ambiDiffCol=SLCol4f::WHITE)
Sets the ambient and diffuse powers with the same color.
Definition: SLLight.h:88
SLfloat spotCosCut() const
Definition: SLLight.h:141
void doSmoothShadows(SLbool doSS)
Definition: SLLight.h:126
void id(const SLint id)
Definition: SLLight.h:70
SLfloat _specularPower
Specular light power (0-N)
Definition: SLLight.h:215
static SLfloat oneOverGamma()
Definition: SLLight.h:203
void specularPower(const SLfloat specPow)
Definition: SLLight.h:110
void isOn(const SLbool on)
Definition: SLLight.h:71
SLfloat specularPower() const
Definition: SLLight.h:139
virtual void renderShadowMap(SLSceneView *sv, SLNode *root)
SLLight::renderShadowMap renders the shadow map of the light.
Definition: SLLight.cpp:104
virtual void createShadowMap(float lightClipNear=0.1f, float lightClipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024))=0
SLLight(SLfloat ambiPower=0.1f, SLfloat diffPower=1.0f, SLfloat specPower=1.0f, SLint id=-1)
Construct a new SLLight::SLLight object.
Definition: SLLight.cpp:30
virtual void createShadowMapAutoSize(SLCamera *camera, SLVec2i texSize=SLVec2i(1024, 1024), int numCascades=0)=0
SLCol4f diffuseColor()
Definition: SLLight.h:136
SLCol4f specularColor()
Definition: SLLight.h:138
SLfloat _spotExponent
Spot attenuation from center to edge of cone.
Definition: SLLight.h:218
SLuint softShadowLevel() const
Definition: SLLight.h:151
SLCol4f _diffuseColor
Diffuse light color (RGB 0-1)
Definition: SLLight.h:212
SLShadowMap * shadowMap()
Definition: SLLight.h:149
void smoothShadowLevel(SLuint ssLevel)
Definition: SLLight.h:127
virtual SLCol4f diffuse()=0
Returns normally _diffuseColor * _diffusePower.
SLfloat _spotCutOffDEG
Half the spot cone angle.
Definition: SLLight.h:216
SLfloat _ambientPower
Ambient light power (0-N)
Definition: SLLight.h:211
virtual SLCol4f ambient()=0
Return normally _ambientColor * _ambientPower.
SLbool _createsShadows
flag if light creates shadows or not
Definition: SLLight.h:223
SLfloat _shadowMinBias
Min. bias at 0 deg. to use to prevent shadow acne.
Definition: SLLight.h:227
void diffusePower(const SLfloat diffPow)
Definition: SLLight.h:108
void diffuseColor(const SLCol4f &diff)
Definition: SLLight.h:107
SLfloat _spotCosCutOffRAD
cosine of spotCutoff angle
Definition: SLLight.h:217
void specularColor(const SLCol4f &spec)
Definition: SLLight.h:109
virtual SLfloat shadowTestMC(SLRay *ray, const SLVec3f &L, SLfloat lightDist, SLNode *root3D)=0
SLfloat spotExponent() const
Definition: SLLight.h:142
SLfloat shadowMaxBias() const
Definition: SLLight.h:153
SLfloat _kl
Linear light attenuation.
Definition: SLLight.h:220
SLbool _doSoftShadows
flag if percentage-closer filtering for smooth shadows is enabled
Definition: SLLight.h:225
SLfloat kl() const
Definition: SLLight.h:144
SLCol4f _specularColor
Specular light color (RGB 0-1)
Definition: SLLight.h:214
void ambiDiffColor(const SLCol4f &ambiDiffCol)
Sets the same color to the ambient and diffuse colors.
Definition: SLLight.h:99
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
void powers(SLfloat ambiPow, SLfloat diffPow, SLfloat specPow, const SLCol4f &ambiDiffSpecCol=SLCol4f::WHITE)
Sets the ambient, diffuse and specular powers all with the same color.
Definition: SLLight.h:74
void shadowMinBias(SLfloat minBias)
Definition: SLLight.h:128
void spotExponent(const SLfloat exp)
Definition: SLLight.h:111
SLint id() const
Definition: SLLight.h:132
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
Ray class with ray and intersection properties.
Definition: SLRay.h:40
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
T z
Definition: SLVec4.h:32
static SLVec4 WHITE
Definition: SLVec4.h:215
T y
Definition: SLVec4.h:32
T x
Definition: SLVec4.h:32
Struct for uniform buffer with std140 layout.
Definition: SLLight.h:31
SLint isOn
Definition: SLLight.h:32
SLVec3f spotDirVS
Definition: SLLight.h:40
SLint doesPCF
Definition: SLLight.h:49
SLVec4f posWS
Definition: SLLight.h:33
SLfloat spotCutoff
Definition: SLLight.h:42
SLVec4f ambient
Definition: SLLight.h:35
SLfloat __pad25
Definition: SLLight.h:39
SLint doAttenuation
Definition: SLLight.h:47
SLint usesCubemap
Definition: SLLight.h:51
SLVec4f posVS
Definition: SLLight.h:34
SLfloat __pad36
Definition: SLLight.h:46
SLVec4f diffuse
Definition: SLLight.h:36
SLMat4f space[6]
Definition: SLLight.h:52
SLVec3f attentuation
Definition: SLLight.h:45
SLuint levelPCF
Definition: SLLight.h:50
SLfloat __pad29
Definition: SLLight.h:41
SLVec4f specular
Definition: SLLight.h:37
SLfloat spotExp
Definition: SLLight.h:44
SLint createsShadows
Definition: SLLight.h:48
SLVec3f spotDirWS
Definition: SLLight.h:38
SLfloat spotCosCut
Definition: SLLight.h:43