SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneSuzanne.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneSuzanne.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 
13 #include <AppDemoSceneSuzanne.h>
14 #include <SLAssetLoader.h>
15 #include <SLLightSpot.h>
16 #include <SLRectangle.h>
17 #include <AppCommon.h>
18 
19 //-----------------------------------------------------------------------------
21  bool textureMapping,
22  bool normalMapping,
23  bool occlusionMapping,
24  bool shadowMapping,
25  bool environmentMapping)
26  : SLScene(name),
27  _textureMapping(textureMapping),
28  _normalMapping(normalMapping),
29  _occlusionMapping(occlusionMapping),
30  _shadowMapping(shadowMapping),
31  _environmentMapping(environmentMapping)
32 {
33  info(name);
34  _skybox = nullptr;
35 }
36 //-----------------------------------------------------------------------------
37 //! All assets the should be loaded in parallel must be registered in here.
39 {
42  "GLTF/AO-Baked-Test/AO-Baked-Test.gltf",
43  nullptr,
44  false, // delete tex images after build
45  true, // load meshes only
46  nullptr, // override material
47  0.5f);
48 
51  al.modelPath() +
52  "GLTF/glTF-Sample-Models/hdris/envmap_malibu.hdr",
53  SLVec2i(256, 256),
54  "HDR Skybox");
55 }
56 //-----------------------------------------------------------------------------
57 //! After parallel loading of the assets the scene gets assembled in here.
59 {
60  // Create a scene group node
61  SLNode* scene = new SLNode("scene node");
62  root3D(scene);
63 
64  // Create camera in the center
65  SLCamera* cam1 = new SLCamera("Camera 1");
66  cam1->translation(0, 0.5f, 2);
67  cam1->lookAt(0, 0.5f, 0);
68  cam1->setInitialState();
69  cam1->focalDist(2);
70  scene->addChild(cam1);
71 
72  // Create directional light for the sunlight
73  SLLightDirect* light;
74 
76  {
77  SLLight::gamma = 2.0f;
78  light = new SLLightDirect(am, this, 0.1f);
79  light->ambientPower(0.0f);
80  light->diffusePower(3.0f);
81  }
82  else
83  {
84  SLLight::gamma = 1.0f;
85  light = new SLLightDirect(am, this, 0.1f);
86  light->ambientPower(0.6f);
87  light->diffusePower(0.6f);
88  }
89 
90  light->attenuation(1, 0, 0);
91  light->translate(0, 0, 0.5);
92  light->lookAt(1, -1, 0.5);
93  SLAnimation* lightAnim = animManager().createNodeAnimation("LightAnim",
94  4.0f,
95  true,
98  lightAnim->createNodeAnimTrackForRotation(light,
99  -180,
100  SLVec3f(0, 1, 0));
101 
102  scene->addChild(light);
103 
104  // Add shadow mapping
105  if (_shadowMapping)
106  {
107  light->createsShadows(true);
108  light->createShadowMap(-3,
109  3,
110  SLVec2f(5, 5),
111  SLVec2i(2048, 2048));
112  light->doSmoothShadows(true);
113  }
114 
115  SLCol4f stoneColor(0.56f, 0.50f, 0.44f);
116 
117  // Remove unwanted textures
118  auto materialUpdater = [=](SLMaterial* mat)
119  {
120  if (!_textureMapping)
121  {
122  mat->removeTextureType(TT_diffuse);
123  mat->ambientDiffuse(stoneColor);
124  }
125 
126  if (!_normalMapping)
127  mat->removeTextureType(TT_normal);
128 
129  if (!_occlusionMapping)
130  mat->removeTextureType(TT_occlusion);
131 
133  {
134  mat->skybox(_skybox);
135  mat->reflectionModel(RM_CookTorrance);
136  this->skybox(_skybox);
137  }
138  };
139  _suzanneInCube->updateMeshMat(materialUpdater,
140  true);
141 
142  scene->addChild(_suzanneInCube);
143 
144  sv->camera(cam1);
145 
146  // Save energy
147  sv->doWaitOnIdle(true);
148 }
149 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
string SLstring
Definition: SL.h:158
@ EC_inOutSine
sine easing in and then out
Definition: SLEnums.h:200
@ RM_CookTorrance
Definition: SLEnums.h:290
@ AL_pingPongLoop
loop forward and backwards
Definition: SLEnums.h:171
@ TT_normal
Definition: SLGLTexture.h:79
@ TT_diffuse
Definition: SLGLTexture.h:78
@ TT_occlusion
Definition: SLGLTexture.h:83
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
static SLstring modelPath
Path to 3D models.
Definition: AppCommon.h:85
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.
AppDemoSceneSuzanne(SLstring name, bool textureMapping, bool normalMapping, bool occlusionMapping, bool shadowMapping, bool environmentMapping)
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForRotation(SLNode *target, SLfloat angleDeg1, const SLVec3f &axis)
void addSkyboxToLoad(SLSkybox *&skybox, const SLstring &path, SLVec2i resolution, SLstring name)
Add skybox with HDR texture to load.
SLstring modelPath() const
Definition: SLAssetLoader.h:69
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.
Active or visible camera node class.
Definition: SLCamera.h:54
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
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
static SLfloat gamma
final output gamma value
Definition: SLLight.h:204
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
void ambientPower(const SLfloat ambPow)
Definition: SLLight.h:106
void doSmoothShadows(SLbool doSS)
Definition: SLLight.h:126
void diffusePower(const SLfloat diffPow)
Definition: SLLight.h:108
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
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 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 updateMeshMat(std::function< void(SLMaterial *m)> setMat, bool recursive)
Updates the mesh material recursively with a material lambda.
Definition: SLNode.cpp:1161
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
const SLstring & name() const
Definition: SLObject.h:38
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SLSkybox * skybox()
Definition: SLScene.h:101
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
void doWaitOnIdle(SLbool doWI)
Definition: SLSceneView.h:149