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