LibCarna Version 3.4.0
Loading...
Searching...
No Matches
RenderQueue.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef RENDERQUEUE_H_6014714286
16#define RENDERQUEUE_H_6014714286
17
18#include <LibCarna/LibCarna.hpp>
25#include <vector>
26#include <algorithm>
27#include <climits>
28
34namespace LibCarna
35{
36
37namespace base
38{
39
40
41
42// ----------------------------------------------------------------------------------
43// RenderQueue
44// ----------------------------------------------------------------------------------
45
65template< typename RenderableCompare >
67{
68
70
71 std::vector< Renderable > renderables;
72 std::size_t nextRenderableIndex;
73
74public:
75
79 const unsigned int geometryType;
80
84 const unsigned int geometryTypeMask;
85
90 const static unsigned int EXACT_MATCH_GEOMETRY_TYPE_MASK;
91
98
104 void build( const Node& root, const math::Matrix4f& viewTransform );
105
111 void rewind();
112
119 void updateModelViewTransforms( const math::Matrix4f& viewTransform );
120
124 bool isEmpty() const;
125
132 const Renderable& poll();
133
140 const Renderable& first() const;
141
148 const Renderable& last() const;
149
150}; // RenderQueue
151
152
153template< typename RenderableCompare >
154const unsigned int RenderQueue< RenderableCompare >::EXACT_MATCH_GEOMETRY_TYPE_MASK = std::numeric_limits< unsigned int >::max();
155
156
157template< typename RenderableCompare >
158RenderQueue< RenderableCompare >::RenderQueue( unsigned int geometryType, unsigned int geometryTypeMask )
159 : geometryType( geometryType )
160 , geometryTypeMask( geometryTypeMask )
161{
162}
163
164
165template< typename RenderableCompare >
166struct RenderableSort
167{
168 static void sort( std::vector< Renderable >& renderables, bool skipIfViewDependent )
169 {
170 if( renderables.size() >= 2 && ( RenderableCompare::isViewDependent || !skipIfViewDependent ) )
171 {
172 std::sort( renderables.begin(), renderables.end(), RenderableCompare() );
173 }
174 }
175};
176
177
178template< >
179struct RenderableSort< void >
180{
181 static void sort( std::vector< Renderable >& renderables, bool skipIfViewDependent )
182 {
183 }
184};
185
186
187template< typename RenderableCompare >
188void RenderQueue< RenderableCompare >::build( const Node& root, const math::Matrix4f& viewTransform )
189{
190 renderables.clear();
191 nextRenderableIndex = 0;
192
193 /* Collect all geometries.
194 */
195 root.visitChildren( true, [&]( const Spatial& spatial )
196 {
197 const Geometry* const geom = dynamic_cast< const Geometry* >( &spatial );
198 if( geom != nullptr && ( geom->geometryType & geometryTypeMask ) == geometryType )
199 {
200 const math::Matrix4f modelViewTransform = viewTransform * geom->worldTransform();
201 renderables.push_back( Renderable( *geom, modelViewTransform ) );
202 }
203 }
204 );
205
206 /* Order geometries as required. Do not skip anything.
207 */
208 RenderableSort< RenderableCompare >::sort( renderables, false );
209}
210
211
212template< typename RenderableCompare >
214{
215 nextRenderableIndex = 0;
216}
217
218
219template< typename RenderableCompare >
221{
222 /* Recompute the model-view transforms.
223 */
224 std::for_each( renderables.begin(), renderables.end(),
225 [&viewTransform]( Renderable& r )
226 {
227 r.setModelViewTransform( viewTransform * r.geometry().worldTransform() );
228 }
229 );
230
231 /* Order geometries as required. Skip if the order is not view-dependent.
232 */
233 RenderableSort< RenderableCompare >::sort( renderables, true );
234}
235
236
237template< typename RenderableCompare >
239{
240 return nextRenderableIndex >= renderables.size();
241}
242
243
244template< typename RenderableCompare >
246{
247 LIBCARNA_ASSERT( !isEmpty() );
248 return renderables[ nextRenderableIndex++ ];
249}
250
251
252template< typename RenderableCompare >
254{
255 LIBCARNA_ASSERT( !isEmpty() );
256 return renderables.front();
257}
258
259
260template< typename RenderableCompare >
262{
263 LIBCARNA_ASSERT( !isEmpty() );
264 return renderables.back();
265}
266
267
268
269} // namespace LibCarna :: base
270
271} // namespace LibCarna
272
273#endif // RENDERQUEUE_H_6014714286
Defines LibCarna::base::Geometry.
Defines LibCarna::base::LibCarnaException and LibCarna::base::AssertionFailure.
#define LIBCARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Contains forward-declarations.
Defines LibCarna::base::Node.
Defines LibCarna::base::Renderable.
Represents an association.
Defines scene graph leafs. Instances of this class represent visible geometry that can be rendered....
Definition Geometry.hpp:63
Defines the inner node of a scene graph. Implements a spatial scene element that is allowed to have c...
Definition Node.hpp:48
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,...
const Renderable & poll()
References the next element of the queue and moves ahead. The referenced object stays alive until the...
const unsigned int geometryTypeMask
Holds the mask that this queue uses for matching geometry nodes.
const Renderable & last() const
References the last element of the queue. The referenced object stays alive until the queue is delete...
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 ...
void rewind()
Rewinds this queue. This is an operation in contrast to build, so prefer it whenever possible....
const unsigned int geometryType
Holds the geometry type that this queue uses for matching geometry nodes.
void updateModelViewTransforms(const math::Matrix4f &viewTransform)
Recomputes the model-view transforms of all enqueued renderables. This only is neccessary in certain ...
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 ...
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 ...
bool isEmpty() const
Tells whether this queue has reached it's end.
Represents a Geometry object that has been queued into a RenderQueue. The object's model-view transfo...
Represents a spatial scene element. It's location is determined relatively to another spatial that is...
Definition Spatial.hpp:48
Defines LibCarna::base::math namespace and LIBCARNA_FOR_VECTOR3UI.
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > Matrix4f
Defines matrix.
Definition math.hpp:197
Defines LibCarna::base::noncopyable and NON_COPYABLE.
#define NON_COPYABLE
Marks the class that it is placed in as non-copyable.