SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneGLTF.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneGLTF.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 
13 #include <AppDemoSceneGLTF.h>
14 #include <AppCommon.h>
15 #include <AppDemoSceneID.h>
16 #include <SLAssetLoader.h>
17 #include <SLLightSpot.h>
18 #include <SLSphere.h>
19 #include <SLLightDirect.h>
20 
21 //-----------------------------------------------------------------------------
23  : SLScene("GLTF File Demo Scene"),
24  _sceneID(sceneID)
25 {
26  info("GLTF File Format Test Scene");
27 }
28 //-----------------------------------------------------------------------------
29 //! All assets the should be loaded in parallel must be registered in here.
31 {
33  al.modelPath() +
34  "GLTF/glTF-Sample-Models/hdris/envmap_malibu.hdr",
35  SLVec2i(256, 256),
36  "HDR Skybox");
37  switch (_sceneID)
38  {
40  _modelFile = "GLTF/glTF-Sample-Models/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf";
41  this->name("glTF-Sample-Model: Damaged Helmet");
42  break;
44  _modelFile = "GLTF/glTF-Sample-Models/2.0/FlightHelmet/glTF/FlightHelmet.gltf";
45  this->name("glTF-Sample-Model: Flight Helmet");
46  break;
47  case SID_glTF_Sponza:
48  _modelFile = "GLTF/glTF-Sample-Models/2.0/Sponza/glTF/Sponza.gltf";
49  this->name("glTF-Sample-Model: Sponza Palace in Dubrovnic");
50  break;
52  _modelFile = "GLTF/glTF-Sample-Models/2.0/WaterBottle/glTF/WaterBottle.gltf";
53  this->name("glTF-Sample-Model: WaterBottle");
54  break;
55  }
56 
59 }
60 //-----------------------------------------------------------------------------
61 //! After parallel loading of the assets the scene gets assembled in here.
63 {
64  SLVec3f camPos, lookAt;
65  SLfloat camClipFar = 100;
66 
67  switch (_sceneID)
68  {
70  {
71  camPos.set(0, 0, 3);
72  lookAt.set(0, camPos.y, 0);
73  camClipFar = 20;
74  break;
75  }
77  {
78  camPos.set(0, 0.33f, 1.1f);
79  lookAt.set(0, camPos.y, 0);
80  camClipFar = 20;
81  break;
82  }
83  case SID_glTF_Sponza:
84  {
85  camPos.set(-8, 1.6f, 0);
86  lookAt.set(0, camPos.y, 0);
87  break;
88  }
90  {
91  camPos.set(0, 0, 0.5f);
92  lookAt.set(0, camPos.y, 0);
93  camClipFar = 20;
94  break;
95  }
96  }
97 
98  // Create a scene group node
99  SLNode* scene = new SLNode("scene node");
100  this->root3D(scene);
101 
102  // Create camera and initialize its parameters
103  SLCamera* cam1 = new SLCamera("Camera 1");
104  cam1->translation(camPos);
105  cam1->lookAt(lookAt);
106  cam1->background().colors(SLCol4f(0.2f, 0.2f, 0.2f));
107  cam1->focalDist(camPos.length());
108  cam1->clipFar(camClipFar);
109  cam1->setInitialState();
110  scene->addChild(cam1);
111 
112  // Add directional light with a position that corresponds roughly to the sun direction
113  SLLight::gamma = 2.2f;
114  SLLightDirect* light = new SLLightDirect(am,
115  this,
116  0.55f,
117  1.0f,
118  -0.2f,
119  0.2f,
120  0,
121  1,
122  1);
123  light->lookAt(0, 0, 0);
124  light->attenuation(1, 0, 0);
125  light->createsShadows(true);
126  light->createShadowMapAutoSize(cam1,
127  SLVec2i(2048, 2048),
128  4);
129  light->shadowMap()->cascadesFactor(1.0);
130  light->doSmoothShadows(true);
131  light->castsShadows(false);
132  light->shadowMinBias(0.001f);
133  light->shadowMaxBias(0.003f);
134  scene->addChild(light);
135 
136  // Update all materials and set their skybox to _skybox
138  { m->skybox(_skybox); },
139  true);
140 
141  scene->addChild(_modelGLTF);
142 
143  this->skybox(_skybox);
144  sv->camera(cam1);
145  sv->doWaitOnIdle(true); // Saves energy
146 }
147 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
Definition of scene IDs in the demo app.
@ SID_glTF_Sponza
@ SID_glTF_DamagedHelmet
@ SID_glTF_FlightHelmet
@ SID_glTF_WaterBottle
float SLfloat
Definition: SL.h:173
int SLSceneID
Scene identifier.
Definition: SLEnums.h:91
SLScene SLSceneView SLint sceneID
Definition: SLScene.h:33
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.
AppDemoSceneGLTF(SLSceneID sceneID)
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.
void colors(const SLCol4f &uniformColor)
Sets a uniform background color.
Active or visible camera node class.
Definition: SLCamera.h:54
void clipFar(const SLfloat cFar)
Definition: SLCamera.h:109
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 createShadowMapAutoSize(SLCamera *camera, SLVec2i texSize=SLVec2i(1024, 1024), int numCascades=4) override
static SLfloat gamma
final output gamma value
Definition: SLLight.h:204
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
void shadowMaxBias(SLfloat maxBias)
Definition: SLLight.h:129
void shadowMap(SLShadowMap *shadowMap)
Definition: SLLight.h:125
void doSmoothShadows(SLbool doSS)
Definition: SLLight.h:126
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
void shadowMinBias(SLfloat minBias)
Definition: SLLight.h:128
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
void skybox(SLSkybox *sb)
Definition: SLMaterial.h:207
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 updateMeshMat(std::function< void(SLMaterial *m)> setMat, bool recursive)
Updates the mesh material recursively with a material lambda.
Definition: SLNode.cpp:1161
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
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
T y
Definition: SLVec3.h:43
T length() const
Definition: SLVec3.h:122
void set(const T X, const T Y, const T Z)
Definition: SLVec3.h:59