SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShadowLightTypes.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShadowLightTypes.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 <SLLightRect.h>
17 #include <SLSpheric.h>
18 #include <SLBox.h>
19 #include <AppCommon.h>
20 
21 //-----------------------------------------------------------------------------
23  : SLScene("Shadow Mapping Types Demo Scene")
24 {
25  info("Shadow Mapping is implemented for these light types.");
26 }
27 //-----------------------------------------------------------------------------
28 //! All assets the should be loaded in parallel must be registered in here.
30 {
33  "FBX/Teapot/Teapot.fbx");
34 }
35 //-----------------------------------------------------------------------------
36 //! After parallel loading of the assets the scene gets assembled in here.
38 {
39  SLMaterial* mat1 = new SLMaterial(am, "mat1");
40 
41  // Base root group node for the scene
42  SLNode* scene = new SLNode;
43  this->root3D(scene);
44 
45  SLCamera* cam1 = new SLCamera("Camera 1");
46  cam1->translation(0, 2, 20);
47  cam1->lookAt(0, 2, 0);
48  cam1->focalDist(20);
49  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
50  cam1->setInitialState();
51  scene->addChild(cam1);
52 
53  // Create light sources
54  vector<SLLight*> lights = {
55  new SLLightDirect(am, this),
56  new SLLightRect(am, this),
57  new SLLightSpot(am, this, 0.3f, 25.0f),
58  new SLLightSpot(am, this, 0.1f, 180.0f)};
59 
60  for (SLint i = 0; i < lights.size(); ++i)
61  {
62  SLLight* light = lights[i];
63  SLNode* node = dynamic_cast<SLNode*>(light);
64  SLfloat x = ((float)i - ((SLfloat)lights.size() - 1.0f) / 2.0f) * 5;
65 
66  if (i == 0) // Make direct light less bright
67  {
68  light->powers(0.0f, 0.4f, 0.4f);
69  light->attenuation(1, 0, 0);
70  }
71  else
72  {
73  light->powers(0.0f, 2.0f, 2.0f);
74  light->attenuation(0, 0, 1);
75  }
76 
77  node->translation(x, 5, 0);
78  node->lookAt(x, 0, 0);
79  light->createsShadows(true);
80  light->createShadowMap();
81  light->shadowMap()->rayCount(SLVec2i(16, 16));
82  scene->addChild(node);
83  }
84 
85  SLAnimation* teapotAnim = this->animManager().createNodeAnimation("teapot_anim",
86  8.0f,
87  true,
88  EC_linear,
89  AL_loop);
90 
91  for (SLLight* light : lights)
92  {
93  SLNode* teapot = _teapot->copyRec();
94 
95  teapot->translate(light->positionWS().x, 2, 0);
96  teapot->children()[0]->castsShadows(true);
97  scene->addChild(teapot);
98 
99  // Create animation
100  SLNodeAnimTrack* track = teapotAnim->createNodeAnimTrack();
101  track->animatedNode(teapot);
102 
103  SLTransformKeyframe* frame0 = track->createNodeKeyframe(0.0f);
104  frame0->translation(teapot->translationWS());
105  frame0->rotation(SLQuat4f(0, 0, 0));
106 
107  SLTransformKeyframe* frame1 = track->createNodeKeyframe(4.0f);
108  frame1->translation(teapot->translationWS());
109  frame1->rotation(SLQuat4f(0, 1 * PI, 0));
110 
111  SLTransformKeyframe* frame2 = track->createNodeKeyframe(8.0f);
112  frame2->translation(teapot->translationWS());
113  frame2->rotation(SLQuat4f(0, 2 * PI, 0));
114  }
115 
116  // Add a box which receives shadows
117  SLfloat minx = lights.front()->positionWS().x - 3;
118  SLfloat maxx = lights.back()->positionWS().x + 3;
119  SLNode* boxNode = new SLNode(new SLBox(am,
120  minx,
121  -1,
122  -5,
123  maxx,
124  0,
125  5,
126  "Box",
127  mat1));
128  boxNode->castsShadows(false);
129  scene->addChild(boxNode);
130 
131  sv->camera(cam1);
132 }
133 //-----------------------------------------------------------------------------
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
@ EC_linear
linear easing with constant velocity
Definition: SLEnums.h:181
@ AL_loop
loop
Definition: SLEnums.h:169
SLQuat4< SLfloat > SLQuat4f
Definition: SLQuat4.h:846
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 * createNodeAnimTrack()
Definition: SLAnimation.cpp:94
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
Abstract Light class for OpenGL light sources.
Definition: SLLight.h:61
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
void shadowMap(SLShadowMap *shadowMap)
Definition: SLLight.h:125
virtual void createShadowMap(float lightClipNear=0.1f, float lightClipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024))=0
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
Light node class for a rectangular light source.
Definition: SLLightRect.h:39
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
Specialized animation track for node animations.
Definition: SLAnimTrack.h:66
void animatedNode(SLNode *target)
Definition: SLAnimTrack.h:73
SLTransformKeyframe * createNodeKeyframe(SLfloat time)
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
void addChild(SLNode *child)
Definition: SLNode.cpp:207
SLVNode & children()
Definition: SLNode.h:305
void translation(const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:828
void castsShadows(SLbool castsShadows)
Definition: SLNode.h:282
virtual SLNode * copyRec()
Definition: SLNode.cpp:572
SLVec3f translationWS() const
Definition: SLNode.h:531
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
SLVLight & lights()
Definition: SLScene.h:107
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
SLTransformKeyframe is a specialized SLKeyframe for node transformations.
void translation(const SLVec3f &t)
void rotation(const SLQuat4f &r)
static const float PI
Definition: Utils.h:237