Visualization Library 2.0.0-b3

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ActorTreeAbstract.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, 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 
33 #include <vlGraphics/Camera.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39 {
40  VL_DEBUG_SET_OBJECT_NAME()
42  mParent = NULL;
43  mEnabled = true;
44 }
45 //-----------------------------------------------------------------------------
47 {
48  AABB aabb;
49  for(int i=0; i<actors()->size(); ++i)
50  {
51  actors()->at(i)->computeBounds();
52  aabb += actors()->at(i)->boundingBox();
53  }
54  for(int i=0; i<childrenCount(); ++i)
55  {
56  if (child(i))
57  {
58  child(i)->computeAABB();
59  aabb += child(i)->aabb();
60  }
61  }
62  mAABB = aabb;
63 }
64 //-----------------------------------------------------------------------------
66 {
67  for( int i = 0; i < actors()->size(); ++i ) {
68  list.push_back( actors()->at(i) );
69  }
70 
71  for( int i = 0; i < childrenCount(); ++i ) {
72  if ( child(i) ) {
73  child(i)->extractActors( list );
74  }
75  }
76 }
77 //-----------------------------------------------------------------------------
78 void ActorTreeAbstract::extractVisibleActors(ActorCollection& list, const Camera* camera, unsigned enable_mask)
79 {
80  // If enabled try and cull the whole node
81  if ( ! isEnabled() || ( camera && camera->frustum().cull( aabb() ) ) ) {
82  return;
83  }
84 
85  // Cull / extract this node's Actors
86  for( int i = 0; i < actors()->size(); ++i )
87  {
88  if ( actors()->at(i)->isEnabled() && ( enable_mask & actors()->at(i)->enableMask() ) )
89  {
90  actors()->at(i)->computeBounds();
91  if ( camera && camera->frustum().cull( actors()->at(i)->boundingSphere() ) ) {
92  continue;
93  } else {
94  list.push_back(actors()->at(i));
95  }
96  }
97  }
98 
99  // Descend to child nodes
100  for( int i = 0; i < childrenCount(); ++i ) {
101  if ( child(i) ) {
102  child(i)->extractVisibleActors( list, camera );
103  }
104  }
105 }
106 //-----------------------------------------------------------------------------
108 {
109  int pos = actors()->find(actor);
110  if (pos != -1)
111  {
112  actors()->eraseAt(pos);
113  return this;
114  }
115  else
116  {
117  ActorTreeAbstract* node = NULL;
118  for(int i=0; !node && i<childrenCount(); ++i)
119  if (child(i))
120  node = child(i)->eraseActor(actor);
121  return node;
122  }
123 }
124 //-----------------------------------------------------------------------------
126 {
127  ref<Actor> act = new Actor(renderable,eff,tr);
128  actors()->push_back( act.get() );
129  return act.get();
130 }
131 //-----------------------------------------------------------------------------
133 {
134  actors()->push_back(actor);
135  return actor;
136 }
137 //-----------------------------------------------------------------------------
139 {
140  // finds the root transforms
141 
142  std::set< Transform* > root_transforms;
143  for(int i=0; i<(int)actors.size(); ++i)
144  {
145  for( Transform* root = actors[i]->transform(); root; root = root->parent() )
146  if ( !root->parent() )
147  root_transforms.insert(root);
148  }
149 
150  // setup the matrices
151 
152  std::set< Transform* >::iterator tra = root_transforms.begin();
153  while( tra != root_transforms.end() )
154  {
155  (*tra)->computeWorldMatrixRecursive();
156  ++tra;
157  }
158 
159  // setup the Actors bounding box and bounding sphere
160 
161  for(int i=0; i<(int)actors.size(); ++i)
162  actors[i]->computeBounds();
163 }
164 //-----------------------------------------------------------------------------
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
const T * at(int i) const
Definition: Collection.hpp:100
bool isEnabled() const
Whether an Actor should be considered for rendering, picking, scene bounding box calculation etc...
Definition: Actor.hpp:277
void setAutomaticDelete(bool autodel_on)
If set to true the Object is deleted when its reference count reaches 0.
Definition: Object.hpp:275
void extractVisibleActors(ActorCollection &list, const Camera *camera, unsigned enable_mask=0xFFFFFFFF)
Extracts the enabled and visible Actors contained in th ActorTree hierarchy and appends them to the g...
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
const T * get() const
Definition: Object.hpp:128
ActorTreeAbstract * mParent
void eraseAt(int index)
Definition: Collection.hpp:159
int size() const
Definition: Collection.hpp:82
Actor * addActor(Renderable *renderable, Effect *eff, Transform *tr=NULL)
Utility function that adds an Actor and binds it to the given Renderable, Effect and Transform...
bool cull(const Sphere &sphere) const
Definition: Frustum.hpp:60
int find(T *obj) const
Definition: Collection.hpp:113
static void prepareActors(ActorCollection &actors)
Updates the Transform and the bounds of the given Actors.
unsigned int enableMask() const
The enable mask of an Actor is usually used to defines whether the actor should be rendered or not de...
Definition: Actor.hpp:270
const AABB & aabb() const
Returns the bounding box of a node. Such bounding box contains both the bounding boxes of the node&#39;s ...
Visualization Library main namespace.
const Frustum & frustum() const
The view frustum of the camera used to perform frustum culling.
Definition: Camera.hpp:132
const ActorCollection * actors() const
Returns the actors contained in a ActorTree node.
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
An abstract class that represents all the objects that can be rendered.
Definition: Renderable.hpp:58
void push_back(T *data)
Definition: Collection.hpp:76
The ActorTreeAbstract class implements the interface of a generic tree containing Actors in its nodes...
virtual ActorTreeAbstract * child(int i)=0
Returns the i-th child node of an ActorTreeAbstract node.
void computeAABB()
Recursively computes the bounding box of a node so that it includes the bounding boxes of the node&#39;s ...
const AABB & boundingBox() const
Returns the bounding box (not guaranteed to be up to date) that contains this Actor.
Definition: Actor.hpp:205
#define NULL
Definition: OpenGLDefs.hpp:81
Defines the sequence of Shader objects used to render an Actor.
Definition: Effect.hpp:91
bool isEnabled() const
If false then extractVisibleActors() will ignore this node and all its children.
Defined as a simple subclass of Collection<Actor>, see Collection for more information.
Definition: Actor.hpp:479
ActorTreeAbstract * eraseActor(Actor *actor)
Removes the given Actor from the ActorTreeAbstract.
void computeBounds()
Computes the bounding box and bounding sphere of an Actor if boundsDirty().
Definition: Actor.cpp:95
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
void extractActors(ActorCollection &list)
Extracts all the Actors contained in th ActorTree hierarchy and appends them to the given ActorCollec...
virtual int childrenCount() const =0
Returns the number of child nodes of an ActorTreeAbstract node.