SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneRTMuttenzerBox.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneRTMuttenzerBox.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 Ray Tracing")
23 {
24  info("Muttenzer Box with environment mapped reflective sphere and "
25  "transparent refractive glass sphere. Try ray 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(7.0f, 7.0f, 7.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
87  SLMaterial* refr = new SLMaterial(am,
88  "refr",
89  blackRGB,
90  blackRGB,
91  100,
92  0.05f,
93  0.95f,
94  1.5f);
95  refr->translucency(1000);
97  refr->addTexture(_tex1);
98  refr->program(_spRefr);
99 
100  SLNode* sphere1 = new SLNode(new SLSphere(am,
101  0.5f,
102  32,
103  32,
104  "Sphere1",
105  refl));
106  sphere1->translate(-0.65f, -0.75f, -0.55f, TS_object);
107 
108  SLNode* sphere2 = new SLNode(new SLSphere(am,
109  0.45f,
110  32,
111  32,
112  "Sphere2",
113  refr));
114  sphere2->translate(0.73f, -0.8f, 0.10f, TS_object);
115 
116  SLNode* balls = new SLNode;
117  balls->addChild(sphere1);
118  balls->addChild(sphere2);
119 
120  // Rectangular light
121  SLLightRect* lightRect = new SLLightRect(am,
122  this,
123  1,
124  0.65f);
125  lightRect->rotate(90, -1.0f, 0.0f, 0.0f);
126  lightRect->translate(0.0f, -0.25f, 1.18f, TS_object);
127  lightRect->spotCutOffDEG(90);
128  lightRect->spotExponent(1.0);
129  lightRect->ambientColor(SLCol4f::WHITE);
130  lightRect->ambientPower(0.25f);
131  lightRect->diffuseColor(lightEmisRGB);
132  lightRect->attenuation(0, 0, 1);
133  lightRect->samplesXY(11, 7);
134  lightRect->createsShadows(true);
135  lightRect->createShadowMap();
136 
137  SLLight::globalAmbient.set(lightEmisRGB * 0.01f);
138 
139  // create camera
140  SLCamera* cam1 = new SLCamera();
141  cam1->translation(0, 0, 7.2f);
142  cam1->fov(27);
143  cam1->focalDist(cam1->translationOS().length());
144  cam1->background().colors(SLCol4f(0.0f, 0.0f, 0.0f));
145  cam1->setInitialState();
147 
148  // assemble scene
149  SLNode* scene = new SLNode;
150  root3D(scene);
151  scene->addChild(cam1);
152  scene->addChild(lightRect);
153 
154  // create wall polygons
155  SLfloat pL = -1.48f, pR = 1.48f; // left/right
156  SLfloat pB = -1.25f, pT = 1.19f; // bottom/top
157  SLfloat pN = 1.79f, pF = -1.55f; // near/far
158 
159  // bottom plane
160  SLNode* b = new SLNode(new SLRectangle(am,
161  SLVec2f(pL, -pN),
162  SLVec2f(pR, -pF),
163  6,
164  6,
165  "bottom",
166  cream));
167  b->rotate(90, -1, 0, 0);
168  b->translate(0, 0, pB, TS_object);
169  scene->addChild(b);
170 
171  // top plane
172  SLNode* t = new SLNode(new SLRectangle(am,
173  SLVec2f(pL, pF),
174  SLVec2f(pR, pN),
175  6,
176  6,
177  "top",
178  cream));
179  t->rotate(90, 1, 0, 0);
180  t->translate(0, 0, -pT, TS_object);
181  scene->addChild(t);
182 
183  // far plane
184  SLNode* f = new SLNode(new SLRectangle(am,
185  SLVec2f(pL, pB),
186  SLVec2f(pR, pT),
187  6,
188  6,
189  "far",
190  cream));
191  f->translate(0, 0, pF, TS_object);
192  scene->addChild(f);
193 
194  // left plane
195  SLNode* l = new SLNode(new SLRectangle(am,
196  SLVec2f(-pN, pB),
197  SLVec2f(-pF, pT),
198  6,
199  6,
200  "left",
201  red));
202  l->rotate(90, 0, 1, 0);
203  l->translate(0, 0, pL, TS_object);
204  scene->addChild(l);
205 
206  // right plane
207  SLNode* r = new SLNode(new SLRectangle(am,
208  SLVec2f(pF, pB),
209  SLVec2f(pN, pT),
210  6,
211  6,
212  "right",
213  blue));
214  r->rotate(90, 0, -1, 0);
215  r->translate(0, 0, -pR, TS_object);
216  scene->addChild(r);
217 
218  scene->addChild(balls);
219 
220  sv->camera(cam1);
221 }
222 //-----------------------------------------------------------------------------
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
@ 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
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: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
SLVec3f translationOS() const
Definition: SLNode.h:468
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: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
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