0027684: Coding rules - drop unused declarations from Graphic3d
[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
c04c30b3 22#include <Graphic3d_GraphicDriver.hxx>
59f45b7c 23
24//=======================================================================
25//function : OpenGl_LayerList
26//purpose : Constructor
27//=======================================================================
28
29OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
a1954302 30: myNbPriorities (theNbPriorities),
31 myNbStructures (0),
32 myImmediateNbStructures (0)
59f45b7c 33{
a1954302 34 // insert default priority layers
c5751993 35 myLayers.Append (OpenGl_Layer (myNbPriorities));
af65fb19 36 myLayerIds.Bind (Graphic3d_ZLayerId_BotOSD, myLayers.Upper());
37
38 myLayers.Append (OpenGl_Layer (myNbPriorities));
a1954302 39 myLayerIds.Bind (Graphic3d_ZLayerId_Default, myLayers.Upper());
59f45b7c 40
a1954302 41 myLayers.Append (OpenGl_Layer (myNbPriorities));
42 myLayerIds.Bind (Graphic3d_ZLayerId_Top, myLayers.Upper());
59f45b7c 43
a1954302 44 myLayers.Append (OpenGl_Layer (myNbPriorities));
45 myLayerIds.Bind (Graphic3d_ZLayerId_Topmost, myLayers.Upper());
59f45b7c 46
a1954302 47 myLayers.Append (OpenGl_Layer (myNbPriorities));
48 myLayerIds.Bind (Graphic3d_ZLayerId_TopOSD, myLayers.Upper());
59f45b7c 49}
50
51//=======================================================================
a1954302 52//function : ~OpenGl_LayerList
53//purpose : Destructor
59f45b7c 54//=======================================================================
55
a1954302 56OpenGl_LayerList::~OpenGl_LayerList()
59f45b7c 57{
59f45b7c 58}
59
60//=======================================================================
61//function : AddLayer
62//purpose :
63//=======================================================================
64
a1954302 65void OpenGl_LayerList::AddLayer (const Graphic3d_ZLayerId theLayerId)
59f45b7c 66{
a1954302 67 if (myLayerIds.IsBound (theLayerId))
68 {
59f45b7c 69 return;
a1954302 70 }
59f45b7c 71
72 // add the new layer
c5751993 73 myLayers.Append (OpenGl_Layer (myNbPriorities));
59f45b7c 74 myLayerIds.Bind (theLayerId, myLayers.Length());
75}
76
77//=======================================================================
c5751993 78//function : Layer
79//purpose :
80//=======================================================================
a1954302 81OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId)
c5751993 82{
83 return myLayers.ChangeValue (myLayerIds.Find (theLayerId));
84}
85
86//=======================================================================
87//function : Layer
88//purpose :
89//=======================================================================
a1954302 90const OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId) const
c5751993 91{
92 return myLayers.Value (myLayerIds.Find (theLayerId));
93}
94
95//=======================================================================
59f45b7c 96//function : RemoveLayer
97//purpose :
98//=======================================================================
99
a1954302 100void OpenGl_LayerList::RemoveLayer (const Graphic3d_ZLayerId theLayerId)
59f45b7c 101{
a1954302 102 if (!myLayerIds.IsBound (theLayerId)
103 || theLayerId <= 0)
104 {
59f45b7c 105 return;
a1954302 106 }
59f45b7c 107
a1954302 108 const Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
59f45b7c 109
110 // move all displayed structures to first layer
a1954302 111 const OpenGl_Layer& aLayerToMove = myLayers.Value (aRemovePos);
112 myLayers.ChangeFirst().Append (aLayerToMove);
59f45b7c 113
114 // remove layer
115 myLayers.Remove (aRemovePos);
116 myLayerIds.UnBind (theLayerId);
117
118 // updated sequence indexes in map
119 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
120 for ( ; aMapIt.More (); aMapIt.Next ())
121 {
122 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
123 if (aSeqIdx > aRemovePos)
124 aSeqIdx--;
125 }
126}
127
128//=======================================================================
129//function : AddStructure
130//purpose :
131//=======================================================================
132
a1954302 133void OpenGl_LayerList::AddStructure (const OpenGl_Structure* theStruct,
134 const Graphic3d_ZLayerId theLayerId,
135 const Standard_Integer thePriority,
136 Standard_Boolean isForChangePriority)
59f45b7c 137{
138 // add structure to associated layer,
139 // if layer doesn't exists, display structure in default layer
a1954302 140 Standard_Integer aSeqPos = myLayers.Lower();
141 myLayerIds.Find (theLayerId, aSeqPos);
59f45b7c 142
a1954302 143 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
144 aLayer.Add (theStruct, thePriority, isForChangePriority);
145 ++myNbStructures;
146 if (aLayer.LayerSettings().IsImmediate)
147 {
148 ++myImmediateNbStructures;
149 }
e276548b 150
151 // Note: In ray-tracing mode we don't modify modification
152 // state here. It is redundant, because the possible changes
153 // will be handled in the loop for structures
59f45b7c 154}
155
156//=======================================================================
157//function : RemoveStructure
a1954302 158//purpose :
59f45b7c 159//=======================================================================
160
c357e426 161void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure* theStructure)
59f45b7c 162{
c357e426 163 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
a1954302 164
165 Standard_Integer aSeqPos = myLayers.Lower();
166 myLayerIds.Find (aLayerId, aSeqPos);
167
168 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
169 Standard_Integer aPriority = -1;
59f45b7c 170
171 // remove structure from associated list
172 // if the structure is not found there,
173 // scan through layers and remove it
c357e426 174 if (aLayer.Remove (theStructure, aPriority))
59f45b7c 175 {
a1954302 176 --myNbStructures;
177 if (aLayer.LayerSettings().IsImmediate)
178 {
179 --myImmediateNbStructures;
180 }
e276548b 181
c357e426 182 if (theStructure->IsRaytracable())
e276548b 183 {
a1954302 184 ++myModificationState;
e276548b 185 }
e276548b 186
59f45b7c 187 return;
188 }
a1954302 189
59f45b7c 190 // scan through layers and remove it
191 Standard_Integer aSeqId = 1;
a1954302 192 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
59f45b7c 193 {
a1954302 194 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
59f45b7c 195 if (aSeqPos == aSeqId)
59f45b7c 196 {
a1954302 197 continue;
198 }
e276548b 199
c357e426 200 if (aLayerEx.Remove (theStructure, aPriority))
a1954302 201 {
202 --myNbStructures;
203 if (aLayerEx.LayerSettings().IsImmediate)
e276548b 204 {
a1954302 205 --myImmediateNbStructures;
e276548b 206 }
e276548b 207
c357e426 208 if (theStructure->IsRaytracable())
a1954302 209 {
210 ++myModificationState;
211 }
59f45b7c 212 return;
213 }
214 }
215}
216
217//=======================================================================
b7cd4ba7 218//function : InvalidateBVHData
219//purpose :
220//=======================================================================
a1954302 221void OpenGl_LayerList::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
b7cd4ba7 222{
a1954302 223 Standard_Integer aSeqPos = myLayers.Lower();
224 myLayerIds.Find (theLayerId, aSeqPos);
225 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
226 aLayer.InvalidateBVHData();
b7cd4ba7 227}
228
229//=======================================================================
59f45b7c 230//function : ChangeLayer
231//purpose :
232//=======================================================================
233
a1954302 234void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure* theStructure,
235 const Graphic3d_ZLayerId theOldLayerId,
236 const Graphic3d_ZLayerId theNewLayerId)
59f45b7c 237{
a1954302 238 Standard_Integer aSeqPos = myLayers.Lower();
239 myLayerIds.Find (theOldLayerId, aSeqPos);
240 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
241 Standard_Integer aPriority = -1;
59f45b7c 242
243 // take priority and remove structure from list found by <theOldLayerId>
244 // if the structure is not found there, scan through all other layers
a1954302 245 if (aLayer.Remove (theStructure, aPriority, Standard_True))
59f45b7c 246 {
a1954302 247 --myNbStructures;
248 if (aLayer.LayerSettings().IsImmediate)
249 {
250 --myImmediateNbStructures;
251 }
252
a0c20252 253 // isForChangePriority should be Standard_False below, because we want
254 // the BVH tree in the target layer to be updated with theStructure
255 AddStructure (theStructure, theNewLayerId, aPriority);
a1954302 256 return;
59f45b7c 257 }
a1954302 258
259 // scan through layers and remove it
260 Standard_Integer aSeqId = 1;
261 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
59f45b7c 262 {
a1954302 263 if (aSeqPos == aSeqId)
59f45b7c 264 {
a1954302 265 continue;
266 }
59f45b7c 267
a1954302 268 // try to remove structure and get priority value from this layer
269 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
270 if (aLayerEx.Remove (theStructure, aPriority, Standard_True))
271 {
272 --myNbStructures;
273 if (aLayerEx.LayerSettings().IsImmediate)
b7cd4ba7 274 {
a1954302 275 --myImmediateNbStructures;
b7cd4ba7 276 }
a1954302 277
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);
281 return;
b7cd4ba7 282 }
283 }
284}
285
286//=======================================================================
287//function : ChangePriority
288//purpose :
289//=======================================================================
a1954302 290void OpenGl_LayerList::ChangePriority (const OpenGl_Structure* theStructure,
291 const Graphic3d_ZLayerId theLayerId,
292 const Standard_Integer theNewPriority)
b7cd4ba7 293{
a1954302 294 Standard_Integer aSeqPos = myLayers.Lower();
295 myLayerIds.Find (theLayerId, aSeqPos);
296 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
297 Standard_Integer anOldPriority = -1;
b7cd4ba7 298
a1954302 299 if (aLayer.Remove (theStructure, anOldPriority, Standard_True))
b7cd4ba7 300 {
a1954302 301 --myNbStructures;
302 if (aLayer.LayerSettings().IsImmediate)
303 {
304 --myImmediateNbStructures;
305 }
306
b7cd4ba7 307 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
a1954302 308 return;
b7cd4ba7 309 }
a1954302 310
311 Standard_Integer aSeqId = 1;
312 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
b7cd4ba7 313 {
a1954302 314 if (aSeqPos == aSeqId)
b7cd4ba7 315 {
a1954302 316 continue;
317 }
b7cd4ba7 318
a1954302 319 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
320 if (aLayerEx.Remove (theStructure, anOldPriority, Standard_True))
321 {
322 --myNbStructures;
323 if (aLayerEx.LayerSettings().IsImmediate)
59f45b7c 324 {
a1954302 325 --myImmediateNbStructures;
59f45b7c 326 }
a1954302 327
328 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
329 return;
59f45b7c 330 }
331 }
332}
333
334//=======================================================================
a1954302 335//function : SetLayerSettings
336//purpose :
59f45b7c 337//=======================================================================
a1954302 338void OpenGl_LayerList::SetLayerSettings (const Graphic3d_ZLayerId theLayerId,
339 const Graphic3d_ZLayerSettings& theSettings)
340{
341 OpenGl_Layer& aLayer = Layer (theLayerId);
342 if (aLayer.LayerSettings().IsImmediate != theSettings.IsImmediate)
343 {
344 if (theSettings.IsImmediate)
345 {
346 myImmediateNbStructures += aLayer.NbStructures();
347 }
348 else
349 {
350 myImmediateNbStructures -= aLayer.NbStructures();
351 }
352 }
353 aLayer.SetLayerSettings (theSettings);
354}
59f45b7c 355
a1954302 356//=======================================================================
357//function : Render
358//purpose :
359//=======================================================================
360void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace)& theWorkspace,
91c60b57 361 const Standard_Boolean theToDrawImmediate,
362 const OpenGl_LayerFilter theLayersToProcess) const
59f45b7c 363{
550f3b8b 364 OpenGl_GlobalLayerSettings aDefaultSettings;
c5751993 365
a1954302 366 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
367 aCtx->core11fwd->glGetIntegerv (GL_DEPTH_FUNC, &aDefaultSettings.DepthFunc);
368 aCtx->core11fwd->glGetBooleanv (GL_DEPTH_WRITEMASK, &aDefaultSettings.DepthMask);
369
91c60b57 370 Standard_Integer aSeqId = myLayers.Lower(), aMainId = myLayerIds.Find (Graphic3d_ZLayerId_Default);
a1954302 371 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
59f45b7c 372 {
91c60b57 373 if (theLayersToProcess == OpenGl_LF_Bottom)
374 {
375 if (aSeqId >= aMainId) continue;
376 }
377 else if (theLayersToProcess == OpenGl_LF_Upper)
378 {
379 if (aSeqId <= aMainId) continue;
380 }
381 else if (theLayersToProcess == OpenGl_LF_Default)
382 {
383 if (aSeqId != aMainId) continue;
384 }
385
a1954302 386 const OpenGl_Layer& aLayer = anIts.Value();
387 if (aLayer.NbStructures() < 1)
388 {
389 continue;
390 }
391 else if (theToDrawImmediate)
59f45b7c 392 {
a1954302 393 if (!aLayer.LayerSettings().IsImmediate)
394 {
395 continue;
396 }
59f45b7c 397 }
a1954302 398 else
399 {
400 if (aLayer.LayerSettings().IsImmediate)
401 {
402 continue;
403 }
404 }
405
406 // render layer
407 aLayer.Render (theWorkspace, aDefaultSettings);
59f45b7c 408 }
550f3b8b 409
a1954302 410 aCtx->core11fwd->glDepthMask (aDefaultSettings.DepthMask);
411 aCtx->core11fwd->glDepthFunc (aDefaultSettings.DepthFunc);
59f45b7c 412}