1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGRENDERPARTITION_H_
40 #define _OSGRENDERPARTITION_H_
45 //---------------------------------------------------------------------------
47 //---------------------------------------------------------------------------
49 #include <boost/function.hpp>
51 #include "OSGRenderPartitionBase.h"
52 #include "OSGRenderActionFwd.h"
53 #include "OSGDrawEnv.h"
54 #include "OSGStatIntElem.h"
55 #include "OSGFrustumVolume.h"
57 #include "OSGViewport.h"
58 #include "OSGBackground.h"
59 #include "OSGForegroundFields.h"
60 #include "OSGMaterial.h"
61 #include "OSGNamedPool.h"
62 #include "OSGStateOverride.h"
68 //---------------------------------------------------------------------------
70 //---------------------------------------------------------------------------
73 class FrameBufferObject
;
76 //---------------------------------------------------------------------------
78 //---------------------------------------------------------------------------
80 //---------------------------------------------------------------------------
82 //---------------------------------------------------------------------------
84 /*! \brief RenderPartition is the core class for keeping track of the actions
85 necessary to draw a scene.
87 \ingroup GrpSystemRenderAction
88 \ingroup GrpLibOSGSystem
91 class OSG_SYSTEM_DLLMAPPING RenderPartition
: public RenderPartitionBase
95 //-----------------------------------------------------------------------
97 //-----------------------------------------------------------------------
99 /*! Number of nodes tested for culling */
100 static StatElemDesc
<StatIntElem
> statCullTestedNodes
;
102 /*! Number of nodes culled */
103 static StatElemDesc
<StatIntElem
> statCulledNodes
;
105 //-----------------------------------------------------------------------
107 //-----------------------------------------------------------------------
109 //-----------------------------------------------------------------------
111 //-----------------------------------------------------------------------
113 /*! MatrixStore is stored internally to keep track of the current matrix.
114 The UInt32 is used to quickly identify the same matrix without having
115 to compare actual matrix elements.
117 #ifndef OSG_ENABLE_DOUBLE_MATRIX_STACK
118 typedef std::pair
<UInt32
, Matrix
> MatrixStore
;
119 typedef Matrix MatrixT
;
121 typedef std::pair
<UInt32
, Matrix4d
> MatrixStore
;
122 typedef Matrix4d MatrixT
;
125 /*! DrawFunctor is the signature for the methods that are called
126 from within the draw tree
128 typedef DrawEnv::DrawFunctor DrawFunctor
;
130 /*! SimpleDrawCallback is the type of functions that are used for simple
131 partitions (i.e. the ones that only call one function and do not draw
134 typedef boost::function
<void (DrawEnv
*)> SimpleDrawCallback
;
136 /*! BuildKeyMap is used to index from the material's sortKey to the
137 StateSorter that keeps the tree for that sort key
139 typedef std::map
<Int32
, TreeBuilderBase
*> BuildKeyMap
;
140 typedef BuildKeyMap::iterator BuildKeyMapIt
;
141 typedef BuildKeyMap::const_iterator BuildKeyMapConstIt
;
143 /*! VisibilityStack keeps track of the planes of the current frustum that
144 need to be tested. If a bounding volume is inside all
145 planes, none need to be tested for its children.
147 typedef std::vector
<FrustumVolume::PlaneSet
> VisibilityStack
;
149 /*! OverrideStack keeps track of active StateOverrides during traversal */
150 typedef std::stack
<StateOverride
*> OverrideStack
;
152 /*! MatrixStack keeps track of matrices during traversal */
153 typedef std::vector
<MatrixStore
> MatrixStack
;
155 typedef std::vector
<RenderPartition
*> GroupStore
;
157 typedef std::vector
<RenderFunctor
> RenderCallbackStore
;
159 //-----------------------------------------------------------------------
161 //-----------------------------------------------------------------------
163 //-----------------------------------------------------------------------
165 //-----------------------------------------------------------------------
167 //-----------------------------------------------------------------------
168 // instance functions
169 //-----------------------------------------------------------------------
171 /*------------------------- your_category -------------------------------*/
173 void setAction (RenderAction
*pAction
);
175 void setNodePool (RenderTreeNodePool
*pNodePool
);
176 void setStatePool (StateOverridePool
*pStatePool
);
177 void setTreeBuilderPool(TreeBuilderPool
*pTreeBuilderPool
);
178 void setStatCollector (StatCollectorP pStatCollector
);
180 /*------------------------- your_operators ------------------------------*/
182 StatCollectorP
getStatCollector(void);
184 /*------------------------- your_operators ------------------------------*/
186 void setWindow (Window
*pWindow
);
187 Window
*getWindow (void );
189 void setBackground (Background
*pBackground
);
191 void pushToForegrounds(Foreground
*pForeground
);
192 void clearForegrounds (void );
194 /*------------------------- your_operators ------------------------------*/
197 void setupProjection (const Matrix4f
&projection
,
198 const Matrix4f
&translation
);
200 const Matrix4f
&getFullProjection ( void );
201 const Matrix4f
&getProjection ( void );
202 const Matrix4f
&getProjectionTrans ( void );
204 const Matrix4f
&getVPFullProjection ( void );
205 const Matrix4f
&getVPProjection ( void );
206 const Matrix4f
&getVPProjectionTrans( void );
208 void setupViewing (const Matrix4f
&matrix
);
210 const Matrix4f
&getViewing ( void );
211 const Matrix4f
&getCameraToWorld ( void );
212 const Matrix4f
&getVPViewing ( void );
213 const Matrix4f
&getVPCameraToWorld ( void );
215 /*------------------------- your_operators ------------------------------*/
217 void addPartition(RenderPartition
*pPart
);
219 /*------------------------- your_operators ------------------------------*/
221 DrawEnv
&getDrawEnv(void);
223 UInt32
getKeyGen (void) const;
225 /*------------------------- your_operators ------------------------------*/
227 void setNear(Real32 camNear
);
228 void setFar (Real32 camFar
);
230 Real32
getNear(void );
231 Real32
getFar (void );
233 /*------------------------- your_operators ------------------------------*/
235 void calcFrustum (void );
237 void setViewportDimension (Int32 iPixelLeft
,
243 void calcViewportDimension(Real32 rLeft
,
249 UInt16 iTargetHeight
);
251 Int32
getViewportWidth (void );
252 Int32
getViewportHeight (void );
255 /*------------------------- your_operators ------------------------------*/
257 void setSetupMode (UInt32 uiSetupMode
);
258 void addSetupModeBit (UInt32 uiSetupModeBit
);
259 void subSetupMode (UInt32 uiSetupModeBit
);
261 void initFrom (RenderPartition
*pSource
,
262 RenderPartition
*pInitial
,
263 Int32 uiCopyOnPush
);
265 void initVPMatricesFromCamera(void );
267 void setVPCameraMatrices (const Matrix
&mFullprojection
,
268 const Matrix
&mProjection
,
269 const Matrix
&mProjectionTrans
,
270 const Matrix
&mViewing
,
271 const Matrix
&mToWorld
,
272 const Matrix
&mWorldToScreen
);
274 /*------------------------- your_operators ------------------------------*/
279 /*------------------------- assignment ----------------------------------*/
281 template<class MatrixType
>
282 void pushMatrix (const MatrixType
&matrix
);
283 void popMatrix ( void );
285 const Matrix
&topMatrix ( void );
287 const Matrix
&getModelMatrix ( void ) const;
289 const MatrixT
&getModelViewMatrix( void ) const;
291 const MatrixStore
&getMatrixStackTop ( void ) const;
293 /*------------------------- comparison ----------------------------------*/
295 void dropFunctor(DrawFunctor
&func
,
298 bool bIgnoreOverrids
= false);
300 void dropFunctor(SimpleDrawCallback
&oFunc
);
302 /*------------------------- comparison ----------------------------------*/
304 void pushState (void );
305 void popState (void );
307 void addOverride (UInt32 uiSlot
,
310 void subOverride (UInt32 uiSlot
,
313 const StateOverride
*getCurrentOverrides(void ) const;
315 /*-------------------------- comparison ---------------------------------*/
317 Int32
allocateLightIndex(void);
318 void releaseLightIndex (void);
319 UInt32
getLightState (void) const;
321 /*-------------------------- comparison ---------------------------------*/
323 void setKeyGen(UInt32 uiKeyGen
);
325 /*-------------------------- comparison ---------------------------------*/
327 void overrideMaterial(Material
* pMaterial
,
328 Node
* const pNode
);
330 Material
*getMaterial (void );
332 /*-------------------------- comparison ---------------------------------*/
334 void setRenderTarget(FrameBufferObject
*pTarget
);
335 FrameBufferObject
*getRenderTarget(void );
337 void setDrawBuffer (GLenum eBuffer
);
339 /*-------------------------- comparison ---------------------------------*/
341 // test a single node
342 bool isVisible (Node
* pNode
);
345 bool pushVisibility(Node
* const pNode
);
346 void popVisibility (void );
348 /*-------------------------- comparison ---------------------------------*/
350 // control activation of frustum culling
351 bool getFrustumCulling(void ) const;
352 void setFrustumCulling(bool val
= true );
354 // control drawing of checked volumes
355 bool getVolumeDrawing (void ) const;
356 void setVolumeDrawing (bool val
= false );
359 const FrustumVolume
&getFrustum (void ) const;
360 void setFrustum (FrustumVolume
&frust
);
362 /*-------------------------- comparison ---------------------------------*/
364 /*-------------------------- comparison ---------------------------------*/
366 /*-------------------------- comparison ---------------------------------*/
371 /*-------------------------- comparison ---------------------------------*/
373 void addPreRenderCallback (const RenderFunctor
&oCallback
);
374 void addPostRenderCallback(const RenderFunctor
&oCallback
);
376 /*-------------------------- comparison ---------------------------------*/
379 void setDebugString(std::string szDebugString
);
384 //-----------------------------------------------------------------------
386 //-----------------------------------------------------------------------
388 //-----------------------------------------------------------------------
390 //-----------------------------------------------------------------------
392 typedef RenderPartitionBase Inherited
;
394 //-----------------------------------------------------------------------
396 //-----------------------------------------------------------------------
402 GroupStore _vGroupStore
;
406 SimpleDrawCallback _oSimpleDrawCallback
;
408 Background
*_pBackground
;
410 std::vector
<Foreground
*> _vpForegrounds
;
415 mutable bool _modelMatrixValid
;
417 MatrixStore _modelViewMatrix
;
418 mutable Matrix _modelMatrix
;
420 MatrixStack _modelViewMatrixStack
;
424 RenderTreeNodePool
*_pNodePool
;
426 BuildKeyMap _mMatTrees
;
427 BuildKeyMap _mTransMatTrees
;
431 UInt32 _uiActiveMatrix
;
435 StateOverridePool
*_pStatePool
;
437 OverrideStack _sStateOverrides
;
439 TreeBuilderPool
*_pTreeBuilderPool
;
443 Int32 _iNextLightIndex
;
444 UInt32 _uiLightState
;
450 Material
*_pMaterial
;
451 Node
*_pMaterialNode
;
452 bool _addedStateOverride
;
455 FrameBufferObject
*_pRenderTarget
;
461 VisibilityStack _visibilityStack
;
463 bool _bFrustumCulling
;
464 bool _bVolumeDrawing
;
466 FrustumVolume _oFrustum
;
468 RenderCallbackStore _vPreRenderCallbacks
;
469 RenderCallbackStore _vPostRenderCallbacks
;
476 std::string _szDebugString
;
479 //-----------------------------------------------------------------------
481 //-----------------------------------------------------------------------
483 //-----------------------------------------------------------------------
484 // instance variables
485 //-----------------------------------------------------------------------
487 //-----------------------------------------------------------------------
488 // instance functions
489 //-----------------------------------------------------------------------
491 RenderPartition(Mode eMode
= StateSorting
);
493 virtual ~RenderPartition(void);
495 /*-------------------------- comparison ---------------------------------*/
497 void reset (Mode eMode
= StateSorting
);
499 void updateModelMatrix(void ) const;
501 void setNode (Node
*pNode
);
502 Node
*getNode (void );
504 /*-------------------------- comparison ---------------------------------*/
506 bool pushShaderState(State
*pState
);
509 void setupExecution (bool bUpdateGlobalViewport
= false);
510 void doExecution (bool bRestoreViewport
= false);
512 /*-------------------------- comparison ---------------------------------*/
514 virtual void execute(HardwareContext
*pContext
, DrawEnv
*pEnv
);
515 virtual void dump (UInt32 uiIndent
);
519 //-----------------------------------------------------------------------
521 //-----------------------------------------------------------------------
523 //-----------------------------------------------------------------------
525 //-----------------------------------------------------------------------
527 //-----------------------------------------------------------------------
529 //-----------------------------------------------------------------------
531 friend class SimplePool
<RenderPartition
,
533 MemObjRefCountPolicy
,
536 friend class RenderAction
;
538 //-----------------------------------------------------------------------
540 //-----------------------------------------------------------------------
542 //-----------------------------------------------------------------------
544 //-----------------------------------------------------------------------
546 //-----------------------------------------------------------------------
548 //-----------------------------------------------------------------------
550 //-----------------------------------------------------------------------
551 // instance variables
552 //-----------------------------------------------------------------------
554 //-----------------------------------------------------------------------
555 // instance functions
556 //-----------------------------------------------------------------------
558 /*!\brief prohibit default function (move to 'public' if needed) */
559 RenderPartition(const RenderPartition
&source
);
560 void operator =(const RenderPartition
&source
);
563 //---------------------------------------------------------------------------
565 //---------------------------------------------------------------------------
569 #include "OSGRenderPartition.inl"
571 #endif /* _OSGRENDERPARTITION_H_ */