0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / OpenGl / OpenGl_Clipping.hxx
1 // Created on: 2013-09-05
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-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_Clipping_H__
17 #define _OpenGl_Clipping_H__
18
19 #include <Graphic3d_SequenceOfHClipPlane.hxx>
20 #include <NCollection_Vector.hxx>
21 #include <Standard_TypeDef.hxx>
22
23 class OpenGl_ClippingIterator;
24
25 //! This class contains logics related to tracking and modification of clipping plane
26 //! state for particular OpenGl context. It contains information about enabled
27 //! clipping planes and provides method to change clippings in context. The methods
28 //! should be executed within OpenGl context associated with instance of this
29 //! class.
30 class OpenGl_Clipping
31 {
32   friend class OpenGl_ClippingIterator;
33 public: //! @name general methods
34
35   //! Default constructor.
36   Standard_EXPORT OpenGl_Clipping();
37
38   //! Initialize.
39   Standard_EXPORT void Init();
40
41   //! Setup list of global (for entire view) clipping planes
42   //! and clears local plane list if it was not released before.
43   Standard_EXPORT void Reset (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes);
44
45   //! Setup list of local (for current object) clipping planes.
46   Standard_EXPORT void SetLocalPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes);
47
48   //! @return true if there are enabled capping planes
49   Standard_Boolean IsCappingOn() const { return myNbCapping > 0; }
50
51   //! @return true if there are enabled clipping or capping planes
52   Standard_Boolean IsClippingOrCappingOn() const { return NbClippingOrCappingOn() > 0; }
53
54   //! @return number of enabled clipping + capping planes
55   Standard_Integer NbClippingOrCappingOn() const
56   {
57     if (IsCappingDisableAllExcept())
58     {
59       return 1; // all Chains are disabled - only single (sub) plane is active
60     }
61     return myNbClipping + myNbCapping
62         + (IsCappingEnableAllExcept() ? -1 : 0); // exclude 1 plane with Capping filter turned ON
63   }
64
65   //! Return TRUE if there are clipping chains in the list (defining more than 1 sub-plane)
66   Standard_Boolean HasClippingChains() const
67   {
68     if (IsCappingDisableAllExcept()                 // all chains are disabled - only single (sub) plane is active;
69      || myNbChains == (myNbClipping + myNbCapping)) // no sub-planes
70     {
71       return Standard_False;
72     }
73     return !IsCappingEnableAllExcept()
74          || myCappedChain->NbChainNextPlanes() == 1
75          || myNbChains > 1; // if capping filter ON - chains counter should be decremented
76   }
77
78 public: //! @name advanced method for disabling defined planes
79
80   //! Return true if some clipping planes have been temporarily disabled.
81   Standard_Boolean HasDisabled() const { return myNbDisabled > 0; }
82
83   //! Disable plane temporarily.
84   Standard_EXPORT Standard_Boolean SetEnabled (const OpenGl_ClippingIterator& thePlane,
85                                                const Standard_Boolean         theIsEnabled);
86
87   //! Temporarily disable all planes from the global (view) list, keep only local (object) list.
88   Standard_EXPORT void DisableGlobal();
89
90   //! Restore all temporarily disabled planes.
91   //! Does NOT affect constantly disabled planes Graphic3d_ClipPlane::IsOn().
92   Standard_EXPORT void RestoreDisabled();
93
94 //! @name capping algorithm filter
95 public:
96
97   //! Chain which is either temporary disabled or the only one enabled for Capping algorithm.
98   const Handle(Graphic3d_ClipPlane)& CappedChain() const { return myCappedChain; }
99
100   //! Sub-plane index within filtered Chain; positive number for DisableAllExcept and negative for EnableAllExcept.
101   Standard_Integer CappedSubPlane() const { return myCappedSubPlane; }
102
103   //! Return TRUE if capping algorithm is in state, when all clipping planes are temporarily disabled except currently processed one.
104   bool IsCappingFilterOn() const { return !myCappedChain.IsNull(); }
105
106   //! Return TRUE if capping algorithm is in state, when all clipping planes are temporarily disabled except currently processed one.
107   bool IsCappingDisableAllExcept() const { return myCappedSubPlane > 0; }
108
109   //! Return TRUE if capping algorithm is in state, when all clipping planes are enabled except currently rendered one.
110   bool IsCappingEnableAllExcept() const { return myCappedSubPlane < 0; }
111
112   //! Temporarily disable all planes except specified one for Capping algorithm.
113   //! Does not affect already disabled planes.
114   Standard_EXPORT void DisableAllExcept (const Handle(Graphic3d_ClipPlane)& theChain,
115                                          const Standard_Integer theSubPlaneIndex);
116
117   //! Enable back planes disabled by ::DisableAllExcept() for Capping algorithm.
118   //! Keeps only specified plane enabled.
119   Standard_EXPORT void EnableAllExcept (const Handle(Graphic3d_ClipPlane)& theChain,
120                                         const Standard_Integer theSubPlaneIndex);
121
122   //! Resets chain filter for Capping algorithm.
123   Standard_EXPORT void ResetCappingFilter();
124
125 protected: //! @name clipping state modification commands
126
127   //! Add planes to the context clipping at the specified system of coordinates.
128   //! This methods loads appropriate transformation matrix from workspace to
129   //! to transform equation coordinates. The planes become enabled in the context.
130   //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
131   //! are simply ignored.
132   //!
133   //! Within FFP, method also temporarily resets ModelView matrix before calling glClipPlane().
134   //! Otherwise the method just redirects to addLazy().
135   //!
136   //! @param thePlanes [in/out] the list of planes to be added
137   //! The list then provides information on which planes were really added to clipping state.
138   //! This list then can be used to fall back to previous state.
139   Standard_EXPORT void add (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes,
140                             const Standard_Integer theStartIndex);
141
142   //! Remove the passed set of clipping planes from the context state.
143   //! @param thePlanes [in] the planes to remove from list.
144   Standard_EXPORT void remove (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes,
145                                const Standard_Integer theStartIndex);
146
147 private:
148
149   Handle(Graphic3d_SequenceOfHClipPlane)   myPlanesGlobal;   //!< global clipping planes
150   Handle(Graphic3d_SequenceOfHClipPlane)   myPlanesLocal;    //!< object clipping planes
151   NCollection_Vector<Standard_Boolean>     myDisabledPlanes; //!< ids of disabled planes
152
153   Handle(Graphic3d_ClipPlane)              myCappedChain;    //!< chain which is either temporary disabled or the only one enabled for Capping algorithm
154   Standard_Integer                         myCappedSubPlane; //!< sub-plane index within filtered chain; positive number for DisableAllExcept and negative for EnableAllExcept
155
156   Standard_Integer                         myNbClipping;     //!< number of enabled clipping-only planes (NOT capping)
157   Standard_Integer                         myNbCapping;      //!< number of enabled capping  planes
158   Standard_Integer                         myNbChains;       //!< number of enabled chains
159   Standard_Integer                         myNbDisabled;     //!< number of defined but disabled planes
160
161 private:
162   //! Copying allowed only within Handles
163   OpenGl_Clipping            (const OpenGl_Clipping& );
164   OpenGl_Clipping& operator= (const OpenGl_Clipping& );
165 };
166
167 #endif