SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneShaderCook.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneShaderCook.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 <AppDemoSceneShaderCook.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("Cook-Torrance Shading")
23 {
24  info("Cook-Torrance reflection model. Left-Right: roughness 0.05-1, Top-Down: metallic: 1-0. "
25  "The center sphere has roughness and metallic encoded in textures. "
26  "The reflection model produces a more physically based light reflection "
27  "than the standard Blinn-Phong reflection model.");
28 }
29 //-----------------------------------------------------------------------------
30 //! All assets the should be loaded in parallel must be registered in here.
32 {
35  "rusty-metal_2048_C.jpg");
38  "rusty-metal_2048_N.jpg");
41  "rusty-metal_2048_M.jpg");
44  "rusty-metal_2048_R.jpg");
46  AppCommon::shaderPath + "PerPixCook.vert",
47  AppCommon::shaderPath + "PerPixCook.frag");
49  AppCommon::shaderPath + "PerPixCookTm.vert",
50  AppCommon::shaderPath + "PerPixCookTm.frag");
51 }
52 //-----------------------------------------------------------------------------
53 //! After parallel loading of the assets the scene gets assembled in here.
55 {
56  // Base root group node for the scene
57  SLNode* scene = new SLNode;
58  this->root3D(scene);
59 
60  SLCamera* cam1 = new SLCamera("Camera 1");
61  cam1->translation(0, 0, 30);
62  cam1->lookAt(0, 0, 0);
64  cam1->focalDist(30);
65  cam1->setInitialState();
67  scene->addChild(cam1);
68 
69  // Create spheres and materials with roughness & metallic values between 0 and 1
70  const SLint nrRows = 7;
71  const SLint nrCols = 7;
72  SLfloat spacing = 2.5f;
73  SLfloat maxX = (float)(nrCols - 1) * spacing * 0.5f;
74  SLfloat maxY = (float)(nrRows - 1) * spacing * 0.5f;
75  SLfloat deltaR = 1.0f / (float)(nrRows - 1);
76  SLfloat deltaM = 1.0f / (float)(nrCols - 1);
77 
78  SLMaterial* mat[nrRows * nrCols];
79  SLint i = 0;
80  SLfloat y = -maxY;
81  for (SLint m = 0; m < nrRows; ++m)
82  {
83  SLfloat x = -maxX;
84  for (SLint r = 0; r < nrCols; ++r)
85  {
86  if (m == nrRows / 2 && r == nrCols / 2)
87  {
88  // The center sphere has roughness and metallic encoded in textures
89  mat[i] = new SLMaterial(am,
90  "CookTorranceMatTex",
91  nullptr,
92  _texC,
93  _texN,
94  _texM,
95  _texR,
96  nullptr,
97  _spTex);
98  }
99  else
100  {
101  // Cook-Torrance material without textures
102  mat[i] = new SLMaterial(am,
103  "CookTorranceMat",
104  nullptr,
105  SLCol4f::RED * 0.5f,
106  Utils::clamp((float)r * deltaR, 0.05f, 1.0f),
107  (float)m * deltaM,
108  _sp);
109  }
110 
111  SLNode* node = new SLNode(new SLSpheric(am, 1.0f, 0.0f, 180.0f, 32, 32, "Sphere", mat[i]));
112  node->translate(x, y, 0);
113  scene->addChild(node);
114  x += spacing;
115  i++;
116  }
117  y += spacing;
118  }
119 
120  // Add 5 Lights: 2 point lights, 2 directional lights and 1 spotlight in the center.
121  SLLight::gamma = 2.2f;
122  SLLightSpot* light1 = new SLLightSpot(am,
123  this,
124  -maxX,
125  maxY,
126  maxY,
127  0.2f,
128  180,
129  0,
130  1000,
131  1000);
132  light1->attenuation(0, 0, 1);
133  SLLightDirect* light2 = new SLLightDirect(am,
134  this,
135  maxX,
136  maxY,
137  maxY,
138  0.5f,
139  0,
140  10,
141  10);
142  light2->lookAt(0, 0, 0);
143  light2->attenuation(0, 0, 1);
144  SLLightSpot* light3 = new SLLightSpot(am,
145  this,
146  0,
147  0,
148  maxY,
149  0.2f,
150  36,
151  0,
152  1000,
153  1000);
154  light3->attenuation(0, 0, 1);
155  SLLightDirect* light4 = new SLLightDirect(am,
156  this,
157  -maxX,
158  -maxY,
159  maxY,
160  0.5f,
161  0,
162  10,
163  10);
164  light4->lookAt(0, 0, 0);
165  light4->attenuation(0, 0, 1);
166  SLLightSpot* light5 = new SLLightSpot(am,
167  this,
168  maxX,
169  -maxY,
170  maxY,
171  0.2f,
172  180,
173  0,
174  1000,
175  1000);
176  light5->attenuation(0, 0, 1);
177  scene->addChild(light1);
178  scene->addChild(light2);
179  scene->addChild(light3);
180  scene->addChild(light4);
181  scene->addChild(light5);
182  sv->camera(cam1);
183 }
184 //-----------------------------------------------------------------------------
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
static SLDeviceRotation devRot
Mobile device rotation from IMU.
Definition: AppCommon.h:64
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
static SLstring shaderPath
Path to GLSL shader programs.
Definition: AppCommon.h:84
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.
void addProgramToLoad(SLGLProgram *&program, const SLstring &vertShaderFile, const SLstring &fragShaderFile)
Add generic GLSL program with shader files 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 devRotLoc(SLDeviceRotation *devRot, SLDeviceLocation *devLoc)
Definition: SLCamera.h:120
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
static SLfloat gamma
final output gamma value
Definition: SLLight.h:204
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
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
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 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
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
static SLVec4 BLACK
Definition: SLVec4.h:213
static SLVec4 RED
Definition: SLVec4.h:216
T clamp(T a, T min, T max)
Definition: Utils.h:253