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]
expandResourceDatabase.cpp
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 
34 #include <vlGraphics/Actor.hpp>
35 #include <vlGraphics/Camera.hpp>
36 #include <vlGraphics/Light.hpp>
37 #include <vlGraphics/ClipPlane.hpp>
38 #include <set>
39 #include <algorithm>
40 
41 using namespace vl;
42 
43 namespace
44 {
45  struct expanded_res
46  {
47  std::set< ref<Renderable> > ren;
48  std::set< ref<Actor> > act;
49  std::set< ref<Transform> > tr;
50  std::set< ref<Effect> > fx;
51  std::set< ref<Shader> > sh;
52  std::set< ref<RenderState> > rs;
53  std::set< ref<Uniform> > un;
54  std::set< ref<Viewport> > vp;
55  std::set< ref<Camera> > cam;
56  std::set< ref<Object> > misc;
57  };
58 
59  std::vector< ref<RenderState> > sortRenderStates(const std::set< ref<RenderState> >& rs_set)
60  {
61  std::vector< ref<RenderState> > rs;
62  rs.insert( rs.end(), rs_set.begin(), rs_set.end() );
63 
64  // for some reason GCC 3.4.5 does not allow operator() to sort
65  struct rs_less
66  {
67  static bool compare(const ref<RenderState>& a, const ref<RenderState>& b)
68  {
69  return a->type() < b->type();
70  }
71  };
72 
73  std::sort( rs.begin(), rs.end(), rs_less::compare );
74  return rs;
75  }
76 
77  void expandShader(Shader* shader, expanded_res& set)
78  {
79  // Shader
80  set.sh.insert( shader );
81  // Uniforms
82  if (shader->getUniformSet())
83  for(size_t i=0; i<shader->uniforms().size(); ++i)
84  set.un.insert(shader->uniforms()[i].get());
85  // RenderStates
86  if (shader->getRenderStateSet())
87  for(size_t i=0; i<shader->renderStatesCount(); ++i)
88  set.rs.insert( shader->renderStates()[i].mRS );
89  }
90 
91  void expandEffect(Effect* fx, expanded_res& set)
92  {
93  // Effect
94  set.fx.insert(fx);
95  // Shaders
96  for(int i=0; i<VL_MAX_EFFECT_LOD; ++i)
97  {
98  if( fx->lod(i) )
99  {
100  for(int j=0; j<fx->lod(i)->size(); ++j)
101  {
102  Shader* shader = fx->lod(i)->at(j);
103  set.sh.insert( shader );
104  expandShader(shader, set);
105  }
106  }
107  }
108  }
109 
110  void expandActor(Actor* actor, expanded_res& set)
111  {
112  // Actor
113  set.act.insert( actor );
114  // Transform
115  if (actor->transform())
116  set.tr.insert( actor->transform() );
117  // Uniforms
118  if (actor->getUniformSet())
119  for(size_t i=0; i<actor->uniforms().size(); ++i)
120  set.un.insert(actor->uniforms()[i].get());
121  // Renderables
122  for(int i=0; i<VL_MAX_ACTOR_LOD; ++i)
123  if ( actor->lod(i) )
124  set.ren.insert( actor->lod(i) );
125  // Effects
126  if (actor->effect())
127  expandEffect(actor->effect(), set);
128  }
129 }
130 
131 // Extracts and sorts Shaders, Effects, Renderables, RenderStates, Transforms etc. from their parent objects.
133 {
134  expanded_res set;
135 
136  for(size_t i=0; i<db->resources().size(); ++i)
137  {
138  Object* res = db->resources()[i].get();
139 
140  if( res->isOfType( Renderable::Type() ) )
141  set.ren.insert( res->as<Renderable>() );
142  else
143  if( res->isOfType( Actor::Type() ) )
144  expandActor(res->as<Actor>(), set);
145  else
146  if( res->isOfType( Transform::Type() ) )
147  set.tr.insert( res->as<Transform>() );
148  else
149  if( res->isOfType( Effect::Type() ) )
150  expandEffect( res->as<Effect>(), set );
151  else
152  if( res->isOfType( Shader::Type() ) )
153  expandShader( res->as<Shader>(), set );
154  else
155  if( res->isOfType( RenderState::Type() ) )
156  set.rs.insert( res->as<RenderState>() );
157  else
158  if( res->isOfType( Uniform::Type() ) )
159  set.un.insert( res->as<Uniform>() );
160  else
161  if( res->isOfType( Viewport::Type() ) )
162  set.vp.insert( res->as<Viewport>() );
163  else
164  if( res->isOfType( Camera::Type() ) )
165  {
166  Camera* camera = res->as<Camera>();
167  set.cam.insert( camera );
168  if ( camera->viewport() )
169  set.vp.insert( camera->viewport() );
170  if ( camera->boundTransform() )
171  set.tr.insert( camera->boundTransform() );
172  }
173  else
174  set.misc.insert( res );
175  }
176 
177  // reconstruct the resources
178  db->resources().clear();
179  db->resources().insert( db->resources().end(), set.vp.begin(), set.vp.end() );
180  db->resources().insert( db->resources().end(), set.cam.begin(), set.cam.end() );
181  db->resources().insert( db->resources().end(), set.tr.begin(), set.tr.end() );
182  std::vector< ref<RenderState> > sorted_rs = sortRenderStates( set.rs );
183  db->resources().insert( db->resources().end(), sorted_rs.begin(), sorted_rs.end() );
184  db->resources().insert( db->resources().end(), set.un.begin(), set.un.end() );
185  db->resources().insert( db->resources().end(), set.sh.begin(), set.sh.end() );
186  db->resources().insert( db->resources().end(), set.fx.begin(), set.fx.end() );
187  db->resources().insert( db->resources().end(), set.ren.begin(), set.ren.end() );
188  db->resources().insert( db->resources().end(), set.act.begin(), set.act.end() );
189  db->resources().insert( db->resources().end(), set.misc.begin(), set.misc.end() );
190 }
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
const std::vector< ref< Uniform > > & uniforms() const
Equivalent to getUniformSet()->uniforms()
Definition: Actor.cpp:121
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
const Transform * boundTransform() const
Returns the Transform bound to a camera.
Definition: Camera.hpp:150
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
const std::vector< ref< Uniform > > & uniforms() const
Equivalent to gocUniformSet()->uniforms(...)
Definition: Shader.hpp:2205
const UniformSet * getUniformSet() const
Returns the installed UniformSet.
Definition: Actor.hpp:345
size_t renderStatesCount() const
Definition: Shader.hpp:2180
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:141
Visualization Library main namespace.
VLGRAPHICS_EXPORT void expandResourceDatabase(ResourceDatabase *db)
Extracts and sorts Shaders, Effects, Renderables, RenderStates, Transforms etc. from their parent obj...
Base class for most of the OpenGL render state wrapper classes.
Definition: RenderState.hpp:50
const std::vector< ref< Object > > & resources() const
The base class for all the reference counted objects.
Definition: Object.hpp:158
An abstract class that represents all the objects that can be rendered.
Definition: Renderable.hpp:58
Implements the viewport and clearing settings associated to a Camera.
Definition: Viewport.hpp:51
T * as()
Casts an Object to the specified class.
Definition: Object.hpp:282
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1830
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
UniformSet * getUniformSet()
Returns the UniformSet installed.
Definition: Shader.hpp:2261
RenderStateSet * getRenderStateSet()
Definition: Shader.hpp:2235
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
virtual ERenderState type() const
Definition: RenderState.hpp:59
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
const RenderStateSlot * renderStates() const
Definition: Shader.hpp:2182
ref< RenderState > mRS
The ResourceDatabase class contains and manipulates a set of resources.
const ref< ShaderPasses > & lod(int lod_level) const
Returns the ShaderPasses representing the specified LOD level.
Definition: Effect.hpp:171