SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLLightDirect Class Reference

SLLightDirect class for a directional light source. More...

#include <SLLightDirect.h>

Inheritance diagram for SLLightDirect:
[legend]

Public Member Functions

 SLLightDirect (SLAssetManager *assetMgr, SLScene *s, SLfloat arrowLength=0.5f, SLbool hasMesh=true, SLbool doCascadedShadows=false)
 Construct a new SLLightDirect::SLLightDirect object. More...
 
 SLLightDirect (SLAssetManager *assetMgr, SLScene *s, SLfloat posx, SLfloat posy, SLfloat posz, SLfloat arrowLength=0.5f, SLfloat ambiPower=1.0f, SLfloat diffPower=10.0f, SLfloat specPower=10.0f, SLbool hasMesh=true, SLbool doCascadedShadows=false)
 Construct a new SLLightDirect::SLLightDirect object. More...
 
 ~SLLightDirect () override
 
void init (SLScene *s)
 
bool hitRec (SLRay *ray) override
 
void statsRec (SLNodeStats &stats) override
 SLLightDirect::statsRec updates the statistic parameters. More...
 
void drawMesh (SLSceneView *sv) override
 
SLfloat shadowTest (SLRay *ray, const SLVec3f &L, SLfloat lightDist, SLNode *root3D) override
 
SLfloat shadowTestMC (SLRay *ray, const SLVec3f &L, SLfloat lightDist, SLNode *root3D) override
 
void createShadowMap (float clipNear=0.1f, float clipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024)) override
 
void createShadowMapAutoSize (SLCamera *camera, SLVec2i texSize=SLVec2i(1024, 1024), int numCascades=4) override
 
SLCol4f calculateSunLight (SLfloat standardPower)
 Calculates the sunlight color depending on the zenith angle. More...
 
void doSunPowerAdaptation (SLbool enabled)
 
void sunLightPowerMin (SLfloat minPower)
 
void doCascadedShadows (bool b)
 
SLfloat radius () const
 
SLfloat dirLength () const
 
SLbool doSunPowerAdaptation ()
 
SLTexColorLUTsunLightColorLUT ()
 
SLbool doCascadedShadows () const override
 
SLVec4f positionWS () const override
 
SLVec3f spotDirWS () override
 
SLCol4f ambient () override
 Returns the product of the ambient light color and the ambient light power. More...
 
SLCol4f diffuse () override
 Returns the product of the diffuse light color and the diffuse light power. More...
 
SLCol4f specular () override
 Returns the product of the specular light color and the specular light power. More...
 
- Public Member Functions inherited from SLNode
 SLNode (const SLstring &name="Node")
 Construct a new SLNode::SLNode object. More...
 
 SLNode (SLMesh *mesh, const SLstring &name="Node")
 
 SLNode (SLMesh *mesh, const SLVec3f &translation, const SLstring &name)
 
 ~SLNode () override
 
virtual void cull3DRec (SLSceneView *sv)
 
virtual void cullChildren3D (SLSceneView *sv)
 Initializer function to call SLNode::cull3DRec recursively. More...
 
virtual void cull2DRec (SLSceneView *sv)
 
virtual SLNodecopyRec ()
 
virtual SLAABBoxupdateAABBRec (SLbool updateAlsoAABBinOS)
 
virtual void dumpRec ()
 
void setDrawBitsRec (SLuint bit, SLbool state)
 
void setPrimitiveTypeRec (SLGLPrimitiveType primitiveType)
 
virtual void addMesh (SLMesh *mesh)
 
bool removeMesh ()
 Returns true if a mesh was assigned and set it to nullptr. More...
 
bool removeMesh (SLMesh *mesh)
 Returns true if the passed mesh was assigned and sets it to nullptr. More...
 
SLint numChildren ()
 
void addChild (SLNode *child)
 
bool insertChild (SLNode *insertC, SLNode *afterC)
 
void deleteChildren ()
 
bool deleteChild ()
 
bool deleteChild (SLNode *child)
 
bool deleteChild (const SLstring &name)
 
bool removeChild (SLNode *child)
 remove child from vector of children. Removes false if not found, else true. More...
 
template<typename T >
T * find (const SLstring &name="", SLbool findRecursive=true)
 
template<typename T >
T * findChild (const SLstring &name="", SLbool findRecursive=true)
 
template<typename T >
deque< T * > findChildren (const SLstring &name="", SLbool findRecursive=true, SLbool canContain=false)
 
deque< SLNode * > findChildren (const SLMesh *mesh, SLbool findRecursive=true)
 
deque< SLNode * > findChildren (SLuint drawbit, SLbool findRecursive=true)
 
SLVec3f translationOS () const
 
SLVec3f forwardOS () const
 
SLVec3f rightOS () const
 
SLVec3f upOS () const
 
SLVec3f axisXOS () const
 
SLVec3f axisYOS () const
 
SLVec3f axisZOS () const
 
SLVec3f translationWS () const
 
SLVec3f forwardWS () const
 
