fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Action / RenderAction / OSGRenderPartition.h
blob83a7e7b5516330087f61a1f8e98a0d96ab009f82
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGRENDERPARTITION_H_
40 #define _OSGRENDERPARTITION_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 //---------------------------------------------------------------------------
46 // Includes
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"
64 #include <stack>
66 OSG_BEGIN_NAMESPACE
68 //---------------------------------------------------------------------------
69 // Forward References
70 //---------------------------------------------------------------------------
72 class State;
73 class FrameBufferObject;
74 class RenderAction;
76 //---------------------------------------------------------------------------
77 // Types
78 //---------------------------------------------------------------------------
80 //---------------------------------------------------------------------------
81 // Class
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
93 public:
95 //-----------------------------------------------------------------------
96 // constants
97 //-----------------------------------------------------------------------
99 /*! Number of nodes tested for culling */
100 static StatElemDesc<StatIntElem> statCullTestedNodes;
102 /*! Number of nodes culled */
103 static StatElemDesc<StatIntElem> statCulledNodes;
105 //-----------------------------------------------------------------------
106 // enums
107 //-----------------------------------------------------------------------
109 //-----------------------------------------------------------------------
110 // types
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;
120 #else
121 typedef std::pair<UInt32, Matrix4d> MatrixStore;
122 typedef Matrix4d MatrixT;
123 #endif
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
132 Nodes
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 //-----------------------------------------------------------------------
160 // class functions
161 //-----------------------------------------------------------------------
163 //-----------------------------------------------------------------------
164 // class functions
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,
238 Int32 iPixelBottom,
239 Int32 iPixelRight,
240 Int32 iPixelTop,
241 bool bFull );
243 void calcViewportDimension(Real32 rLeft,
244 Real32 rBottom,
245 Real32 rRight,
246 Real32 rTop,
248 UInt16 iTargetWidth,
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 ------------------------------*/
276 void execute(void);
277 void disable(void);
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,
296 State *pState,
297 Int32 uiBuildKey,
298 bool bIgnoreOverrids = false);
300 void dropFunctor(SimpleDrawCallback &oFunc);
302 /*------------------------- comparison ----------------------------------*/
304 void pushState (void );
305 void popState (void );
307 void addOverride (UInt32 uiSlot,
308 StateChunk *pChunk);
310 void subOverride (UInt32 uiSlot,
311 StateChunk *pChunk);
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);
344 // visibility levels
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 );
358 // control frustum
359 const FrustumVolume &getFrustum (void ) const;
360 void setFrustum (FrustumVolume &frust);
362 /*-------------------------- comparison ---------------------------------*/
364 /*-------------------------- comparison ---------------------------------*/
366 /*-------------------------- comparison ---------------------------------*/
368 void init(void);
369 void exit(void);
371 /*-------------------------- comparison ---------------------------------*/
373 void addPreRenderCallback (const RenderFunctor &oCallback);
374 void addPostRenderCallback(const RenderFunctor &oCallback);
376 /*-------------------------- comparison ---------------------------------*/
378 #ifdef OSG_DEBUG
379 void setDebugString(std::string szDebugString);
380 #endif
382 protected:
384 //-----------------------------------------------------------------------
385 // enums
386 //-----------------------------------------------------------------------
388 //-----------------------------------------------------------------------
389 // types
390 //-----------------------------------------------------------------------
392 typedef RenderPartitionBase Inherited;
394 //-----------------------------------------------------------------------
395 // class variables
396 //-----------------------------------------------------------------------
398 Mode _eMode;
399 UInt32 _uiSetupMode;
400 bool _bDone;
402 GroupStore _vGroupStore;
404 DrawEnv _oDrawEnv;
406 SimpleDrawCallback _oSimpleDrawCallback;
408 Background *_pBackground;
410 std::vector<Foreground*> _vpForegrounds;
412 // Transform
414 UInt32 _uiMatrixId;
415 mutable bool _modelMatrixValid;
417 MatrixStore _modelViewMatrix;
418 mutable Matrix _modelMatrix;
420 MatrixStack _modelViewMatrixStack;
422 // Roots
424 RenderTreeNodePool *_pNodePool;
426 BuildKeyMap _mMatTrees;
427 BuildKeyMap _mTransMatTrees;
429 // Active Elements
431 UInt32 _uiActiveMatrix;
433 // State
435 StateOverridePool *_pStatePool;
437 OverrideStack _sStateOverrides;
439 TreeBuilderPool *_pTreeBuilderPool;
441 // State
443 Int32 _iNextLightIndex;
444 UInt32 _uiLightState;
446 UInt32 _uiKeyGen;
448 // Material Override
450 Material *_pMaterial;
451 Node *_pMaterialNode;
452 bool _addedStateOverride;
453 // Target
455 FrameBufferObject *_pRenderTarget;
456 GLenum _eDrawBuffer;
459 // Culling
461 VisibilityStack _visibilityStack;
463 bool _bFrustumCulling;
464 bool _bVolumeDrawing;
465 bool _bAutoFrustum;
466 FrustumVolume _oFrustum;
468 RenderCallbackStore _vPreRenderCallbacks;
469 RenderCallbackStore _vPostRenderCallbacks;
471 Node *_pNode;
473 #ifdef OSG_DEBUG
474 // Debug
476 std::string _szDebugString;
477 #endif
479 //-----------------------------------------------------------------------
480 // class functions
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 );
508 // Roots
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);
517 private:
519 //-----------------------------------------------------------------------
520 // enums
521 //-----------------------------------------------------------------------
523 //-----------------------------------------------------------------------
524 // types
525 //-----------------------------------------------------------------------
527 //-----------------------------------------------------------------------
528 // friend classes
529 //-----------------------------------------------------------------------
531 friend class SimplePool<RenderPartition,
532 PoolDefaultTag,
533 MemObjRefCountPolicy,
534 NoLockPolicy >;
536 friend class RenderAction;
538 //-----------------------------------------------------------------------
539 // friend functions
540 //-----------------------------------------------------------------------
542 //-----------------------------------------------------------------------
543 // class variables
544 //-----------------------------------------------------------------------
546 //-----------------------------------------------------------------------
547 // class functions
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 //---------------------------------------------------------------------------
564 // Exported Types
565 //---------------------------------------------------------------------------
567 OSG_END_NAMESPACE
569 #include "OSGRenderPartition.inl"
571 #endif /* _OSGRENDERPARTITION_H_ */