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

SLLightSpot class for a spot light source. More...

#include <SLLightSpot.h>

Inheritance diagram for SLLightSpot:
[legend]

Public Member Functions

 SLLightSpot (SLAssetManager *assetMgr, SLScene *s, SLfloat radius=0.3f, SLfloat spotAngleDEG=180.0f, SLbool hasMesh=true)
 Construct a new SLLightSpot::SLLightSpot object. More...
 
 SLLightSpot (SLAssetManager *assetMgr, SLScene *s, SLfloat posx, SLfloat posy, SLfloat posz, SLfloat radius=0.3f, SLfloat spotAngleDEG=180.0f, SLfloat ambiPower=1.0f, SLfloat diffPower=10.0f, SLfloat specPower=10.0f, SLbool hasMesh=true)
 
 ~SLLightSpot () override
 
void init (SLScene *s)
 
bool hitRec (SLRay *ray) override
 
void statsRec (SLNodeStats &stats) override
 SLLightSpot::statsRec updates the statistic parameters. More...
 
void drawMesh (SLSceneView *sv) override
 
void createShadowMap (float lightClipNear=0.1f, float lightClipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024)) override
 
void createShadowMapAutoSize (SLCamera *camera, SLVec2i texSize=SLVec2i(1024, 1024), int numCascades=0) 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 samples (SLuint x, SLuint y)
 
SLfloat radius () const
 
SLuint samples ()
 
SLCol4f ambient () override
 Return normally _ambientColor * _ambientPower. More...
 
SLCol4f diffuse () override
 Returns normally _diffuseColor * _diffusePower. More...
 
SLCol4f specular () override
 Returns normally _specularColor * _specularPower. More...
 
SLVec4f positionWS () const override
 
SLVec3f spotDirWS () override
 
- 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
 
virtual SLbool doCascadedShadows () const
 
virtual void renderShadowMap (SLSceneView *sv, SLNode *root)
 SLLight::renderShadowMap renders the shadow map of the light. More...
 

Private Attributes

SLfloat _radius
 The sphere lights radius. More...
 
SLRaySamples2D _samples
 2D sample points for soft shadows 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

SLLightSpot class for a spot light source.

SLLightSpot is a node and a light that can have a spot mesh for its representation. 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 34 of file SLLightSpot.h.

Constructor & Destructor Documentation

◆ SLLightSpot() [1/2]

SLLightSpot::SLLightSpot ( SLAssetManager assetMgr,
SLScene s,
SLfloat  radius = 0.3f,
SLfloat  spotAngleDEG = 180.0f,
SLbool  hasMesh = true 
)
explicit

Construct a new SLLightSpot::SLLightSpot 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
radiusRadius of the spot light sphere
spotAngleDEGSpot cone shine angle in degrees
hasMeshBoolean if a mesh should be created and shown

Definition at line 33 of file SLLightSpot.cpp.