SLVec3f rightWS () const
 
SLVec3f upWS () const
 
SLVec3f axisXWS () const
 
SLVec3f axisYWS () const
 
SLVec3f axisZWS () const
 
void translation (const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
 
void translation (SLfloat x, SLfloat y, SLfloat z, SLTransformSpace relativeTo=TS_parent)
 
void rotation (const SLQuat4f &rot, SLTransformSpace relativeTo=TS_parent)
 
void rotation (SLfloat angleDeg, const SLVec3f &axis, SLTransformSpace relativeTo=TS_parent)
 
void scaling (SLfloat s)
 
void scaling (SLfloat x, SLfloat y, SLfloat z)
 
void scaling (const SLVec3f &scaling)
 
void lookAt (SLfloat targetX, SLfloat targetY, SLfloat targetZ, SLfloat upX=0, SLfloat upY=1, SLfloat upZ=0, SLTransformSpace relativeTo=TS_world)
 
void lookAt (const SLVec3f &target, const SLVec3f &up=SLVec3f::AXISY, SLTransformSpace relativeTo=TS_world)
 
void translate (const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
 
void translate (SLfloat x, SLfloat y, SLfloat z, SLTransformSpace relativeTo=TS_object)
 
void rotate (const SLQuat4f &rot, SLTransformSpace relativeTo=TS_object)
 
void rotate (SLfloat angleDeg, const SLVec3f &axis, SLTransformSpace relativeTo=TS_object)
 
void rotate (SLfloat angleDeg, SLfloat x, SLfloat y, SLfloat z, SLTransformSpace relativeTo=TS_object)
 
void rotateAround (const SLVec3f &point, SLVec3f &axis, SLfloat angleDeg, SLTransformSpace relativeTo=TS_world)
 
void scale (SLfloat s)
 
void scale (SLfloat x, SLfloat y, SLfloat z)
 
void scale (const SLVec3f &scale)
 
void scaleToCenter (SLfloat maxDim)
 
void setInitialState ()
 
void resetToInitialState ()
 
void parent (SLNode *p)
 
void entityID (SLint entityID)
 
void om (const SLMat4f &mat)
 
void animation (SLAnimation *a)
 
void castsShadows (SLbool castsShadows)
 
virtual void needUpdate ()
 
void needWMUpdate ()
 
void needAABBUpdate ()
 
void isSelected (bool isSelected)
 
void minLodCoverage (SLfloat minLodCoverage)
 
void levelForSM (SLubyte lfsm)
 
void onUpdateCB (function< void()> callbackFunc)
 
SLNodeparent ()
 
SLint depth () const
 
SLint entityID () const
 
const SLMat4fom ()
 
const SLMat4finitialOM ()
 
const SLMat4fupdateAndGetWM () const
 
const SLMat4fupdateAndGetWMI () const
 
SLDrawBitsdrawBits ()
 
SLbool drawBit (SLuint bit)
 
SLAABBoxaabb ()
 
SLAnimationanimation ()
 
SLbool castsShadows ()
 
SLMeshmesh ()
 
SLVNodechildren ()
 
const SLAnimSkeletonskeleton ()
 Returns the first skeleton found in the meshes. More...
 
void updateRec ()
 
virtual void doUpdate ()
 
bool updateMeshSkins (bool forceCPUSkinning, const std::function< void(SLMesh *)> &cbInformNodes)
 Update all skinned meshes recursively. More...
 
void updateMeshAccelStructs ()
 
void updateMeshMat (std::function< void(SLMaterial *m)> setMat, bool recursive)
 Updates the mesh material recursively with a material lambda. More...
 
void setMeshMat (SLMaterial *mat, bool recursive)
 Set the mesh material recursively. More...
 
bool isSelected ()
 
SLfloat minLodCoverage ()
 
SLubyte levelForSM ()
 
- Public Member Functions inherited from SLObject
 SLObject (const SLstring &Name="", const SLstring &url="")
 
virtual ~SLObject ()
 
void name (const SLstring &Name)
 
void url (const SLstring &url)
 
const SLstringname () const
 
const SLstringurl () const
 
- Public Member Functions inherited from SLEventHandler
 SLEventHandler ()
 
virtual ~SLEventHandler ()
 
virtual SLbool onMouseDown (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onMouseUp (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onMouseMove (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onDoubleClick (const SLMouseButton button, const SLint x, const SLint y, const SLKey mod)
 
virtual SLbool onMouseWheel (const SLint delta, const SLKey mod)
 
virtual SLbool onTouch2Down (const SLint x1, const SLint y1, const SLint x2, const SLint y2)
 
virtual SLbool onTouch2Move (const SLint x1, const SLint y1, const SLint x2, const SLint y2)
 
virtual SLbool onTouch2Up (const SLint x1, const SLint y1, const SLint x2, const SLint y2)
 
virtual SLbool onTouch3Down (const SLint x1, const SLint y1)
 
virtual SLbool onTouch3Move (const SLint x1, const SLint y1)
 
virtual SLbool onTouch3Up (const SLint x1, const SLint y1)
 
virtual SLbool onKeyPress (const SLKey key, const SLKey mod)
 
virtual SLbool onKeyRelease (const SLKey key, const SLKey mod)
 
virtual SLbool onRotationPYR (const SLfloat pitchRAD, const SLfloat yawRAD, const SLfloat rollRAD)
 
void mouseRotationFactor (SLfloat rf)
 
SLfloat mouseRotationFactor ()
 
- Public Member Functions inherited from SLLight
 SLLight (SLfloat ambiPower=0.1f, SLfloat diffPower=1.0f, SLfloat specPower=1.0f, SLint id=-1)
 Construct a new SLLight::SLLight object. More...
 
virtual ~SLLight ()=default
 
void id (const SLint id)
 
void isOn (const SLbool on)
 
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. More...
 
void ambiDiffPowers (SLfloat ambiPow, SLfloat diffPow, const SLCol4f &ambiDiffCol=SLCol4f::WHITE)
 Sets the ambient and diffuse powers with the same color. More...
 
void ambiDiffColor (const SLCol4f &ambiDiffCol)
 Sets the same color to the ambient and diffuse colors. More...
 
void ambientColor (const SLCol4f &ambi)
 
void ambientPower (const SLfloat ambPow)
 
void diffuseColor (const SLCol4f &diff)
 
void diffusePower (const SLfloat diffPow)
 
void specularColor (const SLCol4f &spec)
 
void specularPower (const SLfloat specPow)
 
void spotExponent (const SLfloat exp)
 
void spotCutOffDEG (SLfloat cutOffAngleDEG)
 
void kc (SLfloat kc)
 
void kl (SLfloat kl)
 
void kq (SLfloat kq)
 
void attenuation (const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
 
void createsShadows (SLbool createsShadows)
 
void shadowMap (SLShadowMap *shadowMap)
 
void doSmoothShadows (SLbool doSS)
 
void smoothShadowLevel (SLuint ssLevel)
 
void shadowMinBias (SLfloat minBias)
 
void shadowMaxBias (SLfloat maxBias)
 
SLint id () const
 
SLbool isOn () const
 
SLCol4f ambientColor ()
 
SLfloat ambientPower () const
 
SLCol4f diffuseColor ()
 
SLfloat diffusePower () const
 
SLCol4f specularColor ()
 
SLfloat specularPower () const
 
SLfloat spotCutOffDEG () const
 
SLfloat spotCosCut () const
 
SLfloat spotExponent () const
 
SLfloat kc () const
 
SLfloat kl () const
 
SLfloat kq () const
 
SLbool isAttenuated () const
 
SLfloat attenuation (SLfloat dist) const
 
SLbool createsShadows () const
 
SLShadowMapshadowMap ()
 
SLbool doSoftShadows () const
 
SLuint softShadowLevel () const
 
SLfloat shadowMinBias () const
 
SLfloat shadowMaxBias () const
 

Protected Member Functions

void renderShadowMap (SLSceneView *sv, SLNode *root) override
 SLLight::renderShadowMap renders the shadow map of the light. More...
 

Private Attributes

SLfloat _arrowRadius
 The sphere lights radius. More...
 
SLfloat _arrowLength
 Length of direction line. More...
 
SLbool _doSunPowerAdaptation
 Flag for sun power scaling. More...
 
SLfloat _sunLightPowerMin
 Min. zenith power scale factor for sun. More...
 
SLTexColorLUT _sunLightColorLUT
 Sun light color LUT. More...
 
bool _doCascadedShadows
 Cascaded shadow. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from SLLight
static SLfloat oneOverGamma ()
 
- Static Public Attributes inherited from SLNode
static SLuint numWMUpdates = 0
 NO. of calls to updateWMRec per frame. More...
 
static unsigned int instanceIndex = 0
 ??? More...
 
- Static Public Attributes inherited from SLLight
static SLCol4f globalAmbient = SLCol4f(0.1f, 0.1f, 0.1f, 1.0f)
 static global ambient light intensity More...
 
static SLfloat gamma = 1.0f
 final output gamma value More...
 
static SLbool doColoredShadows = false
 flag if shadows should be displayed with colors for debugging More...
 
- Protected Attributes inherited from SLNode
SLNode_parent
 pointer to the parent node More...
 
SLVNode _children
 vector of children nodes More...
 
SLMesh_mesh
 pointer to a single mesh More...
 
SLint _depth
 depth of the node in a scene tree More...
 
SLint _entityID
 ID in the SLVEntity graph for Data Oriented Design. More...
 
SLMat4f _om
 object matrix for local transforms More...
 
SLMat4f _initialOM
 the initial om state More...
 
SLMat4f _wm
 world matrix for world transform More...
 
SLMat4f _wmI
 inverse world matrix More...
 
SLbool _isWMUpToDate
 is the WM of this node still valid More...
 
SLbool _isWMIUpToDate
 is the inverse WM of this node still valid More...
 
SLbool _isAABBUpToDate
 is the saved aabb still valid More...
 
bool _castsShadows
 flag if meshes of node should cast shadows More...
 
bool _isSelected
 flag if node and one or more of its meshes are selected More...
 
SLDrawBits _drawBits
 node level drawing flags More...
 
SLAABBox _aabb
 axis aligned bounding box More...
 
SLAnimation_animation
 animation of the node More...
 
SLfloat _minLodCoverage
 Min. LOD coverage for visibility (0.0 < _minLodCoverage < 1.0) More...
 
SLubyte _levelForSM
 Level of LOD to use for shadow mapping (0 = the visible one will be drawn) More...
 
function< void()> _onUpdateCB
 Optional lambda callback once per update. More...
 
- Protected Attributes inherited from SLObject
SLstring _name
 name of an object More...
 
SLstring _url
 uniform resource locator More...
 
- Protected Attributes inherited from SLEventHandler
SLfloat _mouseRotationFactor
 Mouse rotation sensibility. More...
 
SLfloat _keyboardDeltaPos
 Delta dist. for keyboard translation. More...
 
- Protected Attributes inherited from SLLight
SLint _id
 OpenGL light number (0-7) More...
 
SLbool _isOn
 Flag if light is on or off. More...
 
SLCol4f _ambientColor
 Ambient light color (RGB 0-1) More...
 
SLfloat _ambientPower
 Ambient light power (0-N) More...
 
SLCol4f _diffuseColor
 Diffuse light color (RGB 0-1) More...
 
SLfloat _diffusePower
 Diffuse light power (0-N) More...
 
SLCol4f _specularColor
 Specular light color (RGB 0-1) More...
 
SLfloat _specularPower
 Specular light power (0-N) More...
 
SLfloat _spotCutOffDEG
 Half the spot cone angle. More...
 
SLfloat _spotCosCutOffRAD
 cosine of spotCutoff angle More...
 
SLfloat _spotExponent
 Spot attenuation from center to edge of cone. More...
 
SLfloat _kc
 Constant light attenuation. More...
 
SLfloat _kl
 Linear light attenuation. More...
 
SLfloat _kq
 Quadratic light attenuation. More...
 
SLbool _isAttenuated
 fast attenuation flag for ray tracing More...
 
SLbool _createsShadows
 flag if light creates shadows or not More...
 
SLShadowMap_shadowMap
 Used for shadow mapping. More...
 
SLbool _doSoftShadows
 flag if percentage-closer filtering for smooth shadows is enabled More...
 
SLuint _softShadowLevel
 Radius to smoothing (1 = 3 * 3; 2 = 5 * 5; ...) More...
 
SLfloat _shadowMinBias
 Min. bias at 0 deg. to use to prevent shadow acne. More...
 
SLfloat _shadowMaxBias
 Max. bias at 90 deg. to use to prevent shadow acne. More...
 

Detailed Description

SLLightDirect class for a directional light source.

SLLightDirect is a node and a light that can have a sphere mesh with a line for its direction representation. For directional lights the position vector is in infinite distance. We use its homogeneous component w as zero as the directional light flag. The spot direction is used in the shaders for the light direction.
If a light node is added to the scene it stays fix in the scene.
If a light node is added to the camera it moves with the camera.
See the scene examples for Per-Vertex-Blinn or Per-Pixel-Blinn lighting where all light node types are used.
All light nodes inherited from SLLight work automatically together with the automatically generated shader in SLGLProgramGenerated.

Definition at line 37 of file SLLightDirect.h.

Constructor & Destructor Documentation

◆ SLLightDirect() [1/2]

SLLightDirect::SLLightDirect ( SLAssetManager assetMgr,
SLScene s,
SLfloat  arrowLength = 0.5f,
SLbool  hasMesh = true,
SLbool  doCascadedShadows = false 
)

Construct a new SLLightDirect::SLLightDirect object.

It is important that during instantiation NO OpenGL functions (gl*) get called because this constructor will be most probably called in a parallel thread from within a SLScene::assemble function. All objects that get rendered have to do their OpenGL initialization when they are used the first time during rendering in the main thread.

Parameters
assetMgrAssetManager that will own the light mesh
sSLScene pointer
arrowLengthLength of the arrow visualization mesh
hasMeshBoolean if a mesh should be created and shown
doCascadedShadowsBoolean for doing cascaded shadow mapping

Definition at line 33 of file SLLightDirect.cpp.

39  : SLNode("LightDirect Node"),
40  _arrowRadius(arrowLength * 0.1f),
41  _arrowLength(arrowLength),
42  _doSunPowerAdaptation(false),
46 {
47  if (hasMesh)
48  {
49  SLMaterial* mat = new SLMaterial(assetMgr,
50  "LightDirect Mesh Mat",
53  addMesh(new SLArrow(assetMgr,
56  _arrowLength * 0.3f,
57  _arrowRadius * 2.0f,
58  16,
59  "LightDirect Mesh",
60  mat));
61  _castsShadows = false;
62  }
63 
64  init(s);
65 }
@ CLUT_DAYLIGHT
daylight from sunrise to sunset with noon in the middle
Definition: SLTexColorLUT.h:35
SLArrow is creates an arrow mesh based on its SLRevolver methods.
Definition: SLArrow.h:27
SLbool _doSunPowerAdaptation
Flag for sun power scaling.
SLfloat _sunLightPowerMin
Min. zenith power scale factor for sun.
SLfloat _arrowLength
Length of direction line.
SLfloat _arrowRadius
The sphere lights radius.
bool _doCascadedShadows
Cascaded shadow.
void init(SLScene *s)
SLbool doCascadedShadows() const override
Definition: SLLightDirect.h:91
SLTexColorLUT _sunLightColorLUT
Sun light color LUT.
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
bool _castsShadows
flag if meshes of node should cast shadows
Definition: SLNode.h:357
virtual void addMesh(SLMesh *mesh)
Definition: SLNode.cpp:157
SLNode(const SLstring &name="Node")
Construct a new SLNode::SLNode object.
Definition: SLNode.cpp:40
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
static SLVec4 BLACK
Definition: SLVec4.h:213

◆ SLLightDirect() [2/2]

SLLightDirect::SLLightDirect ( SLAssetManager assetMgr,
SLScene s,
SLfloat  posx,
SLfloat  posy,
SLfloat  posz,
SLfloat  arrowLength = 0.5f,
SLfloat  ambiPower = 1.0f,
SLfloat  diffPower = 10.0f,
SLfloat  specPower = 10.0f,
SLbool  hasMesh = true,
SLbool  doCascadedShadows = false 
)

Construct a new SLLightDirect::SLLightDirect object.

Remarks
It is important that during instantiation NO OpenGL functions (gl*) get called because this constructor will be most probably called in a parallel thread from within an SLScene::registerAssetsToLoad or SLScene::assemble function. All objects that get rendered have to do their OpenGL initialization when they are used the first time during rendering in the main thread.
Parameters
assetMgrAssetManager that will own the light mesh
sSLScene pointer
posxLight position x
posyLight position y
poszLight position z
arrowLengthLength of the arrow visualization mesh
ambiPowerAmbient light power
diffPowerDiffuse light power
specPowerSpecular light power
hasMeshBoolean if a mesh should be created and shown
doCascadedShadowsBoolean for doing cascaded shadow mapping

Definition at line 86 of file SLLightDirect.cpp.

97  : SLNode("Directional Light"),
98  SLLight(ambiPower, diffPower, specPower),
99  _arrowRadius(arrowLength * 0.1f),
100  _arrowLength(arrowLength),
101  _doSunPowerAdaptation(false),
105 {
106  translate(posx, posy, posz, TS_object);
107 
108  if (hasMesh)
109  {
110  SLMaterial* mat = new SLMaterial(assetMgr,
111  "LightDirect Mesh Mat",
114  addMesh(new SLArrow(assetMgr,
115  _arrowRadius,
116  _arrowLength,
117  _arrowLength * 0.3f,
118  _arrowRadius * 2.0f,
119  16,
120  "LightDirect Mesh",
121  mat));
122  }
123  init(s);
124 }
@ TS_object
Definition: SLEnums.h:210
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
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906

◆ ~SLLightDirect()

SLLightDirect::~SLLightDirect ( )
override

Definition at line 126 of file SLLightDirect.cpp.

127 {
128  delete _shadowMap;
129 
130  // Clear the color LUT that is also an OpenGL texture
132 }
void deleteData(SLbool deleteAlsoOnGPU)
Delete all data (CVImages and GPU textures)
SLShadowMap * _shadowMap
Used for shadow mapping.
Definition: SLLight.h:224

Member Function Documentation

◆ ambient()

SLCol4f SLLightDirect::ambient ( )
overridevirtual

Returns the product of the ambient light color and the ambient light power.

Implements SLLight.

Definition at line 324 of file SLLightDirect.cpp.

325 {
326  return _ambientColor * _ambientPower;
327 }
SLCol4f _ambientColor
Ambient light color (RGB 0-1)
Definition: SLLight.h:210
SLfloat _ambientPower
Ambient light power (0-N)
Definition: SLLight.h:211

◆ calculateSunLight()

SLCol4f SLLightDirect::calculateSunLight ( SLfloat  standardPower)

Calculates the sunlight color depending on the zenith angle.

If the angle is 0 it return 1 and _sunLightPowerMin at 90 degrees or more. This can be used to the downscale the directional light to simulate the reduced power of the sun. The color is take from a color ramp that is white at 0 degree zenith angle.

Definition at line 309 of file SLLightDirect.cpp.

310 {
311  SLVec3f toSunDirWS = -forwardOS();
312 
313  // The sun power is equal to the cosine of the sun zenith angle
314  SLfloat cosZenithAngle = std::max(toSunDirWS.dot(SLVec3f::AXISY),
316 
317  // The color is take from a color ramp that is white at 0 degree zenith
318  SLCol4f sunColor = _sunLightColorLUT.getTexelf(cosZenithAngle, 0);
319 
320  return sunColor * standardPower * cosZenithAngle;
321 }
float SLfloat
Definition: SL.h:173
SLCol4f getTexelf(SLfloat u, SLfloat v, SLuint imgIndex=0)
SLGLTexture::getTexelf returns a pixel color from u & v texture coordinates.
SLVec3f forwardOS() const
Definition: SLNode.h:477
static SLVec3 AXISY
Definition: SLVec3.h:298
T dot(const SLVec3 &v) const
Definition: SLVec3.h:117

◆ createShadowMap()

void SLLightDirect::createShadowMap ( float  clipNear = 0.1f,
float  clipFar = 20.0f,
SLVec2f  size = SLVec2f(8, 8),
SLVec2i  texSize = SLVec2i(1024, 1024) 
)
overridevirtual

Creates an fixed sized standard shadow map for a directional light.

Parameters
clipNearThe light frustums near clipping distance
clipFarThe light frustums near clipping distance
sizeWidth and height of the orthographic light frustum
texSizeShadow texture map size

Implements SLLight.

Definition at line 212 of file SLLightDirect.cpp.

216 {
217  if (!_shadowMap)
218  delete _shadowMap;
219 
220  _shadowMap = new SLShadowMap(this,
221  clipNear,
222  clipFar,
223  size,
224  texSize);
225 }
Class for standard and cascaded shadow mapping.
Definition: SLShadowMap.h:39

◆ createShadowMapAutoSize()

void SLLightDirect::createShadowMapAutoSize ( SLCamera camera,
SLVec2i  texSize = SLVec2i(1024, 1024),
int  numCascades = 4 
)
overridevirtual

Creates an automatic sized and cascaded shadow map for the directional light.

Parameters
cameraPointer to the camera for witch the shadow map gets sized
texSizeShadow texture map size (equal for all cascades)
numCascadesNO. of cascades shadow maps

Implements SLLight.

Definition at line 232 of file SLLightDirect.cpp.

235 {
236  if (!_shadowMap)
237  delete _shadowMap;
238 
239  _doCascadedShadows = true;
240  _shadowMap = new SLShadowMap(this,
241  camera,
242  texSize,
243  numCascades);
244 }

◆ diffuse()

SLCol4f SLLightDirect::diffuse ( )
overridevirtual

Returns the product of the diffuse light color and the diffuse light power.

If the directional light is the sun the color and the power is depending from the zenith angle of the sun. At noon it is high and bright and at sunrise and sunset it is low and reddish.

Implements SLLight.

Definition at line 334 of file SLLightDirect.cpp.

335 {
338  else
339  return _diffuseColor * _diffusePower;
340 }
SLCol4f calculateSunLight(SLfloat standardPower)
Calculates the sunlight color depending on the zenith angle.
SLfloat _diffusePower
Diffuse light power (0-N)
Definition: SLLight.h:213
SLCol4f _diffuseColor
Diffuse light color (RGB 0-1)
Definition: SLLight.h:212

◆ dirLength()

SLfloat SLLightDirect::dirLength ( ) const
inline

Definition at line 88 of file SLLightDirect.h.

88 { return _arrowLength; }

◆ doCascadedShadows() [1/2]

SLbool SLLightDirect::doCascadedShadows ( ) const
inlineoverridevirtual

Reimplemented from SLLight.

Definition at line 91 of file SLLightDirect.h.

91 { return _doCascadedShadows; }

◆ doCascadedShadows() [2/2]

void SLLightDirect::doCascadedShadows ( bool  b)
inline

Definition at line 84 of file SLLightDirect.h.

84 { _doCascadedShadows = b; }

◆ doSunPowerAdaptation() [1/2]

SLbool SLLightDirect::doSunPowerAdaptation ( )
inline

Definition at line 89 of file SLLightDirect.h.

89 { return _doSunPowerAdaptation; }

◆ doSunPowerAdaptation() [2/2]

void SLLightDirect::doSunPowerAdaptation ( SLbool  enabled)
inline

Definition at line 82 of file SLLightDirect.h.

82 { _doSunPowerAdaptation = enabled; }

◆ drawMesh()

void SLLightDirect::drawMesh ( SLSceneView sv)
overridevirtual

SLLightDirect::drawMeshes sets the light states and calls then the drawMeshes method of its node.

Reimplemented from SLNode.

Definition at line 184 of file SLLightDirect.cpp.

185 {
186  if (_id != -1)
187  {
188  // Set emissive light material to the lights diffuse color
189  if (_mesh)
190  if (_mesh->mat())
192 
193  // now draw the meshes of the node
195 
196  // Draw the volume affected by the shadow map
197  if (_createsShadows && _isOn && sv->s()->singleNodeSelected() == this)
198  {
199  if (sv->camera() != _shadowMap->camera())
201  _shadowMap->drawRays();
202  }
203  }
204 }
SLCol4f diffuse() override
Returns the product of the diffuse light color and the diffuse light power.
SLint _id
OpenGL light number (0-7)
Definition: SLLight.h:208
SLbool _isOn
Flag if light is on or off.
Definition: SLLight.h:209
SLbool _createsShadows
flag if light creates shadows or not
Definition: SLLight.h:223
void emissive(const SLCol4f &emis)
Definition: SLMaterial.h:174
SLMaterial * mat() const
Definition: SLMesh.h:177
virtual void drawMesh(SLSceneView *sv)
Draws the single mesh.
Definition: SLNode.cpp:176
SLMesh * _mesh
pointer to a single mesh
Definition: SLNode.h:346
SLNode * singleNodeSelected()
Returns the node if only one is selected. See also SLMesh::selectNodeMesh.
Definition: SLScene.h:116
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
void camera(SLCamera *camera)
Definition: SLSceneView.h:145
SLScene * s()
Definition: SLSceneView.h:167
void drawFrustum()
SLShadowMap::drawFrustum draws the volume affected by the shadow map.
void drawRays()
SLCamera * camera()
Definition: SLShadowMap.h:90

◆ hitRec()

SLbool SLLightDirect::hitRec ( SLRay ray)
overridevirtual

SLLightDirect::hitRec calls the recursive node intersection.

Reimplemented from SLNode.

Definition at line 160 of file SLLightDirect.cpp.

161 {
162  // do not intersect shadow rays
163  if (ray->type == SHADOW) return false;
164 
165  // only allow intersection with primary rays (no lights in reflections)
166  if (ray->type != PRIMARY) return false;
167 
168  // call the intersection routine of the node
169  return SLNode::hitRec(ray);
170 }
@ SHADOW
Definition: SLRay.h:26
@ PRIMARY
Definition: SLRay.h:23
virtual bool hitRec(SLRay *ray)
Definition: SLNode.cpp:508
SLRayType type
PRIMARY, REFLECTED, REFRACTED, SHADOW.
Definition: SLRay.h:92

◆ init()

void SLLightDirect::init ( SLScene s)

SLLightDirect::init sets the light id, the light states & creates an emissive material.

Definition at line 138 of file SLLightDirect.cpp.

139 {
140  // Check if OpenGL lights are available
141  if (s->lights().size() >= SL_MAX_LIGHTS)
142  SL_EXIT_MSG("Max. NO. of lights is exceeded!");
143 
144  // Add the light to the lights array of the scene
145  if (_id == -1)
146  {
147  _id = (SLint)s->lights().size();
148  s->lights().push_back(this);
149  }
150 
151  // Set emissive light material to the lights diffuse color
152  if (_mesh)
153  if (_mesh->mat())
155 }
#define SL_EXIT_MSG(message)
Definition: SL.h:240
int SLint
Definition: SL.h:170
static const SLint SL_MAX_LIGHTS
max. number of used lights
Definition: SLGLState.h:49
SLCol4f diffuseColor()
Definition: SLLight.h:136
SLVLight & lights()
Definition: SLScene.h:107

◆ positionWS()

SLVec4f SLLightDirect::positionWS ( ) const
inlineoverridevirtual

Implements SLLight.

Definition at line 95 of file SLLightDirect.h.

96  {
98  pos.w = 0.0f;
99  return pos;
100  }
void translation(const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:828
const SLMat4f & updateAndGetWM() const
Definition: SLNode.cpp:703

◆ radius()

SLfloat SLLightDirect::radius ( ) const
inline

Definition at line 87 of file SLLightDirect.h.

87 { return _arrowRadius; }

◆ renderShadowMap()

void SLLightDirect::renderShadowMap ( SLSceneView sv,
SLNode root 
)
overrideprotectedvirtual

SLLight::renderShadowMap renders the shadow map of the light.

Reimplemented from SLLight.

Definition at line 355 of file SLLightDirect.cpp.

356 {
357  // Check if no shadow map was created at load time
358  if (!_shadowMap)
359  {
360  this->createShadowMap();
361  }
362  _shadowMap->renderShadows(sv, root);
363 }
void createShadowMap(float clipNear=0.1f, float clipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024)) override
void renderShadows(SLSceneView *sv, SLNode *root)

◆ shadowTest()

SLfloat SLLightDirect::shadowTest ( SLRay ray,
const SLVec3f L,
SLfloat  lightDist,
SLNode root3D 
)
overridevirtual

SLLightDirect::shadowTest returns 0.0 if the hit point is completely shaded and 1.0 if it is 100% lighted. A directional light can not generate soft shadows.

Implements SLLight.

Definition at line 249 of file SLLightDirect.cpp.

253 {
254  // define shadow ray and shoot
255  SLRay shadowRay(lightDist, L, ray);
256  root3D->hitRec(&shadowRay);
257 
258  if (shadowRay.length < lightDist)
259  {
260  // Handle shadow value of transparent materials
261  if (shadowRay.hitMesh->mat()->hasAlpha())
262  {
263  shadowRay.hitMesh->preShade(&shadowRay);
264  SLfloat shadowTransp = Utils::abs(shadowRay.dir.dot(shadowRay.hitNormal));
265  return shadowTransp * shadowRay.hitMesh->mat()->kt();
266  }
267  else
268  return 0.0f;
269  }
270  else
271  return 1.0f;
272 }
Ray class with ray and intersection properties.
Definition: SLRay.h:40
T abs(T a)
Definition: Utils.h:249

◆ shadowTestMC()

SLfloat SLLightDirect::shadowTestMC ( SLRay ray,
const SLVec3f L,
SLfloat  lightDist,
SLNode root3D 
)
overridevirtual

SLLightDirect::shadowTestMC returns 0.0 if the hit point is completely shaded and 1.0 if it is 100% lighted. A directional light can not generate soft shadows.

Implements SLLight.

Definition at line 278 of file SLLightDirect.cpp.

282 {
283  // define shadow ray and shoot
284  SLRay shadowRay(lightDist, L, ray);
285  root3D->hitRec(&shadowRay);
286 
287  if (shadowRay.length < lightDist)
288  {
289  // Handle shadow value of transparent materials
290  if (shadowRay.hitMesh->mat()->hasAlpha())
291  {
292  shadowRay.hitMesh->preShade(&shadowRay);
293  SLfloat shadowTransp = Utils::abs(shadowRay.dir.dot(shadowRay.hitNormal));
294  return shadowTransp * shadowRay.hitMesh->mat()->kt();
295  }
296  else
297  return 0.0f;
298  }
299  else
300  return 1.0f;
301 }

◆ specular()

SLCol4f SLLightDirect::specular ( )
overridevirtual

Returns the product of the specular light color and the specular light power.

If the directional light is the sun the color and the power is depending from the zenith angle of the sun. At noon it is high and bright and at sunrise and sunset it is low and reddish.

Implements SLLight.

Definition at line 347 of file SLLightDirect.cpp.

348 {
351  else
353 }
SLfloat _specularPower
Specular light power (0-N)
Definition: SLLight.h:215
SLCol4f _specularColor
Specular light color (RGB 0-1)
Definition: SLLight.h:214

◆ spotDirWS()

SLVec3f SLLightDirect::spotDirWS ( )
inlineoverridevirtual

Implements SLLight.

Definition at line 101 of file SLLightDirect.h.

101 { return forwardOS(); }

◆ statsRec()

void SLLightDirect::statsRec ( SLNodeStats stats)
overridevirtual

SLLightDirect::statsRec updates the statistic parameters.

Reimplemented from SLNode.

Definition at line 173 of file SLLightDirect.cpp.

174 {
175  stats.numBytes += sizeof(SLLightDirect);
176  SLNode::statsRec(stats);
177 }
SLLightDirect(SLAssetManager *assetMgr, SLScene *s, SLfloat arrowLength=0.5f, SLbool hasMesh=true, SLbool doCascadedShadows=false)
Construct a new SLLightDirect::SLLightDirect object.
virtual void statsRec(SLNodeStats &stats)
Definition: SLNode.cpp:479
SLuint numBytes
NO. of bytes allocated.
Definition: SLNode.h:39

◆ sunLightColorLUT()

SLTexColorLUT* SLLightDirect::sunLightColorLUT ( )
inline

Definition at line 90 of file SLLightDirect.h.

90 { return &_sunLightColorLUT; }

◆ sunLightPowerMin()

void SLLightDirect::sunLightPowerMin ( SLfloat  minPower)
inline

Definition at line 83 of file SLLightDirect.h.

83 { _sunLightPowerMin = minPower; }

Member Data Documentation

◆ _arrowLength

SLfloat SLLightDirect::_arrowLength
private

Length of direction line.

Definition at line 111 of file SLLightDirect.h.

◆ _arrowRadius

SLfloat SLLightDirect::_arrowRadius
private

The sphere lights radius.

Definition at line 110 of file SLLightDirect.h.

◆ _doCascadedShadows

bool SLLightDirect::_doCascadedShadows
private

Cascaded shadow.

Definition at line 115 of file SLLightDirect.h.

◆ _doSunPowerAdaptation

SLbool SLLightDirect::_doSunPowerAdaptation
private

Flag for sun power scaling.

Definition at line 112 of file SLLightDirect.h.

◆ _sunLightColorLUT

SLTexColorLUT SLLightDirect::_sunLightColorLUT
private

Sun light color LUT.

Definition at line 114 of file SLLightDirect.h.

◆ _sunLightPowerMin

SLfloat SLLightDirect::_sunLightPowerMin
private

Min. zenith power scale factor for sun.

Definition at line 113 of file SLLightDirect.h.


The documentation for this class was generated from the following files: