Carna Version 3.3.3
Loading...
Searching...
No Matches
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>
19#include <Carna/base/math.h>
22#include <vector>
23#include <algorithm>
24#include <climits>
25
30namespace Carna
31{
32
33namespace base
34{
35
36
37
38// ----------------------------------------------------------------------------------
39// RenderQueue
40// ----------------------------------------------------------------------------------
41
62template< typename RenderableCompare >
64{
65
67
68 std::vector< Renderable > renderables;
69 std::size_t nextRenderableIndex;
70
71public:
72
76 const unsigned int geometryType;
77
81 const unsigned int geometryTypeMask;
82
87 const static unsigned int EXACT_MATCH_GEOMETRY_TYPE_MASK;
88
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
150template< typename RenderableCompare >
151const unsigned int RenderQueue< RenderableCompare >::EXACT_MATCH_GEOMETRY_TYPE_MASK = std::numeric_limits< unsigned int >::max();
152
153
154template< typename RenderableCompare >
155RenderQueue< RenderableCompare >::RenderQueue( unsigned int geometryType, unsigned int geometryTypeMask )
156 : geometryType( geometryType )
157 , geometryTypeMask( geometryTypeMask )
158{
159}
160
161
162template< typename RenderableCompare >
163struct 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
175template< >
176struct RenderableSort< void >
177{
178 static void sort( std::vector< Renderable >& renderables, bool skipIfViewDependent )
179 {
180 }
181};
182
183
184template< typename RenderableCompare >
185void 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
209template< typename RenderableCompare >
211{
212 nextRenderableIndex = 0;
213}
214
215
216template< 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
234template< typename RenderableCompare >
236{
237 return nextRenderableIndex >= renderables.size();
238}
239
240
241template< typename RenderableCompare >
243{
244 CARNA_ASSERT( !isEmpty() );
245 return renderables[ nextRenderableIndex++ ];
246}
247
248
249template< typename RenderableCompare >
251{
252 CARNA_ASSERT( !isEmpty() );
253 return renderables.front();
254}
255
256
257template< 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::CarnaException, Carna::base::AssertionFailure.
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Defines Carna::base::Geometry.
Defines Carna::base::Node.
Defines Carna::base::Renderable.
Represents an association.
Definition Association.h:45
Defines scene graph leafs. Instances of this class represent visible geometry that can be rendered....
Definition Geometry.h:60
Defines the inner node of a scene graph. Implements a spatial scene element that is allowed to have c...
Definition Node.h:45
void visitChildren(bool recursively, const MutableVisitor &visit)
Invokes visit once on each child of this node recursively.
Gathers renderable geometry nodes from scene graph and provides those in a particular order,...
Definition RenderQueue.h:64
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 ...
const Renderable & last() const
References the last element of the queue. The referenced object stays alive until the queue is delete...
const unsigned int geometryTypeMask
Holds the mask that this queue uses for matching geometry nodes.
Definition RenderQueue.h:81
const Renderable & poll()
References the next element of the queue and moves ahead. The referenced object stays alive until the...
bool isEmpty() const
Tells whether this queue has reached it's end.
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
void build(const Node &root, const math::Matrix4f &viewTransform)
Rebuilds this queue by gathering matching geometry nodes from children of root recursively....
const Renderable & first() const
References the next element of the queue, but does not move ahead. The referenced object stays alive ...
void updateModelViewTransforms(const math::Matrix4f &viewTransform)
Recomputes the model-view transforms of all enqueued renderables. This only is neccessary in certain ...
const unsigned int geometryType
Holds the geometry type that this queue uses for matching geometry nodes.
Definition RenderQueue.h:76
void rewind()
Rewinds this queue. This is an operation in contrast to build, so prefer it whenever possible....
Represents a Geometry object that has been queued into a RenderQueue. The object's model-view transfo...
Definition Renderable.h:46
Represents a spatial scene element. It's location is determined relatively to another spatial that is...
Definition Spatial.h:45
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > Matrix4f
Defines matrix.
Definition math.h:193
#define NON_COPYABLE
Features class it is placed in as non-copyable.