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]
Actor.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 Actor_INCLUDE_ONCE
33 #define Actor_INCLUDE_ONCE
34 
36 #include <vlCore/Collection.hpp>
37 #include <vlCore/Sphere.hpp>
38 #include <vlCore/AABB.hpp>
39 #include <vlCore/Transform.hpp>
40 #include <vlGraphics/Effect.hpp>
44 #include <vlGraphics/Scissor.hpp>
45 
46 namespace vl
47 {
48  //------------------------------------------------------------------------------
49  // ActorEventCallback
50  //------------------------------------------------------------------------------
76  {
78 
79  public:
80  ActorEventCallback(): mEnabled(true) {}
81 
90  virtual void onActorRenderStarted(Actor* actor, real frame_clock, const Camera* cam, Renderable* renderable, const Shader* shader, int pass) = 0;
91 
93  virtual void onActorDelete(Actor* actor) = 0;
94 
95  void setEnabled(bool enabled) { mEnabled = enabled; }
96  bool isEnabled() const { return mEnabled; }
97 
98  protected:
99  bool mEnabled;
100  };
101 
102  //------------------------------------------------------------------------------
103  // Actor
104  //------------------------------------------------------------------------------
131  {
133 
134  public:
142  Actor(Renderable* renderable = NULL, Effect* effect = NULL, Transform* transform = NULL, int block = 0, int rank = 0):
143  mEffect(effect), mTransform(transform), mRenderBlock(block), mRenderRank(rank),
144  mTransformUpdateTick(-1), mBoundsUpdateTick(-1), mEnableMask(0xFFFFFFFF), mOcclusionQuery(0), mOcclusionQueryTick(0xFFFFFFFF), mIsOccludee(true), mEnabled(true)
145  {
146  VL_DEBUG_SET_OBJECT_NAME()
147  mActorEventCallbacks.setAutomaticDelete(false);
148  setLod(0,renderable);
149  // actor user data
150  #ifdef VL_USER_DATA_ACTOR
151  mActorUserData = NULL;
152  #endif
153  }
154 
156  virtual ~Actor();
157 
159  void setLod(int lod_index, Renderable* renderable)
160  {
161  mRenderables[lod_index] = renderable;
162 
163  // schedule update of the Actor's bounds.
164  if (lod_index == 0)
165  {
166  mBoundsUpdateTick = -1;
167  mAABB.setNull();
168  mSphere.setNull();
169  }
170  }
171 
173  const Renderable* lod(int lod_index) const { return mRenderables[lod_index].get(); }
174 
176  Renderable* lod(int lod_index) { return mRenderables[lod_index].get(); }
177 
179  void setLODs(Renderable* lod0, Renderable* lod1=NULL, Renderable* lod2=NULL, Renderable* lod3=NULL, Renderable* lod4=NULL, Renderable* lod5=NULL);
180 
182  void setTransform(Transform* transform)
183  {
184  mTransform = transform;
185  mTransformUpdateTick = -1;
186  mBoundsUpdateTick = -1;
187  }
188 
190  Transform* transform() { return mTransform.get(); }
191 
193  const Transform* transform() const { return mTransform.get(); }
194 
196  void setEffect(Effect* effect) { mEffect = effect; }
197 
199  Effect* effect() { return mEffect.get(); }
200 
202  const Effect* effect() const { return mEffect.get(); }
203 
205  const AABB& boundingBox() const { return mAABB; }
206 
208  const Sphere& boundingSphere() const { return mSphere; }
209 
211  const AABB& boundingBoxSafe() { computeBounds(); return mAABB; }
212 
214  const Sphere& boundingSphereSafe() { computeBounds(); return mSphere; }
215 
217  void computeBounds();
218 
220  bool boundsDirty() const;
221 
231  void setRenderRank(int rank) { mRenderRank = rank; }
232 
243  void setRenderBlock(int block) { mRenderBlock = block; }
244 
246  int renderRank() const { return mRenderRank; }
247 
249  int renderBlock() const { return mRenderBlock; }
250 
252  void setLODEvaluator(LODEvaluator* lod_evaluator) { mLODEvaluator = lod_evaluator; }
253 
255  LODEvaluator* lodEvaluator() { return mLODEvaluator.get(); }
256 
258  const LODEvaluator* lodEvaluator() const { return mLODEvaluator.get(); }
259 
260  int evaluateLOD(Camera* camera);
261 
265  void setEnableMask(unsigned int mask) { mEnableMask = mask; }
266 
270  unsigned int enableMask() const { return mEnableMask; }
271 
274  void setEnabled(bool enabled) { mEnabled = enabled; }
277  bool isEnabled() const { return mEnabled; }
278 
279  // uniforms methods
280 
284  void setUniform(Uniform* uniform);
285 
289  const std::vector< ref<Uniform> >& uniforms() const;
290 
294  std::vector< ref<Uniform> >& uniforms();
295 
299  void eraseUniform(const char* name);
300 
304  void eraseUniform(const Uniform* uniform);
305 
309  void eraseAllUniforms();
310 
314  Uniform* gocUniform(const char* name);
315 
319  Uniform* getUniform(const char* name);
320 
324  const Uniform* getUniform(const char* name) const;
325 
334  void setUniformSet(UniformSet* uniforms) { mUniformSet = uniforms; }
335 
345  const UniformSet* getUniformSet() const { return mUniformSet.get(); }
346 
356  UniformSet* getUniformSet() { return mUniformSet.get(); }
357 
367  UniformSet* gocUniformSet() { if (!mUniformSet) mUniformSet = new UniformSet; return mUniformSet.get(); }
368 
370  const Collection<ActorEventCallback>* actorEventCallbacks() const { return &mActorEventCallbacks; }
371 
373  Collection<ActorEventCallback>* actorEventCallbacks() { return &mActorEventCallbacks; }
374 
376  void dispatchOnActorRenderStarted( real frame_clock, const Camera* camera, Renderable* renderable, const Shader* shader, int pass)
377  {
378  for(size_t i=0; i<actorEventCallbacks()->size(); ++i)
379  {
380  ActorEventCallback& cb = *actorEventCallbacks()->at(i);
381  if (cb.isEnabled())
382  cb.onActorRenderStarted(this, frame_clock, camera, renderable, shader, pass);
383  }
384  }
385 
388  {
389  for(size_t i=0; i<actorEventCallbacks()->size(); ++i)
390  {
391  ActorEventCallback& cb = *actorEventCallbacks()->at(i);
392  cb.onActorDelete(this);
393  }
394  }
395 
404  void setScissor(Scissor* scissor) { mScissor = scissor; }
411  const Scissor* scissor() const { return mScissor.get(); }
412 
419  Scissor* scissor() { return mScissor.get(); }
420 
423  void setOccludee(bool is_occludee) { mIsOccludee = is_occludee; }
424 
427  bool isOccludee() const { return mIsOccludee; }
428 
431  void createOcclusionQuery();
432 
435  void deleteOcclusionQuery();
436 
439  GLuint occlusionQuery() const { return mOcclusionQuery; }
440 
442  void setOcclusionQueryTick(unsigned tick) { mOcclusionQueryTick = tick; }
443 
445  unsigned occlusionQueryTick() const { return mOcclusionQueryTick; }
446 
447 #ifdef VL_USER_DATA_ACTOR
448  public:
449  const Object* actorUserData() const { return mActorUserData.get(); }
450  Object* actorUserData() { return mActorUserData.get(); }
451  void setActorUserData(Object* user_data) { mActorUserData = user_data; }
452 
453  private:
454  ref<Object> mActorUserData;
455 #endif
456 
457  protected:
461  ref<Renderable> mRenderables[VL_MAX_ACTOR_LOD];
470  long long mBoundsUpdateTick;
471  unsigned int mEnableMask;
475  bool mEnabled;
476  };
477  //---------------------------------------------------------------------------
479  class ActorCollection: public Collection<Actor> {};
480  //---------------------------------------------------------------------------
481 }
482 
483 #endif
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
const Renderable * lod(int lod_index) const
Returns the Renderable object representing the LOD level specifed by lod_index.
Definition: Actor.hpp:173
Wraps an OpenGL Shading Language uniform to be associated to a GLSLProgram (see vl::GLSLProgram docum...
Definition: Uniform.hpp:59
void setEffect(Effect *effect)
Binds an Effect to an Actor.
Definition: Actor.hpp:196
GLuint mOcclusionQuery
Definition: Actor.hpp:472
bool isEnabled() const
Whether an Actor should be considered for rendering, picking, scene bounding box calculation etc...
Definition: Actor.hpp:277
const Effect * effect() const
Returns the Effect bound to an Actor.
Definition: Actor.hpp:202
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
bool mIsOccludee
Definition: Actor.hpp:474
bool isEnabled() const
Definition: Actor.hpp:96
GLuint occlusionQuery() const
For internal use only.
Definition: Actor.hpp:439
Collection< ActorEventCallback > mActorEventCallbacks
Definition: Actor.hpp:466
Collection< ActorEventCallback > * actorEventCallbacks()
Returns the list of ActorEventCallback bound to an Actor.
Definition: Actor.hpp:373
const Transform * transform() const
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:193
int mRenderBlock
Definition: Actor.hpp:467
long long mTransformUpdateTick
Definition: Actor.hpp:469
int renderBlock() const
Returns the rendering block of an Actor.
Definition: Actor.hpp:249
const Collection< ActorEventCallback > * actorEventCallbacks() const
Returns the list of ActorEventCallback bound to an Actor.
Definition: Actor.hpp:370
AABB mAABB
Definition: Actor.hpp:458
UniformSet * gocUniformSet()
Creates and/or returns the installed UniformSet.
Definition: Actor.hpp:367
void dispatchOnActorDelete()
Calls all the onActorDelete() of all the ActorEventCallback installed on this Actor.
Definition: Actor.hpp:387
const UniformSet * getUniformSet() const
Returns the installed UniformSet.
Definition: Actor.hpp:345
ref< Transform > mTransform
Definition: Actor.hpp:462
const Sphere & boundingSphere() const
Returns the bounding sphere (not guaranteed to be up to date) that contains this Actor.
Definition: Actor.hpp:208
void setEnableMask(unsigned int mask)
The enable mask of an Actor is usually used to defines whether the actor should be rendered or not de...
Definition: Actor.hpp:265
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Actor(Renderable *renderable=NULL, Effect *effect=NULL, Transform *transform=NULL, int block=0, int rank=0)
Constructor.
Definition: Actor.hpp:142
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
ref< Scissor > mScissor
Definition: Actor.hpp:465
Visualization Library main namespace.
Sphere mSphere
Definition: Actor.hpp:459
virtual void onActorDelete(Actor *actor)=0
Event notifying that an Actor is being deleted.
long long mBoundsUpdateTick
Definition: Actor.hpp:470
Renderable * lod(int lod_index)
Returns the Renderable object representing the LOD level specifed by lod_index.
Definition: Actor.hpp:176
ref< Effect > mEffect
Definition: Actor.hpp:460
UniformSet * getUniformSet()
Returns the installed UniformSet.
Definition: Actor.hpp:356
bool isOccludee() const
If is_occludee equals true an occlusion test will be performed before the rendering of the Actor (if ...
Definition: Actor.hpp:427
const Scissor * scissor() const
Returns the Scissor used when rendering an Actor.
Definition: Actor.hpp:411
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
const LODEvaluator * lodEvaluator() const
Returns the installed LODEvaluator (if any) or NULL.
Definition: Actor.hpp:258
The base class for all the reference counted objects.
Definition: Object.hpp:158
void setRenderRank(int rank)
Modifies the rendering rank of an Actor.
Definition: Actor.hpp:231
An abstract class that represents all the objects that can be rendered.
Definition: Renderable.hpp:58
int renderRank() const
Returns the rendering rank of an Actor.
Definition: Actor.hpp:246
LODEvaluator * lodEvaluator()
Returns the installed LODEvaluator (if any) or NULL.
Definition: Actor.hpp:255
void setTransform(Transform *transform)
Binds a Transform to an Actor.
Definition: Actor.hpp:182
void setOccludee(bool is_occludee)
If is_occludee equals true an occlusion test will be performed before the rendering of the Actor (if ...
Definition: Actor.hpp:423
Scissor * scissor()
Returns the Scissor used when rendering an Actor.
Definition: Actor.hpp:419
virtual void onActorRenderStarted(Actor *actor, real frame_clock, const Camera *cam, Renderable *renderable, const Shader *shader, int pass)=0
Event generated just before an Actor is rendered but after the render states are ready and setup...
void dispatchOnActorRenderStarted(real frame_clock, const Camera *camera, Renderable *renderable, const Shader *shader, int pass)
Calls all the onActorRenderStarted() of all the ActorEventCallback installed on this Actor...
Definition: Actor.hpp:376
const AABB & boundingBox() const
Returns the bounding box (not guaranteed to be up to date) that contains this Actor.
Definition: Actor.hpp:205
void setOcclusionQueryTick(unsigned tick)
For internal use only.
Definition: Actor.hpp:442
void setEnabled(bool enabled)
Whether an Actor should be considered for rendering, picking, scene bounding box calculation etc...
Definition: Actor.hpp:274
The ActorEventCallback class defines a callback object to react to Actor-related events.
Definition: Actor.hpp:75
#define NULL
Definition: OpenGLDefs.hpp:81
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1830
The Sphere class defines a sphere using a center and a radius using vl::real precision.
Definition: Sphere.hpp:43
Abstract class to compute the appropriate LOD of an Actor or Effect.
void setUniformSet(UniformSet *uniforms)
Installs a new UniformSet.
Definition: Actor.hpp:334
Defines the sequence of Shader objects used to render an Actor.
Definition: Effect.hpp:91
Effect * effect()
Returns the Effect bound to an Actor.
Definition: Actor.hpp:199
Defined as a simple subclass of Collection<Actor>, see Collection for more information.
Definition: Actor.hpp:479
unsigned occlusionQueryTick() const
For internal use only.
Definition: Actor.hpp:445
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
void setLODEvaluator(LODEvaluator *lod_evaluator)
Installs the LODEvaluator used to compute the current LOD at rendering time.
Definition: Actor.hpp:252
const AABB & boundingBoxSafe()
Returns the bounding box (guaranteed to be up to date) that contains this Actor.
Definition: Actor.hpp:211
It&#39;s basically an std::vector for Objects that is itself an Object so it can be reference counted and...
Definition: Collection.hpp:49
ref< UniformSet > mUniformSet
Definition: Actor.hpp:464
void setLod(int lod_index, Renderable *renderable)
Sets the Renderable object representing the LOD level specifed by lod_index.
Definition: Actor.hpp:159
bool mEnabled
Definition: Actor.hpp:475
unsigned int mEnableMask
Definition: Actor.hpp:471
void setEnabled(bool enabled)
Definition: Actor.hpp:95
A set of Uniform objects managed by a Shader.
Definition: UniformSet.hpp:50
void setScissor(Scissor *scissor)
Sets the Scissor to be used when rendering an Actor.
Definition: Actor.hpp:404
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
const Sphere & boundingSphereSafe()
Returns the bounding sphere (guaranteed to be up to date) that contains this Actor.
Definition: Actor.hpp:214
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
The Scissor class wraps the OpenGL function glScissor(), see http://www.opengl.org/sdk/docs/man/xhtml...
Definition: Scissor.hpp:47
ref< LODEvaluator > mLODEvaluator
Definition: Actor.hpp:463
int mRenderRank
Definition: Actor.hpp:468
unsigned mOcclusionQueryTick
Definition: Actor.hpp:473
void setRenderBlock(int block)
Modifies the rendering block of an Actor.
Definition: Actor.hpp:243