0023645: Modeling Algorithms User's Guide contains wrong statement about GeomFill_Con...
[occt.git] / src / OpenGl / OpenGl_Clipping.cxx
CommitLineData
4269bd1b 1// Created on: 2013-09-05
2// Created by: Anton POLETAEV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
4269bd1b 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
4269bd1b 6//
d5f74e42 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
973c2be1 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.
4269bd1b 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
4269bd1b 15
30f0ad28 16#include <OpenGl_Clipping.hxx>
4269bd1b 17#include <OpenGl_GlCore11.hxx>
b859a34d 18#include <OpenGl_Workspace.hxx>
4269bd1b 19
ca3c13d1 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
23#endif
24
4269bd1b 25// =======================================================================
26// function : OpenGl_ClippingState
27// purpose :
28// =======================================================================
30f0ad28 29OpenGl_Clipping::OpenGl_Clipping ()
ca3c13d1 30: myEmptyPlaneIds (new Aspect_GenId (GL_CLIP_PLANE0, GL_CLIP_PLANE0 + 5)),
3b1817a9 31 myNbClipping (0),
32 myNbCapping (0)
4269bd1b 33{}
34
35// =======================================================================
36// function : Init
37// purpose :
38// =======================================================================
30f0ad28 39void OpenGl_Clipping::Init (const Standard_Integer theMaxPlanes)
4269bd1b 40{
41 myPlanes.Clear();
42 myPlaneStates.Clear();
3b1817a9 43 myNbClipping = 0;
44 myNbCapping = 0;
4269bd1b 45 Standard_Integer aLowerId = GL_CLIP_PLANE0;
46 Standard_Integer aUpperId = GL_CLIP_PLANE0 + theMaxPlanes - 1;
47 myEmptyPlaneIds = new Aspect_GenId (aLowerId, aUpperId);
48}
49
50// =======================================================================
b859a34d 51// function : Add
4269bd1b 52// purpose :
53// =======================================================================
51b10cd4 54void OpenGl_Clipping::Add (Graphic3d_SequenceOfHClipPlane& thePlanes,
30f0ad28 55 const EquationCoords& theCoordSpace,
56 const Handle(OpenGl_Workspace)& theWS)
4269bd1b 57{
c827ea3a 58 Handle(OpenGl_Context) aContext = theWS->GetGlContext();
4269bd1b 59
c827ea3a 60 if (EquationCoords_View == theCoordSpace)
4269bd1b 61 {
c827ea3a 62 aContext->WorldViewState.Push();
63 aContext->WorldViewState.SetIdentity();
4269bd1b 64 }
65
c827ea3a 66 // Set either identity or pure view matrix.
67 aContext->ApplyWorldViewMatrix();
4269bd1b 68
b859a34d 69 Add (thePlanes, theCoordSpace);
4269bd1b 70
c827ea3a 71 if (EquationCoords_View == theCoordSpace)
4269bd1b 72 {
c827ea3a 73 aContext->WorldViewState.Pop();
4269bd1b 74 }
c827ea3a 75
76 // Restore combined model-view matrix.
77 aContext->ApplyModelViewMatrix();
78
4269bd1b 79}
80
81// =======================================================================
b859a34d 82// function : Add
4269bd1b 83// purpose :
84// =======================================================================
51b10cd4 85void OpenGl_Clipping::Add (Graphic3d_SequenceOfHClipPlane& thePlanes, const EquationCoords& theCoordSpace)
4269bd1b 86{
51b10cd4 87 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
a9568545 88 while (aPlaneIt.More() && myEmptyPlaneIds->HasFree())
4269bd1b 89 {
90 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
b859a34d 91 if (Contains (aPlane))
92 {
93 thePlanes.Remove (aPlaneIt);
4269bd1b 94 continue;
b859a34d 95 }
4269bd1b 96
b859a34d 97 Standard_Integer anID = myEmptyPlaneIds->Next();
51b10cd4 98 myPlanes.Append (aPlane);
b859a34d 99 myPlaneStates.Bind (aPlane, PlaneProps (theCoordSpace, anID, Standard_True));
4269bd1b 100
ca3c13d1 101 #if !defined(GL_ES_VERSION_2_0)
102 ::glEnable ((GLenum)anID);
103 ::glClipPlane ((GLenum)anID, aPlane->GetEquation());
104 #endif
3b1817a9 105 if (aPlane->IsCapping())
106 {
107 ++myNbCapping;
108 }
109 else
110 {
111 ++myNbClipping;
112 }
113
b859a34d 114 aPlaneIt.Next();
115 }
4269bd1b 116
a9568545 117 if (!myEmptyPlaneIds->HasFree())
b859a34d 118 {
a9568545 119 while (aPlaneIt.More())
120 {
121 thePlanes.Remove (aPlaneIt);
122 }
4269bd1b 123 }
124}
125
4269bd1b 126// =======================================================================
b859a34d 127// function : Remove
4269bd1b 128// purpose :
129// =======================================================================
51b10cd4 130void OpenGl_Clipping::Remove (const Graphic3d_SequenceOfHClipPlane& thePlanes)
4269bd1b 131{
51b10cd4 132 Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt (thePlanes);
b859a34d 133 for (; aPlaneIt.More(); aPlaneIt.Next())
134 {
135 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
136 if (!Contains (aPlane))
137 {
138 continue;
139 }
140
141 Standard_Integer anID = myPlaneStates.Find (aPlane).ContextID;
3b1817a9 142 PlaneProps& aProps = myPlaneStates.ChangeFind (aPlane);
143 if (aProps.IsEnabled)
144 {
ca3c13d1 145 #if !defined(GL_ES_VERSION_2_0)
3b1817a9 146 glDisable ((GLenum)anID);
ca3c13d1 147 #endif
3b1817a9 148 if (aPlane->IsCapping())
149 {
150 --myNbCapping;
151 }
152 else
153 {
154 --myNbClipping;
155 }
156 }
157
b859a34d 158 myEmptyPlaneIds->Free (anID);
159 myPlaneStates.UnBind (aPlane);
b859a34d 160 }
161
162 // renew collection of planes
163 aPlaneIt.Init (myPlanes);
164 while (aPlaneIt.More())
165 {
166 const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value();
167 if (!myPlaneStates.IsBound (aPlane))
168 {
169 myPlanes.Remove (aPlaneIt);
170 }
171 else
172 {
173 aPlaneIt.Next();
174 }
175 }
4269bd1b 176}
177
178// =======================================================================
179// function : SetEnabled
180// purpose :
181// =======================================================================
30f0ad28 182void OpenGl_Clipping::SetEnabled (const Handle(Graphic3d_ClipPlane)& thePlane,
51b10cd4 183 const Standard_Boolean theIsEnabled)
4269bd1b 184{
b859a34d 185 if (!Contains (thePlane))
186 {
4269bd1b 187 return;
b859a34d 188 }
4269bd1b 189
b859a34d 190 PlaneProps& aProps = myPlaneStates.ChangeFind (thePlane);
191 if (theIsEnabled == aProps.IsEnabled)
192 {
4269bd1b 193 return;
b859a34d 194 }
4269bd1b 195
ca3c13d1 196#if !defined(GL_ES_VERSION_2_0)
b859a34d 197 GLenum anID = (GLenum)aProps.ContextID;
ca3c13d1 198#endif
4269bd1b 199 if (theIsEnabled)
b859a34d 200 {
ca3c13d1 201 #if !defined(GL_ES_VERSION_2_0)
b859a34d 202 glEnable (anID);
ca3c13d1 203 #endif
3b1817a9 204 if (thePlane->IsCapping())
205 {
206 ++myNbCapping;
207 }
208 else
209 {
210 ++myNbClipping;
211 }
b859a34d 212 }
4269bd1b 213 else
b859a34d 214 {
ca3c13d1 215 #if !defined(GL_ES_VERSION_2_0)
b859a34d 216 glDisable (anID);
ca3c13d1 217 #endif
3b1817a9 218 if (thePlane->IsCapping())
219 {
220 --myNbCapping;
221 }
222 else
223 {
224 --myNbClipping;
225 }
b859a34d 226 }
4269bd1b 227
b859a34d 228 aProps.IsEnabled = theIsEnabled;
4269bd1b 229}