SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoScenePTMuttenzerBox.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoScenePTMuttenzerBox.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 
14 #include <AppCommon.h>
15 #include <SLAssetLoader.h>
16 #include <SLLightRect.h>
17 #include <SLRectangle.h>
18 #include <SLSphere.h>
19 
20 //-----------------------------------------------------------------------------
22  : SLScene("Muttenzer Box Path Tracing")
23 {
24  info("Muttenzer Box with environment mapped reflective sphere and "
25  "transparent refractive glass sphere. Try path tracing for real "
26  "reflections and soft shadows.");
27 }
28 //-----------------------------------------------------------------------------
29 //! All assets the should be loaded in parallel must be registered in here.
31 {
32 
34  AppCommon::texturePath + "MuttenzerBox+X0512_C.png",
35  AppCommon::texturePath + "MuttenzerBox-X0512_C.png",
36  AppCommon::texturePath + "MuttenzerBox+Y0512_C.png",
37  AppCommon::texturePath + "MuttenzerBox-Y0512_C.png",
38  AppCommon::texturePath + "MuttenzerBox+Z0512_C.png",
39  AppCommon::texturePath + "MuttenzerBox-Z0512_C.png");
40 
42  AppCommon::shaderPath + "Reflect.vert",
43  AppCommon::shaderPath + "Reflect.frag");
45  AppCommon::shaderPath + "RefractReflect.vert",
46  AppCommon::shaderPath + "RefractReflect.frag");
47 }
48 //-----------------------------------------------------------------------------
49 //! After parallel loading of the assets the scene gets assembled in here.
51  SLSceneView* sv)
52 {
53  SLCol4f lightEmisRGB(10.0f, 10.0f, 10.0f);
54  SLCol4f grayRGB(0.75f, 0.75f, 0.75f);
55  SLCol4f redRGB(0.75f, 0.25f, 0.25f);
56  SLCol4f blueRGB(0.25f, 0.25f, 0.75f);
57  SLCol4f blackRGB(0.00f, 0.00f, 0.00f);
58 
59  // create materials
60  SLMaterial* cream = new SLMaterial(am,
61  "cream",
62  grayRGB,
64  0);
65  SLMaterial* red = new SLMaterial(am,
66  "red",
67  redRGB,
69  0);
70  SLMaterial* blue = new SLMaterial(am,
71  "blue",
72  blueRGB,
74  0);
75 
76  // Material for mirror sphere
77  SLMaterial* refl = new SLMaterial(am,
78  "refl",
79  blackRGB,
81  1000,
82  1.0f);
83  refl->addTexture(_tex1);
84  refl->program(_spRefl);
85 
86  // Material for glass sphere. The shininess is the width of the Fresnel
87  // reflection lobe of the path tracer and has to stay at PERFECT for the
88  // clear glass of this scene; it used to be 100, which had no effect while
89  // the transmissive branch reflected as a perfect mirror regardless. It is
90  // unused everywhere else here, because the specular color is black and
91  // RefractReflect.frag has no specular term at all.
92  SLMaterial* refr = new SLMaterial(am,
93  "refr",
94  blackRGB,
95  blackRGB,
97  0.05f,
98  0.95f,
99  1.5f);
100  refr->translucency(1000);
102  refr->addTexture(_tex1);
103  refr->program(_spRefr);
104 
105  SLNode* sphere1 = new SLNode(new SLSphere(am,
106  0.5f,
107  32,
108  32,
109  "Sphere1",
110  refl));
111  sphere1->translate(-0.65f, -0.75f, -0.55f, TS_object);
112 
113  SLNode* sphere2 = new SLNode(new SLSphere(am,
114  0.45f,
115  32,
116  32,
117  "Sphere2",
118  refr));
119  sphere2->translate(0.73f, -0.8f, 0.10f, TS_object);
120 
121  SLNode* balls = new SLNode;
122  balls->addChild(sphere1);
123  balls->addChild(sphere2);
124 
125  // Rectangular light
126  SLLightRect* lightRect = new SLLightRect(am,
127  this,
128  1,
129  0.65f);
130  lightRect->rotate(90, -1.0f, 0.0f, 0.0f);
131  lightRect->translate(0.0f, -0.25f, 1.18f, TS_object);
132  lightRect->spotCutOffDEG(90);
133  lightRect->spotExponent(1.0);
134  lightRect->ambientColor(SLCol4f::WHITE);
135  lightRect->ambientPower(0.25f);
136  lightRect->diffuseColor(lightEmisRGB);
137  lightRect->attenuation(0, 0, 1);
138  lightRect->samplesXY(11, 7);
139  lightRect->createsShadows(true);
140  lightRect->createShadowMap();
141 
142  SLLight::globalAmbient.set(lightEmisRGB * 0.01f);
143 
144  // create camera
145  SLCamera* cam1 = new SLCamera();
146  cam1->translation(0, 0, 7.2f);
147  cam1->fov(27);
148  cam1->focalDist(cam1->translationOS().length());
149  cam1->background().colors(SLCol4f(0.0f, 0.0f, 0.0f));
150  cam1->setInitialState();
152 
153  // assemble scene
154  SLNode* scene = new SLNode;
155  root3D(scene);
156  scene->addChild(cam1);
157  scene->addChild(lightRect);
158 
159  // create wall polygons
160  SLfloat pL = -1.48f, pR = 1.48f; // left/right
161  SLfloat pB = -1.25f, pT = 1.19f; // bottom/top
162  SLfloat pN = 1.79f, pF = -1.55f; // near/far
163 
164  // bottom plane
165  SLNode* b = new SLNode(new SLRectangle(am,
166  SLVec2f(pL, -pN),
167  SLVec2f(pR, -pF),
168  6,
169  6,
170  "bottom",
171  cream));
172  b->rotate(90, -1, 0, 0);
173  b->translate(0, 0, pB, TS_object);
174  scene->addChild(b);
175 
176  // top plane
177  SLNode* t = new SLNode(new SLRectangle(am,
178  SLVec2f(pL, pF),
179  SLVec2f(pR, pN),
180  6,
181  6,
182  "top",
183  cream));
184  t->rotate(90, 1, 0, 0);
185  t->translate(0, 0, -pT, TS_object);
186  scene->addChild(t);
187 
188  // far plane
189  SLNode* f = new SLNode(new SLRectangle(am,
190  SLVec2f(pL, pB),
191  SLVec2f(pR, pT),
192  6,
193  6,
194  "far",
195  cream));
196  f->translate(0, 0, pF, TS_object);
197  scene->addChild(f);
198 
199  // left plane
200  SLNode* l = new SLNode(new SLRectangle(am,
201  SLVec2f(-pN, pB),
202  SLVec2f(-pF, pT),
203  6,
204  6,
205  "left",
206  red));
207  l->rotate(90, 0, 1, 0);
208  l->translate(0, 0, pL, TS_object);
209  scene->addChild(l);
210 
211  // right plane
212  SLNode* r = new SLNode(new SLRectangle(am,
213  SLVec2f(pF, pB),
214  SLVec2f(pN, pT),
215  6,
216  6,
217  "right",
218  blue));
219  r->rotate(90, 0, -1, 0);
220  r->translate(0, 0, -pR, TS_object);
221  scene->addChild(r);
222 
223  scene->addChild(balls);
224 
225  sv->camera(cam1);
226 }
227 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
float SLfloat
analog to GLfloat
Definition: SL.h:200
@ TS_object
Definition: SLEnums.h:210
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
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
static SLstring shaderPath
Path to GLSL shader programs.
Definition: AppCommon.h:84
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.
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
void fov(const SLfloat fov)
vertical field of view
Definition: SLCamera.h:98
void ambientColor(const SLCol4f &ambi)
Definition: SLLight.h:105
void createsShadows(SLbool createsShadows)
Definition: SLLight.cpp:98
static SLCol4f globalAmbient
static global ambient light intensity
Definition: SLLight.h:202
void ambientPower(const SLfloat ambPow)
Definition: SLLight.h:106
void spotCutOffDEG(SLfloat cutOffAngleDEG)
Definition: SLLight.cpp:92
void diffuseColor(const SLCol4f &diff)
Definition: SLLight.h:107
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
void spotExponent(const SLfloat exp)
Definition: SLLight.h:111
Light node class for a rectangular light source.
Definition: SLLightRect.h:39
void samplesXY(SLint x, SLint y)
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 translucency(SLfloat transl)
Definition: SLMaterial.h:176
static SLfloat PERFECT
PM: shininess/translucency limit.
Definition: SLMaterial.h:238
void addTexture(SLGLTexture *texture)
Adds the passed texture to the equivalent texture type vector.
Definition: SLMaterial.cpp:348
void transmissive(const SLCol4f &transm)
Definition: SLMaterial.h:175
void program(SLGLProgram *sp)
Definition: SLMaterial.h:205
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:148
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
SLVec3f translationOS() const
Definition: SLNode.h:469
void setInitialState()
Definition: SLNode.cpp:1084
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
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:149
SLSphere creates a sphere mesh based on SLSpheric w. 180 deg polar angle.
Definition: SLSphere.h:33
T length() const
Definition: SLVec3.h:122
static SLVec4 BLACK
Definition: SLVec4.h:213
static SLVec4 WHITE
Definition: SLVec4.h:215
void set(const T X, const T Y, const T Z, const T W=1)
Definition: SLVec4.h:49