0025552: Visualization - provide the way to hide the object in specified view of...
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.hxx
1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_GraphicDriver_HeaderFile
17 #define _OpenGl_GraphicDriver_HeaderFile
18
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Handle_OpenGl_GraphicDriver.hxx>
21 #include <OpenGl_Context.hxx>
22 #include <OpenGl_PrinterContext.hxx>
23
24 #include <Standard_CString.hxx>
25
26 #include <Handle_TColStd_HArray1OfByte.hxx>
27 #include <Handle_TColStd_HArray1OfReal.hxx>
28 #include <Quantity_PlaneAngle.hxx>
29 #include <Quantity_NameOfColor.hxx>
30 #include <Handle_OpenGl_View.hxx>
31 #include <Handle_OpenGl_Workspace.hxx>
32
33 #include <Aspect_Display.hxx>
34 #include <Aspect_DisplayConnection.hxx>
35 #include <Aspect_GradientFillMethod.hxx>
36 #include <Aspect_FillMethod.hxx>
37 #include <Aspect_CLayer2d.hxx>
38 #include <Aspect_TypeOfTriedronPosition.hxx>
39 #include <Aspect_TypeOfTriedronEcho.hxx>
40 #include <Aspect_Handle.hxx>
41 #include <Aspect_PrintAlgo.hxx>
42 #include <gp_Ax2.hxx>
43 #include <Graphic3d_CView.hxx>
44 #include <Graphic3d_CStructure.hxx>
45 #include <Graphic3d_TextPath.hxx>
46 #include <Graphic3d_HorizontalTextAlignment.hxx>
47 #include <Graphic3d_VerticalTextAlignment.hxx>
48 #include <Graphic3d_CUserDraw.hxx>
49 #include <Graphic3d_CGraduatedTrihedron.hxx>
50 #include <Graphic3d_TypeOfComposition.hxx>
51 #include <Graphic3d_ExportFormat.hxx>
52 #include <Graphic3d_SortType.hxx>
53 #include <Graphic3d_PtrFrameBuffer.hxx>
54 #include <Graphic3d_Array1OfVertex.hxx>
55 #include <Graphic3d_Array2OfVertex.hxx>
56 #include <Graphic3d_BufferType.hxx>
57 #include <NCollection_DataMap.hxx>
58
59 class TColStd_Array1OfInteger;
60 class TColStd_Array1OfReal;
61 class TColStd_Array2OfReal;
62 class TColStd_HArray1OfByte;
63 class Graphic3d_Vector;
64 class Quantity_Color;
65 class Graphic3d_Vertex;
66 class TCollection_ExtendedString;
67 class Image_PixMap;
68 class TColStd_HArray1OfReal;
69 class Handle(OpenGl_Workspace);
70 class OpenGl_Element;
71 class OpenGl_Structure;
72 class OpenGl_Text;
73
74 //! Tool class to implement consistent state counter
75 //! for objects inside the same driver instance.
76 class OpenGl_StateCounter
77 {
78 public:
79
80   OpenGl_StateCounter() : myCounter (0) { }
81
82   Standard_Size Increment() { return ++myCounter; }
83
84 private:
85
86   Standard_Size myCounter;
87 };
88
89 //! This class defines an OpenGl graphic driver
90 class OpenGl_GraphicDriver : public Graphic3d_GraphicDriver
91 {
92 public:
93
94   //! Constructor.
95   //! @param theDisp connection to display, required on Linux but optional on other systems
96   //! @param theToInitialize perform initialization of default OpenGL context on construction
97   Standard_EXPORT OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
98                                         const Standard_Boolean                  theToInitialize = Standard_True);
99
100   //! Destructor.
101   Standard_EXPORT virtual ~OpenGl_GraphicDriver();
102
103   //! Release default context.
104   Standard_EXPORT void ReleaseContext();
105
106   //! Perform initialization of default OpenGL context.
107   Standard_EXPORT Standard_Boolean InitContext();
108
109 #if defined(HAVE_EGL) || defined(__ANDROID__)
110   //! Initialize default OpenGL context using existing one.
111   //! @param theEglDisplay EGL connection to the Display
112   //! @param theEglContext EGL rendering context
113   //! @param theEglConfig  EGL configuration
114   Standard_EXPORT Standard_Boolean InitEglContext (Aspect_Display          theEglDisplay,
115                                                    Aspect_RenderingContext theEglContext,
116                                                    void*                   theEglConfig);
117 #endif
118
119   Standard_EXPORT Standard_Integer InquireLightLimit ();
120   Standard_EXPORT Standard_Integer InquireViewLimit ();
121
122 public: // Methods for graphical structures
123
124   Standard_EXPORT void DisplayStructure (const Graphic3d_CView&             theCView,
125                                          const Handle(Graphic3d_Structure)& theStructure,
126                                          const Standard_Integer             thePriority);
127   Standard_EXPORT void EraseStructure (const Graphic3d_CView&             theCView,
128                                        const Handle(Graphic3d_Structure)& theStructure);
129   Standard_EXPORT void RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure);
130   Standard_EXPORT Handle(Graphic3d_CStructure) Structure (const Handle(Graphic3d_StructureManager)& theManager);
131
132   Standard_EXPORT Standard_Boolean SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
133                                                                 const Standard_Boolean theDrawToFrontBuffer);
134   Standard_EXPORT void DisplayImmediateStructure (const Graphic3d_CView&             theCView,
135                                                   const Handle(Graphic3d_Structure)& theStructure);
136   Standard_EXPORT void EraseImmediateStructure (const Graphic3d_CView&      theCView,
137                                                 const Graphic3d_CStructure& theCStructure);
138
139 public:
140
141   Standard_EXPORT void ActivateView (const Graphic3d_CView& ACView);
142   Standard_EXPORT void AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
143   Standard_EXPORT void Background (const Graphic3d_CView& ACView);
144   Standard_EXPORT void GradientBackground (const Graphic3d_CView& ACView, const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod FillStyle);
145   Standard_EXPORT void BackgroundImage (const Standard_CString FileName, const Graphic3d_CView& ACView, const Aspect_FillMethod FillStyle);
146   Standard_EXPORT void SetBgImageStyle (const Graphic3d_CView& ACView, const Aspect_FillMethod FillStyle);
147   Standard_EXPORT void SetBgGradientStyle (const Graphic3d_CView& ACView, const Aspect_GradientFillMethod FillStyle);
148   Standard_EXPORT void ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait);
149   Standard_EXPORT void DeactivateView (const Graphic3d_CView& ACView);
150   Standard_EXPORT void DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
151   Standard_EXPORT void RatioWindow (const Graphic3d_CView& ACView);
152   Standard_EXPORT void Redraw (const Graphic3d_CView& theCView,
153                                const Aspect_CLayer2d& theCUnderLayer,
154                                const Aspect_CLayer2d& theCOverLayer,
155                                const Standard_Integer theX = 0,
156                                const Standard_Integer theY = 0,
157                                const Standard_Integer theWidth  = 0,
158                                const Standard_Integer theHeight = 0);
159   Standard_EXPORT void RedrawImmediate (const Graphic3d_CView& theCView,
160                                         const Aspect_CLayer2d& theCUnderLayer,
161                                         const Aspect_CLayer2d& theCOverLayer);
162   Standard_EXPORT void Invalidate (const Graphic3d_CView& theCView);
163   Standard_EXPORT void RemoveView (const Graphic3d_CView& ACView);
164   Standard_EXPORT void SetLight (const Graphic3d_CView& ACView);
165   Standard_EXPORT void SetClipPlanes (const Graphic3d_CView& theCView);
166   Standard_EXPORT void SetCamera (const Graphic3d_CView& theCView);
167   Standard_EXPORT void SetVisualisation (const Graphic3d_CView& ACView);
168   Standard_EXPORT Standard_Boolean View (Graphic3d_CView& ACView);
169   Standard_EXPORT void Environment (const Graphic3d_CView& ACView);
170   Standard_EXPORT void ZBufferTriedronSetup (const Quantity_NameOfColor XColor = Quantity_NOC_RED, const Quantity_NameOfColor YColor = Quantity_NOC_GREEN, const Quantity_NameOfColor ZColor = Quantity_NOC_BLUE1, const Standard_Real SizeRatio = 0.8, const Standard_Real AxisDiametr = 0.05, const Standard_Integer NbFacettes = 12);
171   Standard_EXPORT void TriedronDisplay (const Graphic3d_CView& ACView, const Aspect_TypeOfTriedronPosition APosition = Aspect_TOTP_CENTER, const Quantity_NameOfColor AColor = Quantity_NOC_WHITE, const Standard_Real AScale = 0.02, const Standard_Boolean AsWireframe = Standard_True);
172   Standard_EXPORT void TriedronErase (const Graphic3d_CView& ACView);
173   Standard_EXPORT void TriedronEcho (const Graphic3d_CView& ACView, const Aspect_TypeOfTriedronEcho AType = Aspect_TOTE_NONE);
174   Standard_EXPORT void GraduatedTrihedronDisplay (const Graphic3d_CView& view, const Graphic3d_CGraduatedTrihedron& cubic);
175   Standard_EXPORT void GraduatedTrihedronErase (const Graphic3d_CView& view);
176   Standard_EXPORT void GraduatedTrihedronMinMaxValues (const Standard_ShortReal xmin, const Standard_ShortReal ymin, const Standard_ShortReal zmin, const Standard_ShortReal xmax, const Standard_ShortReal ymax, const Standard_ShortReal zmax);
177   Standard_EXPORT void Layer (Aspect_CLayer2d& ACLayer);
178   Standard_EXPORT void RemoveLayer (const Aspect_CLayer2d& ACLayer);
179   Standard_EXPORT void BeginLayer (const Aspect_CLayer2d& ACLayer);
180   Standard_EXPORT void BeginPolygon2d ();
181   Standard_EXPORT void BeginPolyline2d ();
182   Standard_EXPORT void ClearLayer (const Aspect_CLayer2d& ACLayer);
183   Standard_EXPORT void Draw (const Standard_ShortReal X,const Standard_ShortReal Y);
184   Standard_EXPORT void Edge (const Standard_ShortReal X,const Standard_ShortReal Y);
185   Standard_EXPORT void EndLayer ();
186   Standard_EXPORT void EndPolygon2d ();
187   Standard_EXPORT void EndPolyline2d ();
188   Standard_EXPORT void Move (const Standard_ShortReal X,const Standard_ShortReal Y);
189   Standard_EXPORT void Rectangle (const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal Width,const Standard_ShortReal Height);
190   Standard_EXPORT void SetColor (const Standard_ShortReal R,const Standard_ShortReal G,const Standard_ShortReal B);
191   Standard_EXPORT void SetTransparency (const Standard_ShortReal ATransparency);
192   Standard_EXPORT void UnsetTransparency ();
193   Standard_EXPORT void SetLineAttributes (const Standard_Integer Type,const Standard_ShortReal Width);
194
195   //! Set text attributes for under-/overlayer.
196   //! @param theFontName the name of the font to be used
197   //! @param theType     the display type of the text
198   //! theR theG theB values define the color of decal or subtitle background
199   //! To set the color of the text you can use the SetColor method.
200   Standard_EXPORT void SetTextAttributes (const Standard_CString   theFontName,
201                                           const Standard_Integer   theType,
202                                           const Standard_ShortReal theR,
203                                           const Standard_ShortReal theG,
204                                           const Standard_ShortReal theB);
205   Standard_EXPORT void Text (const Standard_CString AText,const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal AHeight);
206   Standard_EXPORT void TextSize (const Standard_CString AText,const Standard_ShortReal AHeight,Standard_ShortReal& AWidth,Standard_ShortReal& AnAscent,Standard_ShortReal& ADescent) const;
207   Standard_EXPORT void SetBackFacingModel (const Graphic3d_CView& aView);
208
209   //! Print the contents of all layers of the view to the printer.
210   //! @param thePrinterDC        pass the PrinterDeviceContext (HDC)
211   //! @param theToShowBackground when set to FALSE then print the view without background color (background is white) else set to TRUE for printing with current background color
212   //! @param theFileName         if != NULL, then the view will be printed to a file
213   //! @param thePrintAlgorithm   select print algorithm: stretch, tile
214   //! @param theScaleFactor      scaling coefficient, used internally to scale the printings accordingly to the scale factor selected in the printer properties dialog
215   //! @return Standard_True if the data is passed to the printer, otherwise Standard_False if the print operation failed due to the printer errors,
216   //! or lack of system memory. This might be related to insufficient memory or some internal errors.
217   //! All this errors are indicated by the message boxes (on level of OpenGl_GraphicDriver).
218   //! Warning: This function can reuse FBO assigned to the view, please take it into account if you use it for your purposes.
219   Standard_EXPORT Standard_Boolean Print (const Graphic3d_CView& theCView,
220                                           const Aspect_CLayer2d& theCUnderLayer,
221                                           const Aspect_CLayer2d& theCOverLayer,
222                                           const Aspect_Handle    thePrinterDC,
223                                           const Standard_Boolean theToShowBackground,
224                                           const Standard_CString theFileName,
225                                           const Aspect_PrintAlgo thePrintAlgorithm = Aspect_PA_STRETCH,
226                                           const Standard_Real    theScaleFactor = 1.0) const;
227   Standard_EXPORT void SetDepthTestEnabled (const Graphic3d_CView& view,const Standard_Boolean isEnabled) const;
228   Standard_EXPORT Standard_Boolean IsDepthTestEnabled (const Graphic3d_CView& view) const;
229
230   //! Reads depths of shown pixels of the given rectangle (glReadPixels with GL_DEPTH_COMPONENT)
231   Standard_EXPORT void ReadDepths (const Graphic3d_CView& view,const Standard_Integer x,const Standard_Integer y,const Standard_Integer width,const Standard_Integer height,const Standard_Address buffer) const;
232
233   //! Generate offscreen FBO (needs OpenGL2+ hardware).
234   //! If not supported on hardware returns NULL.
235   Standard_EXPORT Graphic3d_PtrFrameBuffer FBOCreate (const Graphic3d_CView& view, const Standard_Integer width, const Standard_Integer height);
236
237   //! Remove offscreen FBO
238   Standard_EXPORT void FBORelease (const Graphic3d_CView& view, Graphic3d_PtrFrameBuffer& fboPtr);
239
240   //! Dump active rendering buffer into specified memory buffer.
241   Standard_EXPORT Standard_Boolean BufferDump (const Graphic3d_CView&      theCView,
242                                                Image_PixMap&               theImage,
243                                                const Graphic3d_BufferType& theBufferType);
244
245   Standard_EXPORT void SetGLLightEnabled (const Graphic3d_CView& view,const Standard_Boolean isEnabled) const;
246
247   Standard_EXPORT Standard_Boolean IsGLLightEnabled (const Graphic3d_CView& view) const;
248
249   Standard_EXPORT Standard_Integer InquirePlaneLimit();
250
251   Standard_EXPORT Standard_ShortReal DefaultTextHeight() const;
252
253   Standard_EXPORT void FBOGetDimensions(const Graphic3d_CView& view,const Graphic3d_PtrFrameBuffer fboPtr,Standard_Integer& width,Standard_Integer& height,Standard_Integer& widthMax,Standard_Integer& heightMax);
254
255   Standard_EXPORT void FBOChangeViewport(const Graphic3d_CView& view,Graphic3d_PtrFrameBuffer& fboPtr,const Standard_Integer width,const Standard_Integer height);
256
257   Standard_EXPORT Standard_Boolean Export(const Standard_CString theFileName,const Graphic3d_ExportFormat theFormat,const Graphic3d_SortType theSortType,const Standard_Integer theWidth,const Standard_Integer theHeight,const Graphic3d_CView& theView,const Aspect_CLayer2d& theLayerUnder,const Aspect_CLayer2d& theLayerOver,const Standard_Real thePrecision = 0.005,const Standard_Address theProgressBarFunc = NULL,const Standard_Address theProgressObject = NULL);
258
259   //! Add a new top-level z layer with ID theLayerId for the view. Z layers allow drawing structures in higher layers in foreground of structures in lower layers.
260   //! To add a structure to desired layer on display it is necessary to set the layer index for the structure.
261   Standard_EXPORT void AddZLayer (const Graphic3d_CView&   theCView,
262                                   const Graphic3d_ZLayerId theLayerId);
263
264   //! Remove Z layer from the specified view.
265   //! All structures displayed at the moment in layer will be displayed in default layer (the bottom-level z layer).
266   //! To unset layer index from associated structures use method UnsetZLayer (...).
267   Standard_EXPORT void RemoveZLayer (const Graphic3d_CView&   theCView,
268                                      const Graphic3d_ZLayerId theLayerId);
269
270   //! Unset Z layer ID for all structures.
271   //! The structure indexes will be set to default layer (the bottom-level z layer with ID = 0).
272   Standard_EXPORT void UnsetZLayer (const Graphic3d_ZLayerId theLayerId);
273
274   //! Change Z layer of a structure already presented in view.
275   //! It is recommended to update z layer of already displayed structures with this method before setting new z layer index to the structure.
276   //! This is usually done by viewer manager.
277   Standard_EXPORT void ChangeZLayer (const Graphic3d_CStructure& theCStructure,
278                                      const Graphic3d_CView&      theCView,
279                                      const Graphic3d_ZLayerId    theNewLayerId);
280
281   //! Sets the settings for a single Z layer of specified view.
282   Standard_EXPORT void SetZLayerSettings (const Graphic3d_CView&          theCView,
283                                           const Graphic3d_ZLayerId        theLayerId,
284                                           const Graphic3d_ZLayerSettings& theSettings);
285
286   //! Changes priority of a structure within its Z layer for the specified view.
287   Standard_EXPORT void ChangePriority (const Graphic3d_CStructure& theCStructure,
288                                        const Graphic3d_CView& theCView,
289                                        const Standard_Integer theNewPriority);
290
291 public:
292
293   //! @return the visualization options
294   inline const OpenGl_Caps& Options() const
295   {
296     return *myCaps.operator->();
297   }
298
299   //! @return the visualization options
300   inline OpenGl_Caps& ChangeOptions()
301   {
302     return *myCaps.operator->();
303   }
304
305   //! VBO usage can be forbidden by this method even if it is supported by GL driver.
306   //! Notice that disabling of VBO will cause rendering performance degradation.
307   //! Warning! This method should be called only before any primitives are displayed in GL scene!
308   Standard_EXPORT void EnableVBO (const Standard_Boolean theToTurnOn);
309
310   //! Returns information about GPU memory usage.
311   //! Please read OpenGl_Context::MemoryInfo() for more description.
312   Standard_EXPORT Standard_Boolean MemoryInfo (Standard_Size&           theFreeBytes,
313                                                TCollection_AsciiString& theInfo) const;
314
315   //! UserDraw function prototype
316   typedef OpenGl_Element* (*OpenGl_UserDrawCallback_t )(const CALL_DEF_USERDRAW* );
317
318   //! Method to setup UserDraw callback
319   Standard_EXPORT OpenGl_UserDrawCallback_t& UserDrawCallback();
320
321 public:
322
323   //! Method to retrieve valid GL context.
324   //! Could return NULL-handle if no window created by this driver.
325   Standard_EXPORT const Handle(OpenGl_Context)& GetSharedContext() const;
326
327   //! Marks BVH tree for given priority list as dirty and
328   //! marks primitive set for rebuild.
329   Standard_EXPORT void InvalidateBVHData (Graphic3d_CView& theCView, const Standard_Integer theLayerId);
330
331 #if defined(HAVE_EGL) || defined(__ANDROID__)
332   Aspect_Display          getRawGlDisplay() const { return myEglDisplay; }
333   Aspect_RenderingContext getRawGlContext() const { return myEglContext;  }
334   void*                   getRawGlConfig()  const { return myEglConfig; }
335 #endif
336
337 public:
338
339   DEFINE_STANDARD_RTTI(OpenGl_GraphicDriver)
340
341 private:
342
343   Standard_Boolean        myIsOwnContext; //!< indicates that shared context has been created within OpenGl_GraphicDriver
344 #if defined(HAVE_EGL) || defined(__ANDROID__)
345   Aspect_Display          myEglDisplay;   //!< EGL connection to the Display : EGLDisplay
346   Aspect_RenderingContext myEglContext;   //!< EGL rendering context         : EGLContext
347   void*                   myEglConfig;    //!< EGL configuration             : EGLConfig
348 #endif
349
350   Handle(OpenGl_Caps)                                             myCaps;
351   NCollection_Map<Handle(OpenGl_View)>                            myMapOfView;
352   NCollection_Map<Handle(OpenGl_Workspace)>                       myMapOfWS;
353   NCollection_DataMap<Standard_Integer, OpenGl_Structure*>        myMapOfStructure;
354   mutable Handle(OpenGl_PrinterContext)                           myPrintContext;
355   OpenGl_UserDrawCallback_t                                       myUserDrawCallback;
356   OpenGl_Text*                                                    myTempText;         //!< variable for compatibility (drawing text in layers)
357
358 public:
359
360   //! State counter for OpenGl structures.
361   OpenGl_StateCounter* GetStateCounter() const { return &myStateCounter; }
362
363   //! Returns unique ID for primitive arrays.
364   const Standard_Size GetNextPrimitiveArrayUID() const { return myUIDGenerator.Increment(); }
365
366 private:
367
368   mutable OpenGl_StateCounter myStateCounter; //!< State counter for OpenGl structures.
369   mutable OpenGl_StateCounter myUIDGenerator; //!< Unique ID counter for primitive arrays.
370
371 };
372
373 #endif //_OpenGl_GraphicDriver_HeaderFile