SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLScene.h
Go to the documentation of this file.
1 /**
2  * \file SLScene.h
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLSCENE_H
11 #define SLSCENE_H
12 
13 #include <utility>
14 #include <vector>
15 #include <map>
16 #include <SL.h>
17 #include <SLAnimManager.h>
18 #include <Averaged.h>
19 #include <SLGLOculus.h>
20 #include <SLLight.h>
21 #include <SLMesh.h>
22 #include <SLEntities.h>
23 
24 class SLCamera;
25 class SLSkybox;
26 class SLAssetLoader;
27 
28 //-----------------------------------------------------------------------------
29 //! C-Callback function typedef for scene load function
30 typedef void(SL_STDCALL* cbOnSceneLoad)(SLAssetManager* am,
34 //-----------------------------------------------------------------------------
35 //! The SLScene class represents the top level instance holding the scene structure
36 /*!
37  The SLScene class holds everything that is common for all sceneviews such as
38  the pointer (_root3D) to the root node of the scene.
39  The scene loading happens in 3 steps:\n
40  1) Registering all expensive assets to load in registerAssetsToLoad.\n
41  2) Parallel loading of assets in threads with SLAssetLoader::loadAll.\n
42  3) Assembling the scene in assemble.\n
43  To load a scene you must therefore inherit from this class and override the
44  methods registerAssetsToLoad and assemble.\n
45 */
46 class SLScene : public SLObject
47 {
48  friend class SLNode;
49 
50 public:
51  SLScene(const SLstring& name);
52  ~SLScene() override;
53 
54  void initOculus(SLstring shaderDir);
55 
56  //! All assets the should be loaded in parallel must be registered in here.
57 
58  //! All scene specific assets have to be registered for async loading in here.
59  /*! @remark All scene sspecific assets have to be loaded async by overriding
60  SLScene::registerAssetsToLoad and SLScene::assemble. Async loading and
61  assembling means that it happens in a parallel thread and that in there are
62  no OpenGL calls allowed. OpenGL calls are only allowed in the main thread.*/
63  virtual void registerAssetsToLoad(SLAssetLoader& al) {}
64 
65  //! After parallel loading of the assets the scene gets assembled in here.
66  /*! @remark All scene-specific assets have to be loaded async by overriding
67  SLScene::registerAssetsToLoad and SLScene::assemble. Async loading and
68  assembling means that it happens in a parallel thread and that in there
69  are no OpenGL calls allowed. OpenGL calls are only allowed in the main
70  thread. It is important that all object instantiations within
71  SLScene::assemble do NOT call any OpenGL functions (gl*) because they happen
72  in a parallel thread. All objects that get rendered have to do their
73  initialization when they are used the first time during rendering in the
74  main thread.*/
75  virtual void assemble(SLAssetManager* am, SLSceneView* sv) {}
76 
77  // Setters
79  {
80  _root3D = root3D;
81 
82 #ifdef SL_USE_ENTITIES
83  SLint rootEntityID = SLScene::entities.getEntityID(root3D);
84  if (rootEntityID == INT32_MIN && root3D)
85  SLScene::entities.addChildEntity(-1, SLEntity(root3D));
86  else if (rootEntityID > -1)
87  SL_EXIT_MSG("Root node exists already with another ID among the entities");
88 #endif
89  }
92  void stopAnimations(SLbool stop) { _stopAnimations = stop; }
93  void info(SLstring i) { _info = std::move(i); }
95 
96  // Getters
99  SLNode* root3D() { return _root3D; }
100  SLNode* root2D() { return _root2D; }
101  SLSkybox* skybox() { return _skybox; }
102  SLstring& info() { return _info; }
103  SLfloat elapsedTimeMS() const { return _frameTimeMS; }
104  SLfloat elapsedTimeSec() const { return _frameTimeMS * 0.001f; }
106  SLfloat loadTimeMS() const { return _loadTimeMS; }
107  SLVLight& lights() { return _lights; }
108  SLfloat fps() const { return _fps; }
114 
115  //! Returns the node if only one is selected. See also SLMesh::selectNodeMesh
116  SLNode* singleNodeSelected() { return _selectedNodes.size() == 1 ? _selectedNodes[0] : nullptr; }
117 
118  //! Returns the node if only one is selected. See also SLMesh::selectNodeMesh
119  SLMesh* singleMeshFullSelected() { return (_selectedNodes.size() == 1 &&
120  _selectedMeshes.size() == 1 &&
121  _selectedMeshes[0]->IS32.empty())
122  ? _selectedMeshes[0]
123  : nullptr; }
126 
129  SLCamera* nextCameraInScene(SLCamera* activeSVCam);
130 
131  // Misc.
132  bool onUpdate(bool renderTypeIsRT,
133  bool voxelsAreShown,
134  bool forceCPUSkinning);
135  void init(SLAssetManager* am);
136  virtual void unInit();
137  void selectNodeMesh(SLNode* nodeToSelect, SLMesh* meshToSelect);
139 
140  SLGLOculus* oculus() { return _oculus.get(); }
141 
142 #ifdef SL_USE_ENTITIES
143  static SLEntities entities;
144 #endif
145 
146 protected:
147  SLVLight _lights; //!< Vector of all lights
148  SLVEventHandler _eventHandlers; //!< Vector of all event handler
149  SLAnimManager _animManager; //!< Animation manager instance
150  SLAssetManager* _assetManager; //!< Pointer to the external assetManager
151 
152  SLNode* _root3D; //!< Root node for 3D scene
153  SLNode* _root2D; //!< Root node for 2D scene displayed in ortho projection
154  SLSkybox* _skybox; //!< pointer to skybox
155  SLstring _info; //!< scene info string
156  SLVNode _selectedNodes; //!< Vector of selected nodes. See SLMesh::selectNodeMesh.
157  SLVMesh _selectedMeshes; //!< Vector of selected meshes. See SLMesh::selectNodeMesh.
158 
159  SLfloat _loadTimeMS; //!< time to load scene in ms
160  SLfloat _frameTimeMS; //!< Last frame time in ms
161  SLfloat _lastUpdateTimeMS; //!< Last time after update in ms
162  SLfloat _fps; //!< Averaged no. of frames per second
163 
164  // major part times
165  AvgFloat _frameTimesMS; //!< Averaged total time per frame in ms
166  AvgFloat _updateTimesMS; //!< Averaged time for update in ms
167  AvgFloat _updateAABBTimesMS; //!< Averaged time for update the nodes AABB in ms
168  AvgFloat _updateAnimTimesMS; //!< Averaged time for update the animations in ms
169  AvgFloat _updateDODTimesMS; //!< Averaged time for update the SLEntities graph
170 
171  SLbool _stopAnimations; //!< Global flag for stopping all animations
172 
173  std::unique_ptr<SLGLOculus> _oculus; //!< Oculus Rift interface
174 };
175 
176 #endif
float SLfloat
Definition: SL.h:173
bool SLbool
Definition: SL.h:175
#define SL_EXIT_MSG(message)
Definition: SL.h:240
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
vector< SLEventHandler * > SLVEventHandler
vector< SLLight * > SLVLight
STL vector of light pointers.
Definition: SLLight.h:232
vector< SLMesh * > SLVMesh
Definition: SLMesh.h:263
deque< SLNode * > SLVNode
SLVNode typedef for a vector of SLNodes.
Definition: SLNode.h:26
typedef void(SL_STDCALL *cbOnSceneLoad)(SLAssetManager *am
C-Callback function typedef for scene load function.
SLScene SLSceneView * sv
Definition: SLScene.h:32
SLScene * s
Definition: SLScene.h:31
SLScene SLSceneView SLint sceneID
Definition: SLScene.h:33
SLAnimManager is the central class for all animation handling.
Definition: SLAnimManager.h:27
Toplevel holder of the assets meshes, materials, textures and shaders.
Active or visible camera node class.
Definition: SLCamera.h:54
Scenegraph in Data Oriented Design with flat std::vector of SLEntity.
Definition: SLEntities.h:47
Wrapper around Oculus Rift Devkit 2.
Definition: SLGLOculus.h:37
An SLMesh object is a triangulated mesh, drawn with one draw call.
Definition: SLMesh.h:134
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
Base class for all other classes.
Definition: SLObject.h:23
const SLstring & name() const
Definition: SLObject.h:38
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
virtual void unInit()
Definition: SLScene.cpp:97
SLVNode _selectedNodes
Vector of selected nodes. See SLMesh::selectNodeMesh.
Definition: SLScene.h:156
SLNode * root2D()
Definition: SLScene.h:100
SLVLight _lights
Vector of all lights.
Definition: SLScene.h:147
SLbool _stopAnimations
Global flag for stopping all animations.
Definition: SLScene.h:171
std::unique_ptr< SLGLOculus > _oculus
Oculus Rift interface.
Definition: SLScene.h:173
bool onUpdate(bool renderTypeIsRT, bool voxelsAreShown, bool forceCPUSkinning)
Updates animations and AABBs.
Definition: SLScene.cpp:124
AvgFloat & frameTimesMS()
Definition: SLScene.h:109
SLSkybox * _skybox
pointer to skybox
Definition: SLScene.h:154
~SLScene() override
Definition: SLScene.cpp:62
void deselectAllNodesAndMeshes()
Deselects all nodes and its meshes.
Definition: SLScene.cpp:338
SLfloat _loadTimeMS
time to load scene in ms
Definition: SLScene.h:159
AvgFloat _frameTimesMS
Averaged total time per frame in ms.
Definition: SLScene.h:165
SLfloat _frameTimeMS
Last frame time in ms.
Definition: SLScene.h:160
SLCamera * nextCameraInScene(SLCamera *activeSVCam)
Returns the next camera in the scene if there is one.
Definition: SLScene.cpp:361
virtual void registerAssetsToLoad(SLAssetLoader &al)
All assets the should be loaded in parallel must be registered in here.
Definition: SLScene.h:63
AvgFloat & updateAABBTimesMS()
Definition: SLScene.h:112
SLSkybox * skybox()
Definition: SLScene.h:101
SLNode * root3D()
Definition: SLScene.h:99
SLMesh * singleMeshFullSelected()
Returns the node if only one is selected. See also SLMesh::selectNodeMesh.
Definition: SLScene.h:119
SLVEventHandler & eventHandlers()
Definition: SLScene.h:105
AvgFloat & updateAnimTimesMS()
Definition: SLScene.h:111
SLAssetManager * assetManager()
Definition: SLScene.h:98
SLint numSceneCameras()
Returns the number of camera nodes in the scene.
Definition: SLScene.cpp:353
AvgFloat _updateDODTimesMS
Averaged time for update the SLEntities graph.
Definition: SLScene.h:169
AvgFloat & updateTimesMS()
Definition: SLScene.h:110
SLVMesh & selectedMeshes()
Definition: SLScene.h:125
SLNode * _root3D
Root node for 3D scene.
Definition: SLScene.h:152
SLVLight & lights()
Definition: SLScene.h:107
SLAssetManager * _assetManager
Pointer to the external assetManager.
Definition: SLScene.h:150
void root2D(SLNode *root2D)
Definition: SLScene.h:90
SLVMesh _selectedMeshes
Vector of selected meshes. See SLMesh::selectNodeMesh.
Definition: SLScene.h:157
virtual void assemble(SLAssetManager *am, SLSceneView *sv)
After parallel loading of the assets the scene gets assembled in here.
Definition: SLScene.h:75
void initOculus(SLstring shaderDir)
Definition: SLScene.cpp:391
SLstring _info
scene info string
Definition: SLScene.h:155
SLAnimManager & animManager()
Definition: SLScene.h:97
SLfloat _fps
Averaged no. of frames per second.
Definition: SLScene.h:162
void selectNodeMesh(SLNode *nodeToSelect, SLMesh *meshToSelect)
Handles the full mesh selection from double-clicks.
Definition: SLScene.cpp:234
SLfloat elapsedTimeMS() const
Definition: SLScene.h:103
AvgFloat _updateAABBTimesMS
Averaged time for update the nodes AABB in ms.
Definition: SLScene.h:167
void skybox(SLSkybox *skybox)
Definition: SLScene.h:91
SLVEventHandler _eventHandlers
Vector of all event handler.
Definition: SLScene.h:148
SLScene(const SLstring &name)
Definition: SLScene.cpp:39
SLNode * _root2D
Root node for 2D scene displayed in ortho projection.
Definition: SLScene.h:153
SLfloat fps() const
Definition: SLScene.h:108
void root3D(SLNode *root3D)
Definition: SLScene.h:78
SLfloat _lastUpdateTimeMS
Last time after update in ms.
Definition: SLScene.h:161
SLfloat elapsedTimeSec() const
Definition: SLScene.h:104
SLstring & info()
Definition: SLScene.h:102
SLGLOculus * oculus()
Definition: SLScene.h:140
SLfloat loadTimeMS() const
Definition: SLScene.h:106
SLNode * singleNodeSelected()
Returns the node if only one is selected. See also SLMesh::selectNodeMesh.
Definition: SLScene.h:116
AvgFloat _updateAnimTimesMS
Averaged time for update the animations in ms.
Definition: SLScene.h:168
SLAnimManager _animManager
Animation manager instance.
Definition: SLScene.h:149
SLVNode & selectedNodes()
Definition: SLScene.h:124
void info(SLstring i)
Definition: SLScene.h:93
AvgFloat _updateTimesMS
Averaged time for update in ms.
Definition: SLScene.h:166
AvgFloat & updateDODTimesMS()
Definition: SLScene.h:113
void loadTimeMS(SLfloat loadTimeMS)
Definition: SLScene.h:94
SLbool stopAnimations() const
Definition: SLScene.h:127
void init(SLAssetManager *am)
Definition: SLScene.cpp:72
void stopAnimations(SLbool stop)
Definition: SLScene.h:92
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
Skybox node class with a SLBox mesh.
Definition: SLSkybox.h:29
SLEntity is the Data Oriented Design version of a SLNode.
Definition: SLEntities.h:28