SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneAnimNodeMass.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneAnimNodeMass.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 <SLBox.h>
18 
19 //-----------------------------------------------------------------------------
21  : SLScene("Mass Animation Test Scene")
22 {
23  info("Performance test for transform updates from many animations.");
24 }
25 //-----------------------------------------------------------------------------
26 //! All assets the should be loaded in parallel must be registered in here.
28 {
29 }
30 //-----------------------------------------------------------------------------
31 //! After parallel loading of the assets the scene gets assembled in here.
33 {
34  // Create a scene group node
35  SLNode* scene = new SLNode("scene node");
36  root3D(scene);
37 
38  // Create and add camera
39  SLCamera* cam1 = new SLCamera("Camera 1");
40  cam1->translation(0, 20, 40);
41  cam1->lookAt(0, 0, 0);
42  cam1->focalDist(42);
43  scene->addChild(cam1);
44  sv->camera(cam1);
45 
46  // Add spotlight
47  SLLightSpot* light1 = new SLLightSpot(am, this, 0.1f);
48  light1->translate(0, 10, 0);
49  scene->addChild(light1);
50 
51  // build a basic scene to have a reference for the occurring rotations
52  SLMaterial* genericMat = new SLMaterial(am, "some material");
53 
54  // we use the same mesh to visualize all the nodes
55  SLBox* box = new SLBox(am,
56  -0.5f,
57  -0.5f,
58  -0.5f,
59  0.5f,
60  0.5f,
61  0.5f,
62  "box",
63  genericMat);
64 
65  // We build a stack of levels, each level has a grid of boxes on it
66  // each box on this grid has another grid above it with child nodes.
67  // Best results are achieved if gridSize is an uneven number.
68  // (gridSize^2)^levels = num nodes. handle with care.
69  const SLint levels = 3;
70  const SLint gridSize = 3;
71  const SLint gridHalf = gridSize / 2;
72  const SLint nodesPerLvl = gridSize * gridSize;
73 
74  // node spacing per level
75  // nodes are 1^3 in size, we want to space the levels so that the densest levels meet
76  // (so exactly 1 unit spacing between blocks)
77  SLfloat nodeSpacing[levels];
78  for (SLint i = 0; i < levels; ++i)
79  nodeSpacing[(levels - 1) - i] = (SLfloat)pow((SLfloat)gridSize, (SLfloat)i);
80 
81  // lists to keep track of previous grid level to set parents correctly
82  vector<SLNode*> parents;
83  vector<SLNode*> curParentsVector;
84 
85  // first parent is the scene root
86  parents.push_back(scene);
87 
88  SLint nodeIndex = 0;
89  for (float lvl : nodeSpacing)
90  {
91  curParentsVector = parents;
92  parents.clear();
93 
94  // for each parent in the previous level, add a completely new grid
95  for (auto parent : curParentsVector)
96  {
97  for (SLint i = 0; i < nodesPerLvl; ++i)
98  {
99  SLNode* node = new SLNode("MassAnimNode");
100  node->addMesh(box);
101  parent->addChild(node);
102  parents.push_back(node);
103 
104  // position
105  SLfloat x = (SLfloat)(i % gridSize - gridHalf);
106  SLfloat z = (SLfloat)((i > 0) ? i / gridSize - gridHalf : -gridHalf);
107  SLVec3f pos(x * lvl * 1.1f, 1.5f, z * lvl * 1.1f);
108 
109  node->translate(pos, TS_object);
110  // node->scale(1.1f);
111 
112  SLfloat duration = 1.0f + 5.0f * ((SLfloat)i / (SLfloat)nodesPerLvl);
113  ostringstream oss;
114 
115  oss << "random anim " << nodeIndex++;
116  SLAnimation* anim = animManager().createNodeAnimation(oss.str(),
117  duration,
118  true,
119  EC_inOutSine,
122  SLVec3f(0.0f, 1.0f, 0.0f));
123  }
124  }
125  }
126 }
127 //-----------------------------------------------------------------------------
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
int SLint
Definition: SL.h:170
@ EC_inOutSine
sine easing in and then out
Definition: SLEnums.h:200
@ TS_object
Definition: SLEnums.h:210
@ AL_pingPongLoop
loop forward and backwards
Definition: SLEnums.h:171
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading in here.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForTranslation(SLNode *target, const SLVec3f &endPos)
Toplevel holder of the assets meshes, materials, textures and shaders.
Axis aligned box mesh.
Definition: SLBox.h:31
Active or visible camera node class.
Definition: SLCamera.h:54
void focalDist(const SLfloat f)
Definition: SLCamera.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
virtual void addMesh(SLMesh *mesh)
Definition: SLNode.cpp:157
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