bc802d051c48921a5e62e29f0a206b19fabb1382
[occt.git] / src / Graphic3d / Graphic3d_CView.hxx
1 // Copyright (c) 2015 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _Graphic3d_CView_HeaderFile
15 #define _Graphic3d_CView_HeaderFile
16
17 #include <Aspect_Handle.hxx>
18 #include <Aspect_RenderingContext.hxx>
19 #include <Aspect_Window.hxx>
20 #include <Graphic3d_BufferType.hxx>
21 #include <Graphic3d_Camera.hxx>
22 #include <Graphic3d_CLight.hxx>
23 #include <Graphic3d_CStructure.hxx>
24 #include <Graphic3d_DataStructureManager.hxx>
25 #include <Graphic3d_DiagnosticInfo.hxx>
26 #include <Graphic3d_GraduatedTrihedron.hxx>
27 #include <Graphic3d_MapOfStructure.hxx>
28 #include <Graphic3d_NMapOfTransient.hxx>
29 #include <Graphic3d_RenderingParams.hxx>
30 #include <Graphic3d_SequenceOfHClipPlane.hxx>
31 #include <Graphic3d_SequenceOfStructure.hxx>
32 #include <Graphic3d_Structure.hxx>
33 #include <Graphic3d_TextureEnv.hxx>
34 #include <Graphic3d_TypeOfAnswer.hxx>
35 #include <Graphic3d_TypeOfBackfacingModel.hxx>
36 #include <Graphic3d_TypeOfShadingModel.hxx>
37 #include <Graphic3d_TypeOfVisualization.hxx>
38 #include <Graphic3d_Vec3.hxx>
39 #include <Graphic3d_ZLayerId.hxx>
40 #include <Graphic3d_ZLayerSettings.hxx>
41 #include <Image_PixMap.hxx>
42 #include <Quantity_NameOfColor.hxx>
43 #include <Standard_Address.hxx>
44 #include <Standard_Transient.hxx>
45 #include <TColStd_IndexedDataMapOfStringString.hxx>
46
47 class Graphic3d_CView;
48 class Graphic3d_GraphicDriver;
49 class Graphic3d_StructureManager;
50
51 DEFINE_STANDARD_HANDLE (Graphic3d_CView, Graphic3d_DataStructureManager)
52
53 //! Base class of a graphical view that carries out rendering process for a concrete
54 //! implementation of graphical driver. Provides virtual interfaces for redrawing its
55 //! contents, management of displayed structures and render settings. The source code 
56 //! of the class itself implements functionality related to management of
57 //! computed (HLR or "view-dependent") structures.
58 class Graphic3d_CView : public Graphic3d_DataStructureManager
59 {
60   friend class Graphic3d_StructureManager;
61   DEFINE_STANDARD_RTTIEXT(Graphic3d_CView, Graphic3d_DataStructureManager)
62 public:
63
64   //! Constructor.
65   Standard_EXPORT Graphic3d_CView (const Handle(Graphic3d_StructureManager)& theMgr);
66
67   //! Destructor.
68   Standard_EXPORT virtual ~Graphic3d_CView();
69
70   //! Returns the identification number of the view.
71   Standard_Integer Identification() const { return myId; }
72
73   //! Activates the view. Maps presentations defined within structure manager onto this view.
74   Standard_EXPORT virtual void Activate();
75
76   //! Deactivates the view. Unmaps presentations defined within structure manager.
77   //! The view in deactivated state will ignore actions on structures such as Display().
78   Standard_EXPORT virtual void Deactivate();
79
80   //! Returns the activity flag of the view.
81   Standard_Boolean IsActive() const { return myIsActive; }
82
83   //! Erases the view and removes from graphic driver.
84   //! No more graphic operations are allowed in this view after the call.
85   Standard_EXPORT virtual void Remove();
86
87   //! Returns true if the view was removed.
88   Standard_Boolean IsRemoved() const { return myIsRemoved; }
89
90 public:
91
92   //! Returns default Shading Model of the view; Graphic3d_TOSM_FRAGMENT by default.
93   Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; }
94
95   //! Sets default Shading Model of the view.
96   //! Will throw an exception on attempt to set Graphic3d_TOSM_DEFAULT.
97   Standard_EXPORT void SetShadingModel (Graphic3d_TypeOfShadingModel theModel);
98
99   //! Returns visualization type of the view.
100   Graphic3d_TypeOfVisualization VisualizationType() const { return myVisualization; }
101
102   //! Sets visualization type of the view.
103   void SetVisualizationType (const Graphic3d_TypeOfVisualization theType) { myVisualization = theType; }
104
105   //! Switches computed HLR mode in the view
106   Standard_EXPORT void SetComputedMode (const Standard_Boolean theMode);
107
108   //! Returns the computed HLR mode state
109   Standard_Boolean ComputedMode() const { return myIsInComputedMode; }
110
111   //! Computes the new presentation of the structure  displayed in this view with the type Graphic3d_TOS_COMPUTED.
112   Standard_EXPORT void ReCompute (const Handle(Graphic3d_Structure)& theStructure);
113
114   //! Invalidates bounding box of specified ZLayerId.
115   Standard_EXPORT void Update (const Graphic3d_ZLayerId theLayerId = Graphic3d_ZLayerId_UNKNOWN);
116
117   //! Computes the new presentation of the structures displayed in this view with the type Graphic3d_TOS_COMPUTED.
118   Standard_EXPORT void Compute();
119
120   //! Returns Standard_True if one of the structures displayed in the view contains Polygons, Triangles or Quadrangles.
121   Standard_EXPORT Standard_Boolean ContainsFacet() const;
122
123   //! Returns Standard_True if one of the structures in the set contains Polygons, Triangles or Quadrangles.
124   Standard_EXPORT Standard_Boolean ContainsFacet (const Graphic3d_MapOfStructure& theSet) const;
125
126   //! Returns the set of structures displayed in this view.
127   Standard_EXPORT void DisplayedStructures (Graphic3d_MapOfStructure& theStructures) const;
128
129   //! Returns number of displayed structures in the view.
130   virtual Standard_Integer NumberOfDisplayedStructures() const { return myStructsDisplayed.Extent(); }
131
132   //! Returns map of objects hidden within this specific view (not viewer-wise).
133   const Handle(Graphic3d_NMapOfTransient)& HiddenObjects() const { return myHiddenObjects; }
134
135   //! Returns map of objects hidden within this specific view (not viewer-wise).
136   Handle(Graphic3d_NMapOfTransient)& ChangeHiddenObjects() { return myHiddenObjects; }
137
138   //! Returns Standard_True in case if the structure with the given <theStructId> is
139   //! in list of structures to be computed and stores computed struct to <theComputedStruct>.
140   Standard_EXPORT Standard_Boolean IsComputed (const Standard_Integer theStructId,
141                                                Handle(Graphic3d_Structure)& theComputedStruct) const;
142
143   //! Returns the bounding box of all structures displayed in the view.
144   //! If theToIncludeAuxiliary is TRUE, then the boundary box also includes minimum and maximum limits
145   //! of graphical elements forming parts of infinite and other auxiliary structures.
146   //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
147   //! @return computed bounding box
148   Standard_EXPORT virtual Bnd_Box MinMaxValues (const Standard_Boolean theToIncludeAuxiliary = Standard_False) const;
149
150   //! Returns the coordinates of the boundary box of all structures in the set <theSet>.
151   //! If <theToIgnoreInfiniteFlag> is TRUE, then the boundary box
152   //! also includes minimum and maximum limits of graphical elements
153   //! forming parts of infinite structures.
154   Standard_EXPORT Bnd_Box MinMaxValues (const Graphic3d_MapOfStructure& theSet,
155                                         const Standard_Boolean theToIncludeAuxiliary = Standard_False) const;
156
157   //! Returns the structure manager handle which manage structures associated with this view.
158   const Handle(Graphic3d_StructureManager)& StructureManager() const { return myStructureManager; }
159
160 private:
161
162   //! Is it possible to display the structure in the view?
163   Standard_EXPORT Graphic3d_TypeOfAnswer acceptDisplay (const Graphic3d_TypeOfStructure theStructType) const;
164
165   //! Clears the structure in this view.
166   Standard_EXPORT void Clear (const Handle(Graphic3d_Structure)& theStructure, const Standard_Boolean theWithDestruction);
167
168   //! Connects the structures.
169   Standard_EXPORT void Connect (const Handle(Graphic3d_Structure)& theMother,
170                                 const Handle(Graphic3d_Structure)& theDaughter);
171
172   //! Disconnects the structures.
173   Standard_EXPORT void Disconnect (const Handle(Graphic3d_Structure)& theMother,
174                                    const Handle(Graphic3d_Structure)& theDaughter);
175
176   //! Displays the structure in the view.
177   Standard_EXPORT void Display (const Handle(Graphic3d_Structure)& theStructure);
178
179   //! Erases the structure from the view.
180   Standard_EXPORT void Erase (const Handle(Graphic3d_Structure)& theStructure);
181
182   //! Highlights the structure in the view.
183   Standard_EXPORT void Highlight (const Handle(Graphic3d_Structure)& theStructure);
184
185   //! Transforms the structure in the view.
186   Standard_EXPORT void SetTransform (const Handle(Graphic3d_Structure)& theStructure,
187                                      const Handle(Geom_Transformation)& theTrsf);
188
189   //! Suppress the highlighting on the structure <AStructure>
190   //! in the view <me>.
191   Standard_EXPORT void UnHighlight (const Handle(Graphic3d_Structure)& theStructure);
192
193   //! Returns an index != 0 if the structure have another structure computed for the view <me>.
194   Standard_EXPORT Standard_Integer IsComputed (const Handle(Graphic3d_Structure)& theStructure) const;
195
196   //! Returns true if the structure is displayed in the view.
197   Standard_EXPORT Standard_Boolean IsDisplayed (const Handle(Graphic3d_Structure)& theStructure) const;
198
199   //! Changes the display priority of the structure.
200   Standard_EXPORT void ChangePriority (const Handle(Graphic3d_Structure)& theStructure,
201                                        const Standard_Integer theOldPriority,
202                                        const Standard_Integer theNewPriority);
203
204   //! Change Z layer of already displayed structure in the view.
205   Standard_EXPORT void ChangeZLayer (const Handle(Graphic3d_Structure)& theStructure,
206                                      const Graphic3d_ZLayerId theLayerId);
207
208   //! Returns an index != 0 if the structure have the same owner than another structure
209   //! in the sequence of the computed structures.
210   Standard_EXPORT Standard_Integer HaveTheSameOwner (const Handle(Graphic3d_Structure)& theStructure) const;
211
212 public:
213
214   //! Redraw content of the view.
215   virtual void Redraw() = 0;
216
217   //! Redraw immediate content of the view.
218   virtual void RedrawImmediate() = 0;
219
220   //! Invalidates content of the view but does not redraw it.
221   virtual void Invalidate() = 0;
222
223   //! Return true if view content cache has been invalidated.
224   virtual Standard_Boolean IsInvalidated() = 0;
225
226   //! Handle changing size of the rendering window.
227   virtual void Resized() = 0;
228
229   //! @param theDrawToFrontBuffer Advanced option to modify rendering mode:
230   //! 1. TRUE.  Drawing immediate mode structures directly to the front buffer over the scene image.
231   //! Fast, so preferred for interactive work (used by default).
232   //! However these extra drawings will be missed in image dump since it is performed from back buffer.
233   //! Notice that since no pre-buffering used the V-Sync will be ignored and rendering could be seen
234   //! in run-time (in case of slow hardware) and/or tearing may appear.
235   //! So this is strongly recommended to draw only simple (fast) structures.
236   //! 2. FALSE. Drawing immediate mode structures to the back buffer.
237   //! The complete scene is redrawn first, so this mode is slower if scene contains complex data and/or V-Sync
238   //! is turned on. But it works in any case and is especially useful for view dump because the dump image is read
239   //! from the back buffer.
240   //! @return previous mode.
241   virtual Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer) = 0;
242
243   //! Creates and maps rendering window to the view.
244   //! @param theWindow [in] the window.
245   //! @param theContext [in] the rendering context. If NULL the context will be created internally.
246   virtual void SetWindow (const Handle(Aspect_Window)& theWindow,
247                           const Aspect_RenderingContext theContext = NULL) = 0;
248
249   //! Returns the window associated to the view.
250   virtual Handle(Aspect_Window) Window() const = 0;
251
252   //! Returns True if the window associated to the view is defined.
253   virtual Standard_Boolean IsDefined() const = 0;
254
255   //! Returns data of a graduated trihedron
256   virtual const Graphic3d_GraduatedTrihedron& GetGraduatedTrihedron() = 0;
257
258   //! Displays Graduated Trihedron.
259   virtual void GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData) = 0;
260
261   //! Erases Graduated Trihedron.
262   virtual void GraduatedTrihedronErase() = 0;
263
264   //! Sets minimum and maximum points of scene bounding box for Graduated Trihedron stored in graphic view object.
265   //! @param theMin [in] the minimum point of scene.
266   //! @param theMax [in] the maximum point of scene.
267   virtual void GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax) = 0;
268
269   //! Dump active rendering buffer into specified memory buffer.
270   virtual Standard_Boolean BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType) = 0;
271
272   //! Marks BVH tree and the set of BVH primitives of correspondent priority list with id theLayerId as outdated.
273   virtual void InvalidateBVHData (const Graphic3d_ZLayerId theLayerId) = 0;
274
275   //! Add a new top-level z layer with ID <theLayerId> for
276   //! the view. Z layers allow drawing structures in higher layers
277   //! in foreground of structures in lower layers. To add a structure
278   //! to desired layer on display it is necessary to set the layer
279   //! ID for the structure.
280   virtual void AddZLayer (const Graphic3d_ZLayerId theLayerId) = 0;
281
282   //! Returns the maximum Z layer ID.
283   //! First layer ID is Graphic3d_ZLayerId_Default, last ID is ZLayerMax().
284   virtual Standard_Integer ZLayerMax() const = 0;
285
286   //! Returns the bounding box of all structures displayed in the Z layer.
287   virtual void InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const = 0;
288
289   //! Returns the bounding box of all structures displayed in the Z layer.
290   //! @param theLayerId            layer identifier
291   //! @param theCamera             camera definition
292   //! @param theWindowWidth        viewport width  (for applying transformation-persistence)
293   //! @param theWindowHeight       viewport height (for applying transformation-persistence)
294   //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
295   //! @return computed bounding box
296   virtual Bnd_Box ZLayerBoundingBox (const Graphic3d_ZLayerId        theLayerId,
297                                      const Handle(Graphic3d_Camera)& theCamera,
298                                      const Standard_Integer          theWindowWidth,
299                                      const Standard_Integer          theWindowHeight,
300                                      const Standard_Boolean          theToIncludeAuxiliary) const = 0;
301
302   //! Remove Z layer from the specified view. All structures
303   //! displayed at the moment in layer will be displayed in default layer
304   //! ( the bottom-level z layer ). To unset layer ID from associated
305   //! structures use method UnsetZLayer (...).
306   virtual void RemoveZLayer (const Graphic3d_ZLayerId theLayerId) = 0;
307
308   //! Sets the settings for a single Z layer of specified view.
309   virtual void SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
310                                   const Graphic3d_ZLayerSettings& theSettings) = 0;
311
312   //! Returns zoom-scale factor.
313   Standard_EXPORT Standard_Real ConsiderZoomPersistenceObjects();
314
315   //! Returns pointer to an assigned framebuffer object.
316   virtual Handle(Standard_Transient) FBO() const = 0;
317
318   //! Sets framebuffer object for offscreen rendering.
319   virtual void SetFBO (const Handle(Standard_Transient)& theFbo) = 0;
320
321   //! Generate offscreen FBO in the graphic library.
322   //! If not supported on hardware returns NULL.
323   virtual Handle(Standard_Transient) FBOCreate (const Standard_Integer theWidth,
324                                                 const Standard_Integer theHeight) = 0;
325
326   //! Remove offscreen FBO from the graphic library
327   virtual void FBORelease (Handle(Standard_Transient)& theFbo) = 0;
328
329   //! Read offscreen FBO configuration.
330   virtual void FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
331                                  Standard_Integer& theWidth,
332                                  Standard_Integer& theHeight,
333                                  Standard_Integer& theWidthMax,
334                                  Standard_Integer& theHeightMax) = 0;
335
336   //! Change offscreen FBO viewport.
337   virtual void FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
338                                   const Standard_Integer theWidth,
339                                   const Standard_Integer theHeight) = 0;
340
341 public:
342
343   //! Copy visualization settings from another view.
344   //! Method is used for cloning views in viewer when its required to create view
345   //! with same view properties.
346   Standard_EXPORT virtual void CopySettings (const Handle(Graphic3d_CView)& theOther);
347
348   //! Returns current rendering parameters and effect settings.
349   const Graphic3d_RenderingParams& RenderingParams() const { return myRenderParams; }
350
351   //! Returns reference to current rendering parameters and effect settings.
352   Graphic3d_RenderingParams& ChangeRenderingParams() { return myRenderParams; }
353
354   //! Returns background  fill color.
355   virtual Aspect_Background Background() const = 0;
356
357   //! Sets background fill color.
358   virtual void SetBackground (const Aspect_Background& theBackground) = 0;
359
360   //! Returns gradient background fill colors.
361   virtual Aspect_GradientBackground GradientBackground() const = 0;
362
363   //! Sets gradient background fill colors.
364   virtual void SetGradientBackground (const Aspect_GradientBackground& theBackground) = 0;
365
366   //! Returns background image texture file path.
367   virtual TCollection_AsciiString BackgroundImage() = 0;
368
369   //! Sets background image texture file path.
370   virtual void SetBackgroundImage (const TCollection_AsciiString& theFilePath) = 0;
371
372   //! Returns background image fill style.
373   virtual Aspect_FillMethod BackgroundImageStyle() const = 0;
374
375   //! Sets background image fill style.
376   virtual void SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle) = 0;
377
378   //! Returns environment texture set for the view.
379   virtual Handle(Graphic3d_TextureEnv) TextureEnv() const = 0; 
380
381   //! Sets environment texture for the view.
382   virtual void SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv) = 0;
383
384   //! Returns the state of frustum culling optimization.
385   virtual Standard_Boolean IsCullingEnabled() const = 0;
386
387   //! Enables or disables frustum culling optimization.
388   virtual void SetCullingEnabled (const Standard_Boolean theIsEnabled) = 0;
389
390   //! Return backfacing model used for the view.
391   virtual Graphic3d_TypeOfBackfacingModel BackfacingModel() const = 0;
392
393   //! Sets backfacing model for the view.
394   virtual void SetBackfacingModel (const Graphic3d_TypeOfBackfacingModel theModel) = 0;
395
396   //! Returns camera object of the view.
397   virtual const Handle(Graphic3d_Camera)& Camera() const = 0;
398
399   //! Sets camera used by the view.
400   virtual void SetCamera (const Handle(Graphic3d_Camera)& theCamera) = 0;
401
402   //! Returns list of lights of the view.
403   virtual const Handle(Graphic3d_LightSet)& Lights() const = 0;
404
405   //! Sets list of lights for the view.
406   virtual void SetLights (const Handle(Graphic3d_LightSet)& theLights) = 0;
407
408   //! Returns list of clip planes set for the view.
409   virtual const Handle(Graphic3d_SequenceOfHClipPlane)& ClipPlanes() const = 0;
410
411   //! Sets list of clip planes for the view.
412   virtual void SetClipPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) = 0;
413
414   //! Fill in the dictionary with diagnostic info.
415   //! Should be called within rendering thread.
416   //!
417   //! This API should be used only for user output or for creating automated reports.
418   //! The format of returned information (e.g. key-value layout)
419   //! is NOT part of this API and can be changed at any time.
420   //! Thus application should not parse returned information to weed out specific parameters.
421   virtual void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
422                                       Graphic3d_DiagnosticInfo theFlags) const = 0;
423
424 private:
425
426   //! Adds the structure to display lists of the view.
427   virtual void displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
428                                  const Standard_Integer thePriority) = 0;
429
430   //! Erases the structure from display lists of the view.
431   virtual void eraseStructure (const Handle(Graphic3d_CStructure)& theStructure) = 0;
432
433   //! Change Z layer of a structure already presented in view.
434   virtual void changeZLayer (const Handle(Graphic3d_CStructure)& theCStructure,
435                              const Graphic3d_ZLayerId theNewLayerId) = 0;
436
437   //! Changes the priority of a structure within its Z layer in the specified view.
438   virtual void changePriority (const Handle(Graphic3d_CStructure)& theCStructure,
439                                const Standard_Integer theNewPriority) = 0;
440
441   //! Returns zoom-scale factor.
442   virtual Standard_Real considerZoomPersistenceObjects (const Graphic3d_ZLayerId        theLayerId,
443                                                         const Handle(Graphic3d_Camera)& theCamera,
444                                                         const Standard_Integer          theWindowWidth,
445                                                         const Standard_Integer          theWindowHeight) const = 0;
446
447 protected:
448
449   Standard_Integer myId;
450   Graphic3d_RenderingParams myRenderParams;
451   Handle(Graphic3d_StructureManager) myStructureManager;
452   Graphic3d_SequenceOfStructure myStructsToCompute;
453   Graphic3d_SequenceOfStructure myStructsComputed;
454   Graphic3d_MapOfStructure myStructsDisplayed;
455   Handle(Graphic3d_NMapOfTransient) myHiddenObjects;
456   Standard_Boolean myIsInComputedMode;
457   Standard_Boolean myIsActive;
458   Standard_Boolean myIsRemoved;
459   Graphic3d_TypeOfShadingModel  myShadingModel;
460   Graphic3d_TypeOfVisualization myVisualization;
461
462 };
463
464 #endif // _Graphic3d_CView_HeaderFile