SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneRTSpheres.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneRTSpheres.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 <AppDemoSceneRTSpheres.h>
14 #include <AppCommon.h>
15 #include <AppDemoSceneID.h>
16 #include <SLAssetLoader.h>
17 #include <SLLightSpot.h>
18 #include <SLRectangle.h>
19 #include <SLSphere.h>
20 
21 //-----------------------------------------------------------------------------
23  : SLScene("Ray Tracing Spheres"),
24  _sceneID(sceneID)
25 {
26  if (sceneID == SID_RTSpheres)
27  info("Classic ray tracing scene with transparent and reflective "
28  "spheres. Be patient on mobile devices.");
29  else if (sceneID == SID_RTSoftShadows)
30  {
31  name("Soft Shadow Ray Tracing");
32  info("Ray tracing with soft shadow light sampling. Each light "
33  "source is sampled 64x per pixel. Be patient on mobile devices.");
34  }
35  else
36  SL_EXIT_MSG("Should not get here!");
37 }
38 //-----------------------------------------------------------------------------
39 //! All assets the should be loaded in parallel must be registered in here.
41 {
42 }
43 //-----------------------------------------------------------------------------
44 //! After parallel loading of the assets the scene gets assembled in here.
46 {
47 
48  if (_sceneID == SID_RTSpheres)
49  {
50  // define materials
51  SLMaterial* matGla = new SLMaterial(am,
52  "Glass",
53  SLCol4f(0.0f, 0.0f, 0.0f),
54  SLCol4f(0.5f, 0.5f, 0.5f),
55  100,
56  0.4f,
57  0.6f,
58  1.5f);
59  SLMaterial* matRed = new SLMaterial(am,
60  "Red",
61  SLCol4f(0.5f, 0.0f, 0.0f),
62  SLCol4f(0.5f, 0.5f, 0.5f),
63  100,
64  0.5f,
65  0.0f,
66  1.0f);
67  SLMaterial* matYel = new SLMaterial(am,
68  "Floor",
69  SLCol4f(0.8f, 0.6f, 0.2f),
70  SLCol4f(0.8f, 0.8f, 0.8f),
71  100,
72  0.5f,
73  0.0f,
74  1.0f);
75 
76  SLCamera* cam1 = new SLCamera();
77  cam1->translation(0, 0.1f, 2.5f);
78  cam1->lookAt(0, 0, 0);
79  cam1->focalDist(cam1->translationOS().length());
80  cam1->background().colors(SLCol4f(0.1f, 0.4f, 0.8f));
81  cam1->setInitialState();
83 
84  SLNode* rect = new SLNode(new SLRectangle(am,
85  SLVec2f(-3, -3),
86  SLVec2f(5, 4),
87  20,
88  20,
89  "Floor",
90  matYel));
91  rect->rotate(90, -1, 0, 0);
92  rect->translate(0, -1, -0.5f, TS_object);
93 
94  SLLightSpot* light1 = new SLLightSpot(am,
95  this,
96  2,
97  2,
98  2,
99  0.1f);
100  light1->powers(1, 7, 7);
101  light1->attenuation(0, 0, 1);
102 
103  SLLightSpot* light2 = new SLLightSpot(am,
104  this,
105  2,
106  2,
107  -2,
108  0.1f);
109  light2->powers(1, 7, 7);
110  light2->attenuation(0, 0, 1);
111 
112  SLNode* scene = new SLNode;
113  sv->camera(cam1);
114  scene->addChild(light1);
115  scene->addChild(light2);
116  scene->addChild(SphereGroupRT(am,
117  3,
118  0,
119  0,
120  0,
121  1,
122  30,
123  matGla,
124  matRed));
125  scene->addChild(rect);
126  scene->addChild(cam1);
127 
128  root3D(scene);
129  }
130  else if (_sceneID == SID_RTSoftShadows)
131  {
132  // Create root node
133  SLNode* scene = new SLNode;
134  root3D(scene);
135 
136  // define materials
137  SLCol4f spec(0.8f, 0.8f, 0.8f);
138  SLMaterial* matBlk = new SLMaterial(am,
139  "Glass",
140  SLCol4f(0.0f, 0.0f, 0.0f),
141  SLCol4f(0.5f, 0.5f, 0.5f),
142  100,
143  0.5f,
144  0.5f,
145  1.5f);
146  SLMaterial* matRed = new SLMaterial(am,
147  "Red",
148  SLCol4f(0.5f, 0.0f, 0.0f),
149  SLCol4f(0.5f, 0.5f, 0.5f),
150  100,
151  0.5f,
152  0.0f,
153  1.0f);
154  SLMaterial* matYel = new SLMaterial(am,
155  "Floor",
156  SLCol4f(0.8f, 0.6f, 0.2f),
157  SLCol4f(0.8f, 0.8f, 0.8f),
158  100,
159  0.0f,
160  0.0f,
161  1.0f);
162 
163  SLCamera* cam1 = new SLCamera;
164  cam1->translation(0, 0.1f, 4);
165  cam1->lookAt(0, 0, 0);
166  cam1->focalDist(cam1->translationOS().length());
167  cam1->background().colors(SLCol4f(0.1f, 0.4f, 0.8f));
168  cam1->setInitialState();
170  scene->addChild(cam1);
171 
172  SLNode* rect = new SLNode(new SLRectangle(am,
173  SLVec2f(-5, -5),
174  SLVec2f(5, 5),
175  1,
176  1,
177  "Rect",
178  matYel));
179  rect->rotate(90, -1, 0, 0);
180  rect->translate(0, 0, -0.5f);
181  rect->castsShadows(false);
182  scene->addChild(rect);
183 
184  SLLightSpot* light1 = new SLLightSpot(am,
185  this,
186  2,
187  2,
188  2,
189  0.3f);
190  light1->samples(8, 8);
191  light1->attenuation(0, 0, 1);
192  light1->createsShadows(true);
193  light1->createShadowMap();
194  scene->addChild(light1);
195 
196  SLLightSpot* light2 = new SLLightSpot(am,
197  this,
198  2,
199  2,
200  -2,
201  0.3f);
202  light2->samples(8, 8);
203  light2->attenuation(0, 0, 1);
204  light2->createsShadows(true);
205  light2->createShadowMap();
206  scene->addChild(light2);
207 
208  scene->addChild(SphereGroupRT(am,
209  1,
210  0,
211  0,
212  0,
213  1,
214  32,
215  matBlk,
216  matRed));
217 
218  sv->camera(cam1);
219  }
220 }
221 //-----------------------------------------------------------------------------
222 //! Creates a recursive sphere group used for the ray tracing scenes
224  SLint depth,
225  SLfloat x,
226  SLfloat y,
227  SLfloat z,
228  SLfloat scale,
229  SLuint resolution,
230  SLMaterial* matGlass,
231  SLMaterial* matRed)
232 {
233 
234  SLstring name = matGlass->kt() > 0 ? "GlassSphere" : "RedSphere";
235  if (depth == 0)
236  {
237  SLSphere* sphere = new SLSphere(am, 0.5f * scale, resolution, resolution, name, matRed);
238  SLNode* sphNode = new SLNode(sphere, "Sphere");
239  sphNode->translate(x, y, z, TS_object);
240  return sphNode;
241  }
242  else
243  {
244  depth--;
245  SLNode* sGroup = new SLNode(new SLSphere(am, 0.5f * scale, resolution, resolution, name, matGlass), "SphereGroupRT");
246  sGroup->translate(x, y, z, TS_object);
247  SLuint newRes = (SLuint)std::max((SLint)resolution - 4, 8);
248  sGroup->addChild(SphereGroupRT(am, depth, 0.643951f * scale, 0, 0.172546f * scale, scale / 3, newRes, matRed, matRed));
249  sGroup->addChild(SphereGroupRT(am, depth, 0.172546f * scale, 0, 0.643951f * scale, scale / 3, newRes, matRed, matRed));
250  sGroup->addChild(SphereGroupRT(am, depth, -0.471405f * scale, 0, 0.471405f * scale, scale / 3, newRes, matRed, matRed));
251  sGroup->addChild(SphereGroupRT(am, depth, -0.643951f * scale, 0, -0.172546f * scale, scale / 3, newRes, matRed, matRed));
252  sGroup->addChild(SphereGroupRT(am, depth, -0.172546f * scale, 0, -0.643951f * scale, scale / 3, newRes, matRed, matRed));
253  sGroup->addChild(SphereGroupRT(am, depth, 0.471405f * scale, 0, -0.471405f * scale, scale / 3, newRes, matRed, matRed));
254  sGroup->addChild(SphereGroupRT(am, depth, 0.272166f * scale, 0.544331f * scale, 0.272166f * scale, scale / 3, newRes, matRed, matRed));
255  sGroup->addChild(SphereGroupRT(am, depth, -0.371785f * scale, 0.544331f * scale, 0.099619f * scale, scale / 3, newRes, matRed, matRed));
256  sGroup->addChild(SphereGroupRT(am, depth, 0.099619f * scale, 0.544331f * scale, -0.371785f * scale, scale / 3, newRes, matRed, matRed));
257  return sGroup;
258  }
259 }
260 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Definition of scene IDs in the demo app.
@ SID_RTSoftShadows
@ SID_RTSpheres
Class declaration for an SLScene inherited class.
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
#define SL_EXIT_MSG(message)
Definition: SL.h:240
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
int SLSceneID
Scene identifier.
Definition: SLEnums.h:91
@ TS_object
Definition: SLEnums.h:210
SLScene SLSceneView SLint sceneID
Definition: SLScene.h:33
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLDeviceRotation devRot
Mobile device rotation from IMU.
Definition: AppCommon.h:64
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
AppDemoSceneRTSpheres(SLSceneID sceneID)
SLNode * SphereGroupRT(SLAssetManager *am, SLint depth, SLfloat x, SLfloat y, SLfloat z, SLfloat scale, SLuint resolution, SLMaterial *matGlass, SLMaterial *matRed)
Creates a recursive sphere group used for the ray tracing scenes.
void registerAssetsToLoad(SLAssetLoader &al) override
All assets the should be loaded in parallel must be registered in here.
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
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
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
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
void samples(SLuint x, SLuint y)
Definition: SLLightSpot.h:77
void createShadowMap(float lightClipNear=0.1f, float lightClipFar=20.0f, SLVec2f size=SLVec2f(8, 8), SLVec2i texSize=SLVec2i(1024, 1024)) override
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
void kt(SLfloat kt)
Definition: SLMaterial.h:190
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 rotate(const SLQuat4f &rot, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:945
void castsShadows(SLbool castsShadows)
Definition: SLNode.h:282
SLVec3f translationOS() const
Definition: SLNode.h:468
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
const SLstring & name() const
Definition: SLObject.h:38
SLRectangle creates a rectangular mesh with a certain resolution.
Definition: SLRectangle.h:29
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 SLSpheric w. 180 deg polar angle.
Definition: SLSphere.h:33
T length() const
Definition: SLVec3.h:122