0030434: Visualization, TKV3d - add "NoUpdate" state of frustum culling optimization
[occt.git] / src / OpenGl / OpenGl_Layer.hxx
CommitLineData
a1954302 1// Created on: 2011-11-02
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2015 OPEN CASCADE SAS
c5751993 4//
5// This file is part of Open CASCADE Technology software library.
6//
0a36ca0a 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
c5751993 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_Layer_Header
17#define _OpenGl_Layer_Header
18
a1954302 19#include <NCollection_Sequence.hxx>
20#include <NCollection_Array1.hxx>
21
a1954302 22#include <OpenGl_BVHClipPrimitiveSet.hxx>
825aa485 23#include <OpenGl_BVHClipPrimitiveTrsfPersSet.hxx>
a1954302 24#include <OpenGl_BVHTreeSelector.hxx>
25
c5751993 26#include <Graphic3d_ZLayerSettings.hxx>
825aa485 27#include <Graphic3d_Camera.hxx>
550f3b8b 28#include <OpenGl_GlCore11.hxx>
c5751993 29
c5751993 30
550f3b8b 31struct OpenGl_GlobalLayerSettings
32{
33 GLint DepthFunc;
34 GLboolean DepthMask;
35};
36
825aa485 37//! Defines index map of OpenGL structures.
38typedef NCollection_IndexedMap<const OpenGl_Structure*> OpenGl_IndexedMapOfStructure;
39
40//! Defines array of indexed maps of OpenGL structures.
41typedef NCollection_Array1<OpenGl_IndexedMapOfStructure> OpenGl_ArrayOfIndexedMapOfStructure;
42
a1954302 43//! Presentations list sorted within priorities.
f5b72419 44class OpenGl_Layer : public Standard_Transient
c5751993 45{
f5b72419 46 DEFINE_STANDARD_RTTIEXT(OpenGl_Layer, Standard_Transient)
c5751993 47public:
48
49 //! Initializes associated priority list and layer properties
f5b72419 50 OpenGl_Layer (const Standard_Integer theNbPriorities,
51 const Handle(Select3D_BVHBuilder3d)& theBuilder);
c5751993 52
a1954302 53 //! Destructor.
54 virtual ~OpenGl_Layer();
55
f5b72419 56 //! Returns BVH tree builder for frustom culling.
57 const Handle(Select3D_BVHBuilder3d)& FrustumCullingBVHBuilder() const { return myBVHPrimitivesTrsfPers.Builder(); }
58
59 //! Assigns BVH tree builder for frustom culling.
60 void SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myBVHPrimitivesTrsfPers.SetBuilder (theBuilder); }
61
7c3ef2f7 62 //! Return true if layer was marked with immediate flag.
63 Standard_Boolean IsImmediate() const { return myLayerSettings.IsImmediate(); }
64
c5751993 65 //! Returns settings of the layer object.
a1954302 66 const Graphic3d_ZLayerSettings& LayerSettings() const { return myLayerSettings; };
c5751993 67
68 //! Sets settings of the layer object.
7c3ef2f7 69 void SetLayerSettings (const Graphic3d_ZLayerSettings& theSettings);
c5751993 70
a1954302 71 void Add (const OpenGl_Structure* theStruct,
72 const Standard_Integer thePriority,
73 Standard_Boolean isForChangePriority = Standard_False);
74
75 //! Remove structure and returns its priority, if the structure is not found, method returns negative value
76 bool Remove (const OpenGl_Structure* theStruct,
77 Standard_Integer& thePriority,
78 Standard_Boolean isForChangePriority = Standard_False);
79
80 //! @return the number of structures
81 Standard_Integer NbStructures() const { return myNbStructures; }
82
15669413 83 //! Number of NOT culled structures in the layer.
84 Standard_Integer NbStructuresNotCulled() const { return myNbStructuresNotCulled; }
85
a1954302 86 //! Returns the number of available priority levels
87 Standard_Integer NbPriorities() const { return myArray.Length(); }
88
89 //! Append layer of acceptable type (with similar number of priorities or less).
90 //! Returns Standard_False if the list can not be accepted.
91 Standard_Boolean Append (const OpenGl_Layer& theOther);
92
93 //! Returns array of OpenGL structures.
9f112210 94 const OpenGl_ArrayOfIndexedMapOfStructure& ArrayOfStructures() const { return myArray; }
c5751993 95
a1954302 96 //! Marks BVH tree for given priority list as dirty and
97 //! marks primitive set for rebuild.
2b8832bb 98 void InvalidateBVHData();
50d06d8f 99
100 //! Marks cached bounding box as obsolete.
101 void InvalidateBoundingBox() const
102 {
103 myIsBoundingBoxNeedsReset[0] = myIsBoundingBoxNeedsReset[1] = true;
104 }
105
106 //! Returns layer bounding box.
3fe9ce0e 107 //! @param theViewId view index to consider View Affinity in structure
108 //! @param theCamera camera definition
109 //! @param theWindowWidth viewport width (for applying transformation-persistence)
110 //! @param theWindowHeight viewport height (for applying transformation-persistence)
111 //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
112 //! @return computed bounding box
7c3ef2f7 113 Bnd_Box BoundingBox (const Standard_Integer theViewId,
114 const Handle(Graphic3d_Camera)& theCamera,
115 const Standard_Integer theWindowWidth,
116 const Standard_Integer theWindowHeight,
117 const Standard_Boolean theToIncludeAuxiliary) const;
50d06d8f 118
119 //! Returns zoom-scale factor.
120 Standard_Real considerZoomPersistenceObjects (const Standard_Integer theViewId,
121 const Handle(Graphic3d_Camera)& theCamera,
122 const Standard_Integer theWindowWidth,
3fe9ce0e 123 const Standard_Integer theWindowHeight) const;
c5751993 124
2b8832bb 125 //! Update culling state - should be called before rendering.
126 //! Traverses through BVH tree to determine which structures are in view volume.
5dc0517d 127 void UpdateCulling (const Standard_Integer theViewId,
128 const OpenGl_BVHTreeSelector& theSelector,
0e3025bc 129 const Graphic3d_RenderingParams::FrustumCulling theFrustumCullingState);
2b8832bb 130
131 //! Returns TRUE if layer is empty or has been discarded entirely by culling test.
15669413 132 bool IsCulled() const { return myNbStructuresNotCulled == 0; }
2b8832bb 133
a1954302 134 // Render all structures.
135 void Render (const Handle(OpenGl_Workspace)& theWorkspace,
136 const OpenGl_GlobalLayerSettings& theDefaultSettings) const;
137
50d06d8f 138 //! Returns number of transform persistence objects.
139 Standard_Integer NbOfTransformPersistenceObjects() const
140 {
141 return myBVHPrimitivesTrsfPers.Size();
142 }
143
15669413 144public:
145
146 //! Returns set of OpenGl_Structures structures for building BVH tree.
147 const OpenGl_BVHClipPrimitiveSet& CullableStructuresBVH() const { return myBVHPrimitives; }
148
149 //! Returns set of transform persistent OpenGl_Structures for building BVH tree.
150 const OpenGl_BVHClipPrimitiveTrsfPersSet& CullableTrsfPersStructuresBVH() const { return myBVHPrimitivesTrsfPers; }
151
152 //! Returns indexed map of always rendered structures.
153 const NCollection_IndexedMap<const OpenGl_Structure*>& NonCullableStructures() const { return myAlwaysRenderedMap; }
154
a1954302 155protected:
156
3fe9ce0e 157 //! Updates BVH trees if their state has been invalidated.
15669413 158 Standard_EXPORT void updateBVH() const;
3fe9ce0e 159
a1954302 160 //! Iterates through the hierarchical list of existing structures and renders them all.
15669413 161 Standard_EXPORT void renderAll (const Handle(OpenGl_Workspace)& theWorkspace) const;
a1954302 162
c5751993 163private:
164
825aa485 165 //! Array of OpenGl_Structures by priority rendered in layer.
9f112210 166 OpenGl_ArrayOfIndexedMapOfStructure myArray;
825aa485 167
168 //! Overall number of structures rendered in the layer.
169 Standard_Integer myNbStructures;
170
15669413 171 //! Number of NOT culled structures in the layer.
172 Standard_Integer myNbStructuresNotCulled;
173
825aa485 174 //! Layer setting flags.
175 Graphic3d_ZLayerSettings myLayerSettings;
176
177 //! Set of OpenGl_Structures structures for building BVH tree.
178 mutable OpenGl_BVHClipPrimitiveSet myBVHPrimitives;
179
180 //! Set of transform persistent OpenGl_Structures for building BVH tree.
181 mutable OpenGl_BVHClipPrimitiveTrsfPersSet myBVHPrimitivesTrsfPers;
182
3fe9ce0e 183 //! Indexed map of always rendered structures.
150ed3d5 184 mutable NCollection_IndexedMap<const OpenGl_Structure*> myAlwaysRenderedMap;
3fe9ce0e 185
825aa485 186 //! Is needed for implementation of stochastic order of BVH traverse.
2b8832bb 187 Standard_Boolean myBVHIsLeftChildQueuedFirst;
825aa485 188
189 //! Defines if the primitive set for BVH is outdated.
190 mutable Standard_Boolean myIsBVHPrimitivesNeedsReset;
a1954302 191
50d06d8f 192 //! Defines if the cached bounding box is outdated.
193 mutable bool myIsBoundingBoxNeedsReset[2];
194
195 //! Cached layer bounding box.
7c3ef2f7 196 mutable Bnd_Box myBoundingBox[2];
50d06d8f 197
c5751993 198};
a1954302 199
c5751993 200#endif //_OpenGl_Layer_Header