SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLNodeLOD.cpp
Go to the documentation of this file.
1 /**
2  * \file SLNodeLOD.cpp
3  * \date July 2021
4  * \authors Jan Dellsperger, 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 #include <SLSceneView.h>
11 #include <SLNodeLOD.h>
12 
13 //-----------------------------------------------------------------------------
14 //! Adds an LOD node with forced decreasing min LOD coverage
15 /*!
16  * The coverage value is the ratio of the nodes bounding rectangle in screen space
17  * to the full viewport. The minLodCoverage value must be > 0.0 and < 1.0 and
18  * < than the minLodCoverage of the last node in the LOD group. If the first
19  * child node has e.g. a minLodCoverage of 0.1 it means that it will be visible
20  * if its bounding rectangle covers more then 10% of the viewport. The first
21  * child node in the group must be the one with the highest resolution.\n
22  * @remarks It is important that during instantiation NO OpenGL functions (gl*)
23  * get called because this constructor will be most probably called in a parallel
24  * thread from within an SLScene::registerAssetsToLoad or SLScene::assemble
25  * function. All objects that get rendered have to do their OpenGL initialization
26  * when they are used the first time during rendering in the main thread.
27  * \param childToAdd LOD child node pointer to add
28  * \param minLodCoverage A value > 0 and < 1 and < than the minLodCoverage
29  * of the last node in the LOD group
30  * \param levelForSM Level to use for shadow mapping (0 uses the active level)
31  */
32 void SLNodeLOD::addChildLOD(SLNode* childToAdd,
33  SLfloat minLodCoverage,
34  SLubyte levelForSM)
35 {
36  assert(minLodCoverage > 0.0f &&
37  minLodCoverage < 1.0f &&
38  "SLNodeLOD::addChildLOD min. LOD limit must be > 0 and < 1");
39 
40  if (!_children.empty() &&
41  _children[_children.size() - 1]->minLodCoverage() <= minLodCoverage)
42  SL_EXIT_MSG("SLNodeLOD::addChildLOD: A new child LOD node must have a smaller LOD limit than the last one.");
43 
44  childToAdd->minLodCoverage(minLodCoverage);
45  childToAdd->levelForSM(levelForSM);
46  addChild(childToAdd);
47 
48  /* Set new node hidden, so they won't be shadowed if they are outside
49  * the view frustum. Only the LOD algorithm in cullChildren3D can make
50  * them visible */
51  childToAdd->drawBits()->set(SL_DB_HIDDEN, true);
52 }
53 //-----------------------------------------------------------------------------
54 //! Culls the LOD children by evaluating the the screen space coverage
56 {
57  if (!_children.empty())
58  {
59  SLfloat rectCoverage = _aabb.rectCoverageInSS();
60 
61  // Set visibility (draw-bit SL_DB_HIDDEN) for each level
62  for (SLint i = 0; i < _children.size(); ++i)
63  {
64  bool isVisible;
65 
66  if (i == 0)
67  isVisible = rectCoverage >= _children[i]->minLodCoverage();
68  else
69  isVisible = rectCoverage < _children[i - 1]->minLodCoverage() &&
70  rectCoverage >= _children[i]->minLodCoverage();
71 
72  _children[i]->drawBits()->set(SL_DB_HIDDEN, !isVisible);
73  _aabb.isVisible(isVisible);
74 
75  // cull check only the visible level
76  if (isVisible)
77  _children[i]->cull3DRec(sv);
78  }
79  }
80 }
81 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
unsigned char SLubyte
Definition: SL.h:167
#define SL_EXIT_MSG(message)
Definition: SL.h:240
int SLint
Definition: SL.h:170
#define SL_DB_HIDDEN
Flags an object as hidden.
Definition: SLDrawBits.h:20
SLfloat rectCoverageInSS()
Calculates the bounding rectangle in screen space and returns coverage in SS.
Definition: SLAABBox.cpp:446
void isVisible(SLbool visible)
Definition: SLAABBox.h:44
void set(SLuint bit, SLbool state)
Sets the specified bit to the passed state.
Definition: SLDrawBits.h:57
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
void addChild(SLNode *child)
Definition: SLNode.cpp:207
void minLodCoverage(SLfloat minLodCoverage)
Definition: SLNode.h:287
SLAABBox _aabb
axis aligned bounding box
Definition: SLNode.h:360
SLDrawBits * drawBits()
Definition: SLNode.h:299
SLVNode _children
vector of children nodes
Definition: SLNode.h:345
void levelForSM(SLubyte lfsm)
Definition: SLNode.h:288
SLubyte levelForSM()
Definition: SLNode.h:317
SLfloat minLodCoverage()
Definition: SLNode.h:316
void addChildLOD(SLNode *child, SLfloat minLodLimit, SLubyte levelForSM=0)
Adds an LOD node with forced decreasing min LOD coverage.
Definition: SLNodeLOD.cpp:32
virtual void cullChildren3D(SLSceneView *sv)
Culls the LOD children by evaluating the the screen space coverage.
Definition: SLNodeLOD.cpp:55
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69