0027787: Visualization, TKOpenGl - Optimize rendering by additional check whether...
[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{
b859a34d 37public: //! @name general methods
38
4269bd1b 39 //! Default constructor.
30f0ad28 40 Standard_EXPORT OpenGl_Clipping();
4269bd1b 41
42 //! Initialize.
43 //! @param theMaxPlanes [in] number of clipping planes supported by OpenGl context.
44 Standard_EXPORT void Init (const Standard_Integer theMaxPlanes);
45
b859a34d 46public: //! @name non-modifying getters
4269bd1b 47
b859a34d 48 //! Check whether the clipping plane has been added to the current context state.
4269bd1b 49 //! @param thePlane [in] the plane to check.
50 //! @return True if plane is set.
b859a34d 51 inline Standard_Boolean Contains (const Handle(Graphic3d_ClipPlane)& thePlane) const
52 {
53 return myPlaneStates.IsBound (thePlane);
54 }
55
56 //! Get clip planes defined for context.
57 //! @return sequence of set clipping planes.
51b10cd4 58 inline const Graphic3d_SequenceOfHClipPlane& Planes() const
b859a34d 59 {
60 return myPlanes;
61 }
62
4269bd1b 63 //! Check whether the clipping plane has been set and enabled for the current context state.
64 //! @param thePlane [in] the plane to check.
65 //! @return True if plane is enabled.
b859a34d 66 inline Standard_Boolean IsEnabled (const Handle(Graphic3d_ClipPlane)& thePlane) const
67 {
68 return myPlaneStates.Find (thePlane).IsEnabled;
69 }
70
3b1817a9 71 //! @return true if there are enabled clipping planes (NOT capping)
72 inline Standard_Boolean IsClippingOn() const
73 {
74 return myNbClipping > 0;
75 }
76
77 //! @return true if there are enabled capping planes
78 inline Standard_Boolean IsCappingOn() const
79 {
80 return myNbCapping > 0;
81 }
82
83 //! @return true if there are enabled clipping or capping planes
84 inline Standard_Boolean IsClippingOrCappingOn() const
85 {
86 return (myNbClipping + myNbCapping) > 0;
87 }
88
1a7ece8f 89 //! @return number of enabled clipping + capping planes
90 Standard_Integer NbClippingOrCappingOn() const
91 {
92 return myNbClipping + myNbCapping;
93 }
94
b859a34d 95public: //! @name clipping state modification commands
96
97 //! Add planes to the context clipping at the specified system of coordinates.
98 //! This methods loads appropriate transformation matrix from workspace to
99 //! to transform equation coordinates. The planes become enabled in the context.
100 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
101 //! are simply ignored.
79f4f036 102 //!
103 //! Within FFP, method also temporarily resets ModelView matrix before calling glClipPlane().
104 //! Otherwise the method just redirects to addLazy().
105 //!
106 //! @param theGlCtx [in] context to access the matrices
79f4f036 107 //! @param thePlanes [in/out] the list of planes to be added
b859a34d 108 //! The list then provides information on which planes were really added to clipping state.
109 //! This list then can be used to fall back to previous state.
79f4f036 110 Standard_EXPORT void add (const Handle(OpenGl_Context)& theGlCtx,
79f4f036 111 Graphic3d_SequenceOfHClipPlane& thePlanes);
b859a34d 112
113 //! Add planes to the context clipping at the specified system of coordinates.
114 //! This method assumes that appropriate matrix is already set in context state.
115 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
116 //! are simply ignored.
117 //! @param thePlanes [in/out] the list of planes to be added.
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 addLazy (const Handle(OpenGl_Context)& theGlCtx,
79f4f036 121 Graphic3d_SequenceOfHClipPlane& thePlanes);
b859a34d 122
123 //! Remove the passed set of clipping planes from the context state.
124 //! @param thePlanes [in] the planes to remove from list.
79f4f036 125 Standard_EXPORT void Remove (const Handle(OpenGl_Context)& theGlCtx,
126 const Graphic3d_SequenceOfHClipPlane& thePlanes);
b859a34d 127
128 //! Enable or disable clipping plane in the OpenGl context.
129 //! @param thePlane [in] the plane to affect.
130 //! @param theIsEnabled [in] the state of the plane.
79f4f036 131 Standard_EXPORT void SetEnabled (const Handle(OpenGl_Context)& theGlCtx,
132 const Handle(Graphic3d_ClipPlane)& thePlane,
133 const Standard_Boolean theIsEnabled);
b859a34d 134
135public: //! @name Short-cuts
136
b859a34d 137 //! Add planes to the context clipping at the world system of coordinates.
138 //! If the number of the passed planes exceeds capabilities of OpenGl, the last planes
139 //! are simply ignored.
79f4f036 140 //! @param theGlCtx [in] context to access the matrices
141 //! @param thePlanes [in/out] the list of planes to be added
b859a34d 142 //! The list then provides information on which planes were really added to clipping state.
143 //! This list then can be used to fall back to previous state.
79f4f036 144 inline void AddWorld (const Handle(OpenGl_Context)& theGlCtx,
145 Graphic3d_SequenceOfHClipPlane& thePlanes)
b859a34d 146 {
89a929ea 147 add (theGlCtx, thePlanes);
b859a34d 148 }
149
b859a34d 150 //! Remove all of the planes from context state.
79f4f036 151 inline void RemoveAll (const Handle(OpenGl_Context)& theGlCtx)
b859a34d 152 {
79f4f036 153 Remove (theGlCtx, Planes());
b859a34d 154 }
155
156private:
4269bd1b 157
b859a34d 158 struct PlaneProps
159 {
160 // declare default constructor
161 // to allow compilation of template collections
162 PlaneProps() {}
89a929ea 163 PlaneProps (const Standard_Integer theID,
b859a34d 164 const Standard_Boolean theIsEnabled)
165 {
b859a34d 166 ContextID = theID;
167 IsEnabled = theIsEnabled;
168 }
169
b859a34d 170 Standard_Integer ContextID;
171 Standard_Boolean IsEnabled;
172 };
4269bd1b 173
174private:
175
b859a34d 176 typedef NCollection_DataMap<Handle(Graphic3d_ClipPlane), PlaneProps> OpenGl_MapOfPlaneStates;
4269bd1b 177 typedef NCollection_Handle<Aspect_GenId> OpenGl_EmptyPlaneIds;
178
3b1817a9 179 Graphic3d_SequenceOfHClipPlane myPlanes; //!< defined clipping planes
180 OpenGl_MapOfPlaneStates myPlaneStates; //!< map of clip planes bound for the props
181 OpenGl_EmptyPlaneIds myEmptyPlaneIds; //!< generator of empty ids
182 Standard_Boolean myNbClipping; //!< number of enabled clipping-only planes (NOT capping)
183 Standard_Boolean myNbCapping; //!< number of enabled capping planes
4269bd1b 184
185private:
186
187 //! Copying allowed only within Handles
30f0ad28 188 OpenGl_Clipping (const OpenGl_Clipping& );
189 OpenGl_Clipping& operator= (const OpenGl_Clipping& );
4269bd1b 190};
191
192#endif