1 // File: OpenGl_LayerList.hxx
2 // Created: 2 February 2012
3 // Author: Anton POLETAEV
4 // Copyright: OPEN CASCADE 2012
6 #include <OpenGl_GlCore11.hxx>
8 #include <OpenGl_LayerList.hxx>
9 #include <OpenGl_Structure.hxx>
11 #include <InterfaceGraphic_Graphic3d.hxx>
12 #include <InterfaceGraphic.hxx>
14 //=======================================================================
15 //function : OpenGl_LayerList
16 //purpose : Constructor
17 //=======================================================================
19 OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
20 : myNbPriorities (theNbPriorities),
23 // insert default priority layer
24 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
25 myLayerIds.Bind (0, myLayers.Length());
28 //=======================================================================
29 //function : ~OpenGl_LayerList
30 //purpose : Destructor
31 //=======================================================================
33 OpenGl_LayerList::~OpenGl_LayerList ()
37 //=======================================================================
38 //function : defaultLayer
40 //=======================================================================
42 OpenGl_PriorityList& OpenGl_LayerList::defaultLayer()
44 return myLayers.ChangeValue (1);
47 //=======================================================================
48 //function : NbPriorities
49 //purpose : Method returns the number of available priorities
50 //=======================================================================
52 Standard_Integer OpenGl_LayerList::NbPriorities () const
54 return myNbPriorities;
57 //=======================================================================
58 //function : NbStructures
59 //purpose : Method returns the number of available structures
60 //=======================================================================
62 Standard_Integer OpenGl_LayerList::NbStructures () const
64 return myNbStructures;
67 //=======================================================================
70 //=======================================================================
72 void OpenGl_LayerList::AddLayer (const Standard_Integer theLayerId)
74 if (HasLayer (theLayerId))
78 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
79 myLayerIds.Bind (theLayerId, myLayers.Length());
82 //=======================================================================
85 //=======================================================================
87 Standard_Boolean OpenGl_LayerList::HasLayer
88 (const Standard_Integer theLayerId) const
90 return myLayerIds.IsBound (theLayerId);
93 //=======================================================================
94 //function : RemoveLayer
96 //=======================================================================
98 void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
100 if (!HasLayer (theLayerId) || theLayerId == 0)
103 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
105 // move all displayed structures to first layer
106 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos);
107 defaultLayer ().Append (aList);
110 myLayers.Remove (aRemovePos);
111 myLayerIds.UnBind (theLayerId);
113 // updated sequence indexes in map
114 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
115 for ( ; aMapIt.More (); aMapIt.Next ())
117 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
118 if (aSeqIdx > aRemovePos)
123 //=======================================================================
124 //function : AddStructure
126 //=======================================================================
128 void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
129 const Standard_Integer theLayerId,
130 const Standard_Integer thePriority)
132 // add structure to associated layer,
133 // if layer doesn't exists, display structure in default layer
134 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer () :
135 myLayers.ChangeValue (myLayerIds.Find (theLayerId));
137 aList.Add (theStructure, thePriority);
141 //=======================================================================
142 //function : RemoveStructure
144 //=======================================================================
146 void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
147 const Standard_Integer theLayerId)
149 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
150 1 : myLayerIds.Find (theLayerId);
152 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
154 // remove structure from associated list
155 // if the structure is not found there,
156 // scan through layers and remove it
157 if (aList.Remove (theStructure) >= 0)
163 // scan through layers and remove it
164 Standard_Integer aSeqId = 1;
165 OpenGl_SequenceOfLayers::Iterator anIts;
166 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
168 OpenGl_PriorityList& aScanList = anIts.ChangeValue ();
169 if (aSeqPos == aSeqId)
172 if (aScanList.Remove (theStructure) >= 0)
180 //=======================================================================
181 //function : ChangeLayer
183 //=======================================================================
185 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
186 const Standard_Integer theOldLayerId,
187 const Standard_Integer theNewLayerId)
189 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
190 1 : myLayerIds.Find (theOldLayerId);
192 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
193 Standard_Integer aPriority;
195 // take priority and remove structure from list found by <theOldLayerId>
196 // if the structure is not found there, scan through all other layers
197 if ((aPriority = aList.Remove (theStructure)) >= 0)
200 AddStructure (theStructure, theNewLayerId, aPriority);
204 // scan through layers and remove it
205 Standard_Integer aSeqId = 1;
206 OpenGl_SequenceOfLayers::Iterator anIts;
207 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
209 OpenGl_PriorityList& aScanList = anIts.ChangeValue ();
210 if (aSeqPos == aSeqId)
213 // try to remove structure and get priority value from this layer
214 if ((aPriority = aList.Remove (theStructure)) >= 0)
217 AddStructure (theStructure, theNewLayerId, aPriority);
224 //=======================================================================
226 //purpose : Render this element
227 //=======================================================================
229 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
231 OpenGl_SequenceOfLayers::Iterator anIts;
232 for(anIts.Init (myLayers); anIts.More (); anIts.Next ())
234 const OpenGl_PriorityList& aList = anIts.Value ();
235 if (aList.NbStructures () > 0)
237 // separate depth buffers
238 glClear (GL_DEPTH_BUFFER_BIT);
240 // render priority list
241 aList.Render (theWorkspace);