38  : SLNode("LightSpot Node")
39 {
40  _radius = radius;
41  _samples.samples(1, 1, false);
42  spotCutOffDEG(spotAngleDEG);
43 
44  if (hasMesh)
45  {
46  SLMaterial* mat = new SLMaterial(assetMgr,
47  "LightSpot Mesh Mat",
50  if (spotAngleDEG < 180.0f)
51  addMesh(new SLSpheric(assetMgr,
52  radius,
53  0.0f,
54  spotAngleDEG,
55  16,
56  16,
57  "LightSpot Mesh",
58  mat));
59  else
60  addMesh(new SLSphere(assetMgr,
61  radius,
62  16,
63  16,
64  "LightSpot Mesh",
65  mat));
66  _castsShadows = false;
67  }
68 
69  init(s);
70 }
SLfloat spotCutOffDEG() const
Definition: SLLight.h:140
SLRaySamples2D _samples
2D sample points for soft shadows
Definition: SLLightSpot.h:124
SLfloat radius() const
Definition: SLLightSpot.h:80
SLfloat _radius
The sphere lights radius.
Definition: SLLightSpot.h:123
void init(SLScene *s)
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
void samples(SLuint x, SLuint y, SLbool evenlyDistributed=true)
Resets the sample point array by the sqrt of the no. of samples.
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SLSphere creates a sphere mesh based on SLSpheric w. 180 deg polar angle.
Definition: SLSphere.h:33
SLSphere creates a sphere mesh based on SLRevolver.
Definition: SLSpheric.h:21
static SLVec4 BLACK
Definition: SLVec4.h:213

◆ SLLightSpot() [2/2]

SLLightSpot::SLLightSpot ( SLAssetManager assetMgr,
SLScene s,
SLfloat  posx,
SLfloat  posy,
SLfloat  posz,
SLfloat  radius = 0.3f,
SLfloat  spotAngleDEG = 180.0f,
SLfloat  ambiPower = 1.0f,
SLfloat  diffPower = 10.0f,
SLfloat  specPower = 10.0f,
SLbool  hasMesh = true 
)

Definition at line 72 of file SLLightSpot.cpp.

83  : SLNode("LightSpot Node"),
84  SLLight(ambiPower, diffPower, specPower)
85 {
86  _radius = radius;
87  _samples.samples(1, 1, false);
88  _castsShadows = false;
89  spotCutOffDEG(spotAngleDEG);
90 
91  translate(posx, posy, posz, TS_object);
92 
93  if (hasMesh)
94  {
95  SLMaterial* mat = new SLMaterial(assetMgr,
96  "LightSpot Mesh Mat",
99  if (spotAngleDEG < 180.0f)
100  addMesh(new SLSpheric(assetMgr,
101  radius,
102  0.0f,
103  spotAngleDEG,
104  32,
105  32,
106  "LightSpot Mesh",
107  mat));
108  else
109  addMesh(new SLSphere(assetMgr,
110  radius,
111  32,
112  32,
113  "LightSpot Mesh",
114  mat));
115  }
116  init(s);
117 }
@ 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

◆ ~SLLightSpot()

SLLightSpot::~SLLightSpot ( )
override

Definition at line 119 of file SLLightSpot.cpp.

120 {
121  delete _shadowMap;
122 }
SLShadowMap * _shadowMap
Used for shadow mapping.
Definition: SLLight.h:224

Member Function Documentation

◆ ambient()

SLCol4f SLLightSpot::ambient ( )
inlineoverridevirtual

Return normally _ambientColor * _ambientPower.

Implements SLLight.

Definition at line 84 of file SLLightSpot.h.

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

◆ createShadowMap()

void SLLightSpot::createShadowMap ( float  lightClipNear = 0.1f,
float  lightClipFar = 20.0f,
SLVec2f  size = SLVec2f(8, 8),
SLVec2i  texSize = SLVec2i(1024, 1024) 
)
overridevirtual

Creates an fixed sized standard shadow map for the spotlight.

Parameters
lightClipNearThe light frustums near clipping distance
lightClipFarThe light frustums near clipping distance
sizeIgnored for spot lights
texSizeShadow texture map size

Implements SLLight.

Definition at line 202 of file SLLightSpot.cpp.

206 {
207  if (!_shadowMap)
208  delete _shadowMap;
209 
210  _shadowMap = new SLShadowMap(this,
211  lightClipNear,
212  lightClipFar,
213  size,
214  texSize);
215 }
Class for standard and cascaded shadow mapping.
Definition: SLShadowMap.h:39

◆ createShadowMapAutoSize()

void SLLightSpot::createShadowMapAutoSize ( SLCamera camera,
SLVec2i  texSize = SLVec2i(1024, 1024),
int  numCascades = 0 
)
overridevirtual

Creates an automatic sized shadow map for the spot light.

Parameters
cameraPointer to the camera for witch the shadow map gets sized
texSizeShadow texture map size
numCascadesThis value is ignored (default 0)

Implements SLLight.

Definition at line 222 of file SLLightSpot.cpp.

225 {
226  (void)numCascades;
227  if (!_shadowMap)
228  delete _shadowMap;
229 
230  _shadowMap = new SLShadowMap(this,
231  camera,
232  texSize,
233  0);
234 }
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.

◆ diffuse()

SLCol4f SLLightSpot::diffuse ( )
inlineoverridevirtual

Returns normally _diffuseColor * _diffusePower.

Implements SLLight.

Definition at line 85 of file SLLightSpot.h.

85 { return _diffuseColor * _diffusePower; }
SLfloat _diffusePower
Diffuse light power (0-N)
Definition: SLLight.h:213
SLCol4f _diffuseColor
Diffuse light color (RGB 0-1)
Definition: SLLight.h:212

◆ drawMesh()

void SLLightSpot::drawMesh ( SLSceneView sv)
overridevirtual

SLLightSpot::drawMesh sets the light states and calls then the drawMesh method of its node.

Reimplemented from SLNode.

Definition at line 173 of file SLLightSpot.cpp.

174 {
175  if (_id != -1)
176  {
177  // Set emissive light mesh material to the lights diffuse color
178  if (_mesh)
179  {
180  if (_mesh->mat())
182 
183  // now draw the single mesh of the node
185  }
186 
187  // Draw the volume affected by the shadow map
188  if (_createsShadows && _isOn && sv->s()->singleNodeSelected() == this)
189  {
191  _shadowMap->drawRays();
192  }
193  }
194 }
SLint _id
OpenGL light number (0-7)
Definition: SLLight.h:208
SLbool _isOn
Flag if light is on or off.
Definition: SLLight.h:209
SLCol4f diffuseColor()
Definition: SLLight.h:136
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
SLScene * s()
Definition: SLSceneView.h:167
void drawFrustum()
SLShadowMap::drawFrustum draws the volume affected by the shadow map.
void drawRays()

◆ hitRec()

SLbool SLLightSpot::hitRec ( SLRay ray)
overridevirtual

SLLightSpot::hitRec calls the recursive node intersection.

Reimplemented from SLNode.

Definition at line 149 of file SLLightSpot.cpp.

150 {
151  // do not intersect shadow rays
152  if (ray->type == SHADOW) return false;
153 
154  // only allow intersection with primary rays (no lights in reflections)
155  if (ray->type != PRIMARY) return false;
156 
157  // call the intersection routine of the node
158  return SLNode::hitRec(ray);
159 }
@ 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 SLLightSpot::init ( SLScene s)

SLLightSpot::init sets the light id, the light states & creates an emissive mat.

Definition at line 127 of file SLLightSpot.cpp.

128 {
129  // Check if OpenGL lights are available
130  if (s->lights().size() >= SL_MAX_LIGHTS)
131  SL_EXIT_MSG("Max. NO. of lights is exceeded!");
132 
133  // Add the light to the lights array of the scene
134  if (_id == -1)
135  {
136  _id = (SLint)s->lights().size();
137  s->lights().push_back(this);
138  }
139 
140  // Set emissive light material to the lights diffuse color
141  if (_mesh)
142  if (_mesh->mat())
144 }
#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
SLVLight & lights()
Definition: SLScene.h:107

◆ positionWS()

SLVec4f SLLightSpot::positionWS ( ) const
inlineoverridevirtual

Implements SLLight.

Definition at line 87 of file SLLightSpot.h.

87 { return SLVec4f(translationWS()); }
SLVec4< SLfloat > SLVec4f
Definition: SLVec4.h:235
SLVec3f translationWS() const
Definition: SLNode.h:531

◆ radius()

SLfloat SLLightSpot::radius ( ) const
inline

Definition at line 80 of file SLLightSpot.h.

80 { return _radius; }

◆ samples() [1/2]

SLuint SLLightSpot::samples ( )
inline

Definition at line 81 of file SLLightSpot.h.

81 { return _samples.samples(); }

◆ samples() [2/2]

void SLLightSpot::samples ( SLuint  x,
SLuint  y 
)
inline

Definition at line 77 of file SLLightSpot.h.

77 { _samples.samples(x, y, false); }

◆ shadowTest()

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

SLLightSpot::shadowTest returns 0.0 if the hit point is completely shaded and 1.0 if it is 100% lighted. A return value in between is calculate by the ratio of the shadow rays not blocked to the total number of casted shadow rays.

Implements SLLight.

Definition at line 241 of file SLLightSpot.cpp.

245 {
246  if (_samples.samples() == 1)
247  {
248  // define shadow ray and shoot
249  SLRay shadowRay(lightDist, L, ray);
250  root3D->hitRec(&shadowRay);
251 
252  if (shadowRay.length < lightDist && shadowRay.hitMesh)
253  {
254  // Handle shadow value of transparent materials
255  if (shadowRay.hitMesh->mat()->hasAlpha())
256  {
257  shadowRay.hitMesh->preShade(&shadowRay);
258  SLfloat shadowTransp = Utils::abs(shadowRay.dir.dot(shadowRay.hitNormal));
259  return shadowTransp * shadowRay.hitMesh->mat()->kt();
260  }
261  else
262  return 0.0f;
263  }
264  else
265  return 1.0f;
266  }
267  else // do light sampling for soft shadows
268  {
269  SLVec3f C(updateAndGetWM().translation()); // Center of light
270  SLVec3f LightX, LightY; // main axis of sample plane
271  SLfloat lighted = 0.0f; // return value
272  SLfloat invSamples = 1.0f / (_samples.samples());
273  SLbool outerCircleIsLighting = true;
274  SLbool innerCircleIsNotLighting = true;
275 
276  // Build normalized plain vectors X and Y that are perpendicular to L (=Z)
277  if (fabs(L.x) >= fabs(L.y))
278  {
279  SLfloat invLength = 1.0f / sqrt(L.x * L.x + L.z * L.z);
280  LightX.set(L.z * invLength, 0, -L.x * invLength);
281  }
282  else
283  {
284  SLfloat invLength = 1.0f / sqrt(L.y * L.y + L.z * L.z);
285  LightX.set(0, L.z * invLength, -L.y * invLength);
286  }
287  LightY.cross(L, LightX);
288  LightY *= _radius;
289  LightX *= _radius;
290 
291  // Loop over radius r and angle phi of light circle
292  for (SLint iR = (SLint)_samples.samplesX() - 1; iR >= 0; --iR)
293  {
294  for (SLint iPhi = (SLint)_samples.samplesY() - 1; iPhi >= 0; --iPhi)
295  {
296  SLVec2f discPos(_samples.point((SLuint)iR, (SLuint)iPhi));
297 
298  // calculate disc position and vector LDisc to it
299  SLVec3f conePos(C + discPos.x * LightX + discPos.y * LightY);
300  SLVec3f LDisc(conePos - ray->hitPoint);
301  LDisc.normalize();
302 
303  SLRay shadowRay(lightDist, LDisc, ray);
304 
305  root3D->hitRec(&shadowRay);
306 
307  if (shadowRay.length < lightDist)
308  outerCircleIsLighting = false;
309  else
310  {
311  lighted += invSamples; // sum up the light
312  innerCircleIsNotLighting = false;
313  }
314  }
315 
316  // Early break 1:
317  // If the outer circle of shadow rays where not blocked return 1.0
318  if (outerCircleIsLighting) return 1.0f;
319 
320  // Early break 2:
321  // If a circle was completely shaded return lighted amount
322  if (innerCircleIsNotLighting) return lighted;
323  innerCircleIsNotLighting = true;
324  }
325  return lighted;
326  }
327 }
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
void translation(const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:828
const SLMat4f & updateAndGetWM() const
Definition: SLNode.cpp:703
Ray class with ray and intersection properties.
Definition: SLRay.h:40
SLVec3f hitPoint
Point of intersection.
Definition: SLRay.h:110
void point(SLuint x, SLuint y, SLVec2f point)
SLuint samplesY()
SLuint samplesX()
T y
Definition: SLVec3.h:43
void cross(const SLVec3 &a, const SLVec3 &b)
Definition: SLVec3.h:118
T x
Definition: SLVec3.h:43
void set(const T X, const T Y, const T Z)
Definition: SLVec3.h:59
T z
Definition: SLVec3.h:43
T abs(T a)
Definition: Utils.h:249

◆ shadowTestMC()

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

SLLightSpot::shadowTest returns 0.0 if the hit point is completely shaded and 1.0 if it is 100% lighted. A return value inbetween is calculate by the ratio of the shadow rays not blocked to the total number of casted shadow rays.

Implements SLLight.

Definition at line 334 of file SLLightSpot.cpp.

338 {
339  if (_samples.samples() == 1)
340  {
341  // define shadow ray and shoot
342  SLRay shadowRay(lightDist, L, ray);
343  root3D->hitRec(&shadowRay);
344 
345  if (shadowRay.length < lightDist)
346  {
347  // Handle shadow value of transparent materials
348  if (shadowRay.hitMesh->mat()->hasAlpha())
349  {
350  shadowRay.hitMesh->preShade(&shadowRay);
351  SLfloat shadowTransp = Utils::abs(shadowRay.dir.dot(shadowRay.hitNormal));
352  return shadowTransp * shadowRay.hitMesh->mat()->kt();
353  }
354  else
355  return 0.0f;
356  }
357  else
358  return 1.0f;
359  }
360  else // do light sampling for soft shadows
361  {
362  SLVec3f C(updateAndGetWM().translation()); // Center of light
363  SLVec3f LightX, LightY; // main axis of sample plane
364  SLfloat lighted = 0.0f; // return value
365  SLfloat invSamples = 1.0f / (_samples.samples());
366  SLbool outerCircleIsLighting = true;
367  SLbool innerCircleIsNotLighting = true;
368 
369  // Build normalized plain vectors X and Y that are perpendicular to L (=Z)
370  if (fabs(L.x) >= fabs(L.y))
371  {
372  SLfloat invLength = 1.0f / sqrt(L.x * L.x + L.z * L.z);
373  LightX.set(L.z * invLength, 0, -L.x * invLength);
374  }
375  else
376  {
377  SLfloat invLength = 1.0f / sqrt(L.y * L.y + L.z * L.z);
378  LightX.set(0, L.z * invLength, -L.y * invLength);
379  }
380  LightY.cross(L, LightX);
381  LightY *= _radius;
382  LightX *= _radius;
383 
384  // Loop over radius r and angle phi of light circle
385  for (SLint iR = (SLint)_samples.samplesX() - 1; iR >= 0; --iR)
386  {
387  for (SLint iPhi = (SLint)_samples.samplesY() - 1; iPhi >= 0; --iPhi)
388  {
389  SLVec2f discPos(_samples.point((SLuint)iR, (SLuint)iPhi));
390 
391  // calculate disc position and vector LDisc to it
392  SLVec3f conePos(C + discPos.x * LightX + discPos.y * LightY);
393  SLVec3f LDisc(conePos - ray->hitPoint);
394  LDisc.normalize();
395 
396  SLRay shadowRay(lightDist, LDisc, ray);
397 
398  root3D->hitRec(&shadowRay);
399 
400  if (shadowRay.length < lightDist)
401  outerCircleIsLighting = false;
402  else
403  {
404  lighted += invSamples; // sum up the light
405  innerCircleIsNotLighting = false;
406  }
407  }
408 
409  // Early break 1:
410  // If the outer circle of shadow rays where not blocked return 1.0
411  if (outerCircleIsLighting) return 1.0f;
412 
413  // Early break 2:
414  // If a circle was completely shaded return lighted amount
415  if (innerCircleIsNotLighting) return lighted;
416  innerCircleIsNotLighting = true;
417  }
418  return 0.0f;
419  }
420 }

◆ specular()

SLCol4f SLLightSpot::specular ( )
inlineoverridevirtual

Returns normally _specularColor * _specularPower.

Implements SLLight.

Definition at line 86 of file SLLightSpot.h.

86 { return _specularColor * _specularPower; }
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 SLLightSpot::spotDirWS ( )
inlineoverridevirtual

Implements SLLight.

Definition at line 88 of file SLLightSpot.h.

88 { return forwardWS(); }
SLVec3f forwardWS() const
Definition: SLNode.h:541

◆ statsRec()

void SLLightSpot::statsRec ( SLNodeStats stats)
overridevirtual

SLLightSpot::statsRec updates the statistic parameters.

Reimplemented from SLNode.

Definition at line 162 of file SLLightSpot.cpp.

163 {
164  stats.numBytes += sizeof(SLLightSpot);
165  stats.numBytes += _samples.sizeInBytes();
166  SLNode::statsRec(stats);
167 }
SLLightSpot(SLAssetManager *assetMgr, SLScene *s, SLfloat radius=0.3f, SLfloat spotAngleDEG=180.0f, SLbool hasMesh=true)
Construct a new SLLightSpot::SLLightSpot object.
Definition: SLLightSpot.cpp:33
virtual void statsRec(SLNodeStats &stats)
Definition: SLNode.cpp:479
SLuint sizeInBytes()
SLuint numBytes
NO. of bytes allocated.
Definition: SLNode.h:39

Member Data Documentation

◆ _radius

SLfloat SLLightSpot::_radius
private

The sphere lights radius.

Definition at line 123 of file SLLightSpot.h.

◆ _samples

SLRaySamples2D SLLightSpot::_samples
private

2D sample points for soft shadows

Definition at line 124 of file SLLightSpot.h.


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