7e4071380212ca7214de81a7e225d8b800a5d51d
[occt.git] / src / OpenGl / OpenGl_Workspace.hxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 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_Workspace_Header
17 #define _OpenGl_Workspace_Header
18
19 #include <Graphic3d_BufferType.hxx>
20
21 #include <OpenGl_AspectFace.hxx>
22 #include <OpenGl_CappingAlgo.hxx>
23 #include <OpenGl_FrameBuffer.hxx>
24 #include <OpenGl_LineAttributes.hxx>
25 #include <OpenGl_Matrix.hxx>
26 #include <OpenGl_NamedStatus.hxx>
27 #include <OpenGl_PrinterContext.hxx>
28 #include <OpenGl_RenderFilter.hxx>
29 #include <OpenGl_ShaderObject.hxx>
30 #include <OpenGl_ShaderProgram.hxx>
31 #include <OpenGl_TextParam.hxx>
32 #include <OpenGl_TextureBufferArb.hxx>
33 #include <OpenGl_Vec.hxx>
34 #include <OpenGl_Window.hxx>
35
36 class OpenGl_View;
37 class Image_PixMap;
38
39 //! OpenGL material definition
40 struct OpenGl_Material
41 {
42
43   OpenGl_Vec4 Ambient;  //!< ambient reflection coefficient
44   OpenGl_Vec4 Diffuse;  //!< diffuse reflection coefficient
45   OpenGl_Vec4 Specular; //!< glossy  reflection coefficient
46   OpenGl_Vec4 Emission; //!< material emission
47   OpenGl_Vec4 Params;   //!< extra packed parameters
48
49   Standard_ShortReal  Shine()              const { return Params.x(); }
50   Standard_ShortReal& ChangeShine()              { return Params.x(); }
51
52   Standard_ShortReal  Transparency()       const { return Params.y(); }
53   Standard_ShortReal& ChangeTransparency()       { return Params.y(); }
54
55   //! Set material color.
56   void SetColor (const OpenGl_Vec4& theColor)
57   {
58     // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
59     Ambient.xyz() = theColor.rgb() * 0.25f;
60     Diffuse.xyz() = theColor.rgb();
61   }
62
63   //! Initialize material
64   void Init (const Graphic3d_MaterialAspect& theProp,
65              const Quantity_Color&           theInteriorColor);
66
67   //! Returns packed (serialized) representation of material properties
68   const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
69   static Standard_Integer NbOfVec4() { return 5; }
70
71 };
72
73 class OpenGl_RaytraceFilter;
74 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
75
76 //! Graphical ray-tracing filter.
77 //! Filters out all raytracable structures.
78 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
79 {
80 public:
81
82   //! Default constructor.
83   OpenGl_RaytraceFilter() {}
84
85   //! Returns the previously set filter.
86   const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
87   {
88     return myPrevRenderFilter;
89   }
90
91   //! Remembers the previously set filter.
92   void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
93   {
94     myPrevRenderFilter = theFilter;
95   }
96
97   //! Checks whether the element can be rendered or not.
98   //! @param theElement [in] the element to check.
99   //! @return True if element can be rendered.
100   virtual Standard_Boolean CanRender (const OpenGl_Element* theElement) Standard_OVERRIDE;
101
102 private:
103
104   Handle(OpenGl_RenderFilter) myPrevRenderFilter;
105
106 public:
107
108   DEFINE_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
109 };
110
111 class OpenGl_Workspace;
112 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
113
114 //! Rendering workspace.
115 //! Provides methods to render primitives and maintain GL state.
116 class OpenGl_Workspace : public Standard_Transient
117 {
118 public:
119
120   //! Constructor of rendering workspace.
121   Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
122
123   //! Destructor
124   Standard_EXPORT virtual ~OpenGl_Workspace();
125
126   //! Activate rendering context.
127   Standard_EXPORT Standard_Boolean Activate();
128
129   OpenGl_View* View() const { return myView; }
130
131   const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
132
133   Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
134
135   Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
136
137   Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
138                                Image_PixMap&                     theImage,
139                                const Graphic3d_BufferType&       theBufferType);
140
141   Standard_EXPORT Standard_Integer Width()  const;
142
143   Standard_EXPORT Standard_Integer Height() const;
144
145   //! Setup Z-buffer usage flag (without affecting GL state!).
146   //! Returns previously set flag.
147   Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
148   {
149     const Standard_Boolean wasUsed = myUseZBuffer;
150     myUseZBuffer = theToUse;
151     return wasUsed;
152   }
153
154   Handle(OpenGl_PrinterContext)& PrinterContext() { return myPrintContext; }
155
156   //! @return true if usage of Z buffer is enabled.
157   Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
158
159   //! @return true if depth writing is enabled.
160   Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
161
162   //! @return true if usage of GL light is enabled.
163   Standard_EXPORT Standard_Boolean  UseGLLight() const;
164
165   //! @return true if clipping algorithm enabled
166   Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
167
168   Standard_Integer NamedStatus;
169
170   //// RELATED TO STATUS ////
171
172   const OpenGl_Vec4* HighlightColor;
173
174   //! Return true if active group might activate face culling (e.g. primitives are closed).
175   bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
176
177   //! Allow or disallow face culling.
178   //! This call does NOT affect current state of back face culling;
179   //! ApplyAspectFace() should be called to update state.
180   void SetAllowFaceCulling (bool theToAllow) { myToAllowFaceCulling = theToAllow; }
181
182   //! Return true if following structures should apply highlight color.
183   bool ToHighlight() const { return myToHighlight; }
184
185   //! Set highlight.
186   void SetHighlight (bool theToHighlight) { myToHighlight = theToHighlight; }
187
188   //! Return line color taking into account highlight flag.
189   const OpenGl_Vec4& LineColor() const
190   {
191     return myToHighlight
192          ? *HighlightColor
193          : myAspectLineSet->Aspect()->ColorRGBA();
194   }
195
196   //! Return edge color taking into account highlight flag.
197   const OpenGl_Vec4& EdgeColor() const
198   {
199     return myToHighlight
200          ? *HighlightColor
201          : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
202   }
203
204   //! Return marker color taking into account highlight flag.
205   const OpenGl_Vec4& MarkerColor() const
206   {
207     return myToHighlight
208          ? *HighlightColor
209          : myAspectMarkerSet->Aspect()->ColorRGBA();
210   }
211
212   //! Return Interior color taking into account highlight flag.
213   const OpenGl_Vec4& InteriorColor() const
214   {
215     return myToHighlight
216          ? *HighlightColor
217          : myAspectFaceSet->Aspect()->InteriorColorRGBA();
218   }
219
220   //! Return text color taking into account highlight flag.
221   const OpenGl_Vec4& TextColor() const
222   {
223     return myToHighlight
224          ? *HighlightColor
225          : myAspectTextSet->Aspect()->ColorRGBA();
226   }
227
228   //! Return text Subtitle color taking into account highlight flag.
229   const OpenGl_Vec4& TextSubtitleColor() const
230   {
231     return myToHighlight
232          ? *HighlightColor
233          : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
234   }
235
236   //! Currently set line aspect (can differ from applied).
237   const OpenGl_AspectLine*   AspectLine()   const { return myAspectLineSet; }
238
239   //! Currently set face aspect (can differ from applied).
240   const OpenGl_AspectFace*   AspectFace()   const { return myAspectFaceSet; }
241
242   //! Currently set marker aspect (can differ from applied).
243   const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
244
245   //! Currently set text aspect (can differ from applied).
246   const OpenGl_AspectText*   AspectText()   const { return myAspectTextSet; }
247
248   //! Assign new line aspect (will be applied within ApplyAspectLine()).
249   Standard_EXPORT const OpenGl_AspectLine*   SetAspectLine   (const OpenGl_AspectLine*   theAspect);
250
251   //! Assign new face aspect (will be applied within ApplyAspectFace()).
252   Standard_EXPORT const OpenGl_AspectFace*   SetAspectFace   (const OpenGl_AspectFace*   theAspect);
253
254   //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
255   Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
256
257   //! Assign new text aspect (will be applied within ApplyAspectText()).
258   Standard_EXPORT const OpenGl_AspectText*   SetAspectText   (const OpenGl_AspectText*   theAspect);
259
260   //! Apply line aspect.
261   //! @return aspect set by SetAspectLine()
262   const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
263
264   //! Apply face aspect.
265   //! @return aspect set by SetAspectFace()
266   Standard_EXPORT const OpenGl_AspectFace*   ApplyAspectFace();
267
268   //! Apply marker aspect.
269   //! @return aspect set by SetAspectMarker()
270   Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
271
272   //! Apply text aspect.
273   //! @return aspect set by SetAspectText()
274   const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
275
276   //! Clear the applied aspect state to default values.
277   void ResetAppliedAspect();
278
279   Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
280   Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)&          theTexture,
281                                                         const Handle(Graphic3d_TextureParams)& theParams = NULL);
282   const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
283
284   //! Set filter for restricting rendering of particular elements.
285   //! Filter can be applied for rendering passes used by recursive
286   //! rendering algorithms for rendering elements of groups.
287   //! @param theFilter [in] the filter instance.
288   inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
289   {
290     myRenderFilter = theFilter;
291   }
292
293   //! Get rendering filter.
294   //! @return filter instance.
295   inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
296   {
297     return myRenderFilter;
298   }
299
300   //! @return applied view matrix.
301   inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
302
303   //! @return applied model structure matrix.
304   inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
305
306   //! Sets and applies current polygon offset.
307   void SetPolygonOffset (const Graphic3d_PolygonOffset& theParams);
308
309   //! Returns currently applied polygon offset params.
310   const Graphic3d_PolygonOffset& AppliedPolygonOffset() { return myPolygonOffsetApplied; }
311
312   //! Returns capping algorithm rendering filter.
313   const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
314   {
315     return myDefaultCappingAlgoFilter;
316   }
317
318   //! Returns face aspect for none culling mode.
319   const OpenGl_AspectFace& NoneCulling() const
320   {
321     return myNoneCulling;
322   }
323
324   //! Returns face aspect for front face culling mode.
325   const OpenGl_AspectFace& FrontCulling() const
326   {
327     return myFrontCulling;
328   }
329
330   //! Sets a new environment texture.
331   void SetEnvironmentTexture (const Handle(OpenGl_Texture)& theTexture)
332   {
333     myEnvironmentTexture = theTexture;
334   }
335
336   //! Returns environment texture.
337   const Handle(OpenGl_Texture)& EnvironmentTexture() const
338   {
339     return myEnvironmentTexture;
340   }
341
342 protected:
343
344   enum
345   {
346     TEL_FRONT_MATERIAL = 1,
347     TEL_BACK_MATERIAL  = 2
348   };
349
350   void updateMaterial (const int theFlag);
351
352   void setTextureParams (Handle(OpenGl_Texture)&                theTexture,
353                          const Handle(Graphic3d_TextureParams)& theParams);
354
355 protected: //! @name protected fields
356
357   OpenGl_View*                     myView;
358   Handle(OpenGl_Window)            myWindow;
359   Handle(OpenGl_Context)           myGlContext;
360   Handle(OpenGl_PrinterContext)    myPrintContext;
361   Handle(OpenGl_LineAttributes)    myLineAttribs;
362   Standard_Boolean                 myUseZBuffer;
363   Standard_Boolean                 myUseDepthWrite;
364   Standard_Boolean                 myUseGLLight;
365   Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
366   OpenGl_AspectFace                myNoneCulling;
367   OpenGl_AspectFace                myFrontCulling;
368
369 protected: //! @name fields related to status
370
371   Handle(OpenGl_RenderFilter) myRenderFilter;
372   Handle(OpenGl_Texture) myTextureBound;    //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
373   const OpenGl_AspectLine*   myAspectLineSet;
374   const OpenGl_AspectFace*   myAspectFaceSet;
375   Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
376   const OpenGl_AspectMarker* myAspectMarkerSet;
377   Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
378   const OpenGl_AspectText*   myAspectTextSet;
379   bool                       myAspectFaceAppliedWithHL;
380
381   const OpenGl_Matrix* ViewMatrix_applied;
382   const OpenGl_Matrix* StructureMatrix_applied;
383
384   OpenGl_Material myMatFront;    //!< current front material state (cached to reduce GL context updates)
385   OpenGl_Material myMatBack;     //!< current back  material state
386   OpenGl_Material myMatTmp;      //!< temporary variable
387   bool            myToAllowFaceCulling; //!< allow back face culling
388   bool            myToHighlight; //!< flag indicating highlighting mode
389
390   OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
391
392   Graphic3d_PolygonOffset myPolygonOffsetApplied; //!< currently applied polygon offset
393
394   OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
395
396   Handle(OpenGl_Texture) myEnvironmentTexture;
397
398 public: //! @name type definition
399
400   DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
401   DEFINE_STANDARD_ALLOC
402
403 };
404
405 #endif // _OpenGl_Workspace_Header