SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShadowBasic.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShadowBasic.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 <SLSpheric.h>
17 #include <SLBox.h>
18 #include <AppCommon.h>
19 
20 //-----------------------------------------------------------------------------
22 {
23  info("Shadow Mapping is a technique to render shadows in two passes."
24  "In pass 1 the scene gets rendered from each light source.\n"
25  "In pass 1 the scene gets rendered from the camera with shadows.");
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  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, 7, 12);
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 source
51  // Do constant attenuation for directional lights since it is infinitely far away
52  SLLightDirect* light = new SLLightDirect(am, this);
53 
54  light->powers(0.0f, 1.0f, 1.0f);
55  light->translation(0, 5, 0);
56  light->lookAt(0, 0, 0);
57  light->attenuation(1, 0, 0);
58  light->createsShadows(true);
59  light->createShadowMap();
60  light->shadowMap()->rayCount(SLVec2i(16, 16));
61  light->castsShadows(false);
62  scene->addChild(light);
63 
64  // Add a sphere which casts shadows
65  SLNode* sphereNode = new SLNode(new SLSpheric(am,
66  1,
67  0,
68  180,
69  20,
70  20,
71  "Sphere",
72  matPerPixSM));
73  sphereNode->translate(0, 2.0, 0);
74  sphereNode->castsShadows(true);
75  scene->addChild(sphereNode);
76 
77  SLAnimation* anim = this->animManager().createNodeAnimation("sphere_anim", 2.0f);
78  anim->createNodeAnimTrackForEllipse(sphereNode,
79  0.5f,
80  A_x,
81  0.5f,
82  A_z);
83 
84  // Add a box which receives shadows
85  SLNode* boxNode = new SLNode(new SLBox(am,
86  -5,
87  -1,
88  -5,
89  5,
90  0,
91  5,
92  "Box",
93  matPerPixSM));
94  boxNode->castsShadows(false);
95  scene->addChild(boxNode);
96 
97  sv->camera(cam1);
98 }
99 //-----------------------------------------------------------------------------
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
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
SLLightDirect class for a directional light source.
Definition: SLLightDirect.h:40
void createShadowMap(float clipNear=0.1f, float clipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024)) 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