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>
18 #include <OpenGl_GlCore11.hxx>
19 #include <OpenGl_Workspace.hxx>
20 #include <OpenGl_Context.hxx>
22 #if defined(GL_ES_VERSION_2_0)
23 // id does not matter for GLSL-based clipping, just for consistency
24 #define GL_CLIP_PLANE0 0x3000
27 // =======================================================================
28 // function : OpenGl_ClippingState
30 // =======================================================================
31 OpenGl_Clipping::OpenGl_Clipping ()
32 : myEmptyPlaneIds (new Aspect_GenId (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + 5)),
37 // =======================================================================
40 // =======================================================================
41 void OpenGl_Clipping::Init (const Standard_Integer theMaxPlanes)
44 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 (const Handle(OpenGl_Context)& theGlCtx,
57 const EquationCoords& theCoordSpace,
58 Graphic3d_SequenceOfHClipPlane& thePlanes)
60 const bool toUseFfp = theGlCtx->core11 != NULL
61 && theGlCtx->caps->ffpEnable;
64 addLazy (theGlCtx, theCoordSpace, thePlanes);
68 if (EquationCoords_View == theCoordSpace)
70 theGlCtx->WorldViewState.Push();
71 theGlCtx->WorldViewState.SetIdentity();
74 // Set either identity or pure view matrix.
75 theGlCtx->ApplyWorldViewMatrix();
77 addLazy (theGlCtx, theCoordSpace, thePlanes);
79 if (EquationCoords_View == theCoordSpace)
81 theGlCtx->WorldViewState.Pop();
84 // Restore combined model-view matrix.
85 theGlCtx->ApplyModelViewMatrix();
88 // =======================================================================
91 // =======================================================================
92 void OpenGl_Clipping::addLazy (const Handle(OpenGl_Context)& theGlCtx,
93 const EquationCoords& theCoordSpace,
94 Graphic3d_SequenceOfHClipPlane& thePlanes)
96 #if !defined(GL_ES_VERSION_2_0)
97 const bool toUseFfp = theGlCtx->core11 != NULL
98 && theGlCtx->caps->ffpEnable;
103 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
104 while (aPlaneIt.More() && myEmptyPlaneIds->HasFree())
106 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
107 if (Contains (aPlane))
109 thePlanes.Remove (aPlaneIt);
113 Standard_Integer anID = myEmptyPlaneIds->Next();
114 myPlanes.Append (aPlane);
115 myPlaneStates.Bind (aPlane, PlaneProps (theCoordSpace, anID, Standard_True));
117 #if !defined(GL_ES_VERSION_2_0)
120 ::glEnable ((GLenum)anID);
121 theGlCtx->core11->glClipPlane ((GLenum)anID, aPlane->GetEquation());
124 if (aPlane->IsCapping())
136 if (!myEmptyPlaneIds->HasFree())
138 while (aPlaneIt.More())
140 thePlanes.Remove (aPlaneIt);
145 // =======================================================================
148 // =======================================================================
149 void OpenGl_Clipping::Remove (const Handle(OpenGl_Context)& theGlCtx,
150 const Graphic3d_SequenceOfHClipPlane& thePlanes)
152 #if !defined(GL_ES_VERSION_2_0)
153 const bool toUseFfp = theGlCtx->core11 != NULL
154 && theGlCtx->caps->ffpEnable;
159 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
160 for (; aPlaneIt.More(); aPlaneIt.Next())
162 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
163 if (!Contains (aPlane))
168 Standard_Integer anID = myPlaneStates.Find (aPlane).ContextID;
169 PlaneProps& aProps = myPlaneStates.ChangeFind (aPlane);
170 if (aProps.IsEnabled)
172 #if !defined(GL_ES_VERSION_2_0)
175 ::glDisable ((GLenum)anID);
178 if (aPlane->IsCapping())
188 myEmptyPlaneIds->Free (anID);
189 myPlaneStates.UnBind (aPlane);
192 // renew collection of planes
193 aPlaneIt.Init (myPlanes);
194 while (aPlaneIt.More())
196 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
197 if (!myPlaneStates.IsBound (aPlane))
199 myPlanes.Remove (aPlaneIt);
208 // =======================================================================
209 // function : SetEnabled
211 // =======================================================================
212 void OpenGl_Clipping::SetEnabled (const Handle(OpenGl_Context)& theGlCtx,
213 const Handle(Graphic3d_ClipPlane)& thePlane,
214 const Standard_Boolean theIsEnabled)
216 if (!Contains (thePlane))
221 PlaneProps& aProps = myPlaneStates.ChangeFind (thePlane);
222 if (theIsEnabled == aProps.IsEnabled)
227 #if !defined(GL_ES_VERSION_2_0)
228 GLenum anID = (GLenum)aProps.ContextID;
229 const bool toUseFfp = theGlCtx->core11 != NULL
230 && theGlCtx->caps->ffpEnable;
236 #if !defined(GL_ES_VERSION_2_0)
242 if (thePlane->IsCapping())
253 #if !defined(GL_ES_VERSION_2_0)
259 if (thePlane->IsCapping())
269 aProps.IsEnabled = theIsEnabled;