a74ea4302a459ed24c6758f1cd63d09b484dfd38
[occt.git] / src / OpenGl / OpenGl_Layer.hxx
1 // Created on: 2011-11-02
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2015 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_Layer_Header
17 #define _OpenGl_Layer_Header
18
19 #include <NCollection_Sequence.hxx>
20 #include <NCollection_Array1.hxx>
21
22 #include <OpenGl_BVHClipPrimitiveSet.hxx>
23 #include <OpenGl_BVHClipPrimitiveTrsfPersSet.hxx>
24 #include <OpenGl_BVHTreeSelector.hxx>
25
26 #include <Graphic3d_ZLayerSettings.hxx>
27 #include <Graphic3d_Camera.hxx>
28 #include <OpenGl_GlCore11.hxx>
29
30
31 struct OpenGl_GlobalLayerSettings
32 {
33   GLint DepthFunc;
34   GLboolean DepthMask;
35 };
36
37 //! Defines index map of OpenGL structures.
38 typedef NCollection_IndexedMap<const OpenGl_Structure*> OpenGl_IndexedMapOfStructure;
39
40 //! Defines array of indexed maps of OpenGL structures.
41 typedef NCollection_Array1<OpenGl_IndexedMapOfStructure> OpenGl_ArrayOfIndexedMapOfStructure;
42
43 //! Presentations list sorted within priorities.
44 class OpenGl_Layer : public Standard_Transient
45 {
46   DEFINE_STANDARD_RTTIEXT(OpenGl_Layer, Standard_Transient)
47 public:
48
49   //! Initializes associated priority list and layer properties
50   OpenGl_Layer (const Standard_Integer theNbPriorities,
51                 const Handle(Select3D_BVHBuilder3d)& theBuilder);
52
53   //! Destructor.
54   virtual ~OpenGl_Layer();
55
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
62   //! Return true if layer was marked with immediate flag.
63   Standard_Boolean IsImmediate() const  { return myLayerSettings.IsImmediate(); }
64
65   //! Returns settings of the layer object.
66   const Graphic3d_ZLayerSettings& LayerSettings() const { return myLayerSettings; };
67
68   //! Sets settings of the layer object.
69   void SetLayerSettings (const Graphic3d_ZLayerSettings& theSettings);
70
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
83   //! Returns the number of available priority levels
84   Standard_Integer NbPriorities() const { return myArray.Length(); }
85
86   //! Append layer of acceptable type (with similar number of priorities or less).
87   //! Returns Standard_False if the list can not be accepted.
88   Standard_Boolean Append (const OpenGl_Layer& theOther);
89
90   //! Returns array of OpenGL structures.
91   const OpenGl_ArrayOfIndexedMapOfStructure& ArrayOfStructures() const { return myArray; }
92
93   //! Marks BVH tree for given priority list as dirty and
94   //! marks primitive set for rebuild.
95   void InvalidateBVHData() const;
96
97   //! Marks cached bounding box as obsolete.
98   void InvalidateBoundingBox() const
99   {
100     myIsBoundingBoxNeedsReset[0] = myIsBoundingBoxNeedsReset[1] = true;
101   }
102
103   //! Returns layer bounding box.
104   //! @param theViewId             view index to consider View Affinity in structure
105   //! @param theCamera             camera definition
106   //! @param theWindowWidth        viewport width  (for applying transformation-persistence)
107   //! @param theWindowHeight       viewport height (for applying transformation-persistence)
108   //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
109   //! @return computed bounding box
110   Bnd_Box BoundingBox (const Standard_Integer          theViewId,
111                        const Handle(Graphic3d_Camera)& theCamera,
112                        const Standard_Integer          theWindowWidth,
113                        const Standard_Integer          theWindowHeight,
114                        const Standard_Boolean          theToIncludeAuxiliary) const;
115
116   //! Returns zoom-scale factor.
117   Standard_Real considerZoomPersistenceObjects (const Standard_Integer          theViewId,
118                                                 const Handle(Graphic3d_Camera)& theCamera,
119                                                 const Standard_Integer          theWindowWidth,
120                                                 const Standard_Integer          theWindowHeight) const;
121
122   // Render all structures.
123   void Render (const Handle(OpenGl_Workspace)&   theWorkspace,
124                const OpenGl_GlobalLayerSettings& theDefaultSettings) const;
125
126   //! Returns number of transform persistence objects.
127   Standard_Integer NbOfTransformPersistenceObjects() const
128   {
129     return myBVHPrimitivesTrsfPers.Size();
130   }
131
132 protected:
133
134   //! Updates BVH trees if their state has been invalidated.
135   void updateBVH() const;
136
137   //! Traverses through BVH tree to determine which structures are in view volume.
138   void traverse (const OpenGl_BVHTreeSelector& theSelector) const;
139
140   //! Iterates through the hierarchical list of existing structures and renders them all.
141   void renderAll (const Handle(OpenGl_Workspace)& theWorkspace) const;
142
143   //! Iterates through the hierarchical list of existing structures and renders only overlapping ones.
144   void renderTraverse (const Handle(OpenGl_Workspace)& theWorkspace) const;
145
146 private:
147
148   //! Array of OpenGl_Structures by priority rendered in layer.
149   OpenGl_ArrayOfIndexedMapOfStructure myArray;
150
151   //! Overall number of structures rendered in the layer.
152   Standard_Integer myNbStructures;
153
154   //! Layer setting flags.
155   Graphic3d_ZLayerSettings myLayerSettings;
156
157   //! Set of OpenGl_Structures structures for building BVH tree.
158   mutable OpenGl_BVHClipPrimitiveSet myBVHPrimitives;
159
160   //! Set of transform persistent OpenGl_Structures for building BVH tree.
161   mutable OpenGl_BVHClipPrimitiveTrsfPersSet myBVHPrimitivesTrsfPers;
162
163   //! Indexed map of always rendered structures.
164   mutable NCollection_IndexedMap<const OpenGl_Structure*> myAlwaysRenderedMap;
165
166   //! Is needed for implementation of stochastic order of BVH traverse.
167   mutable Standard_Boolean myBVHIsLeftChildQueuedFirst;
168
169   //! Defines if the primitive set for BVH is outdated.
170   mutable Standard_Boolean myIsBVHPrimitivesNeedsReset;
171
172   //! Defines if the cached bounding box is outdated.
173   mutable bool myIsBoundingBoxNeedsReset[2];
174
175   //! Cached layer bounding box.
176   mutable Bnd_Box myBoundingBox[2];
177
178 public:
179
180   DEFINE_STANDARD_ALLOC
181
182 };
183
184 #endif //_OpenGl_Layer_Header