370160773436198e58733600e59d9970b4c1cc91
[occt.git] / src / OpenGl / OpenGl_LayerList.hxx
1 // Created on: 2012-02-02
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_LayerList_Header
17 #define _OpenGl_LayerList_Header
18
19 #include <OpenGl_Layer.hxx>
20 #include <OpenGl_LayerFilter.hxx>
21
22 #include <NCollection_Array1.hxx>
23 #include <NCollection_Handle.hxx>
24 #include <NCollection_Sequence.hxx>
25 #include <NCollection_DataMap.hxx>
26
27 class OpenGl_Structure;
28
29 typedef NCollection_Sequence<Handle(OpenGl_Layer)> OpenGl_SequenceOfLayers;
30 typedef NCollection_DataMap<int, int> OpenGl_LayerSeqIds;
31
32 //! Class defining the list of layers.
33 class OpenGl_LayerList
34 {
35 public:
36
37   //! Constructor
38   OpenGl_LayerList (const Standard_Integer theNbPriorities);
39
40   //! Destructor
41   virtual ~OpenGl_LayerList();
42
43   //! Method returns the number of available priorities
44   Standard_Integer NbPriorities() const { return myNbPriorities; }
45
46   //! Number of displayed structures
47   Standard_Integer NbStructures() const { return myNbStructures; }
48
49   //! Return number of structures within immediate layers
50   Standard_Integer NbImmediateStructures() const { return myImmediateNbStructures; }
51
52   //! Insert a new layer with id.
53   void AddLayer (const Graphic3d_ZLayerId theLayerId);
54
55   //! Remove layer by its id.
56   void RemoveLayer (const Graphic3d_ZLayerId theLayerId);
57
58   //! Add structure to list with given priority. The structure will be inserted
59   //! to specified layer. If the layer isn't found, the structure will be put
60   //! to default bottom-level layer.
61   void AddStructure (const OpenGl_Structure*  theStruct,
62                      const Graphic3d_ZLayerId theLayerId,
63                      const Standard_Integer   thePriority,
64                      Standard_Boolean        isForChangePriority = Standard_False);
65
66   //! Remove structure from structure list and return its previous priority
67   void RemoveStructure (const OpenGl_Structure* theStructure);
68
69   //! Change structure z layer
70   //! If the new layer is not presented, the structure will be displayed
71   //! in default z layer
72   void ChangeLayer (const OpenGl_Structure*  theStructure,
73                     const Graphic3d_ZLayerId theOldLayerId,
74                     const Graphic3d_ZLayerId theNewLayerId);
75
76   //! Changes structure priority within its ZLayer
77   void ChangePriority (const OpenGl_Structure*  theStructure,
78                        const Graphic3d_ZLayerId theLayerId,
79                        const Standard_Integer   theNewPriority);
80
81   //! Returns reference to the layer with given ID.
82   OpenGl_Layer& Layer (const Graphic3d_ZLayerId theLayerId);
83
84   //! Returns reference to the layer with given ID.
85   const OpenGl_Layer& Layer (const Graphic3d_ZLayerId theLayerId) const;
86
87   //! Assign new settings to the layer.
88   void SetLayerSettings (const Graphic3d_ZLayerId        theLayerId,
89                          const Graphic3d_ZLayerSettings& theSettings);
90
91   //! Update culling state - should be called before rendering.
92   void UpdateCulling (const Handle(OpenGl_Workspace)& theWorkspace,
93                       const Standard_Boolean theToDrawImmediate);
94
95   //! Render this element
96   void Render (const Handle(OpenGl_Workspace)& theWorkspace,
97                const Standard_Boolean          theToDrawImmediate,
98                const OpenGl_LayerFilter        theLayersToProcess,
99                OpenGl_FrameBuffer*             theReadDrawFbo,
100                OpenGl_FrameBuffer*             theOitAccumFbo) const;
101
102   //! Returns the set of OpenGL Z-layers.
103   const OpenGl_SequenceOfLayers& Layers() const { return myLayers; }
104
105   //! Returns the map of Z-layer IDs to indexes.
106   const OpenGl_LayerSeqIds& LayerIDs() const { return myLayerIds; }
107
108   //! Marks BVH tree for given priority list as dirty and
109   //! marks primitive set for rebuild.
110   void InvalidateBVHData (const Graphic3d_ZLayerId theLayerId);
111
112   //! Returns structure modification state (for ray-tracing).
113   Standard_Size ModificationStateOfRaytracable() const { return myModifStateOfRaytraceable; }
114
115   //! Returns BVH tree builder for frustom culling.
116   const Handle(Select3D_BVHBuilder3d)& FrustumCullingBVHBuilder() const { return myBVHBuilder; }
117
118   //! Assigns BVH tree builder for frustom culling.
119   void SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder);
120
121 protected:
122
123   //! Stack of references to existing layers of predefined maximum size.
124   class OpenGl_LayerStack
125   {
126   public:
127     typedef NCollection_Array1<const OpenGl_Layer*>::iterator iterator;
128
129     //! Reallocate internal buffer of the stack.
130     void Allocate (Standard_Integer theSize)
131     {
132       if (theSize > 0)
133       {
134         myStackSpace = new NCollection_Array1<const OpenGl_Layer*> (1, theSize);
135         myStackSpace->Init (NULL);
136         myBackPtr    = myStackSpace->begin();
137       }
138       else
139       {
140         myStackSpace.Nullify();
141         myBackPtr = iterator();
142       }
143     }
144
145     //! Clear stack.
146     void Clear()
147     {
148       if (!myStackSpace.IsNull())
149       {
150         myStackSpace->Init (NULL);
151         myBackPtr = myStackSpace->begin();
152       }
153     }
154
155     //! Push a new layer reference to the stack.
156     void Push (const OpenGl_Layer* theLayer) { (*myBackPtr++) = theLayer; }
157
158     //! Returns iterator to the origin of the stack.
159     iterator Origin() const { return myStackSpace.IsNull() ? iterator() : myStackSpace->begin(); }
160
161     //! Returns iterator to the back of the stack (after last item added).
162     iterator Back() const { return myBackPtr; }
163
164     //! Returns true if nothing has been pushed into the stack.
165     Standard_Boolean IsEmpty() const { return Back() == Origin(); }
166
167   private:
168
169     NCollection_Handle<NCollection_Array1<const OpenGl_Layer*> > myStackSpace;
170     iterator                                                     myBackPtr;
171   };
172
173   //! Render transparent objects using blending operator.
174   //! Additional accumulation framebuffer is used for blended order-independent
175   //! transparency algorithm. It should support floating-point color components
176   //! and share depth with main reading/drawing framebuffer.
177   //! @param theWorkspace [in] the currently used workspace for rendering.
178   //! @param theLayerIter [in/out] the current iterator of transparent layers to process.
179   //! @param theGlobalSettings [in] the set of global settings used for rendering.
180   //! @param theReadDrawFbo [in] the framebuffer for reading depth and writing final color.
181   //! @param theOitAccumFbo [in] the framebuffer for accumulating color and coverage for OIT process.
182   void renderTransparent (const Handle(OpenGl_Workspace)&   theWorkspace,
183                           OpenGl_LayerStack::iterator&      theLayerIter,
184                           const OpenGl_GlobalLayerSettings& theGlobalSettings,
185                           OpenGl_FrameBuffer*               theReadDrawFbo,
186                           OpenGl_FrameBuffer*               theOitAccumFbo) const;
187
188 protected:
189
190   // number of structures temporary put to default layer
191   OpenGl_SequenceOfLayers myLayers;
192   OpenGl_LayerSeqIds      myLayerIds;
193   Handle(Select3D_BVHBuilder3d) myBVHBuilder;      //!< BVH tree builder for frustom culling
194   Standard_Integer        myDefaultLayerIndex;     //!< index of Graphic3d_ZLayerId_Default layer in myLayers sequence
195
196   Standard_Integer        myNbPriorities;
197   Standard_Integer        myNbStructures;
198   Standard_Integer        myImmediateNbStructures; //!< number of structures within immediate layers
199
200   mutable Standard_Size   myModifStateOfRaytraceable;
201
202   //! Collection of references to layers with transparency gathered during rendering pass.
203   mutable OpenGl_LayerStack myTransparentToProcess;
204
205 public:
206
207   DEFINE_STANDARD_ALLOC
208
209 };
210
211 #endif //_OpenGl_LayerList_Header