SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShaderIBL.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShaderIBL.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 <AppDemoSceneShaderIBL.h>
14 #include <SLAssetLoader.h>
15 #include <SLLightSpot.h>
16 #include <SLLightDirect.h>
17 #include <SLSphere.h>
18 #include <AppCommon.h>
19 
20 //-----------------------------------------------------------------------------
22  : SLScene("Image Based Lighting Test Scene")
23 {
24  info("Image-based lighting from skybox using high dynamic range images. "
25  "It uses the Cook-Torrance reflection model also to calculate the "
26  "ambient light part from the surrounding HDR skybox.");
27 }
28 //-----------------------------------------------------------------------------
29 //! All assets the should be loaded in parallel must be registered in here.
31 {
34  "gold-scuffed_2048_C.png");
37  "gold-scuffed_2048_N.png");
40  "gold-scuffed_2048_M.png");
43  "gold-scuffed_2048_R.png");
46  "gold-scuffed_2048_A.png");
47 
49  al.texturePath() + "env_barce_rooftop.hdr",
50  SLVec2i(256, 256),
51  "HDR Skybox");
52 }
53 //-----------------------------------------------------------------------------
54 //! After parallel loading of the assets the scene gets assembled in here.
56 {
57  // Create a scene group node
58  SLNode* scene = new SLNode("scene node");
59  this->root3D(scene);
60 
61  // Create camera and initialize its parameters
62  SLCamera* cam1 = new SLCamera("Camera 1");
63  cam1->translation(0, 0, 30);
64  cam1->lookAt(0, 0, 0);
65  cam1->background().colors(SLCol4f(0.2f, 0.2f, 0.2f));
66  cam1->focalDist(30);
67  cam1->setInitialState();
68  scene->addChild(cam1);
69 
70  // Add directional light with a position that corresponds roughly to the sun direction
71  SLLight::gamma = 2.2f;
72  SLLightDirect* light1 = new SLLightDirect(am,
73  this,
74  4.0f,
75  .3f,
76  2.0f,
77  0.5f,
78  0,
79  1,
80  1);
81  light1->lookAt(0, 0, 0);
82  light1->attenuation(1, 0, 0);
83  light1->createsShadows(true);
84  light1->createShadowMapAutoSize(cam1, SLVec2i(2048, 2048), 4);
85  light1->shadowMap()->cascadesFactor(30.0);
86  light1->doSmoothShadows(true);
87  light1->castsShadows(false);
88  light1->shadowMinBias(0.001f);
89  light1->shadowMaxBias(0.003f);
90  scene->addChild(light1);
91 
92  // Create spheres and materials with roughness & metallic values between 0 and 1
93  const SLint nrRows = 7;
94  const SLint nrCols = 7;
95  SLfloat spacing = 2.5f;
96  SLfloat maxX = (float)((int)(nrCols / 2) * spacing);
97  SLfloat maxY = (float)((int)(nrRows / 2) * spacing);
98  SLfloat deltaR = 1.0f / (float)(nrRows - 1);
99  SLfloat deltaM = 1.0f / (float)(nrCols - 1);
100 
101  SLMaterial* mat[nrRows * nrCols];
102  SLint i = 0;
103  SLfloat y = -maxY;
104  for (SLint m = 0; m < nrRows; ++m)
105  {
106  SLfloat x = -maxX;
107  for (SLint r = 0; r < nrCols; ++r)
108  {
109  if (m == nrRows / 2 && r == nrCols / 2)
110  {
111  // The center sphere has roughness and metallic encoded in textures
112  // and the prefiltered textures for IBL
113  mat[i] = new SLMaterial(am,
114  "IBLMatTex",
115  _skybox,
116  _texC,
117  _texN,
118  _texM,
119  _texR,
120  _texA);
121  }
122  else
123  {
124  // Cook-Torrance material with IBL but without textures
125  mat[i] = new SLMaterial(am,
126  "IBLMat",
127  _skybox,
128  SLCol4f::WHITE * 0.5f,
129  Utils::clamp((float)r * deltaR, 0.05f, 1.0f),
130  (float)m * deltaM);
131  }
132 
133  SLNode* node = new SLNode(new SLSpheric(am,
134  1.0f,
135  0.0f,
136  180.0f,
137  32,
138  32,
139  "Sphere",
140  mat[i]));
141  node->translate(x, y, 0);
142  scene->addChild(node);
143  x += spacing;
144  i++;
145  }
146  y += spacing;
147  }
148 
149  sv->camera(cam1);
150  this->skybox(_skybox);
151 
152  // Save energy
153  sv->doWaitOnIdle(true);
154 }
155 //-----------------------------------------------------------------------------
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
SLVec2< SLint > SLVec2i
Definition: SLVec2.h:140
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
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.
SLstring texturePath() const
Definition: SLAssetLoader.h:71
void addSkyboxToLoad(SLSkybox *&skybox, const SLstring &path, SLVec2i resolution, SLstring name)
Add skybox with HDR texture to load.
void addTextureToLoad(SLGLTexture *&texture, const SLstring &path, SLint min_filter=GL_LINEAR_MIPMAP_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT)
Add 2D textures with internal image allocation.
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 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
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
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
SLSphere creates a sphere mesh based on SLRevolver.
Definition: SLSpheric.h:21
static SLVec4 WHITE
Definition: SLVec4.h:215
T clamp(T a, T min, T max)
Definition: Utils.h:253