1 // Created on: 2012-02-02
2 // Created by: Anton POLETAEV
3 // Copyright (c) -1999 Matra Datavision
4 // Copyright (c) 2012-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 #include <OpenGl_GlCore11.hxx>
24 #include <OpenGl_LayerList.hxx>
25 #include <OpenGl_Structure.hxx>
27 #include <InterfaceGraphic_Graphic3d.hxx>
28 #include <InterfaceGraphic.hxx>
30 //=======================================================================
31 //function : OpenGl_LayerList
32 //purpose : Constructor
33 //=======================================================================
35 OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
36 : myNbPriorities (theNbPriorities),
39 // insert default priority layer
40 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
41 myLayerIds.Bind (0, myLayers.Length());
44 //=======================================================================
45 //function : ~OpenGl_LayerList
46 //purpose : Destructor
47 //=======================================================================
49 OpenGl_LayerList::~OpenGl_LayerList ()
53 //=======================================================================
54 //function : defaultLayer
56 //=======================================================================
58 OpenGl_PriorityList& OpenGl_LayerList::defaultLayer()
60 return myLayers.ChangeValue (1);
63 //=======================================================================
64 //function : NbPriorities
65 //purpose : Method returns the number of available priorities
66 //=======================================================================
68 Standard_Integer OpenGl_LayerList::NbPriorities () const
70 return myNbPriorities;
73 //=======================================================================
74 //function : NbStructures
75 //purpose : Method returns the number of available structures
76 //=======================================================================
78 Standard_Integer OpenGl_LayerList::NbStructures () const
80 return myNbStructures;
83 //=======================================================================
86 //=======================================================================
88 void OpenGl_LayerList::AddLayer (const Standard_Integer theLayerId)
90 if (HasLayer (theLayerId))
94 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
95 myLayerIds.Bind (theLayerId, myLayers.Length());
98 //=======================================================================
101 //=======================================================================
103 Standard_Boolean OpenGl_LayerList::HasLayer
104 (const Standard_Integer theLayerId) const
106 return myLayerIds.IsBound (theLayerId);
109 //=======================================================================
110 //function : RemoveLayer
112 //=======================================================================
114 void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
116 if (!HasLayer (theLayerId) || theLayerId == 0)
119 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
121 // move all displayed structures to first layer
122 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos);
123 defaultLayer ().Append (aList);
126 myLayers.Remove (aRemovePos);
127 myLayerIds.UnBind (theLayerId);
129 // updated sequence indexes in map
130 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
131 for ( ; aMapIt.More (); aMapIt.Next ())
133 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
134 if (aSeqIdx > aRemovePos)
139 //=======================================================================
140 //function : AddStructure
142 //=======================================================================
144 void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
145 const Standard_Integer theLayerId,
146 const Standard_Integer thePriority)
148 // add structure to associated layer,
149 // if layer doesn't exists, display structure in default layer
150 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer () :
151 myLayers.ChangeValue (myLayerIds.Find (theLayerId));
153 aList.Add (theStructure, thePriority);
157 //=======================================================================
158 //function : RemoveStructure
160 //=======================================================================
162 void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
163 const Standard_Integer theLayerId)
165 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
166 1 : myLayerIds.Find (theLayerId);
168 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
170 // remove structure from associated list
171 // if the structure is not found there,
172 // scan through layers and remove it
173 if (aList.Remove (theStructure) >= 0)
179 // scan through layers and remove it
180 Standard_Integer aSeqId = 1;
181 OpenGl_SequenceOfLayers::Iterator anIts;
182 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
184 OpenGl_PriorityList& aScanList = anIts.ChangeValue ();
185 if (aSeqPos == aSeqId)
188 if (aScanList.Remove (theStructure) >= 0)
196 //=======================================================================
197 //function : ChangeLayer
199 //=======================================================================
201 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
202 const Standard_Integer theOldLayerId,
203 const Standard_Integer theNewLayerId)
205 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
206 1 : myLayerIds.Find (theOldLayerId);
208 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
209 Standard_Integer aPriority;
211 // take priority and remove structure from list found by <theOldLayerId>
212 // if the structure is not found there, scan through all other layers
213 if ((aPriority = aList.Remove (theStructure)) >= 0)
216 AddStructure (theStructure, theNewLayerId, aPriority);
220 // scan through layers and remove it
221 Standard_Integer aSeqId = 1;
222 OpenGl_SequenceOfLayers::Iterator anIts;
223 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
225 OpenGl_PriorityList& aScanList = anIts.ChangeValue ();
226 if (aSeqPos == aSeqId)
229 // try to remove structure and get priority value from this layer
230 if ((aPriority = aList.Remove (theStructure)) >= 0)
233 AddStructure (theStructure, theNewLayerId, aPriority);
240 //=======================================================================
242 //purpose : Render this element
243 //=======================================================================
245 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
247 OpenGl_SequenceOfLayers::Iterator anIts;
248 for(anIts.Init (myLayers); anIts.More (); anIts.Next ())
250 const OpenGl_PriorityList& aList = anIts.Value ();
251 if (aList.NbStructures () > 0)
253 // separate depth buffers
254 glClear (GL_DEPTH_BUFFER_BIT);
256 // render priority list
257 aList.Render (theWorkspace);