0028760: Visualization, TKOpenGl - avoid excessive frustum culling traverse within...
[occt.git] / src / OpenGl / OpenGl_LayerList.hxx
CommitLineData
6aca4d39 1// Created on: 2012-02-02
b311480e 2// Created by: Anton POLETAEV
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
59f45b7c 15
16#ifndef _OpenGl_LayerList_Header
17#define _OpenGl_LayerList_Header
18
c5751993 19#include <OpenGl_Layer.hxx>
91c60b57 20#include <OpenGl_LayerFilter.hxx>
59f45b7c 21
a1073ae2 22#include <NCollection_Array1.hxx>
23#include <NCollection_Handle.hxx>
59f45b7c 24#include <NCollection_Sequence.hxx>
25#include <NCollection_DataMap.hxx>
26
27class OpenGl_Structure;
59f45b7c 28
f5b72419 29typedef NCollection_Sequence<Handle(OpenGl_Layer)> OpenGl_SequenceOfLayers;
59f45b7c 30typedef NCollection_DataMap<int, int> OpenGl_LayerSeqIds;
31
bd6a8454 32//! Class defining the list of layers.
59f45b7c 33class OpenGl_LayerList
34{
a1954302 35public:
36
59f45b7c 37 //! Constructor
f5b72419 38 OpenGl_LayerList (const Standard_Integer theNbPriorities);
a1954302 39
59f45b7c 40 //! Destructor
a1954302 41 virtual ~OpenGl_LayerList();
42
59f45b7c 43 //! Method returns the number of available priorities
a1954302 44 Standard_Integer NbPriorities() const { return myNbPriorities; }
59f45b7c 45
46 //! Number of displayed structures
a1954302 47 Standard_Integer NbStructures() const { return myNbStructures; }
48
49 //! Return number of structures within immediate layers
50 Standard_Integer NbImmediateStructures() const { return myImmediateNbStructures; }
59f45b7c 51
52 //! Insert a new layer with id.
a1954302 53 void AddLayer (const Graphic3d_ZLayerId theLayerId);
54
59f45b7c 55 //! Remove layer by its id.
a1954302 56 void RemoveLayer (const Graphic3d_ZLayerId theLayerId);
57
59f45b7c 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.
a1954302 61 void AddStructure (const OpenGl_Structure* theStruct,
62 const Graphic3d_ZLayerId theLayerId,
63 const Standard_Integer thePriority,
64 Standard_Boolean isForChangePriority = Standard_False);
65
59f45b7c 66 //! Remove structure from structure list and return its previous priority
c357e426 67 void RemoveStructure (const OpenGl_Structure* theStructure);
a1954302 68
59f45b7c 69 //! Change structure z layer
70 //! If the new layer is not presented, the structure will be displayed
71 //! in default z layer
a1954302 72 void ChangeLayer (const OpenGl_Structure* theStructure,
73 const Graphic3d_ZLayerId theOldLayerId,
74 const Graphic3d_ZLayerId theNewLayerId);
c5751993 75
b7cd4ba7 76 //! Changes structure priority within its ZLayer
a1954302 77 void ChangePriority (const OpenGl_Structure* theStructure,
78 const Graphic3d_ZLayerId theLayerId,
79 const Standard_Integer theNewPriority);
b7cd4ba7 80
c5751993 81 //! Returns reference to the layer with given ID.
a1954302 82 OpenGl_Layer& Layer (const Graphic3d_ZLayerId theLayerId);
c5751993 83
84 //! Returns reference to the layer with given ID.
a1954302 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
2b8832bb 91 //! Update culling state - should be called before rendering.
92 void UpdateCulling (const Handle(OpenGl_Workspace)& theWorkspace,
93 const Standard_Boolean theToDrawImmediate);
94
59f45b7c 95 //! Render this element
a1954302 96 void Render (const Handle(OpenGl_Workspace)& theWorkspace,
91c60b57 97 const Standard_Boolean theToDrawImmediate,
a1073ae2 98 const OpenGl_LayerFilter theLayersToProcess,
99 OpenGl_FrameBuffer* theReadDrawFbo,
100 OpenGl_FrameBuffer* theOitAccumFbo) const;
e276548b 101
102 //! Returns the set of OpenGL Z-layers.
103 const OpenGl_SequenceOfLayers& Layers() const { return myLayers; }
e276548b 104
50d06d8f 105 //! Returns the map of Z-layer IDs to indexes.
106 const OpenGl_LayerSeqIds& LayerIDs() const { return myLayerIds; }
107
b7cd4ba7 108 //! Marks BVH tree for given priority list as dirty and
109 //! marks primitive set for rebuild.
a1954302 110 void InvalidateBVHData (const Graphic3d_ZLayerId theLayerId);
b7cd4ba7 111
e276548b 112 //! Returns structure modification state (for ray-tracing).
bd6a8454 113 Standard_Size ModificationStateOfRaytracable() const { return myModifStateOfRaytraceable; }
e276548b 114
f5b72419 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
a1073ae2 121protected:
122
123 //! Filter of TKOpenGl elements for processing only shading geometry and
124 //! for collecting number of skipped elements to an external counter.
125 class OpenGl_OpaqueFilter : public OpenGl_RenderFilter
126 {
127 public:
128
129 //! Constructor.
130 //! @param thePrevFilter [in] the previously active filter that should have additive effect.
131 OpenGl_OpaqueFilter() : mySkippedCounter (0) {}
132
133 //! Sets the current active filter in workspace.
134 //! @param thePrevFilter [in] the previously active filter that should have additive effect.
135 void SetPreviousFilter (const Handle(OpenGl_RenderFilter)& thePrevFitler) { myFilter = thePrevFitler; }
136
137 //! Sets the value of the skipped elements counter.
138 void SetSkippedCounter (const Standard_Size theCounter) { mySkippedCounter = theCounter; }
139
140 //! Returns number of skipped elements.
141 Standard_Size NbSkipped() const { return mySkippedCounter; }
142
143 //! Checks whether the element should be rendered or skipped.
144 //! @param theWorkspace [in] the currently used workspace for rendering.
145 //! @param theGlElement [in] the TKOpenGl rendering queue element that should be checked before streaming to GPU.
146 Standard_EXPORT virtual Standard_Boolean ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
147 const OpenGl_Element* theGlElement) Standard_OVERRIDE;
148
149 DEFINE_STANDARD_RTTI_INLINE (OpenGl_OpaqueFilter, OpenGl_RenderFilter)
150
151 private:
152
153 Standard_Size mySkippedCounter; //!< Counter of skipped elements.
154 Handle(OpenGl_RenderFilter) myFilter; //!< Previous active filter that should be combined.
155 };
156
157 //! Filter of TKOpenGl elements for keeping only shading geometry with transparency.
158 class OpenGl_TransparentFilter : public OpenGl_RenderFilter
159 {
160 public:
161
162 //! Constructor.
163 OpenGl_TransparentFilter() {}
164
165 //! Sets the current active filter in workspace.
166 //! @param thePrevFilter [in] the previously active filter that should have additive effect.
167 void SetPreviousFilter (const Handle(OpenGl_RenderFilter)& thePrevFitler) { myFilter = thePrevFitler; }
168
169 //! Checks whether the element should be rendered or skipped.
170 //! @param theWorkspace [in] the currently used workspace for rendering.
171 //! @param theGlElement [in] the TKOpenGl rendering queue element that should be checked before streaming to GPU.
172 Standard_EXPORT virtual Standard_Boolean ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
173 const OpenGl_Element* theGlElement) Standard_OVERRIDE;
174
175 DEFINE_STANDARD_RTTI_INLINE (OpenGl_TransparentFilter, OpenGl_RenderFilter)
176
177 private:
178
179 Handle(OpenGl_RenderFilter) myFilter; //!< Previous active filter that should be combined.
180 };
181
182 //! Stack of references to existing layers of predefined maximum size.
183 class OpenGl_LayerStack
184 {
185 public:
186 typedef NCollection_Array1<const OpenGl_Layer*>::iterator iterator;
187
188 //! Reallocate internal buffer of the stack.
189 void Allocate (Standard_Integer theSize)
190 {
191 if (theSize > 0)
192 {
193 myStackSpace = new NCollection_Array1<const OpenGl_Layer*> (1, theSize);
194 myStackSpace->Init (NULL);
195 myBackPtr = myStackSpace->begin();
196 }
197 else
198 {
199 myStackSpace.Nullify();
200 myBackPtr = iterator();
201 }
202 }
203
204 //! Clear stack.
205 void Clear()
206 {
207 if (!myStackSpace.IsNull())
208 {
209 myStackSpace->Init (NULL);
210 myBackPtr = myStackSpace->begin();
211 }
212 }
213
214 //! Push a new layer reference to the stack.
215 void Push (const OpenGl_Layer* theLayer) { (*myBackPtr++) = theLayer; }
216
217 //! Returns iterator to the origin of the stack.
218 iterator Origin() const { return myStackSpace.IsNull() ? iterator() : myStackSpace->begin(); }
219
220 //! Returns iterator to the back of the stack (after last item added).
221 iterator Back() const { return myBackPtr; }
222
223 //! Returns true if nothing has been pushed into the stack.
224 Standard_Boolean IsEmpty() const { return Back() == Origin(); }
225
226 private:
227
228 NCollection_Handle<NCollection_Array1<const OpenGl_Layer*> > myStackSpace;
229 iterator myBackPtr;
230 };
231
232 //! Render transparent objects using blending operator.
233 //! Additional accumulation framebuffer is used for blended order-independent
234 //! transparency algorithm. It should support floating-point color components
235 //! and share depth with main reading/drawing framebuffer.
236 //! @param theWorkspace [in] the currently used workspace for rendering.
237 //! @param theLayerIter [in/out] the current iterator of transparent layers to process.
238 //! @param theGlobalSettings [in] the set of global settings used for rendering.
239 //! @param theReadDrawFbo [in] the framebuffer for reading depth and writing final color.
240 //! @param theOitAccumFbo [in] the framebuffer for accumulating color and coverage for OIT process.
241 void renderTransparent (const Handle(OpenGl_Workspace)& theWorkspace,
242 OpenGl_LayerStack::iterator& theLayerIter,
243 const OpenGl_GlobalLayerSettings& theGlobalSettings,
244 OpenGl_FrameBuffer* theReadDrawFbo,
245 OpenGl_FrameBuffer* theOitAccumFbo) const;
246
a1954302 247protected:
59f45b7c 248
249 // number of structures temporary put to default layer
250 OpenGl_SequenceOfLayers myLayers;
251 OpenGl_LayerSeqIds myLayerIds;
f5b72419 252 Handle(Select3D_BVHBuilder3d) myBVHBuilder; //!< BVH tree builder for frustom culling
bd6a8454 253 Standard_Integer myDefaultLayerIndex; //!< index of Graphic3d_ZLayerId_Default layer in myLayers sequence
a1954302 254
59f45b7c 255 Standard_Integer myNbPriorities;
256 Standard_Integer myNbStructures;
a1954302 257 Standard_Integer myImmediateNbStructures; //!< number of structures within immediate layers
59f45b7c 258
bd6a8454 259 mutable Standard_Size myModifStateOfRaytraceable;
e276548b 260
a1073ae2 261 //! Collection of references to layers with transparency gathered during rendering pass.
262 mutable OpenGl_LayerStack myTransparentToProcess;
263
264 Handle(OpenGl_OpaqueFilter) myRenderOpaqueFilter; //!< rendering filter for opaque drawing pass (blended OIT).
265 Handle(OpenGl_TransparentFilter) myRenderTranspFilter; //!< rendering filter for transparency drawing pass (blended OIT).
266
a1954302 267public:
268
65c62757 269 DEFINE_STANDARD_ALLOC
a1954302 270
59f45b7c 271};
272
273#endif //_OpenGl_LayerList_Header