SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneRevolver.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneRevolver.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 <AppDemoSceneRevolver.h>
14 #include <AppCommon.h>
15 #include <SLAssetLoader.h>
16 #include <SLLightSpot.h>
17 #include <SLCone.h>
18 #include <SLDisk.h>
19 #include <SLRectangle.h>
20 #include <SLSphere.h>
21 #include <SLCylinder.h>
22 
23 //-----------------------------------------------------------------------------
25 {
26  info("Examples of revolving mesh objects constructed by rotating a 2D curve. "
27  "The glass shader reflects and refracts the environment map. "
28  "Try ray tracing with key R and come back with the ESC key.");
29 }
30 //-----------------------------------------------------------------------------
31 //! All assets the should be loaded in parallel must be registered in here.
33 {
36  "Testmap_1024_C.jpg");
39  "Testmap_1024_N.jpg");
42  "wood0_0512_C.jpg");
45  "bricks1_0256_C.jpg");
48  "wood2_0512_C.jpg");
50  AppCommon::texturePath + "wood2_0256_C.jpg",
51  AppCommon::texturePath + "wood2_0256_C.jpg",
52  AppCommon::texturePath + "gray_0256_C.jpg",
53  AppCommon::texturePath + "wood0_0256_C.jpg",
54  AppCommon::texturePath + "gray_0256_C.jpg",
55  AppCommon::texturePath + "bricks1_0256_C.jpg");
57  AppCommon::shaderPath + "RefractReflect.vert",
58  AppCommon::shaderPath + "RefractReflect.frag");
59 }
60 //-----------------------------------------------------------------------------
61 //! After parallel loading of the assets the scene gets assembled in here.
63 {
64  // Test map material
65  SLMaterial* mat1 = new SLMaterial(am, "mat1", _tex1C, _tex1N);
66 
67  // floor material
68  SLMaterial* mat2 = new SLMaterial(am, "mat2", _tex2);
69  mat2->specular(SLCol4f::BLACK);
70 
71  // Back wall material
72  SLMaterial* mat3 = new SLMaterial(am, "mat3", _tex3);
73  mat3->specular(SLCol4f::BLACK);
74 
75  // Left wall material
76  SLMaterial* mat4 = new SLMaterial(am, "mat4", _tex4);
77  mat4->specular(SLCol4f::BLACK);
78 
79  // Glass material
80  SLMaterial* mat5 = new SLMaterial(am,
81  "glass",
84  255,
85  0.1f,
86  0.9f,
87  1.5f);
88  mat5->addTexture(_tex5);
89  mat5->program(_sp1);
90 
91  // Wine material
92  SLMaterial* mat6 = new SLMaterial(am,
93  "wine",
94  SLCol4f(0.4f, 0.0f, 0.2f),
96  255,
97  0.2f,
98  0.7f,
99  1.3f);
100  mat6->addTexture(_tex5);
101  mat6->program(_sp1);
102 
103  // camera
104  SLCamera* cam1 = new SLCamera();
105  cam1->name("cam1");
106  cam1->translation(0, 1, 17);
107  cam1->lookAt(0, 1, 0);
108  cam1->focalDist(17);
109  cam1->background().colors(SLCol4f(0.7f, 0.7f, 0.7f),
110  SLCol4f(0.2f, 0.2f, 0.2f));
111  cam1->setInitialState();
113 
114  // light
115  SLLightSpot* light1 = new SLLightSpot(am,
116  this,
117  0,
118  4,
119  0,
120  0.3f);
121  light1->powers(0.2f, 1.0f, 1.0f);
122  light1->attenuation(1, 0, 0);
123  SLAnimation* anim = this->animManager().createNodeAnimation("light1_anim", 4.0f);
124  anim->createNodeAnimTrackForEllipse(light1,
125  6.0f,
126  A_z,
127  6.0f,
128  A_x);
129 
130  // glass 2D polygon definition for revolution
131  SLVVec3f revG;
132  revG.push_back(SLVec3f(0.00f, 0.00f)); // foot
133  revG.push_back(SLVec3f(2.00f, 0.00f));
134  revG.push_back(SLVec3f(2.00f, 0.00f));
135  revG.push_back(SLVec3f(2.00f, 0.10f));
136  revG.push_back(SLVec3f(1.95f, 0.15f));
137  revG.push_back(SLVec3f(0.40f, 0.50f)); // stand
138  revG.push_back(SLVec3f(0.25f, 0.60f));
139  revG.push_back(SLVec3f(0.20f, 0.70f));
140  revG.push_back(SLVec3f(0.30f, 3.00f));
141  revG.push_back(SLVec3f(0.30f, 3.00f)); // crack
142  revG.push_back(SLVec3f(0.20f, 3.10f));
143  revG.push_back(SLVec3f(0.20f, 3.10f));
144  revG.push_back(SLVec3f(1.20f, 3.90f)); // outer cup
145  revG.push_back(SLVec3f(1.60f, 4.30f));
146  revG.push_back(SLVec3f(1.95f, 4.80f));
147  revG.push_back(SLVec3f(2.15f, 5.40f));
148  revG.push_back(SLVec3f(2.20f, 6.20f));
149  revG.push_back(SLVec3f(2.10f, 7.10f));
150  revG.push_back(SLVec3f(2.05f, 7.15f));
151  revG.push_back(SLVec3f(2.00f, 7.10f)); // inner cup
152  revG.push_back(SLVec3f(2.05f, 6.00f));
153  SLuint res = 30;
154  SLNode* glass = new SLNode(new SLRevolver(am,
155  revG,
156  SLVec3f(0, 1, 0),
157  res,
158  true,
159  false,
160  "GlassRev",
161  mat5));
162  glass->translate(0.0f, -3.5f, 0.0f, TS_object);
163 
164  // wine 2D polyline definition for revolution with two sided material
165  SLVVec3f revW;
166  revW.push_back(SLVec3f(0.00f, 3.82f));
167  revW.push_back(SLVec3f(0.20f, 3.80f));
168  revW.push_back(SLVec3f(0.80f, 4.00f));
169  revW.push_back(SLVec3f(1.30f, 4.30f));
170  revW.push_back(SLVec3f(1.70f, 4.80f));
171  revW.push_back(SLVec3f(1.95f, 5.40f));
172  revW.push_back(SLVec3f(2.05f, 6.00f));
173  SLMesh* wineMesh = new SLRevolver(am, revW, SLVec3f(0, 1, 0), res, true, false, "WineRev", mat6);
174  wineMesh->matOut(mat5);
175  SLNode* wine = new SLNode(wineMesh);
176  wine->translate(0.0f, -3.5f, 0.0f, TS_object);
177 
178  // wine fluid top
179  SLNode* wineTop = new SLNode(new SLDisk(am, 2.05f, -SLVec3f::AXISY, res, false, "WineRevTop", mat6));
180  wineTop->translate(0.0f, 2.5f, 0.0f, TS_object);
181 
182  // Other revolver objects
183  SLNode* sphere = new SLNode(new SLSphere(am, 1, 16, 16, "sphere", mat1));
184  sphere->translate(3, 0, 0, TS_object);
185  SLNode* cylinder = new SLNode(new SLCylinder(am, 0.1f, 7, 3, 16, true, true, "cylinder", mat1));
186  cylinder->translate(0, 0.5f, 0);
187  cylinder->rotate(90, -1, 0, 0);
188  cylinder->rotate(30, 0, 1, 0);
189  SLNode* cone = new SLNode(new SLCone(am, 1, 3, 3, 16, true, "cone", mat1));
190  cone->translate(-3, -1, 0, TS_object);
191  cone->rotate(90, -1, 0, 0);
192 
193  // Cube dimensions
194  SLfloat pL = -9.0f, pR = 9.0f; // left/right
195  SLfloat pB = -3.5f, pT = 14.5f; // bottom/top
196  SLfloat pN = 9.0f, pF = -9.0f; // near/far
197 
198  // bottom rectangle
199  SLNode* b = new SLNode(new SLRectangle(am, SLVec2f(pL, -pN), SLVec2f(pR, -pF), 10, 10, "PolygonFloor", mat2));
200  b->rotate(90, -1, 0, 0);
201  b->translate(0, 0, pB, TS_object);
202 
203  // top rectangle
204  SLNode* t = new SLNode(new SLRectangle(am, SLVec2f(pL, pF), SLVec2f(pR, pN), 10, 10, "top", mat2));
205  t->rotate(90, 1, 0, 0);
206  t->translate(0, 0, -pT, TS_object);
207 
208  // far rectangle
209  SLNode* f = new SLNode(new SLRectangle(am, SLVec2f(pL, pB), SLVec2f(pR, pT), 10, 10, "far", mat3));
210  f->translate(0, 0, pF, TS_object);
211 
212  // left rectangle
213  SLNode* l = new SLNode(new SLRectangle(am, SLVec2f(-pN, pB), SLVec2f(-pF, pT), 10, 10, "left", mat4));
214  l->rotate(90, 0, 1, 0);
215  l->translate(0, 0, pL, TS_object);
216 
217  // right rectangle
218  SLNode* r = new SLNode(new SLRectangle(am, SLVec2f(pF, pB), SLVec2f(pN, pT), 10, 10, "right", mat4));
219  r->rotate(90, 0, -1, 0);
220  r->translate(0, 0, -pR, TS_object);
221 
222  SLNode* scene = new SLNode;
223  this->root3D(scene);
224  scene->addChild(light1);
225  scene->addChild(glass);
226  scene->addChild(wine);
227  scene->addChild(wineTop);
228  scene->addChild(sphere);
229  scene->addChild(cylinder);
230  scene->addChild(cone);
231  scene->addChild(b);
232  scene->addChild(f);
233  scene->addChild(t);
234  scene->addChild(l);
235  scene->addChild(r);
236  scene->addChild(cam1);
237 
238  sv->camera(cam1);
239 }
240 //-----------------------------------------------------------------------------
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
unsigned int SLuint
Definition: SL.h:171
@ A_z
Definition: SLEnums.h:82
@ A_x
Definition: SLEnums.h:80
@ TS_object
Definition: SLEnums.h:210
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
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.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForEllipse(SLNode *target, SLfloat radiusA, SLAxis axisA, SLfloat radiusB, SLAxis axisB)
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
SLCone creates a cone mesh based on SLRevolver.
Definition: SLCone.h:27
SLCylinder is creates sphere mesh based on its SLRevolver methods.
Definition: SLCylinder.h:27
SLDisk creates a disk mesh based on SLRevolver.
Definition: SLDisk.h:27
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
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
void specular(const SLCol4f &spec)
Definition: SLMaterial.h:173
void addTexture(SLGLTexture *texture)
Adds the passed texture to the equivalent texture type vector.
Definition: SLMaterial.cpp:348
void program(SLGLProgram *sp)
Definition: SLMaterial.h:205
An SLMesh object is a triangulated mesh, drawn with one draw call.
Definition: SLMesh.h:134
SLMaterial * matOut() const
Definition: SLMesh.h:178
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 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
void name(const SLstring &Name)
Definition: SLObject.h:34
SLRectangle creates a rectangular mesh with a certain resolution.
Definition: SLRectangle.h:29
SLRevolver is an SLMesh object built out of revolving points.
Definition: SLRevolver.h:38
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
SLAnimManager & animManager()
Definition: SLScene.h:97
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
static SLVec3 AXISY
Definition: SLVec3.h:298
static SLVec4 BLACK
Definition: SLVec4.h:213
static SLVec4 WHITE
Definition: SLVec4.h:215