SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLLight.cpp
Go to the documentation of this file.
1 /**
2  * \file SLLight.cpp
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <SLLight.h>
11 #include <SLShadowMap.h>
12 
13 //-----------------------------------------------------------------------------
14 SLCol4f SLLight::globalAmbient = SLCol4f(0.1f, 0.1f, 0.1f, 1.0f);
15 SLfloat SLLight::gamma = 1.0f;
17 //-----------------------------------------------------------------------------
18 /**
19  * @brief Construct a new SLLight::SLLight object
20  * @remarks It is important that during instantiation NO OpenGL functions (gl*)
21  * get called because this constructor will be most probably called in a parallel
22  * thread from within an SLScene::registerAssetsToLoad or SLScene::assemble
23  * function. All objects that get rendered have to do their OpenGL initialization
24  * when they are used the first time during rendering in the main thread.
25  * @param ambiPower Ambient light power
26  * @param diffPower Diffuse light power
27  * @param specPower Specular light power
28  * @param id Light ID
29  */
31  SLfloat diffPower,
32  SLfloat specPower,
33  SLint id)
34 {
35  // Set parameter of SLLight
36  _id = id;
37  _isOn = true;
38  _spotCutOffDEG = 180.0f;
40  _spotExponent = 1.0f;
41  _createsShadows = false;
42  _shadowMap = nullptr;
43  _doSoftShadows = false;
44  _softShadowLevel = 1;
45  _shadowMinBias = 0.001f;
46  _shadowMaxBias = 0.008f;
47 
48  // Set parameters of inherited SLMaterial
49  _ambientColor.set(1, 1, 1);
50  _ambientPower = ambiPower;
51  _diffuseColor.set(1, 1, 1);
52  _diffusePower = diffPower;
53  _specularColor.set(1, 1, 1);
54  _specularPower = specPower;
55 
56  // By default, there is no attenuation set. This is physically not correct
57  // Default OpenGL: kc=1, kl=0, kq=0
58  // Physically correct: kc=0, kl=0, kq=1
59  // set quadratic attenuation with d = distance to light
60  // 1
61  // attenuation = ------------------
62  // kc + kl*d + kq*d*d
63 
64  // init values to avoid used of uninitialized values
65  _kc=1.0f;
66  _kl=0.0f;
67  _kq=0.0f;
68 
69  kc(1.0f);
70  kl(0.0f);
71  kq(0.0f);
72 }
73 //-----------------------------------------------------------------------------
75 {
76  _kc = kc;
77  _isAttenuated = !(_kc == 1.0f && _kl == 0.0f && _kq == 0.0f);
78 }
79 //-----------------------------------------------------------------------------
81 {
82  _kl = kl;
83  _isAttenuated = !(_kc == 1.0f && _kl == 0.0f && _kq == 0.0f);
84 }
85 //-----------------------------------------------------------------------------
87 {
88  _kq = kq;
89  _isAttenuated = !(_kc == 1.0f && _kl == 0.0f && _kq == 0.0f);
90 }
91 //-----------------------------------------------------------------------------
92 void SLLight::spotCutOffDEG(const SLfloat cutOffAngleDEG)
93 {
94  _spotCutOffDEG = cutOffAngleDEG;
96 }
97 //-----------------------------------------------------------------------------
98 void SLLight::createsShadows(SLbool createsShadows)
99 {
101 }
102 //-----------------------------------------------------------------------------
103 //! SLLight::renderShadowMap renders the shadow map of the light
105 {
106  // Check if no shadow map was created at load time
107  if (!_shadowMap)
108  {
109  this->createShadowMap();
110  }
111 
112  _shadowMap->renderShadows(sv, root);
113 }
114 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
bool SLbool
Definition: SL.h:175
int SLint
Definition: SL.h:170
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
SLfloat kc() const
Definition: SLLight.h:143
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 _shadowMaxBias
Max. bias at 90 deg. to use to prevent shadow acne.
Definition: SLLight.h:228
SLbool createsShadows() const
Definition: SLLight.h:148
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
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 _isOn
Flag if light is on or off.
Definition: SLLight.h:209
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
SLfloat _specularPower
Specular light power (0-N)
Definition: SLLight.h:215
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
SLfloat _spotExponent
Spot attenuation from center to edge of cone.
Definition: SLLight.h:218
SLCol4f _diffuseColor
Diffuse light color (RGB 0-1)
Definition: SLLight.h:212
SLfloat _spotCutOffDEG
Half the spot cone angle.
Definition: SLLight.h:216
SLfloat _ambientPower
Ambient light power (0-N)
Definition: SLLight.h:211
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
SLfloat _spotCosCutOffRAD
cosine of spotCutoff angle
Definition: SLLight.h:217
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
SLint id() const
Definition: SLLight.h:132
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
void renderShadows(SLSceneView *sv, SLNode *root)
void set(const T X, const T Y, const T Z, const T W=1)
Definition: SLVec4.h:49
static const float DEG2RAD
Definition: Utils.h:239