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]
RenderQueue.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 RenderQueue_INCLUDE_ONCE
33 #define RenderQueue_INCLUDE_ONCE
34 
36 
37 namespace vl
38 {
39  //------------------------------------------------------------------------------
40  // RenderQueue
41  //------------------------------------------------------------------------------
45  class RenderQueue: public Object
46  {
48 
49  public:
51  {
52  VL_DEBUG_SET_OBJECT_NAME()
53  mList.reserve(100);
54  mListMP.reserve(100);
55  }
56 
57  const RenderToken* at(int i) const { return mList[i].get(); }
58 
59  RenderToken* at(int i) { return mList[i].get(); }
60 
61  RenderToken* newToken(bool multipass)
62  {
63  if (multipass)
64  {
65  ++mSizeMP;
66  if ( mSizeMP > (int)mListMP.size() )
67  mListMP.push_back( new RenderToken );
68  return mListMP[mSizeMP-1].get();
69  }
70  else
71  {
72  ++mSize;
73  if ( mSize > (int)mList.size() )
74  mList.push_back( new RenderToken );
75  return mList[mSize-1].get();
76  }
77  }
78 
79  void clear()
80  {
81  mSize = 0;
82  mSizeMP = 0;
83  }
84 
85  bool empty()
86  {
87  return mSize == 0;
88  }
89 
90  int size() const
91  {
92  return mSize;
93  }
94 
95  void sort(RenderQueueSorter* sorter, Camera* camera)
96  {
97  if (sorter->mightNeedZCameraDistance())
98  {
99  for(int i=0; i<size(); ++i)
100  {
101  RenderToken* tok = at(i);
102  vec3 center = tok->mRenderable->boundingBox().isNull() ? vec3(0,0,0) : tok->mRenderable->boundingBox().center();
103  if ( sorter->confirmZCameraDistanceNeed(tok) )
104  {
105  if (tok->mActor->transform())
106  // tok->mCameraDistance = ( camera->viewMatrix() * (tok->mActor->transform()->worldMatrix() * center) ).lengthSquared();
107  tok->mCameraDistance = -( camera->viewMatrix() * (tok->mActor->transform()->worldMatrix() * center) ).z();
108  else
109  // tok->mCameraDistance = ( camera->viewMatrix() * /* I* */ center ).lengthSquared();
110  tok->mCameraDistance = -( camera->viewMatrix() * /* I* */ center ).z();
111  }
112  else
113  tok->mCameraDistance = 0;
114  }
115  }
116 
117  VL_CHECK( sorter )
118  std::sort( mList.begin(), mList.begin() + size(), Sorter( sorter ) );
119  }
120 
121  private:
122  class Sorter
123  {
124  public:
125  Sorter(const RenderQueueSorter* sorter): mRenderQueueSorter(sorter) {}
126  bool operator()(const ref<RenderToken>& a, const ref<RenderToken>& b) const
127  {
128  VL_CHECK(a && b);
129  return mRenderQueueSorter->operator()(a.get(), b.get());
130  }
131  protected:
132  const RenderQueueSorter* mRenderQueueSorter;
133  };
134 
135  protected:
136  // mic fixme: would be nice not to allocate these dinamically.
137  // Necessary because:
138  // 1- RenderToken.mNextPass: the pointer should be stable across std::vector reallocations.
139  // Maybe we can use indices?
140  // 2- The sorting sorts only pointers instead of whole structures.
141  // Note: we need two lists because the sorting must still respect the multipassing order.
142  std::vector< ref<RenderToken> > mList;
143  std::vector< ref<RenderToken> > mListMP;
144  int mSize;
145  int mSizeMP;
146  };
147  //------------------------------------------------------------------------------
148  typedef std::map< float, ref<RenderQueue> > TRenderQueueMap;
149  //------------------------------------------------------------------------------
150 }
151 
152 #endif
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:161
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
const T * get() const
Definition: Object.hpp:128
std::vector< ref< RenderToken > > mListMP
bool isNull() const
Returns true if the AABB is null.
Definition: AABB.hpp:60
int size() const
Definition: RenderQueue.hpp:90
void sort(RenderQueueSorter *sorter, Camera *camera)
Definition: RenderQueue.hpp:95
virtual bool mightNeedZCameraDistance() const =0
Renderable * mRenderable
Definition: RenderToken.hpp:59
virtual bool confirmZCameraDistanceNeed(const RenderToken *) const =0
vec3 center() const
Returns the center of the AABB.
Definition: AABB.cpp:184
std::vector< ref< RenderToken > > mList
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Internally used by the rendering engine.
Definition: RenderToken.hpp:47
Visualization Library main namespace.
The base class for all the reference counted objects.
Definition: Object.hpp:158
const mat4 & worldMatrix() const
Returns the world matrix used for rendering.
Definition: Transform.hpp:168
RenderToken * newToken(bool multipass)
Definition: RenderQueue.hpp:61
The RenderQueue class collects a list of RenderToken objects to be sorted and rendered.
Definition: RenderQueue.hpp:45
RenderToken * at(int i)
Definition: RenderQueue.hpp:59
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
const RenderToken * at(int i) const
Definition: RenderQueue.hpp:57
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
std::map< float, ref< RenderQueue > > TRenderQueueMap
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
const AABB & boundingBox() const
Returns the bounding box of a Renderable without recomputing the bounds if dirty. ...
Definition: Renderable.hpp:151
The RenderQueueSorter class is the abstract base class of all the algorithms used to sort a set of Re...
#define VL_CHECK(expr)
Definition: checks.hpp:73