0026122: Visualization, TKOpenGl - clipping and capping is broken when ffp is disable...
[occt.git] / src / OpenGl / OpenGl_Clipping.hxx
CommitLineData
4269bd1b 1// Created on: 2013-09-05
2// Created by: Anton POLETAEV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
4269bd1b 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
4269bd1b 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.
4269bd1b 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
4269bd1b 15
30f0ad28 16#ifndef _OpenGl_Clipping_H__
17#define _OpenGl_Clipping_H__
4269bd1b 18
19#include <Aspect_GenId.hxx>
20#include <Graphic3d_ClipPlane.hxx>
51b10cd4 21#include <Graphic3d_SequenceOfHClipPlane.hxx>
4269bd1b 22#include <NCollection_DataMap.hxx>
23#include <NCollection_Handle.hxx>
24#include <Standard_TypeDef.hxx>
25#include <OpenGl_Matrix.hxx>
26
79f4f036 27class OpenGl_Context;
c04c30b3 28class OpenGl_Workspace;
29
4269bd1b 30//! This class contains logics related to tracking and modification of clipping plane
31//! state for particular OpenGl context. It contains information about enabled
32//! clipping planes and provides method to change clippings in context. The methods
33//! should be executed within OpenGl context associated with instance of this
34//! class.
30f0ad28 35class OpenGl_Clipping
4269bd1b 36{
37public:
38
b859a34d 39 //! Enumerates supported equation coordinate spaces.
40 enum EquationCoords
41 {
42 EquationCoords_View,
43 EquationCoords_World
44 };
45
46public: //! @name general methods
47
4269bd1b 48 //! Default constructor.
30f0ad28 49 Standard_EXPORT OpenGl_Clipping();
4269bd1b 50
51 //! Initialize.
52 //! @param theMaxPlanes [in] number of clipping planes supported by OpenGl context.
53 Standard_EXPORT void Init (const Standard_Integer theMaxPlanes);
54
b859a34d 55public: //! @name non-modifying getters
4269bd1b 56
b859a34d 57 //! Check whether the clipping plane has been added to the current context state.
4269bd1b 58 //! @param thePlane [in] the plane to check.
59 //! @return True if plane is set.
b859a34d 60 inline Standard_Boolean Contains (const Handle(Graphic3d_ClipPlane)& thePlane) const
61 {
62 return myPlaneStates.IsBound (thePlane);
63 }
64
65 //! Get clip planes defined for context.
66 //! @return sequence of set clipping planes.
51b10cd4 67 inline const Graphic3d_SequenceOfHClipPlane& Planes() const
b859a34d 68 {
69 return myPlanes;
70 }
71
72 //! @return kind of equation coordinate space used for the clip plane.
73 inline EquationCoords GetEquationSpace (const Handle(Graphic3d_ClipPlane)& thePlane) const
74 {
75 return myPlaneStates.Find (thePlane).CoordSpace;
76 }
4269bd1b 77
78 //! Check whether the clipping plane has been set and enabled for the current context state.
79 //! @param thePlane [in] the plane to check.
80 //! @return True if plane is enabled.
b859a34d 81 inline Standard_Boolean IsEnabled (const Handle(Graphic3d_ClipPlane)& thePlane) const
82 {
83 return myPlaneStates.Find (thePlane).IsEnabled;
84 }
85
3b1817a9 86 //! @return true if there are enabled clipping planes (NOT capping)
87 inline Standard_Boolean IsClippingOn() const
88 {
89 return myNbClipping > 0;
90 }
91
92 //! @return true if there are enabled capping planes
93 inline Standard_Boolean IsCappingOn() const
94 {
95 return myNbCapping > 0;
96 }
97
98 //! @return true if there are enabled clipping or capping planes
99 inline Standard_Boolean IsClippingOrCappingOn() const
100 {
101 return (myNbClipping + myNbCapping) > 0;
102 }
103
b859a34d 104public: //! @name clipping state modification commands
105
106 //! Add planes to the context clipping at the specified system of coordinates.
107 //! This methods loads appropriate transformation matrix from workspace to
108 //! to transform equation coordinates. The planes become enabled in the context.
109 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
110 //! are simply ignored.
79f4f036 111 //!
112 //! Within FFP, method also temporarily resets ModelView matrix before calling glClipPlane().
113 //! Otherwise the method just redirects to addLazy().
114 //!
115 //! @param theGlCtx [in] context to access the matrices
116 //! @param theCoordSpace [in] the equation definition space
117 //! @param thePlanes [in/out] the list of planes to be added
b859a34d 118 //! The list then provides information on which planes were really added to clipping state.
119 //! This list then can be used to fall back to previous state.
79f4f036 120 Standard_EXPORT void add (const Handle(OpenGl_Context)& theGlCtx,
121 const EquationCoords& theCoordSpace,
122 Graphic3d_SequenceOfHClipPlane& thePlanes);
b859a34d 123
124 //! Add planes to the context clipping at the specified system of coordinates.
125 //! This method assumes that appropriate matrix is already set in context state.
126 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
127 //! are simply ignored.
128 //! @param thePlanes [in/out] the list of planes to be added.
129 //! The list then provides information on which planes were really added to clipping state.
130 //! This list then can be used to fall back to previous state.
131 //! @param theCoordSpace [in] the equation definition space.
79f4f036 132 Standard_EXPORT void addLazy (const Handle(OpenGl_Context)& theGlCtx,
133 const EquationCoords& theCoordSpace,
134 Graphic3d_SequenceOfHClipPlane& thePlanes);
b859a34d 135
136 //! Remove the passed set of clipping planes from the context state.
137 //! @param thePlanes [in] the planes to remove from list.
79f4f036 138 Standard_EXPORT void Remove (const Handle(OpenGl_Context)& theGlCtx,
139 const Graphic3d_SequenceOfHClipPlane& thePlanes);
b859a34d 140
141 //! Enable or disable clipping plane in the OpenGl context.
142 //! @param thePlane [in] the plane to affect.
143 //! @param theIsEnabled [in] the state of the plane.
79f4f036 144 Standard_EXPORT void SetEnabled (const Handle(OpenGl_Context)& theGlCtx,
145 const Handle(Graphic3d_ClipPlane)& thePlane,
146 const Standard_Boolean theIsEnabled);
b859a34d 147
148public: //! @name Short-cuts
149
150 //! Add planes to the context clipping at the view system of coordinates.
151 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
152 //! are simply ignored.
79f4f036 153 //! @param theGlCtx [in] context to access the matrices
154 //! @param thePlanes [in/out] the list of planes to be added
b859a34d 155 //! The list then provides information on which planes were really added to clipping state.
156 //! This list then can be used to fall back to previous state.
79f4f036 157 inline void AddView (const Handle(OpenGl_Context)& theGlCtx,
158 Graphic3d_SequenceOfHClipPlane& thePlanes)
b859a34d 159 {
79f4f036 160 add (theGlCtx, EquationCoords_View, thePlanes);
b859a34d 161 }
162
163 //! Add planes to the context clipping at the world system of coordinates.
164 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
165 //! are simply ignored.
79f4f036 166 //! @param theGlCtx [in] context to access the matrices
167 //! @param thePlanes [in/out] the list of planes to be added
b859a34d 168 //! The list then provides information on which planes were really added to clipping state.
169 //! This list then can be used to fall back to previous state.
79f4f036 170 inline void AddWorld (const Handle(OpenGl_Context)& theGlCtx,
171 Graphic3d_SequenceOfHClipPlane& thePlanes)
b859a34d 172 {
79f4f036 173 add (theGlCtx, EquationCoords_World, thePlanes);
b859a34d 174 }
175
176 //! Add planes to the context clipping at the world system of coordinates.
177 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
178 //! are simply ignored.
79f4f036 179 //! @param thePlanes [in/out] the list of planes to be added
b859a34d 180 //! The list then provides information on which planes were really added to clipping state.
181 //! This list then can be used to fall back to previous state.
79f4f036 182 inline void AddWorldLazy (const Handle(OpenGl_Context)& theGlCtx,
183 Graphic3d_SequenceOfHClipPlane& thePlanes)
b859a34d 184 {
79f4f036 185 addLazy (theGlCtx, EquationCoords_World, thePlanes);
b859a34d 186 }
187
188 //! Remove all of the planes from context state.
79f4f036 189 inline void RemoveAll (const Handle(OpenGl_Context)& theGlCtx)
b859a34d 190 {
79f4f036 191 Remove (theGlCtx, Planes());
b859a34d 192 }
193
194private:
4269bd1b 195
b859a34d 196 struct PlaneProps
197 {
198 // declare default constructor
199 // to allow compilation of template collections
200 PlaneProps() {}
201 PlaneProps (const EquationCoords theCoords,
202 const Standard_Integer theID,
203 const Standard_Boolean theIsEnabled)
204 {
205 CoordSpace = theCoords;
206 ContextID = theID;
207 IsEnabled = theIsEnabled;
208 }
209
210 EquationCoords CoordSpace;
211 Standard_Integer ContextID;
212 Standard_Boolean IsEnabled;
213 };
4269bd1b 214
215private:
216
b859a34d 217 typedef NCollection_DataMap<Handle(Graphic3d_ClipPlane), PlaneProps> OpenGl_MapOfPlaneStates;
4269bd1b 218 typedef NCollection_Handle<Aspect_GenId> OpenGl_EmptyPlaneIds;
219
3b1817a9 220 Graphic3d_SequenceOfHClipPlane myPlanes; //!< defined clipping planes
221 OpenGl_MapOfPlaneStates myPlaneStates; //!< map of clip planes bound for the props
222 OpenGl_EmptyPlaneIds myEmptyPlaneIds; //!< generator of empty ids
223 Standard_Boolean myNbClipping; //!< number of enabled clipping-only planes (NOT capping)
224 Standard_Boolean myNbCapping; //!< number of enabled capping planes
4269bd1b 225
226private:
227
228 //! Copying allowed only within Handles
30f0ad28 229 OpenGl_Clipping (const OpenGl_Clipping& );
230 OpenGl_Clipping& operator= (const OpenGl_Clipping& );
4269bd1b 231};
232
233#endif