Carna  Version 3.0.1
RenderQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef RENDERQUEUE_H_6014714286
13 #define RENDERQUEUE_H_6014714286
14 
15 #include <Carna/Carna.h>
16 #include <Carna/base/Node.h>
17 #include <Carna/base/Geometry.h>
18 #include <Carna/base/Renderable.h>
19 #include <Carna/base/math.h>
21 #include <Carna/base/noncopyable.h>
22 #include <vector>
23 #include <algorithm>
24 #include <climits>
25 
30 namespace Carna
31 {
32 
33 namespace base
34 {
35 
36 
37 
38 // ----------------------------------------------------------------------------------
39 // RenderQueue
40 // ----------------------------------------------------------------------------------
41 
62 template< typename RenderableCompare >
64 {
65 
67 
68  std::vector< Renderable > renderables;
69  std::size_t nextRenderableIndex;
70 
71 public:
72 
76  const unsigned int geometryType;
77 
81  const unsigned int geometryTypeMask;
82 
87  const static unsigned int EXACT_MATCH_GEOMETRY_TYPE_MASK;
88 
94  RenderQueue( unsigned int geometryType, unsigned int geometryTypeMask = EXACT_MATCH_GEOMETRY_TYPE_MASK );
95 
101  void build( const Node& root, const math::Matrix4f& viewTransform );
102 
108  void rewind();
109 
116  void updateModelViewTransforms( const math::Matrix4f& viewTransform );
117 
121  bool isEmpty() const;
122 
129  const Renderable& poll();
130 
137  const Renderable& first() const;
138 
145  const Renderable& last() const;
146 
147 }; // RenderQueue
148 
149 
150 template< typename RenderableCompare >
151 const unsigned int RenderQueue< RenderableCompare >::EXACT_MATCH_GEOMETRY_TYPE_MASK = std::numeric_limits< unsigned int >::max();
152 
153 
154 template< typename RenderableCompare >
155 RenderQueue< RenderableCompare >::RenderQueue( unsigned int geometryType, unsigned int geometryTypeMask )
156  : geometryType( geometryType )
157  , geometryTypeMask( geometryTypeMask )
158 {
159 }
160 
161 
162 template< typename RenderableCompare >
163 struct RenderableSort
164 {
165  static void sort( std::vector< Renderable >& renderables, bool skipIfViewDependent )
166  {
167  if( renderables.size() >= 2 && ( RenderableCompare::isViewDependent || !skipIfViewDependent ) )
168  {
169  std::sort( renderables.begin(), renderables.end(), RenderableCompare() );
170  }
171  }
172 };
173 
174 
175 template< >
176 struct RenderableSort< void >
177 {
178  static void sort( std::vector< Renderable >& renderables, bool skipIfViewDependent )
179  {
180  }
181 };
182 
183 
184 template< typename RenderableCompare >
185 void RenderQueue< RenderableCompare >::build( const Node& root, const math::Matrix4f& viewTransform )
186 {
187  renderables.clear();
188  nextRenderableIndex = 0;
189 
190  /* Collect all geometries.
191  */
192  root.visitChildren( true, [&]( const Spatial& spatial )
193  {
194  const Geometry* const geom = dynamic_cast< const Geometry* >( &spatial );
195  if( geom != nullptr && ( geom->geometryType & geometryTypeMask ) == geometryType )
196  {
197  const math::Matrix4f modelViewTransform = viewTransform * geom->worldTransform();
198  renderables.push_back( Renderable( *geom, modelViewTransform ) );
199  }
200  }
201  );
202 
203  /* Order geometries as required. Do not skip anything.
204  */
205  RenderableSort< RenderableCompare >::sort( renderables, false );
206 }
207 
208 
209 template< typename RenderableCompare >
211 {
212  nextRenderableIndex = 0;
213 }
214 
215 
216 template< typename RenderableCompare >
218 {
219  /* Recompute the model-view transforms.
220  */
221  std::for_each( renderables.begin(), renderables.end(),
222  [&viewTransform]( Renderable& r )
223  {
224  r.setModelViewTransform( viewTransform * r.geometry().worldTransform() );
225  }
226  );
227 
228  /* Order geometries as required. Skip if the order is not view-dependent.
229  */
230  RenderableSort< RenderableCompare >::sort( renderables, true );
231 }
232 
233 
234 template< typename RenderableCompare >
236 {
237  return nextRenderableIndex >= renderables.size();
238 }
239 
240 
241 template< typename RenderableCompare >
243 {
244  CARNA_ASSERT( !isEmpty() );
245  return renderables[ nextRenderableIndex++ ];
246 }
247 
248 
249 template< typename RenderableCompare >
251 {
252  CARNA_ASSERT( !isEmpty() );
253  return renderables.front();
254 }
255 
256 
257 template< typename RenderableCompare >
259 {
260  CARNA_ASSERT( !isEmpty() );
261  return renderables.back();
262 }
263 
264 
265 
266 } // namespace Carna :: base
267 
268 } // namespace Carna
269 
270 #endif // RENDERQUEUE_H_6014714286
Defines Carna::base::Renderable.
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
void updateModelViewTransforms(const math::Matrix4f &viewTransform)
Recomputes the model-view transforms of all enqueued renderables. This only is neccessary in certain ...
Definition: RenderQueue.h:217
Defines scene graph leafs. Instances of this class represent visible geometry that can be rendered...
Definition: Geometry.h:59
Defines Carna::base::Node.
const Renderable & first() const
References the next element of the queue, but does not move ahead. The referenced object stays alive ...
Definition: RenderQueue.h:250
Gathers renderable geometry nodes from scene graph and provides those in a particular order...
Definition: RenderQueue.h:63
bool isEmpty() const
Tells whether this queue has reached it's end.
Definition: RenderQueue.h:235
const Renderable & last() const
References the last element of the queue. The referenced object stays alive until the queue is delete...
Definition: RenderQueue.h:258
Defines the inner node of a scene graph. Implements a spatial scene element that is allowed to have c...
Definition: Node.h:44
const unsigned int geometryType
Holds the geometry type of this geometry node.
Definition: Geometry.h:70
Represents a spatial scene element. It's location is determined relatively to another spatial that is...
Definition: Spatial.h:44
static const unsigned int EXACT_MATCH_GEOMETRY_TYPE_MASK
Holds the mask that makes this queue only accept such geometry nodes whose geometry type matches the ...
Definition: RenderQueue.h:87
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > Matrix4f
Defines matrix.
Definition: math.h:193
RenderQueue(unsigned int geometryType, unsigned int geometryTypeMask=EXACT_MATCH_GEOMETRY_TYPE_MASK)
Creates new instance that enqueues Geometry scene graph nodes if their geometry type AND-linked with ...
Definition: RenderQueue.h:155
const Renderable & poll()
References the next element of the queue and moves ahead. The referenced object stays alive until the...
Definition: RenderQueue.h:242
const unsigned int geometryTypeMask
Holds the mask that this queue uses for matching geometry nodes.
Definition: RenderQueue.h:81
void visitChildren(bool recursively, const MutableVisitor &visit)
Invokes visit once on each child of this node recursively.
Represents a Geometry object that has been queued into a RenderQueue. The object's model-view transfo...
Definition: Renderable.h:45
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
void build(const Node &root, const math::Matrix4f &viewTransform)
Rebuilds this queue by gathering matching geometry nodes from children of root recursively. The argument viewTransform is required for the computation of the model-view matrix.
Definition: RenderQueue.h:185
void rewind()
Rewinds this queue. This is an operation in contrast to build, so prefer it whenever possible...
Definition: RenderQueue.h:210
const math::Matrix4f & worldTransform() const
Tells the transformation to world space for this spatial that was last computed.
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
#define NON_COPYABLE
Features class it is placed in as non-copyable.
Definition: noncopyable.h:109
const unsigned int geometryType
Holds the geometry type that this queue uses for matching geometry nodes.
Definition: RenderQueue.h:76
Defines Carna::base::Geometry.