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
8 // under the terms of the GNU Lesser General Public 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.
20 #include <OpenGl_GlCore11.hxx>
22 #include <OpenGl_LayerList.hxx>
23 #include <OpenGl_Structure.hxx>
25 #include <InterfaceGraphic_Graphic3d.hxx>
26 #include <InterfaceGraphic.hxx>
28 //=======================================================================
29 //function : OpenGl_LayerList
30 //purpose : Constructor
31 //=======================================================================
33 OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
34 : myNbPriorities (theNbPriorities),
37 // insert default priority layer
38 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
39 myLayerIds.Bind (0, myLayers.Length());
42 //=======================================================================
43 //function : ~OpenGl_LayerList
44 //purpose : Destructor
45 //=======================================================================
47 OpenGl_LayerList::~OpenGl_LayerList ()
51 //=======================================================================
52 //function : defaultLayer
54 //=======================================================================
56 OpenGl_PriorityList& OpenGl_LayerList::defaultLayer()
58 return myLayers.ChangeValue (1);
61 //=======================================================================
62 //function : NbPriorities
63 //purpose : Method returns the number of available priorities
64 //=======================================================================
66 Standard_Integer OpenGl_LayerList::NbPriorities () const
68 return myNbPriorities;
71 //=======================================================================
72 //function : NbStructures
73 //purpose : Method returns the number of available structures
74 //=======================================================================
76 Standard_Integer OpenGl_LayerList::NbStructures () const
78 return myNbStructures;
81 //=======================================================================
84 //=======================================================================
86 void OpenGl_LayerList::AddLayer (const Standard_Integer theLayerId)
88 if (HasLayer (theLayerId))
92 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
93 myLayerIds.Bind (theLayerId, myLayers.Length());
96 //=======================================================================
99 //=======================================================================
101 Standard_Boolean OpenGl_LayerList::HasLayer
102 (const Standard_Integer theLayerId) const
104 return myLayerIds.IsBound (theLayerId);
107 //=======================================================================
108 //function : RemoveLayer
110 //=======================================================================
112 void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
114 if (!HasLayer (theLayerId) || theLayerId == 0)
117 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
119 // move all displayed structures to first layer
120 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos);
121 defaultLayer ().Append (aList);
124 myLayers.Remove (aRemovePos);
125 myLayerIds.UnBind (theLayerId);
127 // updated sequence indexes in map
128 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
129 for ( ; aMapIt.More (); aMapIt.Next ())
131 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
132 if (aSeqIdx > aRemovePos)
137 //=======================================================================
138 //function : AddStructure
140 //=======================================================================
142 void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
143 const Standard_Integer theLayerId,
144 const Standard_Integer thePriority)
146 // add structure to associated layer,
147 // if layer doesn't exists, display structure in default layer
148 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer () :
149 myLayers.ChangeValue (myLayerIds.Find (theLayerId));
151 aList.Add (theStructure, thePriority);
154 // Note: In ray-tracing mode we don't modify modification
155 // state here. It is redundant, because the possible changes
156 // will be handled in the loop for structures
159 //=======================================================================
160 //function : RemoveStructure
162 //=======================================================================
164 void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
165 const Standard_Integer theLayerId)
167 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
168 1 : myLayerIds.Find (theLayerId);
170 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
172 // remove structure from associated list
173 // if the structure is not found there,
174 // scan through layers and remove it
175 if (aList.Remove (theStructure) >= 0)
180 if (theStructure->IsRaytracable())
182 myModificationState++;
189 // scan through layers and remove it
190 Standard_Integer aSeqId = 1;
191 OpenGl_SequenceOfLayers::Iterator anIts;
192 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
194 OpenGl_PriorityList& aScanList = anIts.ChangeValue ();
195 if (aSeqPos == aSeqId)
198 if (aScanList.Remove (theStructure) >= 0)
203 if (theStructure->IsRaytracable())
205 myModificationState++;
214 //=======================================================================
215 //function : ChangeLayer
217 //=======================================================================
219 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
220 const Standard_Integer theOldLayerId,
221 const Standard_Integer theNewLayerId)
223 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
224 1 : myLayerIds.Find (theOldLayerId);
226 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
227 Standard_Integer aPriority;
229 // take priority and remove structure from list found by <theOldLayerId>
230 // if the structure is not found there, scan through all other layers
231 if ((aPriority = aList.Remove (theStructure)) >= 0)
234 AddStructure (theStructure, theNewLayerId, aPriority);
238 // scan through layers and remove it
239 Standard_Integer aSeqId = 1;
240 OpenGl_SequenceOfLayers::Iterator anIts;
241 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
243 if (aSeqPos == aSeqId)
246 // try to remove structure and get priority value from this layer
247 if ((aPriority = aList.Remove (theStructure)) >= 0)
250 AddStructure (theStructure, theNewLayerId, aPriority);
257 //=======================================================================
259 //purpose : Render this element
260 //=======================================================================
262 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
264 OpenGl_SequenceOfLayers::Iterator anIts;
265 for(anIts.Init (myLayers); anIts.More (); anIts.Next ())
267 const OpenGl_PriorityList& aList = anIts.Value ();
268 if (aList.NbStructures () > 0)
270 // separate depth buffers
271 glClear (GL_DEPTH_BUFFER_BIT);
273 // render priority list
274 aList.Render (theWorkspace);