8afca9662299a5a159e17a584d194dfc2672a3ad
[occt.git] / src / OpenGl / OpenGl_View.hxx
1 // Created on: 2011-09-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_View_Header
17 #define _OpenGl_View_Header
18
19 #include <MMgt_TShared.hxx>
20 #include <Standard_DefineHandle.hxx>
21
22 #include <TColStd_Array2OfReal.hxx>
23 #include <NCollection_List.hxx>
24
25 #include <Quantity_NameOfColor.hxx>
26 #include <Aspect_FillMethod.hxx>
27 #include <Aspect_GradientFillMethod.hxx>
28 #include <Aspect_TypeOfTriedronPosition.hxx>
29 #include <Aspect_CLayer2d.hxx>
30
31 #include <InterfaceGraphic_Graphic3d.hxx>
32 #include <InterfaceGraphic_Visual3d.hxx>
33
34 #include <Graphic3d_CView.hxx>
35 #include <Graphic3d_CGraduatedTrihedron.hxx>
36 #include <Graphic3d_SequenceOfHClipPlane.hxx>
37 #include <Graphic3d_ZLayerSettings.hxx>
38 #include <Visual3d_TypeOfSurfaceDetail.hxx>
39 #include <Visual3d_TypeOfModel.hxx>
40
41 #include <OpenGl_AspectFace.hxx>
42 #include <OpenGl_BackgroundArray.hxx>
43 #include <OpenGl_BVHTreeSelector.hxx>
44 #include <OpenGl_LayerList.hxx>
45 #include <OpenGl_Light.hxx>
46 #include <OpenGl_LineAttributes.hxx>
47
48 #include <Handle_OpenGl_Context.hxx>
49 #include <Handle_OpenGl_GraphicDriver.hxx>
50 #include <Handle_OpenGl_Workspace.hxx>
51 #include <Handle_OpenGl_View.hxx>
52 #include <Handle_OpenGl_Texture.hxx>
53
54 struct OPENGL_ZCLIP
55 {
56   struct {
57     Standard_Boolean   IsOn;
58     Standard_ShortReal Limit; /* in the range [0., 1.] */
59   } Back;
60   struct {
61     Standard_Boolean   IsOn;
62     Standard_ShortReal Limit; /* in the range [0., 1.] */
63   } Front;
64 };
65
66 struct OPENGL_FOG
67 {
68   Standard_Boolean   IsOn;
69   Standard_ShortReal Front; /* in the range [0., 1.] */
70   Standard_ShortReal Back; /* in the range [0., 1.] */
71   TEL_COLOUR         Color;
72 };
73
74 struct OpenGl_Matrix;
75 class OpenGl_GraduatedTrihedron;
76 class OpenGl_Structure;
77 class OpenGl_Trihedron;
78 class Handle(OpenGl_PrinterContext);
79 class OpenGl_StateCounter;
80
81 class OpenGl_View : public MMgt_TShared
82 {
83  public:
84   OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext, OpenGl_StateCounter* theCounter);
85   virtual ~OpenGl_View ();
86
87   void ReleaseGlResources (const Handle(OpenGl_Context)& theCtx);
88
89   void SetTextureEnv (const Handle(OpenGl_Context)&       theCtx,
90                       const Handle(Graphic3d_TextureEnv)& theTexture);
91   void SetSurfaceDetail (const Visual3d_TypeOfSurfaceDetail AMode);
92   void SetBackfacing (const Standard_Integer AMode);
93   void SetLights (const CALL_DEF_VIEWCONTEXT &AContext);
94   void SetAntiAliasing (const Standard_Boolean AMode) { myAntiAliasing = AMode; }
95   void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane &thePlanes) { myClipPlanes = thePlanes; }
96   void SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext);
97
98   const Handle(Graphic3d_Camera)& Camera() { return myCamera; }
99   void SetCamera (const Handle(Graphic3d_Camera)& theCamera) { myCamera = theCamera; }
100
101   void SetClipLimit (const Graphic3d_CView& theCView);
102
103   void SetFog (const Graphic3d_CView& theCView, const Standard_Boolean theFlag);
104
105   void TriedronDisplay (const Handle(OpenGl_Context)&       theCtx,
106                         const Aspect_TypeOfTriedronPosition thePosition,
107                         const Quantity_NameOfColor          theColor,
108                         const Standard_Real                 theScale,
109                         const Standard_Boolean              theAsWireframe);
110   void TriedronErase (const Handle(OpenGl_Context)& theCtx);
111
112   void GraduatedTrihedronDisplay (const Handle(OpenGl_Context)&        theCtx,
113                                   const Graphic3d_CGraduatedTrihedron& theCubic);
114   void GraduatedTrihedronErase (const Handle(OpenGl_Context)& theCtx);
115
116   Standard_Real Height () const { return myCamera->ViewDimensions().X(); }
117   Standard_Real Width () const { return myCamera->ViewDimensions().Y(); }
118
119   Standard_Integer Backfacing () const { return myBackfacing; }
120
121   const TEL_TRANSFORM_PERSISTENCE * BeginTransformPersistence (const Handle(OpenGl_Context)& theCtx,
122                                                                const TEL_TRANSFORM_PERSISTENCE *theTransPers);
123   void EndTransformPersistence (const Handle(OpenGl_Context)& theCtx);
124
125   //! Add structure to display list with specified priority.
126   //! The structure will be added to associated with it z layer.
127   //! If the z layer is not presented in the view, the structure will
128   //! be displayed in default bottom-level z layer.
129   void DisplayStructure (const Handle(Graphic3d_Structure)& theStructure,
130                          const Standard_Integer             thePriority);
131
132   //! Erase structure from display list.
133   void EraseStructure (const Handle(Graphic3d_Structure)& theStructure);
134
135   //! Add structure to the list of immediate structures.
136   void DisplayImmediateStructure (const Handle(Graphic3d_Structure)& theStructure);
137
138   //! Erase structure from display list.
139   void EraseImmediateStructure (const OpenGl_Structure* theStructure);
140
141   //! Insert a new top-level z layer with ID <theLayerId>
142   void AddZLayer (const Graphic3d_ZLayerId theLayerId);
143
144   //! Remove a z layer with ID <theLayerId>
145   void RemoveZLayer (const Graphic3d_ZLayerId theLayerId);
146
147   //! Display structure in z layer with ID <theNewLayerId>
148   //! If the layer with ID <theNewLayerId> is not presented in the view,
149   //! the structure will be displayed in default bottom-level layer.
150   void ChangeZLayer (const OpenGl_Structure*  theStructure,
151                      const Graphic3d_ZLayerId theNewLayerId);
152
153   //! Sets the settings for a single Z layer of specified view.
154   void SetZLayerSettings (const Graphic3d_ZLayerId        theLayerId,
155                           const Graphic3d_ZLayerSettings& theSettings);
156
157   //! Changes the priority of a structure within its ZLayer
158   void ChangePriority (const OpenGl_Structure* theStructure,
159                        const Standard_Integer  theNewPriority);
160
161   void CreateBackgroundTexture (const Standard_CString AFileName,
162                                 const Aspect_FillMethod AFillStyle);
163
164   void SetBackgroundTextureStyle (const Aspect_FillMethod FillStyle);
165   void SetBackgroundGradient (const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod AType);
166   void SetBackgroundGradientType (const Aspect_GradientFillMethod AType);
167
168   void Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
169                const Handle(OpenGl_Workspace)&      theWorkspace,
170                const Graphic3d_CView&               theCView,
171                const Aspect_CLayer2d&               theCUnderLayer,
172                const Aspect_CLayer2d&               theCOverLayer,
173                const Standard_Boolean               theToDrawImmediate);
174
175
176   void DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace);
177
178   //! Returns list of OpenGL Z-layers.
179   const OpenGl_LayerList& LayerList() const { return myZLayers; }
180
181   //! Returns list of openGL light sources.
182   const OpenGl_ListOfLight& LightList() const { return myLights; }
183
184   //! Returns OpenGL environment map.
185   const Handle(OpenGl_Texture)& TextureEnv() const { return myTextureEnv; }
186
187   //! Returns visualization mode for objects in the view.
188   Visual3d_TypeOfSurfaceDetail SurfaceDetail() const { return mySurfaceDetail; }
189
190   //! Returns selector for BVH tree, providing a possibility to store information
191   //! about current view volume and to detect which objects are overlapping it.
192   OpenGl_BVHTreeSelector& BVHTreeSelector() { return myBVHSelector; }
193
194   //! Marks BVH tree for given priority list as dirty and
195   //! marks primitive set for rebuild.
196   void InvalidateBVHData (const Standard_Integer theLayerId);
197
198   //! Returns view-mapping and orientation matrices.
199   void GetMatrices (OpenGl_Mat4& theOrientation,
200                     OpenGl_Mat4& theViewMapping) const;
201
202   //! Returns list of immediate structures rendered on top of main presentation
203   const OpenGl_SequenceOfStructure& ImmediateStructures() const
204   {
205     return myImmediateList;
206   }
207
208   //! Returns true if there are immediate structures to display
209   bool HasImmediateStructures() const
210   {
211     return !myImmediateList.IsEmpty()
212          || myZLayers.NbImmediateStructures() != 0;
213   }
214
215   //! Returns modification state for ray-tracing.
216   Standard_Size ModificationState() const { return myModificationState; }
217
218 protected:
219
220   void RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
221                       const Standard_Boolean          theToDrawImmediate);
222   void RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
223                       const Handle(OpenGl_Workspace) &theWorkspace,
224                       const Graphic3d_CView&               theCView,
225                       const Aspect_CLayer2d&               theCLayer);
226   void RedrawTrihedron (const Handle(OpenGl_Workspace) &theWorkspace);
227
228   //! Redraw contents of model scene: clipping planes,
229   //! lights, structures. The peculiar properties of "scene" is that
230   //! it requires empty Z-Buffer and uses projection and orientation
231   //! matrices supplied by 3d view.
232   void RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
233                     const Handle(OpenGl_Workspace)&      theWorkspace,
234                     const Standard_Boolean               theToDrawImmediate);
235
236   Handle(OpenGl_LineAttributes) myLineAttribs;
237   Handle(OpenGl_Texture)        myTextureEnv;
238   Visual3d_TypeOfSurfaceDetail  mySurfaceDetail;
239   Standard_Integer              myBackfacing;
240
241   OPENGL_ZCLIP   myZClip;
242
243   Graphic3d_SequenceOfHClipPlane myClipPlanes;
244   
245   Handle(Graphic3d_Camera) myCamera;
246
247   OPENGL_FOG myFog;
248   OpenGl_Trihedron*          myTrihedron;
249   OpenGl_GraduatedTrihedron* myGraduatedTrihedron;
250
251   //View_LABViewContext
252   int myVisualization;
253   Visual3d_TypeOfModel       myShadingModel;  //!< lighting shading model
254
255   //View_LABLight
256   OpenGl_ListOfLight myLights;
257
258   //View_LABPlane
259   //View_LABAliasing
260   Standard_Boolean myAntiAliasing;
261
262   //View_LABDepthCueing - fixed index used
263
264   OpenGl_LayerList           myZLayers;       //!< main list of displayed structure, sorted by layers
265   OpenGl_SequenceOfStructure myImmediateList; //!< list of immediate structures rendered on top of main presentation
266
267   const TEL_TRANSFORM_PERSISTENCE *myTransPers;
268   Standard_Boolean myIsTransPers;
269
270   //! Modification state
271   Standard_Size myProjectionState;
272   Standard_Size myModelViewState;
273   OpenGl_StateCounter* myStateCounter;
274
275   Standard_Size myCurrLightSourceState;
276
277   typedef std::pair<Standard_Size, Standard_Size> StateInfo;
278
279   StateInfo myLastOrientationState;
280   StateInfo myLastViewMappingState;
281   StateInfo myLastLightSourceState;
282
283   //! Is needed for selection of overlapping objects and storage of the current view volume
284   OpenGl_BVHTreeSelector myBVHSelector;
285
286   Standard_Size myModificationState;
287
288 protected: //! @name Background parameters
289
290   OpenGl_AspectFace*      myTextureParams;   //!< Stores texture and its parameters for textured background
291   OpenGl_BackgroundArray* myBgGradientArray; //!< Primitive array for gradient background
292   OpenGl_BackgroundArray* myBgTextureArray;  //!< Primitive array for texture  background
293
294 public:
295
296   DEFINE_STANDARD_ALLOC
297   DEFINE_STANDARD_RTTI(OpenGl_View) // Type definition
298
299   friend class OpenGl_Workspace;
300
301 };
302
303 #endif // _OpenGl_View_Header