1 // Created on: 2012-02-02
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2012-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_GlCore11.hxx>
18 #include <OpenGl_LayerList.hxx>
19 #include <OpenGl_Structure.hxx>
20 #include <OpenGl_Workspace.hxx>
22 #include <Graphic3d_GraphicDriver.hxx>
24 //=======================================================================
25 //function : OpenGl_LayerList
26 //purpose : Constructor
27 //=======================================================================
29 OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
30 : myNbPriorities (theNbPriorities),
32 myImmediateNbStructures (0)
34 // insert default priority layers
35 myLayers.Append (OpenGl_Layer (myNbPriorities));
36 myLayerIds.Bind (Graphic3d_ZLayerId_BotOSD, myLayers.Upper());
38 myLayers.Append (OpenGl_Layer (myNbPriorities));
39 myLayerIds.Bind (Graphic3d_ZLayerId_Default, myLayers.Upper());
41 myLayers.Append (OpenGl_Layer (myNbPriorities));
42 myLayerIds.Bind (Graphic3d_ZLayerId_Top, myLayers.Upper());
44 myLayers.Append (OpenGl_Layer (myNbPriorities));
45 myLayerIds.Bind (Graphic3d_ZLayerId_Topmost, myLayers.Upper());
47 myLayers.Append (OpenGl_Layer (myNbPriorities));
48 myLayerIds.Bind (Graphic3d_ZLayerId_TopOSD, myLayers.Upper());
51 //=======================================================================
52 //function : ~OpenGl_LayerList
53 //purpose : Destructor
54 //=======================================================================
56 OpenGl_LayerList::~OpenGl_LayerList()
60 //=======================================================================
63 //=======================================================================
65 void OpenGl_LayerList::AddLayer (const Graphic3d_ZLayerId theLayerId)
67 if (myLayerIds.IsBound (theLayerId))
73 myLayers.Append (OpenGl_Layer (myNbPriorities));
74 myLayerIds.Bind (theLayerId, myLayers.Length());
77 //=======================================================================
80 //=======================================================================
81 OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId)
83 return myLayers.ChangeValue (myLayerIds.Find (theLayerId));
86 //=======================================================================
89 //=======================================================================
90 const OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId) const
92 return myLayers.Value (myLayerIds.Find (theLayerId));
95 //=======================================================================
96 //function : RemoveLayer
98 //=======================================================================
100 void OpenGl_LayerList::RemoveLayer (const Graphic3d_ZLayerId theLayerId)
102 if (!myLayerIds.IsBound (theLayerId)
108 const Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
110 // move all displayed structures to first layer
111 const OpenGl_Layer& aLayerToMove = myLayers.Value (aRemovePos);
112 myLayers.ChangeFirst().Append (aLayerToMove);
115 myLayers.Remove (aRemovePos);
116 myLayerIds.UnBind (theLayerId);
118 // updated sequence indexes in map
119 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
120 for ( ; aMapIt.More (); aMapIt.Next ())
122 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
123 if (aSeqIdx > aRemovePos)
128 //=======================================================================
129 //function : AddStructure
131 //=======================================================================
133 void OpenGl_LayerList::AddStructure (const OpenGl_Structure* theStruct,
134 const Graphic3d_ZLayerId theLayerId,
135 const Standard_Integer thePriority,
136 Standard_Boolean isForChangePriority)
138 // add structure to associated layer,
139 // if layer doesn't exists, display structure in default layer
140 Standard_Integer aSeqPos = myLayers.Lower();
141 myLayerIds.Find (theLayerId, aSeqPos);
143 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
144 aLayer.Add (theStruct, thePriority, isForChangePriority);
146 if (aLayer.LayerSettings().IsImmediate)
148 ++myImmediateNbStructures;
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
156 //=======================================================================
157 //function : RemoveStructure
159 //=======================================================================
161 void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure* theStructure)
163 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
165 Standard_Integer aSeqPos = myLayers.Lower();
166 myLayerIds.Find (aLayerId, aSeqPos);
168 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
169 Standard_Integer aPriority = -1;
171 // remove structure from associated list
172 // if the structure is not found there,
173 // scan through layers and remove it
174 if (aLayer.Remove (theStructure, aPriority))
177 if (aLayer.LayerSettings().IsImmediate)
179 --myImmediateNbStructures;
182 if (theStructure->IsRaytracable())
184 ++myModificationState;
190 // scan through layers and remove it
191 Standard_Integer aSeqId = 1;
192 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
194 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
195 if (aSeqPos == aSeqId)
200 if (aLayerEx.Remove (theStructure, aPriority))
203 if (aLayerEx.LayerSettings().IsImmediate)
205 --myImmediateNbStructures;
208 if (theStructure->IsRaytracable())
210 ++myModificationState;
217 //=======================================================================
218 //function : InvalidateBVHData
220 //=======================================================================
221 void OpenGl_LayerList::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
223 Standard_Integer aSeqPos = myLayers.Lower();
224 myLayerIds.Find (theLayerId, aSeqPos);
225 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
226 aLayer.InvalidateBVHData();
229 //=======================================================================
230 //function : ChangeLayer
232 //=======================================================================
234 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure* theStructure,
235 const Graphic3d_ZLayerId theOldLayerId,
236 const Graphic3d_ZLayerId theNewLayerId)
238 Standard_Integer aSeqPos = myLayers.Lower();
239 myLayerIds.Find (theOldLayerId, aSeqPos);
240 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
241 Standard_Integer aPriority = -1;
243 // take priority and remove structure from list found by <theOldLayerId>
244 // if the structure is not found there, scan through all other layers
245 if (aLayer.Remove (theStructure, aPriority, Standard_True))
248 if (aLayer.LayerSettings().IsImmediate)
250 --myImmediateNbStructures;
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);
259 // scan through layers and remove it
260 Standard_Integer aSeqId = 1;
261 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
263 if (aSeqPos == aSeqId)
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))
273 if (aLayerEx.LayerSettings().IsImmediate)
275 --myImmediateNbStructures;
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);
286 //=======================================================================
287 //function : ChangePriority
289 //=======================================================================
290 void OpenGl_LayerList::ChangePriority (const OpenGl_Structure* theStructure,
291 const Graphic3d_ZLayerId theLayerId,
292 const Standard_Integer theNewPriority)
294 Standard_Integer aSeqPos = myLayers.Lower();
295 myLayerIds.Find (theLayerId, aSeqPos);
296 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
297 Standard_Integer anOldPriority = -1;
299 if (aLayer.Remove (theStructure, anOldPriority, Standard_True))
302 if (aLayer.LayerSettings().IsImmediate)
304 --myImmediateNbStructures;
307 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
311 Standard_Integer aSeqId = 1;
312 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
314 if (aSeqPos == aSeqId)
319 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
320 if (aLayerEx.Remove (theStructure, anOldPriority, Standard_True))
323 if (aLayerEx.LayerSettings().IsImmediate)
325 --myImmediateNbStructures;
328 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
334 //=======================================================================
335 //function : SetLayerSettings
337 //=======================================================================
338 void OpenGl_LayerList::SetLayerSettings (const Graphic3d_ZLayerId theLayerId,
339 const Graphic3d_ZLayerSettings& theSettings)
341 OpenGl_Layer& aLayer = Layer (theLayerId);
342 if (aLayer.LayerSettings().IsImmediate != theSettings.IsImmediate)
344 if (theSettings.IsImmediate)
346 myImmediateNbStructures += aLayer.NbStructures();
350 myImmediateNbStructures -= aLayer.NbStructures();
353 aLayer.SetLayerSettings (theSettings);
356 //=======================================================================
359 //=======================================================================
360 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace)& theWorkspace,
361 const Standard_Boolean theToDrawImmediate,
362 const OpenGl_LayerFilter theLayersToProcess) const
364 OpenGl_GlobalLayerSettings aDefaultSettings;
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);
370 Standard_Integer aSeqId = myLayers.Lower(), aMainId = myLayerIds.Find (Graphic3d_ZLayerId_Default);
371 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
373 if (theLayersToProcess == OpenGl_LF_Bottom)
375 if (aSeqId >= aMainId) continue;
377 else if (theLayersToProcess == OpenGl_LF_Upper)
379 if (aSeqId <= aMainId) continue;
381 else if (theLayersToProcess == OpenGl_LF_Default)
383 if (aSeqId != aMainId) continue;
386 const OpenGl_Layer& aLayer = anIts.Value();
387 if (aLayer.NbStructures() < 1)
391 else if (theToDrawImmediate)
393 if (!aLayer.LayerSettings().IsImmediate)
400 if (aLayer.LayerSettings().IsImmediate)
407 aLayer.Render (theWorkspace, aDefaultSettings);
410 aCtx->core11fwd->glDepthMask (aDefaultSettings.DepthMask);
411 aCtx->core11fwd->glDepthFunc (aDefaultSettings.DepthFunc);