1 // Created on: 2013-09-05
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #include <OpenGl_ClippingState.hxx>
21 #include <OpenGl_GlCore11.hxx>
25 static const GLdouble OpenGl_DefaultPlaneEq[] = {0.0, 0.0, 0.0, 0.0};
28 // =======================================================================
29 // function : OpenGl_ClippingState
31 // =======================================================================
32 OpenGl_ClippingState::OpenGl_ClippingState ()
35 myEmptyPlaneIds (new Aspect_GenId (GL_CLIP_PLANE0, GL_CLIP_PLANE5))
38 // =======================================================================
41 // =======================================================================
42 void OpenGl_ClippingState::Init (const Standard_Integer theMaxPlanes)
45 myPlaneStates.Clear();
46 Standard_Integer aLowerId = GL_CLIP_PLANE0;
47 Standard_Integer aUpperId = GL_CLIP_PLANE0 + theMaxPlanes - 1;
48 myEmptyPlaneIds = new Aspect_GenId (aLowerId, aUpperId);
51 // =======================================================================
54 // =======================================================================
55 Graphic3d_SetOfHClipPlane OpenGl_ClippingState::Planes() const
57 Graphic3d_SetOfHClipPlane aRes;
58 OpenGl_MapOfContextPlanes::Iterator anIt (myPlanes);
59 for (; anIt.More(); anIt.Next())
61 aRes.Add (anIt.Key());
67 // =======================================================================
70 // =======================================================================
71 Standard_Boolean OpenGl_ClippingState::IsSet (const Handle(Graphic3d_ClipPlane)& thePlane) const
73 return myPlanes.IsBound (thePlane);
76 // =======================================================================
79 // =======================================================================
80 void OpenGl_ClippingState::Set (const Graphic3d_SetOfHClipPlane& thePlanes,
81 const Standard_Boolean theToEnable)
83 Graphic3d_SetOfHClipPlane::Iterator aPlaneIt (thePlanes);
84 for (; aPlaneIt.More() && myEmptyPlaneIds->Available() > 0; aPlaneIt.Next())
86 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
90 Standard_Integer anId = myEmptyPlaneIds->Next();
91 myPlanes.Bind (aPlane, anId);
92 myPlaneStates.Bind (aPlane, theToEnable);
94 const GLenum anOpenGlId = (GLenum)anId;
97 glEnable (anOpenGlId);
101 glDisable (anOpenGlId);
104 glClipPlane (anOpenGlId, aPlane->GetEquation());
108 // =======================================================================
111 // =======================================================================
112 void OpenGl_ClippingState::Set (const Graphic3d_SetOfHClipPlane& thePlanes,
113 const OpenGl_Matrix* theViewMatrix,
114 const Standard_Boolean theToEnable)
117 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
119 OpenGl_Matrix aCurrentMat;
120 glGetFloatv (GL_MODELVIEW_MATRIX, (GLfloat*)aCurrentMat.mat);
122 if (aMatrixMode != GL_MODELVIEW)
124 glMatrixMode (GL_MODELVIEW);
127 // load equation transform matrices
128 glLoadMatrixf ((theViewMatrix != NULL)
129 ? (const GLfloat*)theViewMatrix->mat
130 : (const GLfloat*)OpenGl_IdentityMatrix.mat);
132 Set (thePlanes, theToEnable);
134 // restore model-view matrix
135 glLoadMatrixf ((GLfloat*)aCurrentMat.mat);
137 // restore context matrix state
138 if (aMatrixMode != GL_MODELVIEW)
140 glMatrixMode (aMatrixMode);
144 // =======================================================================
147 // =======================================================================
148 void OpenGl_ClippingState::Unset (const Graphic3d_SetOfHClipPlane& thePlanes)
150 Graphic3d_SetOfHClipPlane::Iterator aPlaneIt (thePlanes);
151 for (; aPlaneIt.More(); aPlaneIt.Next())
153 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
157 Standard_Integer anId = myPlanes.Find (aPlane);
158 myEmptyPlaneIds->Free (anId);
159 myPlanes.UnBind (aPlane);
160 myPlaneStates.UnBind (aPlane);
162 const GLenum anOpenGlId = (GLenum)anId;
164 glDisable (anOpenGlId);
165 glClipPlane (anOpenGlId, OpenGl_DefaultPlaneEq);
170 //// =======================================================================
171 //// function : SetPlane
173 //// =======================================================================
174 //Standard_Boolean OpenGl_ClippingState::SetPlane (const Handle(Graphic3d_ClipPlane)& thePlane,
175 // const Standard_Boolean theToEnable)
177 // if (myEmptyPlaneIds->Available() == 0)
178 // return Standard_False;
180 // if (IsPlaneSet (thePlane))
181 // return Standard_True;
183 // Standard_Integer aPlaneId = myEmptyPlaneIds->Next();
184 // myPlanes.Bind (thePlane, aPlaneId);
185 // myPlaneStates.Bind (thePlane, theToEnable);
187 // glEnable (aPlaneId);
189 // glDisable (aPlaneId);
191 // glClipPlane (aPlaneId, thePlane->GetEquation());
193 // return Standard_True;
196 //// =======================================================================
197 //// function : UnsetPlane
199 //// =======================================================================
200 //void OpenGl_ClippingState::UnsetPlane (const Handle(Graphic3d_ClipPlane)& thePlane)
202 // if (!IsPlaneSet (thePlane))
205 // Standard_Integer aPlaneId = myPlanes.Find (thePlane);
207 // myEmptyPlaneIds->Free (aPlaneId);
208 // myPlanes.UnBind (thePlane);
209 // myPlaneStates.UnBind (thePlane);
211 // glDisable (aPlaneId);
212 // glClipPlane (aPlaneId, OpenGl_DefaultPlaneEq);
215 // =======================================================================
216 // function : IsEnabled
218 // =======================================================================
219 Standard_Boolean OpenGl_ClippingState::IsEnabled (const Handle(Graphic3d_ClipPlane)& thePlane) const
221 Standard_Boolean isSet;
222 return IsSet (thePlane)
223 && myPlaneStates.Find (thePlane, isSet)
227 // =======================================================================
228 // function : SetEnabled
230 // =======================================================================
231 void OpenGl_ClippingState::SetEnabled (const Handle(Graphic3d_ClipPlane)& thePlane,
232 const Standard_Boolean theIsEnabled)
234 if (!IsSet (thePlane))
237 Standard_Boolean& aState = myPlaneStates.ChangeFind (thePlane);
238 if (theIsEnabled == aState)
241 Standard_Integer aPlaneId = myPlanes.Find (thePlane);
245 glDisable (aPlaneId);
247 aState = theIsEnabled;