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