0030668: Visualization - revise adding ZLayer API
[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 //! 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 InsertLayerBefore (const Graphic3d_ZLayerId theNewLayerId,
54                           const Graphic3d_ZLayerSettings& theSettings,
55                           const Graphic3d_ZLayerId theLayerAfter);
56
57   //! Insert a new layer with id.
58   void InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
59                          const Graphic3d_ZLayerSettings& theSettings,
60                          const Graphic3d_ZLayerId theLayerBefore);
61
62   //! Remove layer by its id.
63   void RemoveLayer (const Graphic3d_ZLayerId theLayerId);
64
65   //! Add structure to list with given priority. The structure will be inserted
66   //! to specified layer. If the layer isn't found, the structure will be put
67   //! to default bottom-level layer.
68   void AddStructure (const OpenGl_Structure*  theStruct,
69                      const Graphic3d_ZLayerId theLayerId,
70                      const Standard_Integer   thePriority,
71                      Standard_Boolean        isForChangePriority = Standard_False);
72
73   //! Remove structure from structure list and return its previous priority
74   void RemoveStructure (const OpenGl_Structure* theStructure);
75
76   //! Change structure z layer
77   //! If the new layer is not presented, the structure will be displayed
78   //! in default z layer
79   void ChangeLayer (const OpenGl_Structure*  theStructure,
80                     const Graphic3d_ZLayerId theOldLayerId,
81                     const Graphic3d_ZLayerId theNewLayerId);
82
83   //! Changes structure priority within its ZLayer
84   void ChangePriority (const OpenGl_Structure*  theStructure,
85                        const Graphic3d_ZLayerId theLayerId,
86                        const Standard_Integer   theNewPriority);
87
88   //! Returns reference to the layer with given ID.
89   OpenGl_Layer& Layer (const Graphic3d_ZLayerId theLayerId) { return *myLayerIds.Find (theLayerId); }
90
91   //! Returns reference to the layer with given ID.
92   const OpenGl_Layer& Layer (const Graphic3d_ZLayerId theLayerId) const { return *myLayerIds.Find (theLayerId); }
93
94   //! Assign new settings to the layer.
95   void SetLayerSettings (const Graphic3d_ZLayerId        theLayerId,
96                          const Graphic3d_ZLayerSettings& theSettings);
97
98   //! Update culling state - should be called before rendering.
99   void UpdateCulling (const Handle(OpenGl_Workspace)& theWorkspace,
100                       const Standard_Boolean theToDrawImmediate);
101
102   //! Render this element
103   void Render (const Handle(OpenGl_Workspace)& theWorkspace,
104                const Standard_Boolean          theToDrawImmediate,
105                const OpenGl_LayerFilter        theLayersToProcess,
106                OpenGl_FrameBuffer*             theReadDrawFbo,
107                OpenGl_FrameBuffer*             theOitAccumFbo) const;
108
109   //! Returns the set of OpenGL Z-layers.
110   const NCollection_List<Handle(Graphic3d_Layer)>& Layers() const { return myLayers; }
111
112   //! Returns the map of Z-layer IDs to indexes.
113   const NCollection_DataMap<Graphic3d_ZLayerId, Handle(Graphic3d_Layer)>& LayerIDs() const { return myLayerIds; }
114
115   //! Marks BVH tree for given priority list as dirty and
116   //! marks primitive set for rebuild.
117   void InvalidateBVHData (const Graphic3d_ZLayerId theLayerId);
118
119   //! Returns structure modification state (for ray-tracing).
120   Standard_Size ModificationStateOfRaytracable() const { return myModifStateOfRaytraceable; }
121
122   //! Returns BVH tree builder for frustom culling.
123   const Handle(Select3D_BVHBuilder3d)& FrustumCullingBVHBuilder() const { return myBVHBuilder; }
124
125   //! Assigns BVH tree builder for frustom culling.
126   void SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder);
127
128 protected:
129
130   //! Stack of references to existing layers of predefined maximum size.
131   class OpenGl_LayerStack
132   {
133   public:
134     typedef NCollection_Array1<const Graphic3d_Layer*>::iterator iterator;
135
136     //! Reallocate internal buffer of the stack.
137     void Allocate (Standard_Integer theSize)
138     {
139       if (theSize > 0)
140       {
141         myStackSpace.Resize (1, theSize, false);
142         myStackSpace.Init (NULL);
143         myBackPtr = myStackSpace.begin();
144       }
145       else
146       {
147         NCollection_Array1<const Graphic3d_Layer*> aDummy;
148         myStackSpace.Move (aDummy);
149         myBackPtr = iterator();
150       }
151     }
152
153     //! Clear stack.
154     void Clear()
155     {
156       myStackSpace.Init (NULL);
157       myBackPtr = myStackSpace.begin();
158     }
159
160     //! Push a new layer reference to the stack.
161     void Push (const OpenGl_Layer* theLayer) { (*myBackPtr++) = theLayer; }
162
163     //! Returns iterator to the origin of the stack.
164     iterator Origin() const { return myStackSpace.IsEmpty() ? iterator() : myStackSpace.begin(); }
165
166     //! Returns iterator to the back of the stack (after last item added).
167     iterator Back() const { return myBackPtr; }
168
169     //! Returns true if nothing has been pushed into the stack.
170     Standard_Boolean IsEmpty() const { return Back() == Origin(); }
171
172   private:
173
174     NCollection_Array1<const OpenGl_Layer*> myStackSpace;
175     iterator                                myBackPtr;
176   };
177
178   //! Render transparent objects using blending operator.
179   //! Additional accumulation framebuffer is used for blended order-independent
180   //! transparency algorithm. It should support floating-point color components
181   //! and share depth with main reading/drawing framebuffer.
182   //! @param theWorkspace [in] the currently used workspace for rendering.
183   //! @param theLayerIter [in/out] the current iterator of transparent layers to process.
184   //! @param theGlobalSettings [in] the set of global settings used for rendering.
185   //! @param theReadDrawFbo [in] the framebuffer for reading depth and writing final color.
186   //! @param theOitAccumFbo [in] the framebuffer for accumulating color and coverage for OIT process.
187   void renderTransparent (const Handle(OpenGl_Workspace)&   theWorkspace,
188                           OpenGl_LayerStack::iterator&      theLayerIter,
189                           const OpenGl_GlobalLayerSettings& theGlobalSettings,
190                           OpenGl_FrameBuffer*               theReadDrawFbo,
191                           OpenGl_FrameBuffer*               theOitAccumFbo) const;
192
193   // Render structures within specified layer.
194   void renderLayer (const Handle(OpenGl_Workspace)& theWorkspace,
195                     const OpenGl_GlobalLayerSettings& theDefaultSettings,
196                     const Graphic3d_Layer& theLayer) const;
197
198 protected:
199
200   NCollection_List<Handle(Graphic3d_Layer)> myLayers;
201   NCollection_DataMap<Graphic3d_ZLayerId, Handle(Graphic3d_Layer)> myLayerIds;
202   Handle(Select3D_BVHBuilder3d) myBVHBuilder;      //!< BVH tree builder for frustom culling
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