1 // Created on: 2013-09-05
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_Clipping.hxx>
17 #include <OpenGl_GlCore11.hxx>
18 #include <OpenGl_Workspace.hxx>
20 // =======================================================================
21 // function : OpenGl_ClippingState
23 // =======================================================================
24 OpenGl_Clipping::OpenGl_Clipping ()
25 : myEmptyPlaneIds (new Aspect_GenId (GL_CLIP_PLANE0, GL_CLIP_PLANE5)),
30 // =======================================================================
33 // =======================================================================
34 void OpenGl_Clipping::Init (const Standard_Integer theMaxPlanes)
37 myPlaneStates.Clear();
40 Standard_Integer aLowerId = GL_CLIP_PLANE0;
41 Standard_Integer aUpperId = GL_CLIP_PLANE0 + theMaxPlanes - 1;
42 myEmptyPlaneIds = new Aspect_GenId (aLowerId, aUpperId);
45 // =======================================================================
48 // =======================================================================
49 void OpenGl_Clipping::Add (Graphic3d_SequenceOfHClipPlane& thePlanes,
50 const EquationCoords& theCoordSpace,
51 const Handle(OpenGl_Workspace)& theWS)
54 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
56 OpenGl_Matrix aCurrentMx;
57 glGetFloatv (GL_MODELVIEW_MATRIX, (GLfloat*) &aCurrentMx);
59 if (aMatrixMode != GL_MODELVIEW)
61 glMatrixMode (GL_MODELVIEW);
64 switch (theCoordSpace)
66 case EquationCoords_View: glLoadMatrixf ((const GLfloat*) &OpenGl_IdentityMatrix); break;
67 case EquationCoords_World: glLoadMatrixf ((const GLfloat*) theWS->ViewMatrix()); break;
70 Add (thePlanes, theCoordSpace);
72 // restore model-view matrix
73 glLoadMatrixf ((GLfloat*) &aCurrentMx);
75 // restore context matrix state
76 if (aMatrixMode != GL_MODELVIEW)
78 glMatrixMode (aMatrixMode);
82 // =======================================================================
85 // =======================================================================
86 void OpenGl_Clipping::Add (Graphic3d_SequenceOfHClipPlane& thePlanes, const EquationCoords& theCoordSpace)
88 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
89 while (aPlaneIt.More() && myEmptyPlaneIds->Available() > 0)
91 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
92 if (Contains (aPlane))
94 thePlanes.Remove (aPlaneIt);
98 Standard_Integer anID = myEmptyPlaneIds->Next();
99 myPlanes.Append (aPlane);
100 myPlaneStates.Bind (aPlane, PlaneProps (theCoordSpace, anID, Standard_True));
102 glEnable ((GLenum)anID);
103 glClipPlane ((GLenum)anID, aPlane->GetEquation());
104 if (aPlane->IsCapping())
116 while (aPlaneIt.More() && myEmptyPlaneIds->Available() == 0)
118 thePlanes.Remove (aPlaneIt);
122 // =======================================================================
125 // =======================================================================
126 void OpenGl_Clipping::Remove (const Graphic3d_SequenceOfHClipPlane& thePlanes)
128 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
129 for (; aPlaneIt.More(); aPlaneIt.Next())
131 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
132 if (!Contains (aPlane))
137 Standard_Integer anID = myPlaneStates.Find (aPlane).ContextID;
138 PlaneProps& aProps = myPlaneStates.ChangeFind (aPlane);
139 if (aProps.IsEnabled)
141 glDisable ((GLenum)anID);
142 if (aPlane->IsCapping())
152 myEmptyPlaneIds->Free (anID);
153 myPlaneStates.UnBind (aPlane);
156 // renew collection of planes
157 aPlaneIt.Init (myPlanes);
158 while (aPlaneIt.More())
160 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
161 if (!myPlaneStates.IsBound (aPlane))
163 myPlanes.Remove (aPlaneIt);
172 // =======================================================================
173 // function : SetEnabled
175 // =======================================================================
176 void OpenGl_Clipping::SetEnabled (const Handle(Graphic3d_ClipPlane)& thePlane,
177 const Standard_Boolean theIsEnabled)
179 if (!Contains (thePlane))
184 PlaneProps& aProps = myPlaneStates.ChangeFind (thePlane);
185 if (theIsEnabled == aProps.IsEnabled)
190 GLenum anID = (GLenum)aProps.ContextID;
194 if (thePlane->IsCapping())
206 if (thePlane->IsCapping())
216 aProps.IsEnabled = theIsEnabled;