1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_GraphicDriver_HeaderFile
17 #define _OpenGl_GraphicDriver_HeaderFile
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <OpenGl_Context.hxx>
21 #include <OpenGl_PrinterContext.hxx>
23 #include <Standard_CString.hxx>
25 #include <TColStd_HArray1OfByte.hxx>
26 #include <TColStd_HArray1OfReal.hxx>
27 #include <Quantity_PlaneAngle.hxx>
28 #include <Quantity_NameOfColor.hxx>
30 #include <Aspect_Display.hxx>
31 #include <Aspect_DisplayConnection.hxx>
32 #include <Aspect_GradientFillMethod.hxx>
33 #include <Aspect_FillMethod.hxx>
34 #include <Aspect_CLayer2d.hxx>
35 #include <Aspect_TypeOfTriedronPosition.hxx>
36 #include <Aspect_TypeOfTriedronEcho.hxx>
37 #include <Aspect_Handle.hxx>
38 #include <Aspect_PrintAlgo.hxx>
40 #include <Graphic3d_CView.hxx>
41 #include <Graphic3d_CStructure.hxx>
42 #include <Graphic3d_TextPath.hxx>
43 #include <Graphic3d_HorizontalTextAlignment.hxx>
44 #include <Graphic3d_VerticalTextAlignment.hxx>
45 #include <Graphic3d_CUserDraw.hxx>
46 #include <Graphic3d_GraduatedTrihedron.hxx>
47 #include <Graphic3d_TypeOfComposition.hxx>
48 #include <Graphic3d_ExportFormat.hxx>
49 #include <Graphic3d_SortType.hxx>
50 #include <Graphic3d_PtrFrameBuffer.hxx>
51 #include <Graphic3d_Array1OfVertex.hxx>
52 #include <Graphic3d_Array2OfVertex.hxx>
53 #include <Graphic3d_BufferType.hxx>
54 #include <NCollection_DataMap.hxx>
56 #include <TColStd_Array1OfInteger.hxx>
57 #include <TColStd_HArray1OfReal.hxx>
58 #include <TColStd_Array2OfReal.hxx>
59 #include <TColStd_HArray1OfByte.hxx>
61 class Graphic3d_Vector;
63 class Graphic3d_Vertex;
64 class TCollection_ExtendedString;
67 class OpenGl_Structure;
71 //! Tool class to implement consistent state counter
72 //! for objects inside the same driver instance.
73 class OpenGl_StateCounter
77 OpenGl_StateCounter() : myCounter (0) { }
79 Standard_Size Increment() { return ++myCounter; }
83 Standard_Size myCounter;
86 class OpenGl_GraphicDriver;
87 DEFINE_STANDARD_HANDLE(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
89 //! This class defines an OpenGl graphic driver
90 class OpenGl_GraphicDriver : public Graphic3d_GraphicDriver
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);
101 Standard_EXPORT virtual ~OpenGl_GraphicDriver();
103 //! Release default context.
104 Standard_EXPORT void ReleaseContext();
106 //! Perform initialization of default OpenGL context.
107 Standard_EXPORT Standard_Boolean InitContext();
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,
119 Standard_EXPORT Standard_Integer InquireLightLimit ();
120 Standard_EXPORT Standard_Integer InquireViewLimit ();
122 public: // Methods for graphical structures
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);
132 Standard_EXPORT Standard_Boolean SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
133 const Standard_Boolean theDrawToFrontBuffer);
137 Standard_EXPORT void ActivateView (const Graphic3d_CView& ACView);
138 Standard_EXPORT void AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
139 Standard_EXPORT void Background (const Graphic3d_CView& ACView);
140 Standard_EXPORT void GradientBackground (const Graphic3d_CView& ACView, const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod FillStyle);
141 Standard_EXPORT void BackgroundImage (const Standard_CString FileName, const Graphic3d_CView& ACView, const Aspect_FillMethod FillStyle);
142 Standard_EXPORT void SetBgImageStyle (const Graphic3d_CView& ACView, const Aspect_FillMethod FillStyle);
143 Standard_EXPORT void SetBgGradientStyle (const Graphic3d_CView& ACView, const Aspect_GradientFillMethod FillStyle);
144 Standard_EXPORT void ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait);
145 Standard_EXPORT void DeactivateView (const Graphic3d_CView& ACView);
146 Standard_EXPORT void DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
147 Standard_EXPORT void RatioWindow (const Graphic3d_CView& ACView);
148 Standard_EXPORT void Redraw (const Graphic3d_CView& theCView,
149 const Aspect_CLayer2d& theCUnderLayer,
150 const Aspect_CLayer2d& theCOverLayer,
151 const Standard_Integer theX = 0,
152 const Standard_Integer theY = 0,
153 const Standard_Integer theWidth = 0,
154 const Standard_Integer theHeight = 0);
155 Standard_EXPORT void RedrawImmediate (const Graphic3d_CView& theCView,
156 const Aspect_CLayer2d& theCUnderLayer,
157 const Aspect_CLayer2d& theCOverLayer);
158 Standard_EXPORT void Invalidate (const Graphic3d_CView& theCView);
159 Standard_EXPORT Standard_Boolean IsInvalidated (const Graphic3d_CView& theCView) const;
160 Standard_EXPORT void RemoveView (const Graphic3d_CView& ACView);
161 Standard_EXPORT void SetLight (const Graphic3d_CView& ACView);
162 Standard_EXPORT void SetClipPlanes (const Graphic3d_CView& theCView);
163 Standard_EXPORT void SetCamera (const Graphic3d_CView& theCView);
164 Standard_EXPORT void SetVisualisation (const Graphic3d_CView& ACView);
165 Standard_EXPORT Standard_Boolean View (Graphic3d_CView& ACView);
166 Standard_EXPORT void Environment (const Graphic3d_CView& ACView);
167 Standard_EXPORT void ZBufferTriedronSetup (const Graphic3d_CView& theCView,
168 const Quantity_NameOfColor XColor = Quantity_NOC_RED,
169 const Quantity_NameOfColor YColor = Quantity_NOC_GREEN,
170 const Quantity_NameOfColor ZColor = Quantity_NOC_BLUE1,
171 const Standard_Real SizeRatio = 0.8,
172 const Standard_Real AxisDiametr = 0.05,
173 const Standard_Integer NbFacettes = 12);
174 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);
175 Standard_EXPORT void TriedronErase (const Graphic3d_CView& ACView);
176 Standard_EXPORT void TriedronEcho (const Graphic3d_CView& ACView, const Aspect_TypeOfTriedronEcho AType = Aspect_TOTE_NONE);
178 //! Displays Graduated trihedron
179 //! @param theView [in] the graphic view
180 //! @param theCubic [in] Graduated Trihedon parameters.
181 //! @sa OpenGl_GraduatedTrihedron
182 //! @sa Graphic3d_GraduatedTrihedron
183 Standard_EXPORT void GraduatedTrihedronDisplay (const Graphic3d_CView& theView, const Graphic3d_GraduatedTrihedron& theCubic);
185 //! Erases Graduated Trihedron from the view.
186 //! theView [in] graphic view
187 //! @sa OpenGl_GraduatedTrihedron
188 Standard_EXPORT void GraduatedTrihedronErase (const Graphic3d_CView& theView);
190 //! Sets minimum and maximum points of scene bounding box for Graduated Trihedron
191 //! stored in graphic view object.
192 //! @param theView [in] current graphic view
193 //! @param theMin [in] the minimum point of scene.
194 //! @param theMax [in] the maximum point of scene.
195 //! @sa OpenGl_GraduatedTrihedron
196 Standard_EXPORT void GraduatedTrihedronMinMaxValues (const Graphic3d_CView& theView,
197 const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax);
199 Standard_EXPORT void Layer (Aspect_CLayer2d& ACLayer);
200 Standard_EXPORT void RemoveLayer (const Aspect_CLayer2d& ACLayer);
201 Standard_EXPORT void BeginLayer (const Aspect_CLayer2d& ACLayer);
202 Standard_EXPORT void BeginPolygon2d ();
203 Standard_EXPORT void BeginPolyline2d ();
204 Standard_EXPORT void ClearLayer (const Aspect_CLayer2d& ACLayer);
205 Standard_EXPORT void Draw (const Standard_ShortReal X,const Standard_ShortReal Y);
206 Standard_EXPORT void Edge (const Standard_ShortReal X,const Standard_ShortReal Y);
207 Standard_EXPORT void EndLayer ();
208 Standard_EXPORT void EndPolygon2d ();
209 Standard_EXPORT void EndPolyline2d ();
210 Standard_EXPORT void Move (const Standard_ShortReal X,const Standard_ShortReal Y);
211 Standard_EXPORT void Rectangle (const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal Width,const Standard_ShortReal Height);
212 Standard_EXPORT void SetColor (const Standard_ShortReal R,const Standard_ShortReal G,const Standard_ShortReal B);
213 Standard_EXPORT void SetTransparency (const Standard_ShortReal ATransparency);
214 Standard_EXPORT void UnsetTransparency ();
215 Standard_EXPORT void SetLineAttributes (const Standard_Integer Type,const Standard_ShortReal Width);
217 //! Set text attributes for under-/overlayer.
218 //! @param theFontName the name of the font to be used
219 //! @param theType the display type of the text
220 //! theR theG theB values define the color of decal or subtitle background
221 //! To set the color of the text you can use the SetColor method.
222 Standard_EXPORT void SetTextAttributes (const Standard_CString theFontName,
223 const Standard_Integer theType,
224 const Standard_ShortReal theR,
225 const Standard_ShortReal theG,
226 const Standard_ShortReal theB);
227 Standard_EXPORT void Text (const Standard_CString AText,const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal AHeight);
228 Standard_EXPORT void TextSize (const Standard_CString AText,const Standard_ShortReal AHeight,Standard_ShortReal& AWidth,Standard_ShortReal& AnAscent,Standard_ShortReal& ADescent) const;
229 Standard_EXPORT void SetBackFacingModel (const Graphic3d_CView& aView);
231 //! Print the contents of all layers of the view to the printer.
232 //! @param thePrinterDC pass the PrinterDeviceContext (HDC)
233 //! @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
234 //! @param theFileName if != NULL, then the view will be printed to a file
235 //! @param thePrintAlgorithm select print algorithm: stretch, tile
236 //! @param theScaleFactor scaling coefficient, used internally to scale the printings accordingly to the scale factor selected in the printer properties dialog
237 //! @return Standard_True if the data is passed to the printer, otherwise Standard_False if the print operation failed due to the printer errors,
238 //! or lack of system memory. This might be related to insufficient memory or some internal errors.
239 //! All this errors are indicated by the message boxes (on level of OpenGl_GraphicDriver).
240 //! Warning: This function can reuse FBO assigned to the view, please take it into account if you use it for your purposes.
241 Standard_EXPORT Standard_Boolean Print (const Graphic3d_CView& theCView,
242 const Aspect_CLayer2d& theCUnderLayer,
243 const Aspect_CLayer2d& theCOverLayer,
244 const Aspect_Handle thePrinterDC,
245 const Standard_Boolean theToShowBackground,
246 const Standard_CString theFileName,
247 const Aspect_PrintAlgo thePrintAlgorithm = Aspect_PA_STRETCH,
248 const Standard_Real theScaleFactor = 1.0) const;
250 //! Reads depths of shown pixels of the given rectangle (glReadPixels with GL_DEPTH_COMPONENT)
251 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;
253 //! Generate offscreen FBO (needs OpenGL2+ hardware).
254 //! If not supported on hardware returns NULL.
255 Standard_EXPORT Graphic3d_PtrFrameBuffer FBOCreate (const Graphic3d_CView& view, const Standard_Integer width, const Standard_Integer height);
257 //! Remove offscreen FBO
258 Standard_EXPORT void FBORelease (const Graphic3d_CView& view, Graphic3d_PtrFrameBuffer& fboPtr);
260 //! Dump active rendering buffer into specified memory buffer.
261 Standard_EXPORT Standard_Boolean BufferDump (const Graphic3d_CView& theCView,
262 Image_PixMap& theImage,
263 const Graphic3d_BufferType& theBufferType);
265 Standard_EXPORT void SetGLLightEnabled (const Graphic3d_CView& view,const Standard_Boolean isEnabled) const;
267 Standard_EXPORT Standard_Boolean IsGLLightEnabled (const Graphic3d_CView& view) const;
269 Standard_EXPORT Standard_Integer InquirePlaneLimit();
271 Standard_EXPORT Standard_ShortReal DefaultTextHeight() const;
273 Standard_EXPORT void FBOGetDimensions(const Graphic3d_CView& view,const Graphic3d_PtrFrameBuffer fboPtr,Standard_Integer& width,Standard_Integer& height,Standard_Integer& widthMax,Standard_Integer& heightMax);
275 Standard_EXPORT void FBOChangeViewport(const Graphic3d_CView& view,Graphic3d_PtrFrameBuffer& fboPtr,const Standard_Integer width,const Standard_Integer height);
277 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);
279 //! 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.
280 //! To add a structure to desired layer on display it is necessary to set the layer index for the structure.
281 Standard_EXPORT void AddZLayer (const Graphic3d_CView& theCView,
282 const Graphic3d_ZLayerId theLayerId);
284 //! Remove Z layer from the specified view.
285 //! All structures displayed at the moment in layer will be displayed in default layer (the bottom-level z layer).
286 //! To unset layer index from associated structures use method UnsetZLayer (...).
287 Standard_EXPORT void RemoveZLayer (const Graphic3d_CView& theCView,
288 const Graphic3d_ZLayerId theLayerId);
290 //! Unset Z layer ID for all structures.
291 //! The structure indexes will be set to default layer (the bottom-level z layer with ID = 0).
292 Standard_EXPORT void UnsetZLayer (const Graphic3d_ZLayerId theLayerId);
294 //! Change Z layer of a structure already presented in view.
295 //! It is recommended to update z layer of already displayed structures with this method before setting new z layer index to the structure.
296 //! This is usually done by viewer manager.
297 Standard_EXPORT void ChangeZLayer (const Graphic3d_CStructure& theCStructure,
298 const Graphic3d_CView& theCView,
299 const Graphic3d_ZLayerId theNewLayerId);
301 //! Sets the settings for a single Z layer of specified view.
302 Standard_EXPORT void SetZLayerSettings (const Graphic3d_CView& theCView,
303 const Graphic3d_ZLayerId theLayerId,
304 const Graphic3d_ZLayerSettings& theSettings);
306 //! Changes priority of a structure within its Z layer for the specified view.
307 Standard_EXPORT void ChangePriority (const Graphic3d_CStructure& theCStructure,
308 const Graphic3d_CView& theCView,
309 const Standard_Integer theNewPriority);
313 //! @return the visualization options
314 inline const OpenGl_Caps& Options() const
316 return *myCaps.operator->();
319 //! @return the visualization options
320 inline OpenGl_Caps& ChangeOptions()
322 return *myCaps.operator->();
325 //! Specify swap buffer behavior.
326 Standard_EXPORT void SetBuffersNoSwap (const Standard_Boolean theIsNoSwap);
328 //! VBO usage can be forbidden by this method even if it is supported by GL driver.
329 //! Notice that disabling of VBO will cause rendering performance degradation.
330 //! Warning! This method should be called only before any primitives are displayed in GL scene!
331 Standard_EXPORT void EnableVBO (const Standard_Boolean theToTurnOn);
333 //! Returns information about GPU memory usage.
334 //! Please read OpenGl_Context::MemoryInfo() for more description.
335 Standard_EXPORT Standard_Boolean MemoryInfo (Standard_Size& theFreeBytes,
336 TCollection_AsciiString& theInfo) const;
338 //! UserDraw function prototype
339 typedef OpenGl_Element* (*OpenGl_UserDrawCallback_t )(const CALL_DEF_USERDRAW* );
341 //! Method to setup UserDraw callback
342 Standard_EXPORT OpenGl_UserDrawCallback_t& UserDrawCallback();
346 //! Method to retrieve valid GL context.
347 //! Could return NULL-handle if no window created by this driver.
348 Standard_EXPORT const Handle(OpenGl_Context)& GetSharedContext() const;
350 //! Marks BVH tree for given priority list as dirty and
351 //! marks primitive set for rebuild.
352 Standard_EXPORT void InvalidateBVHData (Graphic3d_CView& theCView, const Standard_Integer theLayerId);
354 #if defined(HAVE_EGL) || defined(__ANDROID__)
355 Aspect_Display getRawGlDisplay() const { return myEglDisplay; }
356 Aspect_RenderingContext getRawGlContext() const { return myEglContext; }
357 void* getRawGlConfig() const { return myEglConfig; }
362 DEFINE_STANDARD_RTTI(OpenGl_GraphicDriver, Graphic3d_GraphicDriver)
366 Standard_Boolean myIsOwnContext; //!< indicates that shared context has been created within OpenGl_GraphicDriver
367 #if defined(HAVE_EGL) || defined(__ANDROID__)
368 Aspect_Display myEglDisplay; //!< EGL connection to the Display : EGLDisplay
369 Aspect_RenderingContext myEglContext; //!< EGL rendering context : EGLContext
370 void* myEglConfig; //!< EGL configuration : EGLConfig
373 Handle(OpenGl_Caps) myCaps;
374 NCollection_Map<Handle(OpenGl_View)> myMapOfView;
375 NCollection_Map<Handle(OpenGl_Workspace)> myMapOfWS;
376 NCollection_DataMap<Standard_Integer, OpenGl_Structure*> myMapOfStructure;
377 mutable Handle(OpenGl_PrinterContext) myPrintContext;
378 OpenGl_UserDrawCallback_t myUserDrawCallback;
379 OpenGl_Text* myTempText; //!< variable for compatibility (drawing text in layers)
383 //! State counter for OpenGl structures.
384 OpenGl_StateCounter* GetStateCounter() const { return &myStateCounter; }
386 //! Returns unique ID for primitive arrays.
387 const Standard_Size GetNextPrimitiveArrayUID() const { return myUIDGenerator.Increment(); }
391 mutable OpenGl_StateCounter myStateCounter; //!< State counter for OpenGl structures.
392 mutable OpenGl_StateCounter myUIDGenerator; //!< Unique ID counter for primitive arrays.
396 #endif //_OpenGl_GraphicDriver_HeaderFile