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 <InterfaceGraphic_Graphic3d.hxx>
23 #include <InterfaceGraphic.hxx>
25 //=======================================================================
26 //function : OpenGl_LayerList
27 //purpose : Constructor
28 //=======================================================================
30 OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
31 : myNbPriorities (theNbPriorities),
33 myImmediateNbStructures (0)
35 // insert default priority layers
36 myLayers.Append (OpenGl_Layer (myNbPriorities));
37 myLayerIds.Bind (Graphic3d_ZLayerId_BotOSD, myLayers.Upper());
39 myLayers.Append (OpenGl_Layer (myNbPriorities));
40 myLayerIds.Bind (Graphic3d_ZLayerId_Default, myLayers.Upper());
42 myLayers.Append (OpenGl_Layer (myNbPriorities));
43 myLayerIds.Bind (Graphic3d_ZLayerId_Top, myLayers.Upper());
45 myLayers.Append (OpenGl_Layer (myNbPriorities));
46 myLayerIds.Bind (Graphic3d_ZLayerId_Topmost, myLayers.Upper());
48 myLayers.Append (OpenGl_Layer (myNbPriorities));
49 myLayerIds.Bind (Graphic3d_ZLayerId_TopOSD, myLayers.Upper());
52 //=======================================================================
53 //function : ~OpenGl_LayerList
54 //purpose : Destructor
55 //=======================================================================
57 OpenGl_LayerList::~OpenGl_LayerList()
61 //=======================================================================
64 //=======================================================================
66 void OpenGl_LayerList::AddLayer (const Graphic3d_ZLayerId theLayerId)
68 if (myLayerIds.IsBound (theLayerId))
74 myLayers.Append (OpenGl_Layer (myNbPriorities));
75 myLayerIds.Bind (theLayerId, myLayers.Length());
78 //=======================================================================
81 //=======================================================================
82 OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId)
84 return myLayers.ChangeValue (myLayerIds.Find (theLayerId));
87 //=======================================================================
90 //=======================================================================
91 const OpenGl_Layer& OpenGl_LayerList::Layer (const Graphic3d_ZLayerId theLayerId) const
93 return myLayers.Value (myLayerIds.Find (theLayerId));
96 //=======================================================================
97 //function : RemoveLayer
99 //=======================================================================
101 void OpenGl_LayerList::RemoveLayer (const Graphic3d_ZLayerId theLayerId)
103 if (!myLayerIds.IsBound (theLayerId)
109 const Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
111 // move all displayed structures to first layer
112 const OpenGl_Layer& aLayerToMove = myLayers.Value (aRemovePos);
113 myLayers.ChangeFirst().Append (aLayerToMove);
116 myLayers.Remove (aRemovePos);
117 myLayerIds.UnBind (theLayerId);
119 // updated sequence indexes in map
120 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
121 for ( ; aMapIt.More (); aMapIt.Next ())
123 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
124 if (aSeqIdx > aRemovePos)
129 //=======================================================================
130 //function : AddStructure
132 //=======================================================================
134 void OpenGl_LayerList::AddStructure (const OpenGl_Structure* theStruct,
135 const Graphic3d_ZLayerId theLayerId,
136 const Standard_Integer thePriority,
137 Standard_Boolean isForChangePriority)
139 // add structure to associated layer,
140 // if layer doesn't exists, display structure in default layer
141 Standard_Integer aSeqPos = myLayers.Lower();
142 myLayerIds.Find (theLayerId, aSeqPos);
144 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
145 aLayer.Add (theStruct, thePriority, isForChangePriority);
147 if (aLayer.LayerSettings().IsImmediate)
149 ++myImmediateNbStructures;
152 // Note: In ray-tracing mode we don't modify modification
153 // state here. It is redundant, because the possible changes
154 // will be handled in the loop for structures
157 //=======================================================================
158 //function : RemoveStructure
160 //=======================================================================
162 void OpenGl_LayerList::RemoveStructure (const Handle(Graphic3d_Structure)& theStructure)
164 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure* > (theStructure->CStructure().operator->());
165 const Graphic3d_ZLayerId aLayerId = aStruct->ZLayer();
167 Standard_Integer aSeqPos = myLayers.Lower();
168 myLayerIds.Find (aLayerId, aSeqPos);
170 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
171 Standard_Integer aPriority = -1;
173 // remove structure from associated list
174 // if the structure is not found there,
175 // scan through layers and remove it
176 if (aLayer.Remove (aStruct, aPriority))
179 if (aLayer.LayerSettings().IsImmediate)
181 --myImmediateNbStructures;
184 if (aStruct->IsRaytracable())
186 ++myModificationState;
192 // scan through layers and remove it
193 Standard_Integer aSeqId = 1;
194 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
196 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
197 if (aSeqPos == aSeqId)
202 if (aLayerEx.Remove (aStruct, aPriority))
205 if (aLayerEx.LayerSettings().IsImmediate)
207 --myImmediateNbStructures;
210 if (aStruct->IsRaytracable())
212 ++myModificationState;
219 //=======================================================================
220 //function : InvalidateBVHData
222 //=======================================================================
223 void OpenGl_LayerList::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
225 Standard_Integer aSeqPos = myLayers.Lower();
226 myLayerIds.Find (theLayerId, aSeqPos);
227 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
228 aLayer.InvalidateBVHData();
231 //=======================================================================
232 //function : ChangeLayer
234 //=======================================================================
236 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure* theStructure,
237 const Graphic3d_ZLayerId theOldLayerId,
238 const Graphic3d_ZLayerId theNewLayerId)
240 Standard_Integer aSeqPos = myLayers.Lower();
241 myLayerIds.Find (theOldLayerId, aSeqPos);
242 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
243 Standard_Integer aPriority = -1;
245 // take priority and remove structure from list found by <theOldLayerId>
246 // if the structure is not found there, scan through all other layers
247 if (aLayer.Remove (theStructure, aPriority, Standard_True))
250 if (aLayer.LayerSettings().IsImmediate)
252 --myImmediateNbStructures;
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);
261 // scan through layers and remove it
262 Standard_Integer aSeqId = 1;
263 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
265 if (aSeqPos == aSeqId)
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))
275 if (aLayerEx.LayerSettings().IsImmediate)
277 --myImmediateNbStructures;
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);
288 //=======================================================================
289 //function : ChangePriority
291 //=======================================================================
292 void OpenGl_LayerList::ChangePriority (const OpenGl_Structure* theStructure,
293 const Graphic3d_ZLayerId theLayerId,
294 const Standard_Integer theNewPriority)
296 Standard_Integer aSeqPos = myLayers.Lower();
297 myLayerIds.Find (theLayerId, aSeqPos);
298 OpenGl_Layer& aLayer = myLayers.ChangeValue (aSeqPos);
299 Standard_Integer anOldPriority = -1;
301 if (aLayer.Remove (theStructure, anOldPriority, Standard_True))
304 if (aLayer.LayerSettings().IsImmediate)
306 --myImmediateNbStructures;
309 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
313 Standard_Integer aSeqId = 1;
314 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
316 if (aSeqPos == aSeqId)
321 OpenGl_Layer& aLayerEx = anIts.ChangeValue();
322 if (aLayerEx.Remove (theStructure, anOldPriority, Standard_True))
325 if (aLayerEx.LayerSettings().IsImmediate)
327 --myImmediateNbStructures;
330 AddStructure (theStructure, theLayerId, theNewPriority, Standard_True);
336 //=======================================================================
337 //function : SetLayerSettings
339 //=======================================================================
340 void OpenGl_LayerList::SetLayerSettings (const Graphic3d_ZLayerId theLayerId,
341 const Graphic3d_ZLayerSettings& theSettings)
343 OpenGl_Layer& aLayer = Layer (theLayerId);
344 if (aLayer.LayerSettings().IsImmediate != theSettings.IsImmediate)
346 if (theSettings.IsImmediate)
348 myImmediateNbStructures += aLayer.NbStructures();
352 myImmediateNbStructures -= aLayer.NbStructures();
355 aLayer.SetLayerSettings (theSettings);
358 //=======================================================================
361 //=======================================================================
362 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace)& theWorkspace,
363 const Standard_Boolean theToDrawImmediate,
364 const OpenGl_LayerFilter theLayersToProcess) const
366 OpenGl_GlobalLayerSettings aDefaultSettings;
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);
372 Standard_Integer aSeqId = myLayers.Lower(), aMainId = myLayerIds.Find (Graphic3d_ZLayerId_Default);
373 for (OpenGl_SequenceOfLayers::Iterator anIts (myLayers); anIts.More(); anIts.Next(), ++aSeqId)
375 if (theLayersToProcess == OpenGl_LF_Bottom)
377 if (aSeqId >= aMainId) continue;
379 else if (theLayersToProcess == OpenGl_LF_Upper)
381 if (aSeqId <= aMainId) continue;
383 else if (theLayersToProcess == OpenGl_LF_Default)
385 if (aSeqId != aMainId) continue;
388 const OpenGl_Layer& aLayer = anIts.Value();
389 if (aLayer.NbStructures() < 1)
393 else if (theToDrawImmediate)
395 if (!aLayer.LayerSettings().IsImmediate)
402 if (aLayer.LayerSettings().IsImmediate)
409 aLayer.Render (theWorkspace, aDefaultSettings);
412 aCtx->core11fwd->glDepthMask (aDefaultSettings.DepthMask);
413 aCtx->core11fwd->glDepthFunc (aDefaultSettings.DepthFunc);