Visualization Library 2.0.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Effect.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 Effect_INCLUDE_ONCE
33 #define Effect_INCLUDE_ONCE
34 
37 #include <vlGraphics/Shader.hpp>
38 #include <vlCore/Collection.hpp>
39 #include <vector>
40 
41 namespace vl
42 {
43  class Actor;
44  //------------------------------------------------------------------------------
45  // ShaderPasses
46  //------------------------------------------------------------------------------
51  class ShaderPasses: public Collection<Shader>
52  {
54 
55  public:
62  ShaderPasses(Shader* pass1=NULL, Shader* pass2=NULL, Shader* pass3=NULL, Shader* pass4=NULL )
63  {
64  VL_DEBUG_SET_OBJECT_NAME()
65  if (pass1)
66  push_back(pass1);
67  if (pass2)
68  push_back(pass2);
69  if (pass3)
70  push_back(pass3);
71  if (pass4)
72  push_back(pass4);
73  }
74  };
75  //------------------------------------------------------------------------------
76  // Effect
77  //------------------------------------------------------------------------------
92  {
94 
95  // use deepCopy() and shallowCopy() instead
96  Effect(const Effect&): Object() {}
97  Effect& operator=(const Effect&) { return *this; }
98 
99  public:
102  {
103  VL_DEBUG_SET_OBJECT_NAME()
104  mEnableMask = 0xFFFFFFFF;
105  mRenderRank = 0;
106  mActiveLod = 0;
107  mLODShaders[0] = new ShaderPasses(new Shader);
108  }
109 
111  {
112  ref<Effect> fx = new Effect;
113  fx->shallowCopyFrom(*this, shader_copy);
114  return fx;
115  }
116 
117  Effect& shallowCopyFrom(const Effect& other, EShaderCopyMode shader_copy)
118  {
119  for(int i=0; i<VL_MAX_EFFECT_LOD; ++i)
120  mLODShaders[i] = other.mLODShaders[i];
121 
122  if (shader_copy == SCM_OwnShaders)
123  {
124  // create local shallow copies of all the Shaders
125  for(int lod=0; lod<VL_MAX_EFFECT_LOD; ++lod)
126  for(int pass=0; mLODShaders[lod] && pass<mLODShaders[lod]->size(); ++pass)
127  (*mLODShaders[lod])[pass] = (*mLODShaders[lod])[pass]->shallowCopy();
128  }
129 
130  mLODEvaluator = other.mLODEvaluator;
131 
132  mActiveLod = other.mActiveLod;
133  mRenderRank = other.mRenderRank;
134  mEnableMask = other.mEnableMask;
135 
136  return *this;
137  }
138 
140  {
141  ref<Effect> fx = new Effect;
142  fx->deepCopyFrom(*this);
143  return fx;
144  }
145 
146  Effect& deepCopyFrom(const Effect& other)
147  {
148  shallowCopyFrom(other, SCM_ShareShaders);
149 
150  // create local clones of all the Shaders
151  for(int lod=0; lod<VL_MAX_EFFECT_LOD; ++lod)
152  for(int pass=0; mLODShaders[lod] && pass<mLODShaders[lod]->size(); ++pass)
153  (*mLODShaders[lod])[pass] = (*mLODShaders[lod])[pass]->deepCopy();
154 
155  return *this;
156  }
157 
164  void setRenderRank(int rank) { mRenderRank = rank; }
165 
167  int renderRank() const { return mRenderRank; }
168 
171  const ref<ShaderPasses>& lod(int lod_level) const { return mLODShaders[lod_level]; }
172 
175  ref<ShaderPasses>& lod(int lod_level) { return mLODShaders[lod_level]; }
176 
178  Shader* shader(int lodi=0, int pass=0) { return lod(lodi)->at(pass); }
179 
181  const Shader* shader(int lodi=0, int pass=0) const { return lod(lodi)->at(pass); }
182 
184  void setLOD(int lodi, Shader* shader1, Shader* shader2=NULL, Shader* shader3=NULL, Shader* shader4=NULL)
185  {
186  VL_CHECK(lodi<VL_MAX_EFFECT_LOD)
187  lod(lodi) = new ShaderPasses(shader1,shader2,shader3,shader4);
188  }
189 
191  void setLODEvaluator(LODEvaluator* lod_evaluator) { mLODEvaluator = lod_evaluator; }
192 
194  LODEvaluator* lodEvaluator() { return mLODEvaluator.get(); }
195 
197  const LODEvaluator* lodEvaluator() const { return mLODEvaluator.get(); }
198 
200  void setEnableMask(unsigned int mask) { mEnableMask = mask; }
201 
203  unsigned int enableMask() const { return mEnableMask; }
204 
206  int evaluateLOD(Actor* actor, Camera* camera);
207 
209  void setActiveLod(int lod)
210  {
211  VL_CHECK( lod < VL_MAX_EFFECT_LOD )
212  VL_CHECK( lod >= 0 )
213  mActiveLod = lod;
214  }
215 
217  int activeLod() const { return mActiveLod; }
218 
219  protected:
220  ref<ShaderPasses> mLODShaders[VL_MAX_EFFECT_LOD];
224  unsigned int mEnableMask;
225  };
226  //------------------------------------------------------------------------------
227 }
228 
229 #endif
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
EShaderCopyMode
void setEnableMask(unsigned int mask)
The enable mask of an Actor&#39;s Effect defines whether the actor should be rendered or not depending on...
Definition: Effect.hpp:200
int mRenderRank
Definition: Effect.hpp:223
Object()
Constructor.
Definition: Object.hpp:164
ref< ShaderPasses > mLODShaders[VL_MAX_EFFECT_LOD]
Definition: Effect.hpp:220
const Shader * shader(int lodi=0, int pass=0) const
Utility function, same as &#39;lod(lodi)->at(pass);&#39;.
Definition: Effect.hpp:181
Effect()
Constructor.
Definition: Effect.hpp:101
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
int activeLod() const
Returns the lod to be used for rendering.
Definition: Effect.hpp:217
unsigned int mEnableMask
Definition: Effect.hpp:224
Visualization Library main namespace.
Collection & operator=(const std::vector< ref< Shader > > &vector)
Definition: Collection.hpp:65
Effect & deepCopyFrom(const Effect &other)
Definition: Effect.hpp:146
The base class for all the reference counted objects.
Definition: Object.hpp:158
void setLOD(int lodi, Shader *shader1, Shader *shader2=NULL, Shader *shader3=NULL, Shader *shader4=NULL)
Utility function, same as &#39;lod(lodi) = new ShaderPasses(shader1,shader2,shader3,shader4);&#39;.
Definition: Effect.hpp:184
void push_back(Shader *data)
Definition: Collection.hpp:76
const LODEvaluator * lodEvaluator() const
Returns the installed LODEvaluator (if any) or NULL.
Definition: Effect.hpp:197
LODEvaluator * lodEvaluator()
Returns the installed LODEvaluator (if any) or NULL.
Definition: Effect.hpp:194
void setRenderRank(int rank)
Modifies the rendering rank of an Actor.
Definition: Effect.hpp:164
A sequence of Shader objects each of which represent a rendering pass.
Definition: Effect.hpp:51
void setLODEvaluator(LODEvaluator *lod_evaluator)
Installs the LODEvaluator used to compute the current LOD at rendering time.
Definition: Effect.hpp:191
void setActiveLod(int lod)
Sets the lod to be used for rendering.
Definition: Effect.hpp:209
#define NULL
Definition: OpenGLDefs.hpp:81
int renderRank() const
Returns the rendering rank of an Effect.
Definition: Effect.hpp:167
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1830
unsigned int enableMask() const
The enable mask of an Actor&#39;s Effect defines whether the actor should be rendered or not depending on...
Definition: Effect.hpp:203
Abstract class to compute the appropriate LOD of an Actor or Effect.
Shader * shader(int lodi=0, int pass=0)
Utility function, same as &#39;lod(lodi)->at(pass);&#39;.
Definition: Effect.hpp:178
Defines the sequence of Shader objects used to render an Actor.
Definition: Effect.hpp:91
int mActiveLod
Definition: Effect.hpp:222
Effect & shallowCopyFrom(const Effect &other, EShaderCopyMode shader_copy)
Definition: Effect.hpp:117
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
A local copy of the Shaders will be created but the contained render states will be shared...
ref< LODEvaluator > mLODEvaluator
Definition: Effect.hpp:221
The Shader pointer will be copied as is.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
ShaderPasses(Shader *pass1=NULL, Shader *pass2=NULL, Shader *pass3=NULL, Shader *pass4=NULL)
Constructor.
Definition: Effect.hpp:62
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
ref< ShaderPasses > & lod(int lod_level)
Returns the ShaderPasses representing the specified LOD level.
Definition: Effect.hpp:175
#define VL_CHECK(expr)
Definition: checks.hpp:73
ref< Effect > deepCopy() const
Definition: Effect.hpp:139
const ref< ShaderPasses > & lod(int lod_level) const
Returns the ShaderPasses representing the specified LOD level.
Definition: Effect.hpp:171
ref< Effect > shallowCopy(EShaderCopyMode shader_copy) const
Definition: Effect.hpp:110