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>
21 #include <InterfaceGraphic_Graphic3d.hxx>
22 #include <InterfaceGraphic.hxx>
24 //=======================================================================
25 //function : OpenGl_LayerList
26 //purpose : Constructor
27 //=======================================================================
29 OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
30 : myNbPriorities (theNbPriorities),
33 // insert default priority layer
34 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
35 myLayerIds.Bind (0, myLayers.Length());
38 //=======================================================================
39 //function : ~OpenGl_LayerList
40 //purpose : Destructor
41 //=======================================================================
43 OpenGl_LayerList::~OpenGl_LayerList ()
47 //=======================================================================
48 //function : defaultLayer
50 //=======================================================================
52 OpenGl_PriorityList& OpenGl_LayerList::defaultLayer()
54 return myLayers.ChangeValue (1);
57 //=======================================================================
58 //function : NbPriorities
59 //purpose : Method returns the number of available priorities
60 //=======================================================================
62 Standard_Integer OpenGl_LayerList::NbPriorities () const
64 return myNbPriorities;
67 //=======================================================================
68 //function : NbStructures
69 //purpose : Method returns the number of available structures
70 //=======================================================================
72 Standard_Integer OpenGl_LayerList::NbStructures () const
74 return myNbStructures;
77 //=======================================================================
80 //=======================================================================
82 void OpenGl_LayerList::AddLayer (const Standard_Integer theLayerId)
84 if (HasLayer (theLayerId))
88 myLayers.Append (OpenGl_PriorityList (myNbPriorities));
89 myLayerIds.Bind (theLayerId, myLayers.Length());
92 //=======================================================================
95 //=======================================================================
97 Standard_Boolean OpenGl_LayerList::HasLayer
98 (const Standard_Integer theLayerId) const
100 return myLayerIds.IsBound (theLayerId);
103 //=======================================================================
104 //function : RemoveLayer
106 //=======================================================================
108 void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
110 if (!HasLayer (theLayerId) || theLayerId == 0)
113 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
115 // move all displayed structures to first layer
116 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos);
117 defaultLayer ().Append (aList);
120 myLayers.Remove (aRemovePos);
121 myLayerIds.UnBind (theLayerId);
123 // updated sequence indexes in map
124 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
125 for ( ; aMapIt.More (); aMapIt.Next ())
127 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
128 if (aSeqIdx > aRemovePos)
133 //=======================================================================
134 //function : AddStructure
136 //=======================================================================
138 void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
139 const Standard_Integer theLayerId,
140 const Standard_Integer thePriority)
142 // add structure to associated layer,
143 // if layer doesn't exists, display structure in default layer
144 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer () :
145 myLayers.ChangeValue (myLayerIds.Find (theLayerId));
147 aList.Add (theStructure, thePriority);
150 // Note: In ray-tracing mode we don't modify modification
151 // state here. It is redundant, because the possible changes
152 // will be handled in the loop for structures
155 //=======================================================================
156 //function : RemoveStructure
158 //=======================================================================
160 void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
161 const Standard_Integer theLayerId)
163 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
164 1 : myLayerIds.Find (theLayerId);
166 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
168 // remove structure from associated list
169 // if the structure is not found there,
170 // scan through layers and remove it
171 if (aList.Remove (theStructure) >= 0)
175 if (theStructure->IsRaytracable())
177 myModificationState++;
183 // scan through layers and remove it
184 Standard_Integer aSeqId = 1;
185 OpenGl_SequenceOfLayers::Iterator anIts;
186 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
188 OpenGl_PriorityList& aScanList = anIts.ChangeValue ();
189 if (aSeqPos == aSeqId)
192 if (aScanList.Remove (theStructure) >= 0)
196 if (theStructure->IsRaytracable())
198 myModificationState++;
206 //=======================================================================
207 //function : ChangeLayer
209 //=======================================================================
211 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
212 const Standard_Integer theOldLayerId,
213 const Standard_Integer theNewLayerId)
215 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
216 1 : myLayerIds.Find (theOldLayerId);
218 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos);
219 Standard_Integer aPriority;
221 // take priority and remove structure from list found by <theOldLayerId>
222 // if the structure is not found there, scan through all other layers
223 if ((aPriority = aList.Remove (theStructure)) >= 0)
226 AddStructure (theStructure, theNewLayerId, aPriority);
230 // scan through layers and remove it
231 Standard_Integer aSeqId = 1;
232 OpenGl_SequenceOfLayers::Iterator anIts;
233 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
235 if (aSeqPos == aSeqId)
238 // try to remove structure and get priority value from this layer
239 if ((aPriority = aList.Remove (theStructure)) >= 0)
242 AddStructure (theStructure, theNewLayerId, aPriority);
249 //=======================================================================
251 //purpose : Render this element
252 //=======================================================================
254 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
256 OpenGl_SequenceOfLayers::Iterator anIts;
257 for(anIts.Init (myLayers); anIts.More (); anIts.Next ())
259 const OpenGl_PriorityList& aList = anIts.Value ();
260 if (aList.NbStructures () > 0)
262 // separate depth buffers
263 glClear (GL_DEPTH_BUFFER_BIT);
265 // render priority list
266 aList.Render (theWorkspace);