SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShadowLightPoint.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShadowLightPoint.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 Marcus Hudritsch, 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 <SLRectangle.h>
17 #include <SLSpheric.h>
18 #include <SLBox.h>
19 #include <AppCommon.h>
20 
21 //-----------------------------------------------------------------------------
23  : SLScene("Shadow Mapping for point lights")
24 {
25  info("Point lights use cubemaps to store shadow maps.");
26 }
27 //-----------------------------------------------------------------------------
28 //! All assets the should be loaded in parallel must be registered in here.
30 {
31 }
32 //-----------------------------------------------------------------------------
33 //! After parallel loading of the assets the scene gets assembled in here.
35 {
36  // Setup shadow mapping material
37  SLMaterial* matPerPixSM = new SLMaterial(am, "m1");
38 
39  // Base root group node for the scene
40  SLNode* scene = new SLNode;
41  this->root3D(scene);
42 
43  // Create camera
44  SLCamera* cam1 = new SLCamera;
45  cam1->translation(0, 0, 8);
46  cam1->lookAt(0, 0, 0);
47  cam1->fov(27);
48  cam1->focalDist(cam1->translationOS().length());
49  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
50  cam1->setInitialState();
52 
53  // Create lights
54  SLAnimation* anim = this->animManager().createNodeAnimation("light_anim", 4.0f);
55 
56  for (SLint i = 0; i < 3; ++i)
57  {
58  SLLightSpot* light = new SLLightSpot(am, this, 0.1f);
59  light->powers(0.2f,
60  1.5f,
61  1.0f,
62  SLCol4f(i == 0, i == 1, i == 2));
63  light->attenuation(0, 0, 1);
64  light->translate((float)i - 1.0f, (float)i - 1.0f, (float)i - 1.0f);
65  light->createsShadows(true);
66  light->createShadowMap();
67  light->shadowMap()->rayCount(SLVec2i(16, 16));
68  scene->addChild(light);
70  0.2f,
71  A_x,
72  0.2f,
73  A_z);
74  }
75 
76  // Create wall polygons
77  SLfloat pL = -1.48f, pR = 1.48f; // left/right
78  SLfloat pB = -1.25f, pT = 1.19f; // bottom/top
79  SLfloat pN = 1.79f, pF = -1.55f; // near/far
80 
81  // Bottom plane
82  SLNode* b = new SLNode(new SLRectangle(am,
83  SLVec2f(pL, -pN),
84  SLVec2f(pR, -pF),
85  6,
86  6,
87  "bottom",
88  matPerPixSM));
89  b->rotate(90, -1, 0, 0);
90  b->translate(0, 0, pB, TS_object);
91  scene->addChild(b);
92 
93  // Top plane
94  SLNode* t = new SLNode(new SLRectangle(am,
95  SLVec2f(pL, pF),
96  SLVec2f(pR, pN),
97  6,
98  6,
99  "top",
100  matPerPixSM));
101  t->rotate(90, 1, 0, 0);
102  t->translate(0, 0, -pT, TS_object);
103  scene->addChild(t);
104 
105  // Far plane
106  SLNode* f = new SLNode(new SLRectangle(am,
107  SLVec2f(pL, pB),
108  SLVec2f(pR, pT),
109  6,
110  6,
111  "far",
112  matPerPixSM));
113  f->translate(0, 0, pF, TS_object);
114  scene->addChild(f);
115 
116  // near plane
117  SLNode* n = new SLNode(new SLRectangle(am,
118  SLVec2f(pL, pT),
119  SLVec2f(pR, pB),
120  6,
121  6,
122  "near",
123  matPerPixSM));
124  n->translate(0, 0, pN, TS_object);
125  scene->addChild(n);
126 
127  // left plane
128  SLNode* l = new SLNode(new SLRectangle(am,
129  SLVec2f(-pN, pB),
130  SLVec2f(-pF, pT),
131  6,
132  6,
133  "left",
134  matPerPixSM));
135  l->rotate(90, 0, 1, 0);
136  l->translate(0, 0, pL, TS_object);
137  scene->addChild(l);
138 
139  // Right plane
140  SLNode* r = new SLNode(new SLRectangle(am,
141  SLVec2f(pF, pB),
142  SLVec2f(pN, pT),
143  6,
144  6,
145  "right",
146  matPerPixSM));
147  r->rotate(90, 0, -1, 0);
148  r->translate(0, 0, -pR, TS_object);
149  scene->addChild(r);
150 
151  // Create cubes which cast shadows
152  for (SLint i = 0; i < 64; ++i)
153  {
154  SLBox* box = new SLBox(am);
155  box->mat(matPerPixSM);
156  SLNode* boxNode = new SLNode(box);
157 
158  boxNode->scale(Utils::random(0.01f, 0.1f));
159  boxNode->translate(Utils::random(pL + 0.3f, pR - 0.3f),
160  Utils::random(pB + 0.3f, pT - 0.3f),
161  Utils::random(pF + 0.3f, pN - 0.3f),
162  TS_world);
163  boxNode->castsShadows(true);
164 
165  scene->addChild(boxNode);
166  }
167 
168  sv->camera(cam1);
169 }
170 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
float SLfloat
Definition: SL.h:173
int SLint
Definition: SL.h:170
@ A_z
Definition: SLEnums.h:82
@ A_x
Definition: SLEnums.h:80
@ TS_world
Definition: SLEnums.h:208
@ TS_object
Definition: SLEnums.h:210
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLDeviceRotation devRot
Mobile device rotation from IMU.
Definition: AppCommon.h:64
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading 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 devRotLoc(SLDeviceRotation *devRot, SLDeviceLocation *devLoc)
Definition: SLCamera.h:120
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void fov(const SLfloat fov)
vertical field of view
Definition: SLCamera.h:98
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
SLMaterial * mat() const
Definition: SLMesh.h:177
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 rotate(const SLQuat4f &rot, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:945
void scale(SLfloat s)
Definition: SLNode.h:640
void castsShadows(SLbool castsShadows)
Definition: SLNode.h:282
SLVec3f translationOS() const
Definition: SLNode.h:468
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
SLRectangle creates a rectangular mesh with a certain resolution.
Definition: SLRectangle.h:29
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
T length() const
Definition: SLVec3.h:122
float random(float min, float max)
Returns a uniform distributed random float number between min and max.
Definition: Utils.h:265