SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneJansUniverse.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneJansUniverse.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 <SLLightSpot.h>
17 #include <SLRectangle.h>
18 #include <SLSphere.h>
19 
20 #ifndef SL_GLES
21 const SLuint NUM_MAT_MESH = 100;
22 const SLuint NUM_LEVELS = 6;
23 const SLuint NUM_CHILDREN = 8;
24 #else
25 const SLuint NUM_MAT_MESH = 20;
26 const SLuint NUM_LEVELS = 6;
27 const SLuint NUM_CHILDREN = 8;
28 #endif
29 
30 //-----------------------------------------------------------------------------
32  : SLScene("Jan's Universe Test Scene")
33 {
34  info("Jan's Universe Test Scene");
35 }
36 //-----------------------------------------------------------------------------
37 //! All assets the should be loaded in parallel must be registered in here.
39 {
40  al.addTextureToLoad(_textureC, AppCommon::texturePath + "rusty-metal_2048_C.jpg");
41  al.addTextureToLoad(_textureM, AppCommon::texturePath + "rusty-metal_2048_M.jpg");
42  al.addTextureToLoad(_textureR, AppCommon::texturePath + "rusty-metal_2048_R.jpg");
43 }
44 //-----------------------------------------------------------------------------
45 //! After parallel loading of the assets the scene gets assembled in here.
47  SLSceneView* sv)
48 {
49  SLCamera* cam1 = new SLCamera("Camera 1");
50  cam1->clipNear(0.1f);
51  cam1->clipFar(1000);
52  cam1->translation(0, 0, 75);
53  cam1->focalDist(75);
54  cam1->lookAt(0, 0, 0);
55  cam1->background().colors(SLCol4f(0.3f, 0.3f, 0.3f));
56  cam1->setInitialState();
57 
58  // Root scene node
59  SLNode* scene = new SLNode;
60  root3D(scene);
61  scene->addChild(cam1);
62 
63  // Generate NUM_MAT cook-torrance materials
64  SLVMaterial materials(NUM_MAT_MESH);
65  for (int i = 0; i < NUM_MAT_MESH; ++i)
66  {
67  SLstring matName = "mat-" + std::to_string(i);
68  materials[i] = new SLMaterial(am,
69  matName.c_str(),
70  nullptr,
71  _textureC,
72  nullptr,
73  _textureM,
74  _textureR,
75  nullptr,
76  nullptr);
77  SLCol4f color;
78  color.hsva2rgba(SLVec4f(Utils::TWOPI * (float)i / (float)NUM_MAT_MESH, 1.0f, 1.0f));
79  materials[i]->diffuse(color);
80  }
81 
82  // Generate NUM_MESH sphere meshes
83  SLVMesh meshes(NUM_MAT_MESH);
84  for (int i = 0; i < NUM_MAT_MESH; ++i)
85  {
86  SLstring meshName = "mesh-" + std::to_string(i);
87  meshes[i] = new SLSphere(am,
88  1.0f,
89  32,
90  32,
91  meshName.c_str(),
92  materials[i % NUM_MAT_MESH]);
93  }
94 
95  // Create universe
97  this,
98  scene,
99  0,
100  NUM_LEVELS,
101  NUM_CHILDREN,
102  materials,
103  meshes);
104 
105  sv->camera(cam1);
106  sv->doWaitOnIdle(false);
107 }
108 //-----------------------------------------------------------------------------
109 //! Adds another level to Jan's Universe scene
111  SLScene* s,
112  SLNode* parent,
113  SLint parentID,
114  SLuint currentLevel,
115  SLuint levels,
116  SLuint childCount,
117  SLVMaterial& materials,
118  SLVMesh& meshes,
119  SLuint& numNodes)
120 {
121  if (currentLevel >= levels)
122  return;
123 
124  const float degPerChild = 360.0f / (float)childCount;
125  SLuint mod = currentLevel % 3;
126 
127  float scaleFactor = 0.25f;
128 
129  for (SLuint i = 0; i < childCount; i++)
130  {
131  numNodes++;
132  string childName = "Node" + std::to_string(numNodes) +
133  "-L" + std::to_string(currentLevel) +
134  "-C" + std::to_string(i);
135  SLNode* child = new SLNode(meshes[numNodes % meshes.size()], childName);
136 
137  child->rotate((float)i * degPerChild, 0, 0, 1);
138  child->translate(2, 0, 0);
139  child->scale(scaleFactor);
140 
141  // Node animation on child node
142  string animName = "Anim" + std::to_string(numNodes);
143  SLAnimation* childAnim = s->animManager().createNodeAnimation(animName.c_str(),
144  60,
145  true,
146  EC_linear,
147  AL_loop);
148  childAnim->createNodeAnimTrackForRotation360(child, {0, 0, 1});
149 
150  parent->addChild(child);
151 
152  addUniverseLevel(am,
153  s,
154  child,
155  parentID,
156  currentLevel + 1,
157  levels,
158  childCount,
159  materials,
160  meshes,
161  numNodes);
162  }
163 }
164 //-----------------------------------------------------------------------------
165 //! Generates the Jan's Universe scene
167  SLScene* s,
168  SLNode* parent,
169  SLint parentID,
170  SLuint levels,
171  SLuint childCount,
172  SLVMaterial& materials,
173  SLVMesh& meshes)
174 {
175  // Point light without mesh
176  SLLightSpot* light = new SLLightSpot(am,
177  s,
178  0,
179  0,
180  0,
181  1.0f,
182  180,
183  0,
184  1000,
185  1000,
186  true);
187  light->attenuation(1, 0, 0);
188  light->scale(10, 10, 10);
189  light->diffuseColor({1.0f, 1.0f, 0.5f});
190 
191  // Node animation on light node
192  SLAnimation* lightAnim = s->animManager().createNodeAnimation("anim0",
193  60,
194  true,
195  EC_linear,
196  AL_loop);
197  lightAnim->createNodeAnimTrackForRotation360(light,
198  SLVec3f(0, 1, 0));
199 
200  parent->addChild(light);
201 
202  SLuint numNodes = 1;
203 
204  addUniverseLevel(am,
205  s,
206  light,
207  parentID,
208  1,
209  levels,
210  childCount,
211  materials,
212  meshes,
213  numNodes);
214 }
215 //------------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
const SLuint NUM_LEVELS
const SLuint NUM_CHILDREN
const SLuint NUM_MAT_MESH
Class declaration for an SLScene inherited class.
unsigned int SLuint
Definition: SL.h:171
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
@ EC_linear
linear easing with constant velocity
Definition: SLEnums.h:181
@ AL_loop
loop
Definition: SLEnums.h:169
vector< SLMaterial * > SLVMaterial
STL vector of material pointers.
Definition: SLMaterial.h:274
vector< SLMesh * > SLVMesh
Definition: SLMesh.h:263
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
SLVec4< SLfloat > SLVec4f
Definition: SLVec4.h:235
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLstring texturePath
Path to texture images.
Definition: AppCommon.h:86
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
void generateUniverse(SLAssetManager *am, SLScene *s, SLNode *parent, SLint parentID, SLuint levels, SLuint childCount, SLVMaterial &materials, SLVMesh &meshes)
Generates the Jan's Universe scene.
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading in here.
void addUniverseLevel(SLAssetManager *am, SLScene *s, SLNode *parent, SLint parentID, SLuint currentLevel, SLuint levels, SLuint childCount, SLVMaterial &materials, SLVMesh &meshes, SLuint &numNodes)
Adds another level to Jan's Universe scene.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForRotation360(SLNode *target, const SLVec3f &axis)
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 clipFar(const SLfloat cFar)
Definition: SLCamera.h:109
void clipNear(const SLfloat cNear)
Definition: SLCamera.h:108
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void diffuseColor(const SLCol4f &diff)
Definition: SLLight.h:107
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 rotate(const SLQuat4f &rot, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:945
void scale(SLfloat s)
Definition: SLNode.h:640
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
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
void doWaitOnIdle(SLbool doWI)
Definition: SLSceneView.h:149
SLSphere creates a sphere mesh based on SLSpheric w. 180 deg polar angle.
Definition: SLSphere.h:33
void hsva2rgba(const SLVec4 &hsva)
HSVA to RGBA color conversion (http://www.rapidtables.com/convert/color/hsv-to-rgb....
Definition: SLVec4.h:191
static const float TWOPI
Definition: Utils.h:240
T mod(T a, T b)
Definition: Utils.h:250