0025027: Visualization - 2D layer viewport is updated incorrectly on resize
[occt.git] / src / OpenGl / OpenGl_LayerList.cxx
CommitLineData
6aca4d39 1// Created on: 2012-02-02
b311480e 2// Created by: Anton POLETAEV
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
59f45b7c 15
5f8b738e 16#include <OpenGl_GlCore11.hxx>
17
59f45b7c 18#include <OpenGl_LayerList.hxx>
59f45b7c 19#include <OpenGl_Structure.hxx>
550f3b8b 20#include <OpenGl_Workspace.hxx>
59f45b7c 21
22#include <InterfaceGraphic_Graphic3d.hxx>
23#include <InterfaceGraphic.hxx>
24
25//=======================================================================
26//function : OpenGl_LayerList
27//purpose : Constructor
28//=======================================================================
29
30OpenGl_LayerList::OpenGl_LayerList (const Standard_Integer theNbPriorities)
31 : myNbPriorities (theNbPriorities),
32 myNbStructures (0)
33{
34 // insert default priority layer
c5751993 35 myLayers.Append (OpenGl_Layer (myNbPriorities));
59f45b7c 36 myLayerIds.Bind (0, myLayers.Length());
37}
38
39//=======================================================================
40//function : ~OpenGl_LayerList
41//purpose : Destructor
42//=======================================================================
43
44OpenGl_LayerList::~OpenGl_LayerList ()
45{
46}
47
48//=======================================================================
49//function : defaultLayer
50//purpose :
51//=======================================================================
52
c5751993 53OpenGl_Layer& OpenGl_LayerList::defaultLayer()
59f45b7c 54{
55 return myLayers.ChangeValue (1);
56}
57
58//=======================================================================
59//function : NbPriorities
60//purpose : Method returns the number of available priorities
61//=======================================================================
62
63Standard_Integer OpenGl_LayerList::NbPriorities () const
64{
65 return myNbPriorities;
66}
67
68//=======================================================================
69//function : NbStructures
70//purpose : Method returns the number of available structures
71//=======================================================================
72
73Standard_Integer OpenGl_LayerList::NbStructures () const
74{
75 return myNbStructures;
76}
77
78//=======================================================================
79//function : AddLayer
80//purpose :
81//=======================================================================
82
83void OpenGl_LayerList::AddLayer (const Standard_Integer theLayerId)
84{
85 if (HasLayer (theLayerId))
86 return;
87
88 // add the new layer
c5751993 89 myLayers.Append (OpenGl_Layer (myNbPriorities));
59f45b7c 90 myLayerIds.Bind (theLayerId, myLayers.Length());
91}
92
93//=======================================================================
94//function : HasLayer
95//purpose :
96//=======================================================================
97
98Standard_Boolean OpenGl_LayerList::HasLayer
99 (const Standard_Integer theLayerId) const
100{
101 return myLayerIds.IsBound (theLayerId);
102}
103
104//=======================================================================
c5751993 105//function : Layer
106//purpose :
107//=======================================================================
108OpenGl_Layer& OpenGl_LayerList::Layer (const Standard_Integer theLayerId)
109{
110 return myLayers.ChangeValue (myLayerIds.Find (theLayerId));
111}
112
113//=======================================================================
114//function : Layer
115//purpose :
116//=======================================================================
117const OpenGl_Layer& OpenGl_LayerList::Layer (const Standard_Integer theLayerId) const
118{
119 return myLayers.Value (myLayerIds.Find (theLayerId));
120}
121
122//=======================================================================
59f45b7c 123//function : RemoveLayer
124//purpose :
125//=======================================================================
126
127void OpenGl_LayerList::RemoveLayer (const Standard_Integer theLayerId)
128{
129 if (!HasLayer (theLayerId) || theLayerId == 0)
130 return;
131
132 Standard_Integer aRemovePos = myLayerIds.Find (theLayerId);
133
134 // move all displayed structures to first layer
c5751993 135 const OpenGl_PriorityList& aList = myLayers.Value (aRemovePos).PriorityList();
136 defaultLayer ().PriorityList().Append (aList);
59f45b7c 137
138 // remove layer
139 myLayers.Remove (aRemovePos);
140 myLayerIds.UnBind (theLayerId);
141
142 // updated sequence indexes in map
143 OpenGl_LayerSeqIds::Iterator aMapIt (myLayerIds);
144 for ( ; aMapIt.More (); aMapIt.Next ())
145 {
146 Standard_Integer& aSeqIdx = aMapIt.ChangeValue ();
147 if (aSeqIdx > aRemovePos)
148 aSeqIdx--;
149 }
150}
151
152//=======================================================================
153//function : AddStructure
154//purpose :
155//=======================================================================
156
157void OpenGl_LayerList::AddStructure (const OpenGl_Structure *theStructure,
158 const Standard_Integer theLayerId,
159 const Standard_Integer thePriority)
160{
161 // add structure to associated layer,
162 // if layer doesn't exists, display structure in default layer
c5751993 163 OpenGl_PriorityList& aList = !HasLayer (theLayerId) ? defaultLayer ().PriorityList() :
164 myLayers.ChangeValue (myLayerIds.Find (theLayerId)).PriorityList();
59f45b7c 165
166 aList.Add (theStructure, thePriority);
167 myNbStructures++;
e276548b 168
169 // Note: In ray-tracing mode we don't modify modification
170 // state here. It is redundant, because the possible changes
171 // will be handled in the loop for structures
59f45b7c 172}
173
174//=======================================================================
175//function : RemoveStructure
176//purpose :
177//=======================================================================
178
179void OpenGl_LayerList::RemoveStructure (const OpenGl_Structure *theStructure,
180 const Standard_Integer theLayerId)
181{
182 Standard_Integer aSeqPos = !HasLayer (theLayerId) ?
183 1 : myLayerIds.Find (theLayerId);
184
c5751993 185 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
59f45b7c 186
187 // remove structure from associated list
188 // if the structure is not found there,
189 // scan through layers and remove it
190 if (aList.Remove (theStructure) >= 0)
191 {
192 myNbStructures--;
e276548b 193
e276548b 194 if (theStructure->IsRaytracable())
195 {
196 myModificationState++;
197 }
e276548b 198
59f45b7c 199 return;
200 }
201
202 // scan through layers and remove it
203 Standard_Integer aSeqId = 1;
204 OpenGl_SequenceOfLayers::Iterator anIts;
205 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
206 {
c5751993 207 OpenGl_PriorityList& aScanList = anIts.ChangeValue ().PriorityList();
59f45b7c 208 if (aSeqPos == aSeqId)
209 continue;
210
211 if (aScanList.Remove (theStructure) >= 0)
212 {
213 myNbStructures--;
e276548b 214
e276548b 215 if (theStructure->IsRaytracable())
216 {
217 myModificationState++;
218 }
e276548b 219
59f45b7c 220 return;
221 }
222 }
223}
224
225//=======================================================================
226//function : ChangeLayer
227//purpose :
228//=======================================================================
229
230void OpenGl_LayerList::ChangeLayer (const OpenGl_Structure *theStructure,
231 const Standard_Integer theOldLayerId,
232 const Standard_Integer theNewLayerId)
233{
234 Standard_Integer aSeqPos = !HasLayer (theOldLayerId) ?
235 1 : myLayerIds.Find (theOldLayerId);
236
c5751993 237 OpenGl_PriorityList& aList = myLayers.ChangeValue (aSeqPos).PriorityList();
59f45b7c 238 Standard_Integer aPriority;
239
240 // take priority and remove structure from list found by <theOldLayerId>
241 // if the structure is not found there, scan through all other layers
242 if ((aPriority = aList.Remove (theStructure)) >= 0)
243 {
244 myNbStructures--;
245 AddStructure (theStructure, theNewLayerId, aPriority);
246 }
247 else
248 {
249 // scan through layers and remove it
250 Standard_Integer aSeqId = 1;
251 OpenGl_SequenceOfLayers::Iterator anIts;
252 for (anIts.Init (myLayers); anIts.More (); anIts.Next (), aSeqId++)
253 {
59f45b7c 254 if (aSeqPos == aSeqId)
255 continue;
256
257 // try to remove structure and get priority value from this layer
258 if ((aPriority = aList.Remove (theStructure)) >= 0)
259 {
260 myNbStructures--;
261 AddStructure (theStructure, theNewLayerId, aPriority);
262 break;
263 }
264 }
265 }
266}
267
268//=======================================================================
269//function : Render
270//purpose : Render this element
271//=======================================================================
272
273void OpenGl_LayerList::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
274{
550f3b8b 275 OpenGl_GlobalLayerSettings aDefaultSettings;
276
277 glGetIntegerv (GL_DEPTH_FUNC, &aDefaultSettings.DepthFunc);
278 glGetBooleanv (GL_DEPTH_WRITEMASK, &aDefaultSettings.DepthMask);
c5751993 279
59f45b7c 280 OpenGl_SequenceOfLayers::Iterator anIts;
550f3b8b 281 for (anIts.Init (myLayers); anIts.More(); anIts.Next())
59f45b7c 282 {
c5751993 283 const OpenGl_Layer& aLayer = anIts.Value ();
284 if (aLayer.PriorityList().NbStructures () > 0)
59f45b7c 285 {
c5751993 286 // render layer
550f3b8b 287 aLayer.Render (theWorkspace, aDefaultSettings);
59f45b7c 288 }
289 }
550f3b8b 290
291 glDepthMask (aDefaultSettings.DepthMask);
292 glDepthFunc (aDefaultSettings.DepthFunc);
59f45b7c 293}