1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <V3d_Viewer.hxx>
16 #include <Aspect_Grid.hxx>
17 #include <Aspect_IdentDefinitionError.hxx>
18 #include <Graphic3d_AspectMarker3d.hxx>
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Graphic3d_Group.hxx>
21 #include <Graphic3d_Structure.hxx>
22 #include <Standard_ErrorHandler.hxx>
23 #include <Standard_Type.hxx>
25 #include <V3d_BadValue.hxx>
26 #include <V3d_CircularGrid.hxx>
27 #include <V3d_AmbientLight.hxx>
28 #include <V3d_DirectionalLight.hxx>
29 #include <V3d_RectangularGrid.hxx>
30 #include <V3d_View.hxx>
32 IMPLEMENT_STANDARD_RTTIEXT(V3d_Viewer, Standard_Transient)
34 // ========================================================================
35 // function : V3d_Viewer
37 // ========================================================================
38 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver)
39 : myDriver (theDriver),
40 myStructureManager (new Graphic3d_StructureManager (theDriver)),
41 myZLayerGenId (1, IntegerLast()),
42 myBackground (Quantity_NOC_GRAY30),
44 myViewProj (V3d_XposYnegZpos),
45 myVisualization (V3d_ZBUFFER),
46 myShadingModel (V3d_GOURAUD),
47 myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
48 myComputedMode (Standard_True),
49 myDefaultComputedMode (Standard_False),
50 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
51 myDisplayPlane (Standard_False),
52 myDisplayPlaneLength (1000.0),
53 myGridType (Aspect_GT_Rectangular),
54 myGridEcho (Standard_True),
55 myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
57 myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
58 myCGrid = new V3d_CircularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
61 // ========================================================================
62 // function : V3d_Viewer
64 // ========================================================================
65 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver,
66 const Standard_ExtString ,
67 const Standard_CString ,
68 const Standard_Real theViewSize,
69 const V3d_TypeOfOrientation theViewProj,
70 const Quantity_Color& theViewBackground,
71 const V3d_TypeOfVisualization theVisualization,
72 const V3d_TypeOfShadingModel theShadingModel,
73 const Standard_Boolean theComputedMode,
74 const Standard_Boolean theDefaultComputedMode)
75 : myDriver (theDriver),
76 myStructureManager (new Graphic3d_StructureManager (theDriver)),
77 myZLayerGenId (1, IntegerLast()),
78 myBackground (theViewBackground),
79 myViewSize (theViewSize),
80 myViewProj (theViewProj),
81 myVisualization (theVisualization),
82 myShadingModel (theShadingModel),
83 myDefaultTypeOfView (V3d_ORTHOGRAPHIC),
84 myComputedMode (theComputedMode),
85 myDefaultComputedMode (theDefaultComputedMode),
86 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
87 myDisplayPlane (Standard_False),
88 myDisplayPlaneLength (theViewSize),
89 myGridType (Aspect_GT_Rectangular),
90 myGridEcho (Standard_True),
91 myGridEchoLastVert (ShortRealLast(), ShortRealLast(), ShortRealLast())
93 myRGrid = new V3d_RectangularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
94 myCGrid = new V3d_CircularGrid (this, Quantity_Color (Quantity_NOC_GRAY50), Quantity_Color (Quantity_NOC_GRAY70));
95 SetDefaultViewSize (theViewSize);
98 // ========================================================================
99 // function : CreateView
101 // ========================================================================
102 Handle(V3d_View) V3d_Viewer::CreateView ()
104 return new V3d_View(this, myDefaultTypeOfView);
107 // ========================================================================
108 // function : SetViewOn
110 // ========================================================================
111 void V3d_Viewer::SetViewOn()
113 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
115 SetViewOn (aDefViewIter.Value());
119 // ========================================================================
120 // function : SetViewOff
122 // ========================================================================
123 void V3d_Viewer::SetViewOff()
125 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
127 SetViewOff (aDefViewIter.Value());
131 // ========================================================================
132 // function : SetViewOn
134 // ========================================================================
135 void V3d_Viewer::SetViewOn (const Handle(V3d_View)& theView)
137 Handle(Graphic3d_CView) aViewImpl = theView->View();
138 if (!aViewImpl->IsDefined() || myActiveViews.Contains (theView))
143 myActiveViews.Append (theView);
144 aViewImpl->Activate();
145 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
147 theView->SetLightOn (anActiveLightIter.Value());
150 theView->SetGrid (myPrivilegedPlane, Grid ());
151 theView->SetGridActivity (Grid ()->IsActive ());
155 // ========================================================================
156 // function : SetViewOff
158 // ========================================================================
159 void V3d_Viewer::SetViewOff (const Handle(V3d_View)& theView)
161 Handle(Graphic3d_CView) aViewImpl = theView->View();
162 if (aViewImpl->IsDefined() && myActiveViews.Contains (theView))
164 myActiveViews.Remove (theView);
165 aViewImpl->Deactivate() ;
169 // ========================================================================
172 // ========================================================================
173 void V3d_Viewer::Redraw() const
175 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
177 aDefViewIter.Value()->Redraw();
181 // ========================================================================
182 // function : RedrawImmediate
184 // ========================================================================
185 void V3d_Viewer::RedrawImmediate() const
187 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
189 aDefViewIter.Value()->RedrawImmediate();
193 // ========================================================================
194 // function : Invalidate
196 // ========================================================================
197 void V3d_Viewer::Invalidate() const
199 for (V3d_ListOfView::Iterator aDefViewIter (myDefinedViews); aDefViewIter.More(); aDefViewIter.Next())
201 aDefViewIter.Value()->Invalidate();
205 // ========================================================================
208 // ========================================================================
209 void V3d_Viewer::Remove()
211 myStructureManager->Remove();
214 // ========================================================================
217 // ========================================================================
218 void V3d_Viewer::Erase() const
220 myStructureManager->Erase();
223 // ========================================================================
224 // function : UnHighlight
226 // ========================================================================
227 void V3d_Viewer::UnHighlight() const
229 myStructureManager->UnHighlight();
232 void V3d_Viewer::SetDefaultViewSize (const Standard_Real theSize)
235 throw V3d_BadValue("V3d_Viewer::SetDefaultViewSize, bad size");
236 myViewSize = theSize;
239 // ========================================================================
240 // function : IfMoreViews
242 // ========================================================================
243 Standard_Boolean V3d_Viewer::IfMoreViews() const
245 return myDefinedViews.Size() < myStructureManager->MaxNumOfViews();
248 // ========================================================================
249 // function : AddView
251 // ========================================================================
252 void V3d_Viewer::AddView (const Handle(V3d_View)& theView)
254 if (!myDefinedViews.Contains (theView))
256 myDefinedViews.Append (theView);
260 // ========================================================================
261 // function : DelView
263 // ========================================================================
264 void V3d_Viewer::DelView (const Handle(V3d_View)& theView)
266 myActiveViews.Remove (theView);
267 myDefinedViews.Remove (theView);
270 //=======================================================================
271 //function : AddZLayer
273 //=======================================================================
274 Standard_Boolean V3d_Viewer::AddZLayer (Standard_Integer& theLayerId)
279 theLayerId = myZLayerGenId.Next();
281 catch (Aspect_IdentDefinitionError)
283 // new index can't be generated
284 return Standard_False;
287 myLayerIds.Add (theLayerId);
288 myDriver->AddZLayer (theLayerId);
290 return Standard_True;
293 //=======================================================================
294 //function : RemoveZLayer
296 //=======================================================================
297 Standard_Boolean V3d_Viewer::RemoveZLayer (const Standard_Integer theLayerId)
299 if (!myLayerIds.Contains (theLayerId)
300 || theLayerId < myZLayerGenId.Lower()
301 || theLayerId > myZLayerGenId.Upper())
303 return Standard_False;
306 myDriver->RemoveZLayer (theLayerId);
307 myLayerIds.Remove (theLayerId);
308 myZLayerGenId.Free (theLayerId);
310 return Standard_True;
313 //=======================================================================
314 //function : GetAllZLayers
316 //=======================================================================
317 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
319 myDriver->ZLayers (theLayerSeq);
322 //=======================================================================
323 //function : SetZLayerSettings
325 //=======================================================================
326 void V3d_Viewer::SetZLayerSettings (const Standard_Integer theLayerId, const Graphic3d_ZLayerSettings& theSettings)
328 myDriver->SetZLayerSettings (theLayerId, theSettings);
331 //=======================================================================
332 //function : ZLayerSettings
334 //=======================================================================
335 Graphic3d_ZLayerSettings V3d_Viewer::ZLayerSettings (const Standard_Integer theLayerId)
337 return myDriver->ZLayerSettings (theLayerId);
340 //=======================================================================
341 //function : UpdateLights
343 //=======================================================================
344 void V3d_Viewer::UpdateLights()
346 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
348 anActiveViewIter.Value()->UpdateLights();
352 //=======================================================================
353 //function : SetLightOn
355 //=======================================================================
356 void V3d_Viewer::SetLightOn (const Handle(V3d_Light)& theLight)
358 if (!myActiveLights.Contains (theLight))
360 myActiveLights.Append (theLight);
363 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
365 anActiveViewIter.Value()->SetLightOn (theLight);
369 //=======================================================================
370 //function : SetLightOff
372 //=======================================================================
373 void V3d_Viewer::SetLightOff (const Handle(V3d_Light)& theLight)
375 myActiveLights.Remove (theLight);
376 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
378 anActiveViewIter.Value()->SetLightOff (theLight);
382 //=======================================================================
383 //function : SetLightOn
385 //=======================================================================
386 void V3d_Viewer::SetLightOn()
388 for (V3d_ListOfLight::Iterator aDefLightIter (myDefinedLights); aDefLightIter.More(); aDefLightIter.Next())
390 if (!myActiveLights.Contains (aDefLightIter.Value()))
392 myActiveLights.Append (aDefLightIter.Value());
393 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
395 anActiveViewIter.Value()->SetLightOn (aDefLightIter.Value());
401 //=======================================================================
402 //function : SetLightOff
404 //=======================================================================
405 void V3d_Viewer::SetLightOff()
407 for (V3d_ListOfLight::Iterator anActiveLightIter (myActiveLights); anActiveLightIter.More(); anActiveLightIter.Next())
409 for (V3d_ListOfView::Iterator anActiveViewIter (myActiveViews); anActiveViewIter.More(); anActiveViewIter.Next())
411 anActiveViewIter.Value()->SetLightOff (anActiveLightIter.Value());
414 myActiveLights.Clear();
417 //=======================================================================
418 //function : IsGlobalLight
420 //=======================================================================
421 Standard_Boolean V3d_Viewer::IsGlobalLight (const Handle(V3d_Light)& theLight) const
423 return myActiveLights.Contains (theLight);
426 //=======================================================================
427 //function : AddLight
429 //=======================================================================
430 void V3d_Viewer::AddLight (const Handle(V3d_Light)& theLight)
432 if (!myDefinedLights.Contains (theLight))
434 myDefinedLights.Append (theLight);
438 //=======================================================================
439 //function : DelLight
441 //=======================================================================
442 void V3d_Viewer::DelLight (const Handle(V3d_Light)& theLight)
444 SetLightOff (theLight);
445 myDefinedLights.Remove (theLight);
448 //=======================================================================
449 //function : SetDefaultLights
451 //=======================================================================
452 void V3d_Viewer::SetDefaultLights()
454 while (!myDefinedLights.IsEmpty())
456 Handle(V3d_Light) aLight = myDefinedLights.First();
460 SetLightOn (new V3d_DirectionalLight (this, V3d_Zneg, Quantity_NOC_WHITE, Standard_True));
461 SetLightOn (new V3d_AmbientLight (this));