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_Clipping.hxx>
21 #include <OpenGl_GlCore11.hxx>
22 #include <OpenGl_Workspace.hxx>
26 static const GLdouble OpenGl_DefaultPlaneEq[] = {0.0, 0.0, 0.0, 0.0};
29 // =======================================================================
30 // function : OpenGl_ClippingState
32 // =======================================================================
33 OpenGl_Clipping::OpenGl_Clipping ()
36 myEmptyPlaneIds (new Aspect_GenId (GL_CLIP_PLANE0, GL_CLIP_PLANE5))
39 // =======================================================================
42 // =======================================================================
43 void OpenGl_Clipping::Init (const Standard_Integer theMaxPlanes)
46 myPlaneStates.Clear();
47 Standard_Integer aLowerId = GL_CLIP_PLANE0;
48 Standard_Integer aUpperId = GL_CLIP_PLANE0 + theMaxPlanes - 1;
49 myEmptyPlaneIds = new Aspect_GenId (aLowerId, aUpperId);
52 // =======================================================================
55 // =======================================================================
56 void OpenGl_Clipping::Add (Graphic3d_SetOfHClipPlane& thePlanes,
57 const EquationCoords& theCoordSpace,
58 const Handle(OpenGl_Workspace)& theWS)
61 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
63 OpenGl_Matrix aCurrentMx;
64 glGetFloatv (GL_MODELVIEW_MATRIX, (GLfloat*) &aCurrentMx);
66 if (aMatrixMode != GL_MODELVIEW)
68 glMatrixMode (GL_MODELVIEW);
71 switch (theCoordSpace)
73 case EquationCoords_View: glLoadMatrixf ((const GLfloat*) &OpenGl_IdentityMatrix); break;
74 case EquationCoords_World: glLoadMatrixf ((const GLfloat*) theWS->ViewMatrix()); break;
77 Add (thePlanes, theCoordSpace);
79 // restore model-view matrix
80 glLoadMatrixf ((GLfloat*) &aCurrentMx);
82 // restore context matrix state
83 if (aMatrixMode != GL_MODELVIEW)
85 glMatrixMode (aMatrixMode);
89 // =======================================================================
92 // =======================================================================
93 void OpenGl_Clipping::Add (Graphic3d_SetOfHClipPlane& thePlanes, const EquationCoords& theCoordSpace)
95 Graphic3d_SetOfHClipPlane::Iterator aPlaneIt (thePlanes);
96 while (aPlaneIt.More() && myEmptyPlaneIds->Available() > 0)
98 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
99 if (Contains (aPlane))
101 thePlanes.Remove (aPlaneIt);
105 Standard_Integer anID = myEmptyPlaneIds->Next();
106 myPlanes.Add (aPlane);
107 myPlaneStates.Bind (aPlane, PlaneProps (theCoordSpace, anID, Standard_True));
109 glEnable ((GLenum)anID);
110 glClipPlane ((GLenum)anID, aPlane->GetEquation());
114 while (aPlaneIt.More() && myEmptyPlaneIds->Available() == 0)
116 thePlanes.Remove (aPlaneIt);
120 // =======================================================================
123 // =======================================================================
124 void OpenGl_Clipping::Remove (const Graphic3d_SetOfHClipPlane& thePlanes)
126 Graphic3d_SetOfHClipPlane::Iterator aPlaneIt (thePlanes);
127 for (; aPlaneIt.More(); aPlaneIt.Next())
129 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
130 if (!Contains (aPlane))
135 Standard_Integer anID = myPlaneStates.Find (aPlane).ContextID;
136 myEmptyPlaneIds->Free (anID);
137 myPlaneStates.UnBind (aPlane);
139 glDisable ((GLenum)anID);
142 // renew collection of planes
143 aPlaneIt.Init (myPlanes);
144 while (aPlaneIt.More())
146 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
147 if (!myPlaneStates.IsBound (aPlane))
149 myPlanes.Remove (aPlaneIt);
158 // =======================================================================
159 // function : SetEnabled
161 // =======================================================================
162 void OpenGl_Clipping::SetEnabled (const Handle(Graphic3d_ClipPlane)& thePlane,
163 const Standard_Boolean theIsEnabled)
165 if (!Contains (thePlane))
170 PlaneProps& aProps = myPlaneStates.ChangeFind (thePlane);
171 if (theIsEnabled == aProps.IsEnabled)
176 GLenum anID = (GLenum)aProps.ContextID;
186 aProps.IsEnabled = theIsEnabled;