SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoSceneAnimNodeMass2.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoSceneAnimNodeMass2.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 <SLSphere.h>
18 
19 //-----------------------------------------------------------------------------
21  : SLScene("Benchmark Node 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  SLSceneView* sv)
34 {
35  SLCamera* cam1 = new SLCamera("Camera 1");
36  cam1->clipNear(0.1f);
37  cam1->clipFar(100);
38  cam1->translation(0, 2.5f, 20);
39  cam1->focalDist(20);
40  cam1->lookAt(0, 2.5f, 0);
41  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
42  cam1->setInitialState();
43 
44  SLLightSpot* light1 = new SLLightSpot(am,
45  this,
46  15,
47  15,
48  15,
49  0.3f);
50  light1->powers(0.2f, 0.8f, 1.0f);
51  light1->attenuation(1, 0, 0);
52 
53  SLNode* scene = new SLNode;
54  root3D(scene);
55  scene->addChild(cam1);
56  scene->addChild(light1);
57 
58  // Generate NUM_MAT materials
59  const int NUM_MAT = 20;
60  SLVMaterial mat;
61  for (int i = 0; i < NUM_MAT; ++i)
62  {
63  SLGLTexture* texC = new SLGLTexture(am,
64  AppCommon::texturePath + "earth2048_C_Q95.jpg"); // color map
65 
66  SLstring matName = "mat-" + std::to_string(i);
67  mat.push_back(new SLMaterial(am,
68  matName.c_str(),
69  texC));
70  SLCol4f color;
71  color.hsva2rgba(SLVec4f(Utils::TWOPI * (float)i / (float)NUM_MAT,
72  1.0f,
73  1.0f));
74  mat[i]->diffuse(color);
75  }
76 
77  // create rotating sphere group
78  SLint maxDepth = 5;
79 
80  SLint resolution = 18;
81  scene->addChild(RotatingSpheres(am,
82  this,
83  maxDepth,
84  0,
85  0,
86  0,
87  1,
88  resolution,
89  mat));
90 
91  sv->camera(cam1);
92  sv->doWaitOnIdle(false);
93 }
94 //-----------------------------------------------------------------------------
95 //! Creates a recursive rotating sphere group used for performance test
96 /*!
97  * This performance benchmark is expensive in terms of world matrix updates
98  * because all sphere groups rotate. Therefore all children need to update
99  * their wm every frame.
100  * \param am Pointer to the asset manager
101  * \param s Pointer to project scene aka asset manager
102  * \param depth Max. allowed recursion depth
103  * \param x Position in x direction
104  * \param y Position in y direction
105  * \param z Position in z direction
106  * \param scale Scale factor > 0 and < 1 for the children spheres
107  * \param resolution NO. of stack and slices of the spheres
108  * \param mat Reference to an vector of materials
109  * \return Group node of spheres
110  */
112  SLScene* s,
113  SLint depth,
114  SLfloat x,
115  SLfloat y,
116  SLfloat z,
117  SLfloat scale,
118  SLuint resolution,
119  SLVMaterial& mat)
120 {
121  assert(depth >= 0);
122  assert(scale >= 0.0f && scale <= 1.0f);
123  assert(resolution > 0 && resolution < 64);
124 
125  // Choose the material index randomly
126  SLint iMat = Utils::random(0, (int)mat.size() - 1);
127 
128  // Generate unique names for meshes, nodes and animations
129  static int sphereNum = 0;
130  string meshName = "Mesh" + std::to_string(sphereNum);
131  string animName = "Anim" + std::to_string(sphereNum);
132  string nodeName = "Node" + std::to_string(sphereNum);
133  sphereNum++;
134 
135  SLAnimation* nodeAnim = s->animManager().createNodeAnimation(animName,
136  60,
137  true,
138  EC_linear,
139  AL_loop);
140  if (depth == 0)
141  {
142  SLSphere* sphere = new SLSphere(am,
143  5.0f * scale,
144  resolution,
145  resolution,
146  meshName,
147  mat[iMat]);
148  SLNode* sphNode = new SLNode(sphere, nodeName);
149  sphNode->translate(x, y, z, TS_object);
150  nodeAnim->createNodeAnimTrackForRotation360(sphNode,
151  SLVec3f(0, 1, 0));
152  return sphNode;
153  }
154  else
155  {
156  depth--;
157 
158  // decrease resolution to reduce memory consumption
159  if (resolution > 8)
160  resolution -= 2;
161 
162  SLNode* sGroup = new SLNode(new SLSphere(am,
163  5.0f * scale,
164  resolution,
165  resolution,
166  meshName,
167  mat[iMat]),
168  nodeName);
169  sGroup->translate(x, y, z, TS_object);
170  nodeAnim->createNodeAnimTrackForRotation360(sGroup, SLVec3f(0, 1, 0));
171  sGroup->addChild(RotatingSpheres(am, s, depth, 6.43951f * scale, 0, 1.72546f * scale, scale / 3.0f, resolution, mat));
172  sGroup->addChild(RotatingSpheres(am, s, depth, 1.72546f * scale, 0, 6.43951f * scale, scale / 3.0f, resolution, mat));
173  sGroup->addChild(RotatingSpheres(am, s, depth, -4.71405f * scale, 0, 4.71405f * scale, scale / 3.0f, resolution, mat));
174  sGroup->addChild(RotatingSpheres(am, s, depth, -6.43951f * scale, 0, -1.72546f * scale, scale / 3.0f, resolution, mat));
175  sGroup->addChild(RotatingSpheres(am, s, depth, -1.72546f * scale, 0, -6.43951f * scale, scale / 3.0f, resolution, mat));
176  sGroup->addChild(RotatingSpheres(am, s, depth, 4.71405f * scale, 0, -4.71405f * scale, scale / 3.0f, resolution, mat));
177  sGroup->addChild(RotatingSpheres(am, s, depth, 2.72166f * scale, 5.44331f * scale, 2.72166f * scale, scale / 3.0f, resolution, mat));
178  sGroup->addChild(RotatingSpheres(am, s, depth, -3.71785f * scale, 5.44331f * scale, 0.99619f * scale, scale / 3.0f, resolution, mat));
179  sGroup->addChild(RotatingSpheres(am, s, depth, 0.99619f * scale, 5.44331f * scale, -3.71785f * scale, scale / 3.0f, resolution, mat));
180  return sGroup;
181  }
182 }
183 //-----------------------------------------------------------------------------
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
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
@ EC_linear
linear easing with constant velocity
Definition: SLEnums.h:181
@ TS_object
Definition: SLEnums.h:210
@ AL_loop
loop
Definition: SLEnums.h:169
vector< SLMaterial * > SLVMaterial
STL vector of material pointers.
Definition: SLMaterial.h:274
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 registerAssetsToLoad(SLAssetLoader &al) override
All assets the should be loaded in parallel must be registered in here.
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
SLNode * RotatingSpheres(SLAssetManager *am, SLScene *s, SLint depth, SLfloat x, SLfloat y, SLfloat z, SLfloat scale, SLuint resolution, SLVMaterial &mat)
Creates a recursive rotating sphere group used for performance test.
SLAnimation * createNodeAnimation(SLfloat duration)
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
SLNodeAnimTrack * createNodeAnimTrackForRotation360(SLNode *target, const SLVec3f &axis)
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
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
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
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 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
float random(float min, float max)
Returns a uniform distributed random float number between min and max.
Definition: Utils.h:265