Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
SceneManagerPortals.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef PortalSceneManager_INCLUDE_ONCE
33 #define PortalSceneManager_INCLUDE_ONCE
34 
35 #include <vlGraphics/Actor.hpp>
36 #include <vlCore/Vector4.hpp>
37 #include <vlCore/Vector3.hpp>
39 #include <vlCore/Log.hpp>
40 #include <vlGraphics/Frustum.hpp>
41 
42 namespace vl
43 {
44 //-----------------------------------------------------------------------------
45  class Sector;
46  class SceneManagerPortals;
47 //-----------------------------------------------------------------------------
54  {
56 
57  friend class SceneManagerPortals;
58 
59  public:
62  {
63  VL_DEBUG_SET_OBJECT_NAME()
64  mIsOpen = true;
65  mVisitTick = 0;
66  }
67 
69  std::vector<fvec3>& geometry() { return mPortalGeometry; }
71  const std::vector<fvec3>& geometry() const { return mPortalGeometry; }
72 
74  void setTargetSector(Sector* sector) { mTargetSector = sector; }
76  Sector* targetSector() { return mTargetSector; }
78  const Sector* targetSector() const { return mTargetSector; }
79 
81  bool isOpen() const { return mIsOpen; }
83  void setIsOpen(bool is_open) { mIsOpen = is_open; }
84 
85  protected:
87  void setNormal(const fvec3& n) { mNormal = n; }
89  const fvec3& normal() const { return mNormal; }
90 
93  bool computeNormal();
94 
95  protected:
96  std::vector<fvec3> mPortalGeometry;
99  unsigned int mVisitTick;
100  bool mIsOpen;
101  };
102 //-----------------------------------------------------------------------------
110  {
112 
113  public:
120  {
121  public:
130  virtual void operator()(const Camera* cam, SceneManagerPortals* psm, Sector* s, Portal* p) = 0;
131  };
132  public:
135  {
136  VL_DEBUG_SET_OBJECT_NAME()
137  mActors = new ActorCollection;
138  }
139 
141  ActorCollection* actors() { return mActors.get(); }
143  const ActorCollection* actors() const { return mActors.get(); }
144 
146  std::vector< ref<Portal> >& portals() { return mPortals; }
148  const std::vector< ref<Portal> >& portals() const { return mPortals; }
149 
152  std::vector< AABB >& volumes() { return mVolumes; }
155  const std::vector< AABB >& volumes() const { return mVolumes; }
156 
158  AABB computeBoundingBox();
159 
160  std::vector< ref<VisibilityCallback> >& callbacks() { return mCallbacks; }
161  const std::vector< ref<VisibilityCallback> >& callbacks() const { return mCallbacks; }
162  void executeCallbacks(const Camera*cam,SceneManagerPortals* psm, Portal*p);
163 
164  protected:
165  std::vector< ref<Portal> > mPortals;
166  std::vector< AABB > mVolumes;
168  std::vector< ref<VisibilityCallback> > mCallbacks;
169  };
170 //-----------------------------------------------------------------------------
185  {
187 
188  public:
190  SceneManagerPortals(): mExternalSector(new Sector), mVisitTick(1), mShowPortals(false)
191  {
192  VL_DEBUG_SET_OBJECT_NAME()
193  }
194 
196  void extractActors(ActorCollection& list);
198  void extractVisibleActors(ActorCollection& list, const Camera* camera);
199 
201  std::vector< ref<Sector> >& sectors() { return mSectors; }
203  const std::vector< ref<Sector> >& sectors() const { return mSectors; }
204 
212  Sector* externalSector() { return mExternalSector.get(); }
214  const Sector* externalSector() const { return mExternalSector.get(); }
215 
217  void computePortalNormals();
218 
220  void initialize();
221 
223  bool showPortals() const { return mShowPortals; }
225  void setShowPortals(bool show) { mShowPortals = show; }
227  void invalidatePortalActors() { mPortalActorMap.clear(); }
228 
230  const std::vector<Frustum>& frustumStack() const { return mFrustumStack; }
231 
232  protected:
233  void renderPortal(Portal* portal);
234  void visitSector(Sector* prev, Sector* sector, const vec3& eye, const Camera* camera);
235  Sector* computeStartingSector(const Camera* camera);
236 
237  protected:
239  std::vector< ref<Sector> > mSectors;
240  std::vector< ref<Actor> > mTempActors;
241  std::map<Portal*, ref<Actor> > mPortalActorMap;
242  std::vector<Frustum> mFrustumStack;
243  unsigned mVisitTick;
245  };
246 //-----------------------------------------------------------------------------
247 }
248 
249 #endif
Sector * targetSector()
The Sector that is behind the portal and that is seen through the portal.
The SceneManagerPortals calss implements a portal-based hidden surface removal algorithm to efficentl...
std::vector< ref< Actor > > mTempActors
std::vector< ref< Sector > > mSectors
std::map< Portal *, ref< Actor > > mPortalActorMap
ref< ActorCollection > mActors
void setShowPortals(bool show)
Whether portals should be shown in the rendering or not.
const std::vector< ref< Sector > > & sectors() const
The Sectors that are part of the scene.
ActorCollection * actors()
The Actor object contained in a sector. An actor can be part of multiple sectors. ...
bool showPortals() const
Whether portals should be shown in the rendering or not.
std::vector< fvec3 > & geometry()
The set of points defining the shape of the portal. The points must define a planar convex polygon an...
std::vector< AABB > mVolumes
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Sector * mTargetSector
void setTargetSector(Sector *sector)
The Sector that is behind the portal and that is seen through the portal.
const ActorCollection * actors() const
The Actor object contained in a sector. An actor can be part of multiple sectors. ...
Visualization Library main namespace.
unsigned int mVisitTick
A callback object called each time a Sector becomes visible through a Portal.
Sector * externalSector()
Returns the external sector.
const std::vector< Frustum > & frustumStack() const
The stack of frustums active at a given point during sector discovery.
Sector()
Constructor.
const std::vector< ref< VisibilityCallback > > & callbacks() const
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
void setIsOpen(bool is_open)
If a Portal is closed or open. If the portal is closed then the camera cannot see the targetSector() ...
The base class for all the reference counted objects.
Definition: Object.hpp:158
std::vector< ref< Sector > > & sectors()
The Sectors that are part of the scene.
The SceneManager class is the base class for all the scene managers.
void invalidatePortalActors()
Regenerates the portal actors next time their rendering is requested.
std::vector< ref< VisibilityCallback > > mCallbacks
const fvec3 & normal() const
Used internally.
const Sector * targetSector() const
The Sector that is behind the portal and that is seen through the portal.
std::vector< ref< Portal > > mPortals
Defines an area containg a set if Actors that is connected to other Sector[s] through its Portal[s]...
std::vector< Frustum > mFrustumStack
Defined as a simple subclass of Collection<Actor>, see Collection for more information.
Definition: Actor.hpp:479
std::vector< ref< VisibilityCallback > > & callbacks()
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
std::vector< AABB > & volumes()
A set of volumes used to test if the camera is or not inside a Sector.
const std::vector< AABB > & volumes() const
A set of volumes used to test if the camera is or not inside a Sector.
bool isOpen() const
If a Portal is closed or open. If the portal is closed then the camera cannot see the targetSector() ...
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
const Sector * externalSector() const
Returns the external sector.
const std::vector< ref< Portal > > & portals() const
The portals within a sector that connect it to other sectors.
const std::vector< fvec3 > & geometry() const
The set of points defining the shape of the portal. The points must define a planar convex polygon an...
std::vector< fvec3 > mPortalGeometry
void setNormal(const fvec3 &n)
Used internally.
A planar convex polygon used to define the visibility from one Sector to another. ...
std::vector< ref< Portal > > & portals()
The portals within a sector that connect it to other sectors.