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 #if defined(GL_ES_VERSION_2_0)
21 // id does not matter for GLSL-based clipping, just for consistency
22 #define GL_CLIP_PLANE0 0x3000
25 // =======================================================================
26 // function : OpenGl_ClippingState
28 // =======================================================================
29 OpenGl_Clipping::OpenGl_Clipping ()
30 : myEmptyPlaneIds (new Aspect_GenId (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + 5)),
35 // =======================================================================
38 // =======================================================================
39 void OpenGl_Clipping::Init (const Standard_Integer theMaxPlanes)
42 myPlaneStates.Clear();
45 Standard_Integer aLowerId = GL_CLIP_PLANE0;
46 Standard_Integer aUpperId = GL_CLIP_PLANE0 + theMaxPlanes - 1;
47 myEmptyPlaneIds = new Aspect_GenId (aLowerId, aUpperId);
50 // =======================================================================
53 // =======================================================================
54 void OpenGl_Clipping::Add (Graphic3d_SequenceOfHClipPlane& thePlanes,
55 const EquationCoords& theCoordSpace,
56 const Handle(OpenGl_Workspace)& theWS)
58 Handle(OpenGl_Context) aContext = theWS->GetGlContext();
60 if (EquationCoords_View == theCoordSpace)
62 aContext->WorldViewState.Push();
63 aContext->WorldViewState.SetIdentity();
66 // Set either identity or pure view matrix.
67 aContext->ApplyWorldViewMatrix();
69 Add (thePlanes, theCoordSpace);
71 if (EquationCoords_View == theCoordSpace)
73 aContext->WorldViewState.Pop();
76 // Restore combined model-view matrix.
77 aContext->ApplyModelViewMatrix();
81 // =======================================================================
84 // =======================================================================
85 void OpenGl_Clipping::Add (Graphic3d_SequenceOfHClipPlane& thePlanes, const EquationCoords& theCoordSpace)
87 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
88 while (aPlaneIt.More() && myEmptyPlaneIds->HasFree())
90 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
91 if (Contains (aPlane))
93 thePlanes.Remove (aPlaneIt);
97 Standard_Integer anID = myEmptyPlaneIds->Next();
98 myPlanes.Append (aPlane);
99 myPlaneStates.Bind (aPlane, PlaneProps (theCoordSpace, anID, Standard_True));
101 #if !defined(GL_ES_VERSION_2_0)
102 ::glEnable ((GLenum)anID);
103 ::glClipPlane ((GLenum)anID, aPlane->GetEquation());
105 if (aPlane->IsCapping())
117 if (!myEmptyPlaneIds->HasFree())
119 while (aPlaneIt.More())
121 thePlanes.Remove (aPlaneIt);
126 // =======================================================================
129 // =======================================================================
130 void OpenGl_Clipping::Remove (const Graphic3d_SequenceOfHClipPlane& thePlanes)
132 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
133 for (; aPlaneIt.More(); aPlaneIt.Next())
135 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
136 if (!Contains (aPlane))
141 Standard_Integer anID = myPlaneStates.Find (aPlane).ContextID;
142 PlaneProps& aProps = myPlaneStates.ChangeFind (aPlane);
143 if (aProps.IsEnabled)
145 #if !defined(GL_ES_VERSION_2_0)
146 glDisable ((GLenum)anID);
148 if (aPlane->IsCapping())
158 myEmptyPlaneIds->Free (anID);
159 myPlaneStates.UnBind (aPlane);
162 // renew collection of planes
163 aPlaneIt.Init (myPlanes);
164 while (aPlaneIt.More())
166 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
167 if (!myPlaneStates.IsBound (aPlane))
169 myPlanes.Remove (aPlaneIt);
178 // =======================================================================
179 // function : SetEnabled
181 // =======================================================================
182 void OpenGl_Clipping::SetEnabled (const Handle(Graphic3d_ClipPlane)& thePlane,
183 const Standard_Boolean theIsEnabled)
185 if (!Contains (thePlane))
190 PlaneProps& aProps = myPlaneStates.ChangeFind (thePlane);
191 if (theIsEnabled == aProps.IsEnabled)
196 #if !defined(GL_ES_VERSION_2_0)
197 GLenum anID = (GLenum)aProps.ContextID;
201 #if !defined(GL_ES_VERSION_2_0)
204 if (thePlane->IsCapping())
215 #if !defined(GL_ES_VERSION_2_0)
218 if (thePlane->IsCapping())
228 aProps.IsEnabled = theIsEnabled;