0025722: Wrong implementation of SetFuzzyValue method
[occt.git] / src / OpenGl / OpenGl_LayerList.cxx
CommitLineData
6aca4d39 1// Created on: 2012-02-02
b311480e 2// Created by: Anton POLETAEV
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
59f45b7c 15
5f8b738e 16#include <OpenGl_GlCore11.hxx>
17
59f45b7c 18#include <OpenGl_LayerList.hxx>
59f45b7c 19#include <OpenGl_Structure.hxx>
550f3b8b 20#include <OpenGl_Workspace.hxx>
59f45b7c 21
22#include <InterfaceGraphic_Graphic3d.hxx>
23#include <InterfaceGraphic.hxx>
24
25//=======================================================================
26//function : OpenGl_LayerList
27//purpose : Constructor
28//=======================================================================
29
30OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
31 : myNbPriorities (theNbPriorities),
32 myNbStructures (0)
33{
34 // insert default priority layer
c5751993 35 myLayers.Append (OpenGl_Layer (myNbPriorities));
59f45b7c 36 myLayerIds.Bind (0, myLayers.Length());
37}
38
39//=======================================================================
40//function : ~OpenGl_LayerList
41//purpose : Destructor
42//=======================================================================
43
44OpenGl_LayerList::~OpenGl_LayerList ()
45{
46}
47
48//=======================================================================
49//function : defaultLayer
50//purpose :
51//=======================================================================
52
c5751993 53OpenGl_Layer& OpenGl_LayerList::defaultLayer()
59f45b7c 54{
55 return myLayers.ChangeValue (1);
56}
57
58//=======================================================================
59//function : NbPriorities
60//purpose : Method returns the number of available priorities
61//=======================================================================
62
63Standard_Integer OpenGl_LayerList::NbPriorities () const
64{
65 return myNbPriorities;
66}
67
68//=======================================================================
69//function : NbStructures
70//purpose : Method returns the number of available structures
71//=======================================================================
72
73Standard_Integer OpenGl_LayerList::NbStructures () const
74{
75 return myNbStructures;
76}
77
78//=======================================================================
79//function : AddLayer
80//purpose :
81//=======================================================================
82
83void OpenGl_LayerList::AddLayer (const Standard_Integer theLayerId)
84{
85 if (HasLayer (theLayerId))
86 return;
87
88 // add the new layer
c5751993 89 myLayers.Append (OpenGl_Layer (myNbPriorities));
59f45b7c 90 myLayerIds.Bind (theLayerId, myLayers.Length());
91}
92
93//=======================================================================
94//function : HasLayer
95//purpose :
96//=======================================================================
97
98Standard_Boolean OpenGl_LayerList::HasLayer
99 (const Standard_Integer theLayerId) const
100{
101 return myLayerIds.IsBound (theLayerId);
102}
103
104//=======================================================================
c5751993 105//function : Layer
106//purpose :
107//=======================================================================
108OpenGl_Layer& OpenGl_LayerList::Layer (const Standard_Integer theLayerId)
109{
110 return myLayers.ChangeValue (myLayerIds.Find (theLayerId));
111}
112
113//=======================================================================
114//function : Layer
115//purpose :
116//=======================================================================
117const OpenGl_Layer& OpenGl_LayerList::Layer (const Standard_Integer theLayerId) const
118{
119 return myLayers.Value (myLayerIds.Find (theLayerId));
120}
121
122//=======================================================================
59f45b7c 123//function : RemoveLayer
124//purpose :
125//=======================================================================
126
127void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
128{
129 if (!HasLayer (theLayerId) || theLayerId == 0)
130 return;
131
132 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
133
134 // move all displayed structures to first layer
c5751993 135 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos).PriorityList();
136 defaultLayer ().PriorityList().Append (aList);
59f45b7c 137
138 // remove layer
139 myLayers.Remove (aRemovePos);
140 myLayerIds.UnBind (theLayerId);
141
142 // updated sequence indexes in map
143 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
144 for ( ; aMapIt.More (); aMapIt.Next ())
145 {
146 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
147 if (aSeqIdx > aRemovePos)
148 aSeqIdx--;
149 }
150}
151
152//=======================================================================
153//function : AddStructure
154//purpose :
155//=======================================================================
156
157void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
158 const Standard_Integer theLayerId,
b7cd4ba7 159 const Standard_Integer thePriority,
160 Standard_Boolean isForChangePriority)
59f45b7c 161{
162 // add structure to associated layer,
163 // if layer doesn't exists, display structure in default layer
c5751993 164 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer ().PriorityList() :
165 myLayers.ChangeValue (myLayerIds.Find (theLayerId)).PriorityList();
59f45b7c 166
b7cd4ba7 167 aList.Add (theStructure, thePriority, isForChangePriority);
59f45b7c 168 myNbStructures++;
e276548b 169
170 // Note: In ray-tracing mode we don't modify modification
171 // state here. It is redundant, because the possible changes
172 // will be handled in the loop for structures
59f45b7c 173}
174
175//=======================================================================
176//function : RemoveStructure
177//purpose :
178//=======================================================================
179
180void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
181 const Standard_Integer theLayerId)
182{
183 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
184 1 : myLayerIds.Find (theLayerId);
185
c5751993 186 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
59f45b7c 187
188 // remove structure from associated list
189 // if the structure is not found there,
190 // scan through layers and remove it
191 if (aList.Remove (theStructure) >= 0)
192 {
193 myNbStructures--;
e276548b 194
e276548b 195 if (theStructure->IsRaytracable())
196 {
197 myModificationState++;
198 }
e276548b 199
59f45b7c 200 return;
201 }
202
203 // scan through layers and remove it
204 Standard_Integer aSeqId = 1;
205 OpenGl_SequenceOfLayers::Iterator anIts;
206 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
207 {
c5751993 208 OpenGl_PriorityList& aScanList = anIts.ChangeValue ().PriorityList();
59f45b7c 209 if (aSeqPos == aSeqId)
210 continue;
211
212 if (aScanList.Remove (theStructure) >= 0)
213 {
214 myNbStructures--;
e276548b 215
e276548b 216 if (theStructure->IsRaytracable())
217 {
218 myModificationState++;
219 }
e276548b 220
59f45b7c 221 return;
222 }
223 }
224}
225
226//=======================================================================
b7cd4ba7 227//function : InvalidateBVHData
228//purpose :
229//=======================================================================
230void OpenGl_LayerList::InvalidateBVHData (const Standard_Integer theLayerId)
231{
232 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
233 1 : myLayerIds.Find (theLayerId);
234
235 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
236
237 aList.InvalidateBVHData();
238}
239
240//=======================================================================
59f45b7c 241//function : ChangeLayer
242//purpose :
243//=======================================================================
244
245void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
246 const Standard_Integer theOldLayerId,
247 const Standard_Integer theNewLayerId)
248{
249 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
250 1 : myLayerIds.Find (theOldLayerId);
251
c5751993 252 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
59f45b7c 253 Standard_Integer aPriority;
254
255 // take priority and remove structure from list found by <theOldLayerId>
256 // if the structure is not found there, scan through all other layers
b7cd4ba7 257 if ((aPriority = aList.Remove (theStructure, Standard_True)) >= 0)
59f45b7c 258 {
259 myNbStructures--;
a0c20252 260 // isForChangePriority should be Standard_False below, because we want
261 // the BVH tree in the target layer to be updated with theStructure
262 AddStructure (theStructure, theNewLayerId, aPriority);
59f45b7c 263 }
264 else
265 {
266 // scan through layers and remove it
267 Standard_Integer aSeqId = 1;
268 OpenGl_SequenceOfLayers::Iterator anIts;
269 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
270 {
59f45b7c 271 if (aSeqPos == aSeqId)
272 continue;
273
274 // try to remove structure and get priority value from this layer
b7cd4ba7 275 if ((aPriority = aList.Remove (theStructure, Standard_True)) >= 0)
276 {
277 myNbStructures--;
a0c20252 278 // isForChangePriority should be Standard_False below, because we want
279 // the BVH tree in the target layer to be updated with theStructure
280 AddStructure (theStructure, theNewLayerId, aPriority);
b7cd4ba7 281 break;
282 }
283 }
284 }
285}
286
287//=======================================================================
288//function : ChangePriority
289//purpose :
290//=======================================================================
291void OpenGl_LayerList::ChangePriority (const OpenGl_Structure *theStructure,
292 const Standard_Integer theLayerId,
293 const Standard_Integer theNewPriority)
294{
295 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
296 1 : myLayerIds.Find (theLayerId);
297
298 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
299
300 if (aList.Remove (theStructure, Standard_True) >= 0)
301 {
302 myNbStructures--;
303 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
304 }
305 else
306 {
307 Standard_Integer aSeqId = 1;
308 OpenGl_SequenceOfLayers::Iterator anIts;
309 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
310 {
311 if (aSeqPos == aSeqId)
312 continue;
313
314 if (aList.Remove (theStructure, Standard_True) >= 0)
59f45b7c 315 {
316 myNbStructures--;
b7cd4ba7 317 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
59f45b7c 318 break;
319 }
320 }
321 }
322}
323
324//=======================================================================
325//function : Render
326//purpose : Render this element
327//=======================================================================
328
329void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
330{
550f3b8b 331 OpenGl_GlobalLayerSettings aDefaultSettings;
332
333 glGetIntegerv (GL_DEPTH_FUNC, &aDefaultSettings.DepthFunc);
334 glGetBooleanv (GL_DEPTH_WRITEMASK, &aDefaultSettings.DepthMask);
c5751993 335
59f45b7c 336 OpenGl_SequenceOfLayers::Iterator anIts;
550f3b8b 337 for (anIts.Init (myLayers); anIts.More(); anIts.Next())
59f45b7c 338 {
c5751993 339 const OpenGl_Layer& aLayer = anIts.Value ();
340 if (aLayer.PriorityList().NbStructures () > 0)
59f45b7c 341 {
c5751993 342 // render layer
550f3b8b 343 aLayer.Render (theWorkspace, aDefaultSettings);
59f45b7c 344 }
345 }
550f3b8b 346
347 glDepthMask (aDefaultSettings.DepthMask);
348 glDepthFunc (aDefaultSettings.DepthFunc);
59f45b7c 349}