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