SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShadowCascaded.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShadowCascaded.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 <SLLightDirect.h>
16 #include <SLBox.h>
17 #include <SLSpheric.h>
18 #include <AppCommon.h>
19 
20 //-----------------------------------------------------------------------------
22  : SLScene("Cascaded Shadow Mapping Demo Scene")
23 {
24  info("Cascaded Shadow Mapping uses several cascades of shadow maps to "
25  "provide higher resolution shadows near the camera and lower "
26  "resolution shadows further away.");
27 }
28 //-----------------------------------------------------------------------------
29 //! All assets the should be loaded in parallel must be registered in here.
31 {
34  "FBX/Teapot/Teapot.fbx");
35 }
36 //-----------------------------------------------------------------------------
37 //! After parallel loading of the assets the scene gets assembled in here.
39 {
40  // Setup shadow mapping material
41  SLMaterial* matPerPixSM = new SLMaterial(am, "m1");
42 
43  // Base root group node for the scene
44  SLNode* scene = new SLNode;
45  this->root3D(scene);
46 
47  SLCamera* cam1 = new SLCamera("Camera 1");
48  cam1->translation(0, 7, 12);
49  cam1->lookAt(0, 1, 0);
50  cam1->focalDist(8);
51  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
52  cam1->setInitialState();
53  scene->addChild(cam1);
54 
55  // Create light source
56  // Do constant attenuation for directional lights since it is infinitely far away
57  SLLightDirect* light = new SLLightDirect(am, this);
58  light->powers(0.0f, 1.0f, 1.0f);
59  light->translation(0, 5, 0);
60  light->lookAt(0, 0, 0);
61  light->attenuation(1, 0, 0);
62  light->createsShadows(true);
63  light->doCascadedShadows(true);
64  light->createShadowMapAutoSize(cam1);
65  light->shadowMap()->rayCount(SLVec2i(16, 16));
66  light->castsShadows(false);
67  scene->addChild(light);
68 
69  // Add a sphere which casts shadows
70  SLNode* sphereNode = new SLNode(new SLSpheric(am,
71  1,
72  0,
73  180,
74  20,
75  20,
76  "Sphere",
77  matPerPixSM));
78  sphereNode->translate(0, 2.0, 0);
79  sphereNode->castsShadows(true);
80  scene->addChild(sphereNode);
81 
82  SLAnimation* anim = this->animManager().createNodeAnimation("sphere_anim",
83  2.0f);
84  anim->createNodeAnimTrackForEllipse(sphereNode,
85  0.5f,
86  A_x,
87  0.5f,
88  A_z);
89 
90  // Add a box which receives shadows
91  SLNode* boxNode = new SLNode(new SLBox(am,
92  -5,
93  -1,
94  -5,
95  5,
96  0,
97  5,
98  "Box",
99  matPerPixSM));
100  boxNode->castsShadows(false);
101  scene->addChild(boxNode);
102 
103  sv->camera(cam1);
104 }
105 //-----------------------------------------------------------------------------
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
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLstring modelPath
Path to 3D models.
Definition: AppCommon.h:85
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)
void addNodeToLoad(SLNode *&node, const SLstring &modelPath, SLSkybox *skybox=nullptr, SLbool deleteTexImgAfterBuild=false, SLbool loadMeshesOnly=true, SLMaterial *overrideMat=nullptr, float ambientFactor=0.5f, SLbool forceCookTorranceRM=false, SLuint flags=SLProcess_Triangulate|SLProcess_JoinIdenticalVertices|SLProcess_RemoveRedundantMaterials|SLProcess_FindDegenerates|SLProcess_FindInvalidData|SLProcess_SplitLargeMeshes)
Add mesh from file to load via assimp loader.
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
SLLightDirect class for a directional light source.
Definition: SLLightDirect.h:40
void doCascadedShadows(bool b)
Definition: SLLightDirect.h:84
void createShadowMapAutoSize(SLCamera *camera, SLVec2i texSize=SLVec2i(1024, 1024), int numCascades=4) override
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
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