a88fdaf4c0b240ee09ac2e5b180e780e158e50ba
[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 <Visual3d_TypeOfSurfaceDetail.hxx>
38
39 #include <OpenGl_LayerList.hxx>
40 #include <OpenGl_Light.hxx>
41
42 #include <Handle_OpenGl_Context.hxx>
43 #include <Handle_OpenGl_GraphicDriver.hxx>
44 #include <Handle_OpenGl_Display.hxx>
45 #include <Handle_OpenGl_Workspace.hxx>
46 #include <Handle_OpenGl_View.hxx>
47 #include <Handle_OpenGl_Texture.hxx>
48
49 struct OPENGL_BG_TEXTURE
50 {
51   Tuint             TexId;
52   Tint              Width;
53   Tint              Height;
54   Aspect_FillMethod Style;
55 };
56
57 struct OPENGL_BG_GRADIENT
58 {
59   TEL_COLOUR color1;
60   TEL_COLOUR color2;
61   Aspect_GradientFillMethod type;
62 };
63
64 struct OPENGL_ZCLIP
65 {
66   struct {
67     Standard_Boolean   IsOn;
68     Standard_ShortReal Limit; /* in the range [0., 1.] */
69   } Back;
70   struct {
71     Standard_Boolean   IsOn;
72     Standard_ShortReal Limit; /* in the range [0., 1.] */
73   } Front;
74 };
75
76 struct OPENGL_FOG
77 {
78   Standard_Boolean   IsOn;
79   Standard_ShortReal Front; /* in the range [0., 1.] */
80   Standard_ShortReal Back; /* in the range [0., 1.] */
81   TEL_COLOUR         Color;
82 };
83
84 struct OpenGl_Matrix;
85 class OpenGl_GraduatedTrihedron;
86 class OpenGl_Structure;
87 class OpenGl_Trihedron;
88 class Handle(OpenGl_PrinterContext);
89 class OpenGl_StateCounter;
90
91 class OpenGl_View : public MMgt_TShared
92 {
93  public:
94   OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext, OpenGl_StateCounter* theCounter);
95   virtual ~OpenGl_View ();
96
97   void ReleaseGlResources (const Handle(OpenGl_Context)& theCtx);
98
99   void SetTextureEnv (const Handle(OpenGl_Context)&       theCtx,
100                       const Handle(Graphic3d_TextureEnv)& theTexture);
101   void SetSurfaceDetail (const Visual3d_TypeOfSurfaceDetail AMode);
102   void SetBackfacing (const Standard_Integer AMode);
103   void SetLights (const CALL_DEF_VIEWCONTEXT &AContext);
104   void SetAntiAliasing (const Standard_Boolean AMode) { myAntiAliasing = AMode; }
105   void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane &thePlanes) { myClipPlanes = thePlanes; }
106   void SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext);
107
108   void SetCamera (const Handle(Graphic3d_Camera)& theCamera) { myCamera = theCamera; }
109
110   void SetClipLimit (const Graphic3d_CView& theCView);
111
112   void SetFog (const Graphic3d_CView& theCView, const Standard_Boolean theFlag);
113
114   void TriedronDisplay (const Handle(OpenGl_Context)&       theCtx,
115                         const Aspect_TypeOfTriedronPosition thePosition,
116                         const Quantity_NameOfColor          theColor,
117                         const Standard_Real                 theScale,
118                         const Standard_Boolean              theAsWireframe);
119   void TriedronErase (const Handle(OpenGl_Context)& theCtx);
120
121   void GraduatedTrihedronDisplay (const Handle(OpenGl_Context)&        theCtx,
122                                   const Graphic3d_CGraduatedTrihedron& theCubic);
123   void GraduatedTrihedronErase (const Handle(OpenGl_Context)& theCtx);
124
125   Standard_Real Height () const { return myCamera->ViewDimensions().X(); }
126   Standard_Real Width () const { return myCamera->ViewDimensions().Y(); }
127
128   Standard_Integer Backfacing () const { return myBackfacing; }
129
130   const TEL_TRANSFORM_PERSISTENCE * BeginTransformPersistence (const Handle(OpenGl_Context)& theCtx,
131                                                                const TEL_TRANSFORM_PERSISTENCE *theTransPers);
132   void EndTransformPersistence (const Handle(OpenGl_Context)& theCtx);
133
134   //! Add structure to display list with specified priority.
135   //! The structure will be added to associated with it z layer.
136   //! If the z layer is not presented in the view, the structure will
137   //! be displayed in default bottom-level z layer.
138   void DisplayStructure (const OpenGl_Structure* theStructure,
139                          const Standard_Integer  thePriority);
140
141   //! Erase structure from display list.
142   void EraseStructure (const OpenGl_Structure* theStructure);
143
144   //! Add structure to the list of immediate structures.
145   void DisplayImmediateStructure (const OpenGl_Structure* theStructure);
146
147   //! Erase structure from display list.
148   void EraseImmediateStructure (const OpenGl_Structure* theStructure);
149
150   //! Insert a new top-level z layer with ID <theLayerId>
151   void AddZLayer (const Standard_Integer theLayerId);
152
153   //! Remove a z layer with ID <theLayerId>
154   void RemoveZLayer (const Standard_Integer theLayerId);
155
156   //! Display structure in z layer with ID <theNewLayerId>
157   //! If the layer with ID <theNewLayerId> is not presented in the view,
158   //! the structure will be displayed in default bottom-level layer.
159   void ChangeZLayer (const OpenGl_Structure *theStructure,
160                      const Standard_Integer  theNewLayerId);
161
162   void CreateBackgroundTexture (const Standard_CString AFileName, const Aspect_FillMethod AFillStyle);
163   void SetBackgroundTextureStyle (const Aspect_FillMethod FillStyle);
164   void SetBackgroundGradient (const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod AType);
165   void SetBackgroundGradientType (const Aspect_GradientFillMethod AType);
166
167   void Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
168                const Handle(OpenGl_Workspace)&      theWorkspace,
169                const Graphic3d_CView&               theCView,
170                const Aspect_CLayer2d&               theCUnderLayer,
171                const Aspect_CLayer2d&               theCOverLayer);
172
173
174   void DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace);
175
176   //! Returns list of OpenGL Z-layers.
177   const OpenGl_LayerList& LayerList() const { return myZLayers; }
178
179   //! Returns list of openGL light sources.
180   const OpenGl_ListOfLight& LightList() const { return myLights; }
181
182   //! Returns OpenGL environment map.
183   const Handle(OpenGl_Texture)& TextureEnv() const { return myTextureEnv; }
184
185   //! Returns visualization mode for objects in the view.
186   Visual3d_TypeOfSurfaceDetail SurfaceDetail() const { return mySurfaceDetail; }
187
188   void GetMatrices (TColStd_Array2OfReal&  theMatOrient,
189                     TColStd_Array2OfReal&  theMatMapping) const;
190
191   //! Returns list of immediate structures rendered on top of main presentation
192   const OpenGl_SequenceOfStructure& ImmediateStructures() const
193   {
194     return myImmediateList;
195   }
196
197 #ifdef HAVE_OPENCL
198   //! Returns modification state for ray-tracing.
199   Standard_Size ModificationState() const { return myModificationState; }
200 #endif
201
202 protected:
203
204   void RenderStructs (const Handle(OpenGl_Workspace) &AWorkspace);
205   void RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
206                       const Graphic3d_CView&               theCView,
207                       const Aspect_CLayer2d&               theCLayer);
208
209   //! Redraw contents of model scene: clipping planes,
210   //! lights, structures. The peculiar properties of "scene" is that
211   //! it requires empty Z-Buffer and uses projection and orientation
212   //! matrices supplied by 3d view.
213   //! @param thePrintCtx [in] printer context which facilitates tiled printing.
214   //! @param theWorkspace [in] rendering workspace.
215   //! @param theCView [in] view data.
216   //! @param theProjection [in] view projection matrix.
217   //! @param theOrientation [in] view orientation matrix.
218   void RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
219                     const Handle(OpenGl_Workspace)& theWorkspace,
220                     const OpenGl_Matrix* theProjection,
221                     const OpenGl_Matrix* theOrientation);
222
223   Handle(OpenGl_Texture) myTextureEnv;
224   Visual3d_TypeOfSurfaceDetail mySurfaceDetail; //WSSurfaceDetail
225   Standard_Integer myBackfacing; //WSBackfacing
226
227   OPENGL_BG_TEXTURE myBgTexture; //WSBgTexture
228   OPENGL_BG_GRADIENT myBgGradient; //WSBgGradient
229
230   OPENGL_ZCLIP   myZClip;
231
232   Graphic3d_SequenceOfHClipPlane myClipPlanes;
233   
234   Handle(Graphic3d_Camera) myCamera;
235
236   OPENGL_FOG myFog;
237   OpenGl_Trihedron*          myTrihedron;
238   OpenGl_GraduatedTrihedron* myGraduatedTrihedron;
239
240   //View_LABViewContext
241   int myVisualization;
242   int myIntShadingMethod;
243
244   //View_LABLight
245   OpenGl_ListOfLight myLights;
246
247   //View_LABPlane
248   //View_LABAliasing
249   Standard_Boolean myAntiAliasing;
250
251   //View_LABDepthCueing - fixed index used
252
253   OpenGl_LayerList           myZLayers;       //!< main list of displayed structure, sorted by layers
254   OpenGl_SequenceOfStructure myImmediateList; //!< list of immediate structures rendered on top of main presentation
255
256   const TEL_TRANSFORM_PERSISTENCE *myTransPers;
257   Standard_Boolean myIsTransPers;
258
259   //! Modification state
260   Standard_Size myProjectionState;
261   Standard_Size myModelViewState;
262   OpenGl_StateCounter* myStateCounter;
263
264   Standard_Size myCurrLightSourceState;
265
266   typedef std::pair<Standard_Size, Standard_Size> StateInfo;
267
268   StateInfo myLastOrientationState;
269   StateInfo myLastViewMappingState;
270   StateInfo myLastLightSourceState;
271
272 #ifdef HAVE_OPENCL
273   Standard_Size myModificationState;
274 #endif
275
276 public:
277
278   DEFINE_STANDARD_ALLOC
279   DEFINE_STANDARD_RTTI(OpenGl_View) // Type definition
280
281 };
282
283 #endif // _OpenGl_View_Header