SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShadowLightSpot.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShadowLightSpot.cpp
3  * \brief Implementation for an SLScene inherited class
4  * \details For more info about App framework and the scene assembly see:
5  * https://cpvrlab.github.io/SLProject4/app-framework.html
6  * \date May 2024
7  * \authors Marino von Wattenwyl
8  * \copyright http://opensource.org/licenses/GPL-3.0
9  * \remarks Please use clangformat to format the code. See more code style on
10  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
11 */
12 
14 #include <SLAssetLoader.h>
15 #include <SLLightSpot.h>
16 #include <SLBox.h>
17 #include <SLSpheric.h>
18 #include <AppCommon.h>
19 
20 //-----------------------------------------------------------------------------
22  : SLScene("Shadow Mapping for 8 Spot lights")
23 {
24  info("8 Spot lights use a perspective projection for their light space.");
25 }
26 //-----------------------------------------------------------------------------
27 //! All assets the should be loaded in parallel must be registered in here.
29 {
30 }
31 //-----------------------------------------------------------------------------
32 //! After parallel loading of the assets the scene gets assembled in here.
34 {
35  // Setup shadow mapping material
36  SLMaterial* matPerPixSM = new SLMaterial(am, "m1");
37 
38  // Base root group node for the scene
39  SLNode* scene = new SLNode;
40  this->root3D(scene);
41 
42  SLCamera* cam1 = new SLCamera("Camera 1");
43  cam1->translation(0, 5, 13);
44  cam1->lookAt(0, 1, 0);
45  cam1->focalDist(8);
46  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
47  cam1->setInitialState();
48  scene->addChild(cam1);
49 
50  // Create light sources
51  for (int i = 0; i < SL_MAX_LIGHTS; ++i)
52  {
53  SLLightSpot* light = new SLLightSpot(am,
54  this,
55  0.3f,
56  45.0f);
57  SLCol4f color;
58  color.hsva2rgba(SLVec4f(Utils::TWOPI * (float)i / (float)SL_MAX_LIGHTS,
59  1.0f,
60  1.0f));
61  light->powers(0.0f, 5.0f, 5.0f, color);
62  light->translation(2 * sin((Utils::TWOPI / (float)SL_MAX_LIGHTS) * (float)i),
63  5,
64  2 * cos((Utils::TWOPI / (float)SL_MAX_LIGHTS) * (float)i));
65  light->lookAt(0, 0, 0);
66  light->attenuation(0, 0, 1);
67  light->createsShadows(true);
68  light->createShadowMap();
69  light->shadowMap()->rayCount(SLVec2i(16, 16));
70  scene->addChild(light);
71  }
72 
73  // Add a sphere which casts shadows
74  SLNode* sphereNode = new SLNode(new SLSpheric(am,
75  1,
76  0,
77  180,
78  20,
79  20,
80  "Sphere",
81  matPerPixSM));
82  sphereNode->translate(0, 2.0, 0);
83  sphereNode->castsShadows(true);
84  scene->addChild(sphereNode);
85 
86  SLAnimation* anim = this->animManager().createNodeAnimation("sphere_anim",
87  2.0f);
88  anim->createNodeAnimTrackForEllipse(sphereNode,
89  1.0f,
90  A_x,
91  1.0f,
92  A_z);
93 
94  // Add a box which receives shadows
95  SLNode* boxNode = new SLNode(new SLBox(am,
96  -5,
97  -1,
98  -5,
99  5,
100  0,
101  5,
102  "Box",
103  matPerPixSM));
104  boxNode->castsShadows(false);
105  scene->addChild(boxNode);
106 
107  sv->camera(cam1);
108 }
109 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
@ A_z
Definition: SLEnums.h:82
@ A_x
Definition: SLEnums.h:80
static const SLint SL_MAX_LIGHTS
max. number of used lights
Definition: SLGLState.h:49
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec4< SLfloat > SLVec4f
Definition: SLVec4.h:235
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading in here.
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForEllipse(SLNode *target, SLfloat radiusA, SLAxis axisA, SLfloat radiusB, SLAxis axisB)
Toplevel holder of the assets meshes, materials, textures and shaders.
void colors(const SLCol4f &uniformColor)
Sets a uniform background color.
Axis aligned box mesh.
Definition: SLBox.h:31
Active or visible camera node class.
Definition: SLCamera.h:54
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
void shadowMap(SLShadowMap *shadowMap)
Definition: SLLight.h:125
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
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
void createShadowMap(float lightClipNear=0.1f, float lightClipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024)) override
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
void addChild(SLNode *child)
Definition: SLNode.cpp:207
void translation(const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:828
void castsShadows(SLbool castsShadows)
Definition: SLNode.h:282
void setInitialState()
Definition: SLNode.cpp:1084
void lookAt(SLfloat targetX, SLfloat targetY, SLfloat targetZ, SLfloat upX=0, SLfloat upY=1, SLfloat upZ=0, SLTransformSpace relativeTo=TS_world)
Definition: SLNode.h:652
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SLNode * root3D()
Definition: SLScene.h:99
friend class SLNode
Definition: SLScene.h:48
SLAnimManager & animManager()
Definition: SLScene.h:97
SLstring & info()
Definition: SLScene.h:102
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
void camera(SLCamera *camera)
Definition: SLSceneView.h:145
SLSphere creates a sphere mesh based on SLRevolver.
Definition: SLSpheric.h:21
void hsva2rgba(const SLVec4 &hsva)
HSVA to RGBA color conversion (http://www.rapidtables.com/convert/color/hsv-to-rgb....
Definition: SLVec4.h:191
static const float TWOPI
Definition: Utils.h:240