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_Layer (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_Layer& 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_Layer (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 //=======================================================================
106 //=======================================================================
107 OpenGl_Layer& OpenGl_LayerList::Layer (const Standard_Integer theLayerId)
109 return myLayers.ChangeValue (myLayerIds.Find (theLayerId));
112 //=======================================================================
115 //=======================================================================
116 const OpenGl_Layer& OpenGl_LayerList::Layer (const Standard_Integer theLayerId) const
118 return myLayers.Value (myLayerIds.Find (theLayerId));
121 //=======================================================================
122 //function : RemoveLayer
124 //=======================================================================
126 void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
128 if (!HasLayer (theLayerId) || theLayerId == 0)
131 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
133 // move all displayed structures to first layer
134 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos).PriorityList();
135 defaultLayer ().PriorityList().Append (aList);
138 myLayers.Remove (aRemovePos);
139 myLayerIds.UnBind (theLayerId);
141 // updated sequence indexes in map
142 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
143 for ( ; aMapIt.More (); aMapIt.Next ())
145 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
146 if (aSeqIdx > aRemovePos)
151 //=======================================================================
152 //function : AddStructure
154 //=======================================================================
156 void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
157 const Standard_Integer theLayerId,
158 const Standard_Integer thePriority)
160 // add structure to associated layer,
161 // if layer doesn't exists, display structure in default layer
162 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer ().PriorityList() :
163 myLayers.ChangeValue (myLayerIds.Find (theLayerId)).PriorityList();
165 aList.Add (theStructure, thePriority);
168 // Note: In ray-tracing mode we don't modify modification
169 // state here. It is redundant, because the possible changes
170 // will be handled in the loop for structures
173 //=======================================================================
174 //function : RemoveStructure
176 //=======================================================================
178 void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
179 const Standard_Integer theLayerId)
181 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
182 1 : myLayerIds.Find (theLayerId);
184 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
186 // remove structure from associated list
187 // if the structure is not found there,
188 // scan through layers and remove it
189 if (aList.Remove (theStructure) >= 0)
193 if (theStructure->IsRaytracable())
195 myModificationState++;
201 // scan through layers and remove it
202 Standard_Integer aSeqId = 1;
203 OpenGl_SequenceOfLayers::Iterator anIts;
204 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
206 OpenGl_PriorityList& aScanList = anIts.ChangeValue ().PriorityList();
207 if (aSeqPos == aSeqId)
210 if (aScanList.Remove (theStructure) >= 0)
214 if (theStructure->IsRaytracable())
216 myModificationState++;
224 //=======================================================================
225 //function : ChangeLayer
227 //=======================================================================
229 void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
230 const Standard_Integer theOldLayerId,
231 const Standard_Integer theNewLayerId)
233 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
234 1 : myLayerIds.Find (theOldLayerId);
236 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
237 Standard_Integer aPriority;
239 // take priority and remove structure from list found by <theOldLayerId>
240 // if the structure is not found there, scan through all other layers
241 if ((aPriority = aList.Remove (theStructure)) >= 0)
244 AddStructure (theStructure, theNewLayerId, aPriority);
248 // scan through layers and remove it
249 Standard_Integer aSeqId = 1;
250 OpenGl_SequenceOfLayers::Iterator anIts;
251 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
253 if (aSeqPos == aSeqId)
256 // try to remove structure and get priority value from this layer
257 if ((aPriority = aList.Remove (theStructure)) >= 0)
260 AddStructure (theStructure, theNewLayerId, aPriority);
267 //=======================================================================
269 //purpose : Render this element
270 //=======================================================================
272 void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
274 int aDefaultDepthFunc;
275 glGetIntegerv (GL_DEPTH_FUNC, &aDefaultDepthFunc);
277 OpenGl_SequenceOfLayers::Iterator anIts;
278 for(anIts.Init (myLayers); anIts.More (); anIts.Next ())
280 const OpenGl_Layer& aLayer = anIts.Value ();
281 if (aLayer.PriorityList().NbStructures () > 0)
284 aLayer.Render (theWorkspace, aDefaultDepthFunc);