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]
RenderQueueSorter.hpp
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 
32 #ifndef RenderQueueSorter_INCLUDE_ONCE
33 #define RenderQueueSorter_INCLUDE_ONCE
34 
36 
37 namespace vl
38 {
39  //------------------------------------------------------------------------------
40  // RenderQueueSorter
41  //------------------------------------------------------------------------------
45  class RenderQueueSorter: public Object
46  {
48 
49  public:
51  {
52  VL_DEBUG_SET_OBJECT_NAME()
53  }
54  virtual bool operator()(const RenderToken* a, const RenderToken* b) const = 0;
55  virtual bool confirmZCameraDistanceNeed(const RenderToken*) const = 0;
56  virtual bool mightNeedZCameraDistance() const = 0;
57  };
58  //------------------------------------------------------------------------------
59  // RenderQueueSorterByShader
60  //------------------------------------------------------------------------------
62  // no z sort, no effect render rank, no actor render rank
64  {
66 
67  public:
69  {
70  VL_DEBUG_SET_OBJECT_NAME()
71  }
72  virtual bool mightNeedZCameraDistance() const { return false; }
73  virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; }
74  virtual bool operator()(const RenderToken* a, const RenderToken* b) const
75  {
76  return a->mShader < b->mShader;
77  }
78  };
79  //------------------------------------------------------------------------------
80  // RenderQueueSorterByRenderable
81  //------------------------------------------------------------------------------
83  // no z sort, no effect render rank, no actor render rank
85  {
87 
88  public:
90  {
91  VL_DEBUG_SET_OBJECT_NAME()
92  }
93  virtual bool mightNeedZCameraDistance() const { return false; }
94  virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; }
95  virtual bool operator()(const RenderToken* a, const RenderToken* b) const
96  {
97  return a->mRenderable < b->mRenderable;
98  }
99  };
100  //------------------------------------------------------------------------------
101  // RenderQueueSorterBasic
102  //------------------------------------------------------------------------------
105  {
107 
108  public:
110  {
111  VL_DEBUG_SET_OBJECT_NAME()
112  }
113  virtual bool mightNeedZCameraDistance() const { return true; }
114  virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return false; }
115  virtual bool operator()(const RenderToken* a, const RenderToken* b) const
116  {
117  // Actor's render-block
118  if (a->mActor->renderBlock() != b->mActor->renderBlock())
119  return a->mActor->renderBlock() < b->mActor->renderBlock();
120  else
121  // Effect's render-rank
123  return a->mEffectRenderRank < b->mEffectRenderRank;
124  else
125  // Actor's render-rank
126  if (a->mActor->renderRank() != b->mActor->renderRank())
127  return a->mActor->renderRank() < b->mActor->renderRank();
128  else
129  // shader sorting
130  if (a->mShader != b->mShader)
131  return a->mShader < b->mShader;
132  // renderable sorting
133  else
134  return a->mRenderable < b->mRenderable;
135  }
136  };
137  //------------------------------------------------------------------------------
138  // RenderQueueSorterStandard
139  //------------------------------------------------------------------------------
150  {
152 
153  public:
155  {
156  VL_DEBUG_SET_OBJECT_NAME()
157  }
158 
159  virtual bool mightNeedZCameraDistance() const { return true; }
160  virtual bool confirmZCameraDistanceNeed(const RenderToken* a) const
161  {
162  return mDepthSortMode != NeverDepthSort && (mDepthSortMode == AlwaysDepthSort ||
163  (a->mShader->isBlendingEnabled() && (mDepthSortMode == AlphaDepthSort)) );
164  }
165 
166  virtual bool operator()(const RenderToken* a, const RenderToken* b) const
167  {
168  // --------------- user defined sorting ---------------
169 
170  // Actor's render-block
171  if (a->mActor->renderBlock() != b->mActor->renderBlock())
172  return a->mActor->renderBlock() < b->mActor->renderBlock();
173  else
174  // Effect's render-rank
176  return a->mEffectRenderRank < b->mEffectRenderRank;
177  else
178  // Actor's render-rank
179  if (a->mActor->renderRank() != b->mActor->renderRank())
180  return a->mActor->renderRank() < b->mActor->renderRank();
181  else
182 
183  // --------------- shader based sorting ---------------
184 
185  if ( mDepthSortMode != AlwaysDepthSort && (a->mShader->isBlendingEnabled() != b->mShader->isBlendingEnabled()))
186  {
187  // first render opaque objects
188  return !a->mShader->isBlendingEnabled();
189  }
190  // A/b->mShader->isEnabled(OGL_BLEND) are equal or AlwaysDepthSort
191  else
193  {
194  // render first far objects then the close ones
195  return a->mCameraDistance > b->mCameraDistance;
196  }
197  else
198 
199  /*// glsl
200  if ( a->mShader->glslProgram() != b->mShader->glslProgram() )
201  return a->mShader->glslProgram() < b->mShader->glslProgram();
202  else*/
203 
204  /*sort by textures: removed*/
205 
206  // shader sorting
207  if (a->mShader != b->mShader)
208  return a->mShader < b->mShader;
209  // renderable sorting
210  else
211  return a->mRenderable < b->mRenderable;
212  }
213 
214  EDepthSortMode depthSortMode() const { return mDepthSortMode; }
215  void setDepthSortMode(EDepthSortMode mode) { mDepthSortMode = mode; }
216 
217  public:
219  };
220  //------------------------------------------------------------------------------
221  // RenderQueueSorterOcclusion
222  //------------------------------------------------------------------------------
232  {
234 
235  public:
237  {
238  VL_DEBUG_SET_OBJECT_NAME()
239  }
240 
241  virtual bool mightNeedZCameraDistance() const { return true; }
242  virtual bool confirmZCameraDistanceNeed(const RenderToken*) const { return true; }
243 
244  virtual bool operator()(const RenderToken* a, const RenderToken* b) const
245  {
246  // --------------- user defined sorting ---------------
247 
248  // Actor's render-block
249  if (a->mActor->renderBlock() != b->mActor->renderBlock())
250  return a->mActor->renderBlock() < b->mActor->renderBlock();
251  else
252  // Effect's render-rank
254  return a->mEffectRenderRank < b->mEffectRenderRank;
255  else
256  // Actor's render-rank
257  if (a->mActor->renderRank() != b->mActor->renderRank())
258  return a->mActor->renderRank() < b->mActor->renderRank();
259  else
260 
261  // --------------- shader based sorting ---------------
262 
263  if ( a->mShader->isBlendingEnabled() != b->mShader->isBlendingEnabled() )
264  {
265  // first render opaque objects
266  return !a->mShader->isBlendingEnabled();
267  }
268  else
269  // blending on: render first far objects then the close ones
270  if ( a->mShader->isBlendingEnabled() )
271  {
272  return a->mCameraDistance > b->mCameraDistance;
273  }
274  else
275  // blending off: render first close objects then far ones -> i.e. -> if ( !a->mShader->isBlendingEnabled() )
276  if (a->mCameraDistance != b->mCameraDistance)
277  {
278  return a->mCameraDistance < b->mCameraDistance;
279  }
280  else
281  // shader sorting
282  if (a->mShader != b->mShader)
283  return a->mShader < b->mShader;
284  // renderable sorting
285  else
286  return a->mRenderable < b->mRenderable;
287  }
288  };
289  //------------------------------------------------------------------------------
290  // RenderQueueSorterAggressive
291  //------------------------------------------------------------------------------
295  {
297 
298  public:
300  {
301  VL_DEBUG_SET_OBJECT_NAME()
302  }
303 
304  virtual bool mightNeedZCameraDistance() const { return true; }
305  virtual bool confirmZCameraDistanceNeed(const RenderToken* a) const
306  {
307  return mDepthSortMode != NeverDepthSort &&
308  (mDepthSortMode == AlwaysDepthSort || (a->mShader->isBlendingEnabled() && (mDepthSortMode == AlphaDepthSort)) );
309  }
310 
311  virtual bool operator()(const RenderToken* a, const RenderToken* b) const
312  {
313  // --------------- user defined sorting ---------------
314 
315  // Actor's render-block
316  if (a->mActor->renderBlock() != b->mActor->renderBlock())
317  return a->mActor->renderBlock() < b->mActor->renderBlock();
318  else
319  // Effect's render-rank
321  return a->mEffectRenderRank < b->mEffectRenderRank;
322  else
323  // Actor's render-rank
324  if (a->mActor->renderRank() != b->mActor->renderRank())
325  return a->mActor->renderRank() < b->mActor->renderRank();
326  else
327 
328  // --------------- shader based sorting ---------------
329 
330  if ( mDepthSortMode != AlwaysDepthSort && (a->mShader->isBlendingEnabled() != b->mShader->isBlendingEnabled()))
331  {
332  return !a->mShader->isBlendingEnabled(); // first render opaque objects
333  }
334  else // A/b->mShader->isEnabled(OGL_BLEND) are equal or AlwaysDepthSort
336  {
337  return a->mCameraDistance > b->mCameraDistance; // render first far objects then the close ones
338  }
339  else
340 
341  // glsl
342  if ( a->mShader->glslProgram() != b->mShader->glslProgram() )
343  return a->mShader->glslProgram() < b->mShader->glslProgram();
344  else
345 
346  // render state set
347  if ( a->mShader->getRenderStateSet() != b->mShader->getRenderStateSet() )
348  return a->mShader->getRenderStateSet() < b->mShader->getRenderStateSet();
349  else
350 
351  // enables
352  if ( a->mShader->getEnableSet() != b->mShader->getEnableSet() )
353  return a->mShader->getEnableSet() < b->mShader->getEnableSet();
354  else
355 
356  /*sort by textures: removed*/
357 
358  /*sort by lights: removed*/
359 
360  // shader sorting
361  if (a->mShader != b->mShader)
362  return a->mShader < b->mShader;
363  // renderable sorting
364  else
365  return a->mRenderable < b->mRenderable;
366  }
367 
368  EDepthSortMode depthSortMode() const { return mDepthSortMode; }
369  void setDepthSortMode(EDepthSortMode mode) { mDepthSortMode = mode; }
370 
371  public:
373  };
374 }
375 
376 #endif
Sorts the RenderTokens by their Effect rank -> Actor rank -> Shader pointer -> Renderable pointer...
Sorts the RenderTokens by their Renderable pointer.
virtual bool confirmZCameraDistanceNeed(const RenderToken *) const
void setDepthSortMode(EDepthSortMode mode)
Implements a RenderQueueSorter that maximizes the z-buffer test efficiency as much as possible...
virtual bool operator()(const RenderToken *a, const RenderToken *b) const
virtual bool mightNeedZCameraDistance() const
EnableSet * getEnableSet()
Definition: Shader.hpp:2240
virtual bool mightNeedZCameraDistance() const
Sorts the RenderTokens by their Shader pointer.
virtual bool mightNeedZCameraDistance() const =0
Renderable * mRenderable
Definition: RenderToken.hpp:59
virtual bool confirmZCameraDistanceNeed(const RenderToken *) const =0
virtual bool operator()(const RenderToken *a, const RenderToken *b) const
int renderBlock() const
Returns the rendering block of an Actor.
Definition: Actor.hpp:249
virtual bool operator()(const RenderToken *a, const RenderToken *b) const
Sorts the RenderTokens by Effect rank -> Actor rank -> blending on/off -> Z distance form the Camera ...
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Internally used by the rendering engine.
Definition: RenderToken.hpp:47
Visualization Library main namespace.
Implements the default RenderQueueSorter.
void setDepthSortMode(EDepthSortMode mode)
virtual bool operator()(const RenderToken *a, const RenderToken *b) const
virtual bool mightNeedZCameraDistance() const
const Shader * mShader
Definition: RenderToken.hpp:60
The base class for all the reference counted objects.
Definition: Object.hpp:158
EDepthSortMode depthSortMode() const
EDepthSortMode
int renderRank() const
Returns the rendering rank of an Actor.
Definition: Actor.hpp:246
virtual bool confirmZCameraDistanceNeed(const RenderToken *a) const
virtual bool confirmZCameraDistanceNeed(const RenderToken *) const
EDepthSortMode depthSortMode() const
virtual bool operator()(const RenderToken *a, const RenderToken *b) const =0
virtual bool mightNeedZCameraDistance() const
virtual bool mightNeedZCameraDistance() const
virtual bool operator()(const RenderToken *a, const RenderToken *b) const
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
RenderStateSet * getRenderStateSet()
Definition: Shader.hpp:2246
virtual bool mightNeedZCameraDistance() const
const GLSLProgram * glslProgram() const
Returns the GLSLProgram associated to a Shader (if any)
Definition: Shader.hpp:2205
virtual bool confirmZCameraDistanceNeed(const RenderToken *) const
virtual bool confirmZCameraDistanceNeed(const RenderToken *) const
The RenderQueueSorter class is the abstract base class of all the algorithms used to sort a set of Re...
bool isBlendingEnabled() const
Definition: Shader.hpp:2179
virtual bool confirmZCameraDistanceNeed(const RenderToken *a) const
virtual bool operator()(const RenderToken *a, const RenderToken *b) const