0027919: Visualization - support multiple transformation persistence groups within...
[occt.git] / src / Graphic3d / Graphic3d_GraphicDriver.cxx
1 // Created on: 1997-01-28
2 // Created by: CAL
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Graphic3d_GraphicDriver.hxx>
18
19 #include <Graphic3d_Layer.hxx>
20
21 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_GraphicDriver,Standard_Transient)
22
23 // =======================================================================
24 // function : Graphic3d_GraphicDriver
25 // purpose  :
26 // =======================================================================
27 Graphic3d_GraphicDriver::Graphic3d_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp)
28 : myDisplayConnection (theDisp)
29 {
30   // default layers are always presented in display layer sequence and cannot be removed
31   {
32     Graphic3d_ZLayerSettings aSettings;
33     aSettings.SetName ("UNDERLAY");
34     aSettings.SetImmediate          (Standard_False);
35     aSettings.SetRaytracable        (Standard_False);
36     aSettings.SetEnvironmentTexture (Standard_False);
37     aSettings.SetEnableDepthTest    (Standard_False);
38     aSettings.SetEnableDepthWrite   (Standard_False);
39     aSettings.SetClearDepth         (Standard_False);
40     aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
41     Handle(Graphic3d_Layer) aLayer = new Graphic3d_Layer (Graphic3d_ZLayerId_BotOSD, 1, Handle(Select3D_BVHBuilder3d)());
42     aLayer->SetLayerSettings (aSettings);
43     myLayers.Append (aLayer);
44     myLayerIds.Bind (aLayer->LayerId(), aLayer);
45   }
46
47   {
48     Graphic3d_ZLayerSettings aSettings;
49     aSettings.SetName ("DEFAULT");
50     aSettings.SetImmediate          (Standard_False);
51     aSettings.SetRaytracable        (Standard_True);
52     aSettings.SetEnvironmentTexture (Standard_True);
53     aSettings.SetEnableDepthTest    (Standard_True);
54     aSettings.SetEnableDepthWrite   (Standard_True);
55     aSettings.SetClearDepth         (Standard_False);
56     aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
57     Handle(Graphic3d_Layer) aLayer = new Graphic3d_Layer (Graphic3d_ZLayerId_Default, 1, Handle(Select3D_BVHBuilder3d)());
58     aLayer->SetLayerSettings (aSettings);
59     myLayers.Append (aLayer);
60     myLayerIds.Bind (aLayer->LayerId(), aLayer);
61   }
62
63   {
64     Graphic3d_ZLayerSettings aSettings;
65     aSettings.SetName ("TOP");
66     aSettings.SetImmediate          (Standard_True);
67     aSettings.SetRaytracable        (Standard_False);
68     aSettings.SetEnvironmentTexture (Standard_True);
69     aSettings.SetEnableDepthTest    (Standard_True);
70     aSettings.SetEnableDepthWrite   (Standard_True);
71     aSettings.SetClearDepth         (Standard_False);
72     aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
73     Handle(Graphic3d_Layer) aLayer = new Graphic3d_Layer (Graphic3d_ZLayerId_Top, 1, Handle(Select3D_BVHBuilder3d)());
74     aLayer->SetLayerSettings (aSettings);
75     myLayers.Append (aLayer);
76     myLayerIds.Bind (aLayer->LayerId(), aLayer);
77   }
78
79   {
80     Graphic3d_ZLayerSettings aSettings;
81     aSettings.SetName ("TOPMOST");
82     aSettings.SetImmediate          (Standard_True);
83     aSettings.SetRaytracable        (Standard_False);
84     aSettings.SetEnvironmentTexture (Standard_True);
85     aSettings.SetEnableDepthTest    (Standard_True);
86     aSettings.SetEnableDepthWrite   (Standard_True);
87     aSettings.SetClearDepth         (Standard_True);
88     aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
89     Handle(Graphic3d_Layer) aLayer = new Graphic3d_Layer (Graphic3d_ZLayerId_Topmost, 1, Handle(Select3D_BVHBuilder3d)());
90     aLayer->SetLayerSettings (aSettings);
91     myLayers.Append (aLayer);
92     myLayerIds.Bind (aLayer->LayerId(), aLayer);
93   }
94
95   {
96     Graphic3d_ZLayerSettings aSettings;
97     aSettings.SetName ("OVERLAY");
98     aSettings.SetImmediate          (Standard_True);
99     aSettings.SetRaytracable        (Standard_False);
100     aSettings.SetEnvironmentTexture (Standard_False);
101     aSettings.SetEnableDepthTest    (Standard_False);
102     aSettings.SetEnableDepthWrite   (Standard_False);
103     aSettings.SetClearDepth         (Standard_False);
104     aSettings.SetPolygonOffset (Graphic3d_PolygonOffset());
105     Handle(Graphic3d_Layer) aLayer = new Graphic3d_Layer (Graphic3d_ZLayerId_TopOSD, 1, Handle(Select3D_BVHBuilder3d)());
106     aLayer->SetLayerSettings (aSettings);
107     myLayers.Append (aLayer);
108     myLayerIds.Bind (aLayer->LayerId(), aLayer);
109   }
110 }
111
112 // =======================================================================
113 // function : GetDisplayConnection
114 // purpose  :
115 // =======================================================================
116 const Handle(Aspect_DisplayConnection)& Graphic3d_GraphicDriver::GetDisplayConnection() const
117 {
118   return myDisplayConnection;
119 }
120
121 // =======================================================================
122 // function : NewIdentification
123 // purpose  :
124 // =======================================================================
125 Standard_Integer Graphic3d_GraphicDriver::NewIdentification()
126 {
127   return myStructGenId.Next();
128 }
129
130 // =======================================================================
131 // function : RemoveIdentification
132 // purpose  :
133 // =======================================================================
134 void Graphic3d_GraphicDriver::RemoveIdentification(const Standard_Integer theId)
135 {
136   myStructGenId.Free(theId);
137 }
138
139 //=======================================================================
140 //function : ZLayerSettings
141 //purpose  :
142 //=======================================================================
143 const Graphic3d_ZLayerSettings& Graphic3d_GraphicDriver::ZLayerSettings (const Graphic3d_ZLayerId theLayerId) const
144 {
145   const Handle(Graphic3d_Layer)* aLayer = myLayerIds.Seek (theLayerId);
146   if (aLayer == NULL)
147   {
148     throw Standard_OutOfRange ("Graphic3d_GraphicDriver::ZLayerSettings, Layer with theLayerId does not exist");
149   }
150   return (*aLayer)->LayerSettings();
151 }
152
153 //=======================================================================
154 //function : ZLayers
155 //purpose  :
156 //=======================================================================
157 void Graphic3d_GraphicDriver::ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
158 {
159   theLayerSeq.Clear();
160
161   // append normal layers
162   for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
163   {
164     const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
165     if (!aLayer->IsImmediate())
166     {
167       theLayerSeq.Append (aLayer->LayerId());
168     }
169   }
170
171   // append immediate layers
172   for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
173   {
174     const Handle(Graphic3d_Layer)& aLayer = aLayerIter.Value();
175     if (aLayer->IsImmediate())
176     {
177       theLayerSeq.Append (aLayer->LayerId());
178     }
179   }
180 }
181
182 //=======================================================================
183 //function : InsertLayerBefore
184 //purpose  :
185 //=======================================================================
186 void Graphic3d_GraphicDriver::InsertLayerBefore (const Graphic3d_ZLayerId theNewLayerId,
187                                                  const Graphic3d_ZLayerSettings& theSettings,
188                                                  const Graphic3d_ZLayerId theLayerAfter)
189 {
190   Standard_ASSERT_RAISE (theNewLayerId > 0,
191                          "Graphic3d_GraphicDriver::InsertLayerBefore, negative and zero IDs are reserved");
192   Standard_ASSERT_RAISE (!myLayerIds.IsBound (theNewLayerId),
193                          "Graphic3d_GraphicDriver::InsertLayerBefore, Layer with theLayerId already exists");
194
195   Handle(Graphic3d_Layer) aNewLayer = new Graphic3d_Layer (theNewLayerId, 1, Handle(Select3D_BVHBuilder3d)());
196   aNewLayer->SetLayerSettings (theSettings);
197
198   Handle(Graphic3d_Layer) anOtherLayer;
199   if (theLayerAfter != Graphic3d_ZLayerId_UNKNOWN
200    && myLayerIds.Find (theLayerAfter, anOtherLayer))
201   {
202     for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
203     {
204       if (aLayerIter.Value() == anOtherLayer)
205       {
206         myLayers.InsertBefore (aNewLayer, aLayerIter);
207         break;
208       }
209     }
210   }
211   else
212   {
213     myLayers.Prepend (aNewLayer);
214   }
215   myLayerIds.Bind (theNewLayerId, aNewLayer);
216 }
217
218 //=======================================================================
219 //function : InsertLayerAfter
220 //purpose  :
221 //=======================================================================
222 void Graphic3d_GraphicDriver::InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
223                                                 const Graphic3d_ZLayerSettings& theSettings,
224                                                 const Graphic3d_ZLayerId theLayerBefore)
225 {
226   Standard_ASSERT_RAISE (theNewLayerId > 0,
227                          "Graphic3d_GraphicDriver::InsertLayerAfter, negative and zero IDs are reserved");
228   Standard_ASSERT_RAISE (!myLayerIds.IsBound (theNewLayerId),
229                          "Graphic3d_GraphicDriver::InsertLayerAfter, Layer with theLayerId already exists");
230
231   Handle(Graphic3d_Layer) aNewLayer = new Graphic3d_Layer (theNewLayerId, 1, Handle(Select3D_BVHBuilder3d)());
232   aNewLayer->SetLayerSettings (theSettings);
233
234   Handle(Graphic3d_Layer) anOtherLayer;
235   if (theLayerBefore != Graphic3d_ZLayerId_UNKNOWN
236    && myLayerIds.Find (theLayerBefore, anOtherLayer))
237   {
238     for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myLayers); aLayerIter.More(); aLayerIter.Next())
239     {
240       if (aLayerIter.Value() == anOtherLayer)
241       {
242         myLayers.InsertAfter (aNewLayer, aLayerIter);
243         break;
244       }
245     }
246   }
247   else
248   {
249     myLayers.Append (aNewLayer);
250   }
251   myLayerIds.Bind (theNewLayerId, aNewLayer);
252 }
253
254 //=======================================================================
255 //function : RemoveZLayer
256 //purpose  :
257 //=======================================================================
258 void Graphic3d_GraphicDriver::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
259 {
260   Standard_ASSERT_RAISE (theLayerId > 0,
261                          "Graphic3d_GraphicDriver::RemoveZLayer, negative and zero IDs are reserved and cannot be removed");
262
263   Handle(Graphic3d_Layer) aLayerDef;
264   myLayerIds.Find (theLayerId, aLayerDef);
265   Standard_ASSERT_RAISE (!aLayerDef.IsNull(),
266                          "Graphic3d_GraphicDriver::RemoveZLayer, Layer with theLayerId does not exist");
267   myLayers.Remove (aLayerDef);
268   myLayerIds.UnBind (theLayerId);
269 }
270
271 //=======================================================================
272 //function : SetZLayerSettings
273 //purpose  :
274 //=======================================================================
275 void Graphic3d_GraphicDriver::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
276                                                  const Graphic3d_ZLayerSettings& theSettings)
277 {
278   Handle(Graphic3d_Layer) aLayerDef;
279   myLayerIds.Find (theLayerId, aLayerDef);
280   Standard_ASSERT_RAISE (!aLayerDef.IsNull(),
281                          "Graphic3d_GraphicDriver::SetZLayerSettings, Layer with theLayerId does not exist");
282   aLayerDef->SetLayerSettings (theSettings);
283 }
284
285 // =======================================================================
286 // function : DumpJson
287 // purpose  :
288 // =======================================================================
289 void Graphic3d_GraphicDriver::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
290 {
291   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
292   
293   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myStructGenId)
294
295   for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator anIter (myLayers); anIter.More(); anIter.Next())
296   {
297     const Handle(Graphic3d_Layer)& aLayer = anIter.Value();
298     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aLayer.get())
299   }
300 }