0029366: Visualization, OpenGl_Text - artifacts when using Aspect_TODT_SHADOW/Aspect_...
[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_Material.hxx>
25 #include <OpenGl_Matrix.hxx>
26 #include <OpenGl_RenderFilter.hxx>
27 #include <OpenGl_ShaderObject.hxx>
28 #include <OpenGl_ShaderProgram.hxx>
29 #include <OpenGl_TextParam.hxx>
30 #include <OpenGl_TextureBufferArb.hxx>
31 #include <OpenGl_Vec.hxx>
32 #include <OpenGl_Window.hxx>
33
34 class OpenGl_View;
35 class Image_PixMap;
36
37 class OpenGl_RaytraceFilter;
38 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
39
40 //! Graphical ray-tracing filter.
41 //! Filters out all raytracable structures.
42 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
43 {
44 public:
45
46   //! Default constructor.
47   OpenGl_RaytraceFilter() {}
48
49   //! Returns the previously set filter.
50   const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
51   {
52     return myPrevRenderFilter;
53   }
54
55   //! Remembers the previously set filter.
56   void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
57   {
58     myPrevRenderFilter = theFilter;
59   }
60
61   //! Checks whether the element can be rendered or not.
62   //! @param theElement [in] the element to check.
63   //! @return True if element can be rendered.
64   virtual Standard_Boolean ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
65                                          const OpenGl_Element*           theElement) Standard_OVERRIDE;
66
67 private:
68
69   Handle(OpenGl_RenderFilter) myPrevRenderFilter;
70
71 public:
72
73   DEFINE_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
74 };
75
76 class OpenGl_Workspace;
77 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
78
79 //! Rendering workspace.
80 //! Provides methods to render primitives and maintain GL state.
81 class OpenGl_Workspace : public Standard_Transient
82 {
83 public:
84
85   //! Constructor of rendering workspace.
86   Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
87
88   //! Destructor
89   virtual ~OpenGl_Workspace() {}
90
91   //! Activate rendering context.
92   Standard_EXPORT Standard_Boolean Activate();
93
94   OpenGl_View* View() const { return myView; }
95
96   const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
97
98   Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
99
100   Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
101
102   Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
103                                Image_PixMap&                     theImage,
104                                const Graphic3d_BufferType&       theBufferType);
105
106   Standard_EXPORT Standard_Integer Width()  const;
107
108   Standard_EXPORT Standard_Integer Height() const;
109
110   //! Setup Z-buffer usage flag (without affecting GL state!).
111   //! Returns previously set flag.
112   Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
113   {
114     const Standard_Boolean wasUsed = myUseZBuffer;
115     myUseZBuffer = theToUse;
116     return wasUsed;
117   }
118
119   //! @return true if usage of Z buffer is enabled.
120   Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
121
122   //! @return true if depth writing is enabled.
123   Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
124
125   //! @return true if clipping algorithm enabled
126   Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
127
128   //// RELATED TO STATUS ////
129
130   //! Return true if active group might activate face culling (e.g. primitives are closed).
131   bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
132
133   //! Allow or disallow face culling.
134   //! This call does NOT affect current state of back face culling;
135   //! ApplyAspectFace() should be called to update state.
136   void SetAllowFaceCulling (bool theToAllow) { myToAllowFaceCulling = theToAllow; }
137
138   //! Return true if following structures should apply highlight color.
139   bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
140
141   //! Return highlight style.
142   const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
143
144   //! Set highlight style.
145   void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) {  myHighlightStyle = theStyle; }
146
147   //! Return line color taking into account highlight flag.
148   const OpenGl_Vec4& LineColor() const
149   {
150     return !myHighlightStyle.IsNull()
151          ?  myHighlightStyle->ColorRGBA()
152          :  myAspectLineSet->Aspect()->ColorRGBA();
153   }
154
155   //! Return edge color taking into account highlight flag.
156   const OpenGl_Vec4& EdgeColor() const
157   {
158     return !myHighlightStyle.IsNull()
159          ?  myHighlightStyle->ColorRGBA()
160          :  myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
161   }
162
163   //! Return marker color taking into account highlight flag.
164   const OpenGl_Vec4& MarkerColor() const
165   {
166     return !myHighlightStyle.IsNull()
167          ?  myHighlightStyle->ColorRGBA()
168          :  myAspectMarkerSet->Aspect()->ColorRGBA();
169   }
170
171   //! Return Interior color taking into account highlight flag.
172   const OpenGl_Vec4& InteriorColor() const
173   {
174     return !myHighlightStyle.IsNull()
175          ?  myHighlightStyle->ColorRGBA()
176          :  myAspectFaceSet->Aspect()->InteriorColorRGBA();
177   }
178
179   //! Return text color taking into account highlight flag.
180   const OpenGl_Vec4& TextColor() const
181   {
182     return !myHighlightStyle.IsNull()
183          ?  myHighlightStyle->ColorRGBA()
184          :  myAspectTextSet->Aspect()->ColorRGBA();
185   }
186
187   //! Return text Subtitle color taking into account highlight flag.
188   const OpenGl_Vec4& TextSubtitleColor() const
189   {
190     return !myHighlightStyle.IsNull()
191          ?  myHighlightStyle->ColorRGBA()
192          :  myAspectTextSet->Aspect()->ColorSubTitleRGBA();
193   }
194
195   //! Currently set line aspect (can differ from applied).
196   const OpenGl_AspectLine*   AspectLine()   const { return myAspectLineSet; }
197
198   //! Currently set face aspect (can differ from applied).
199   const OpenGl_AspectFace*   AspectFace()   const { return myAspectFaceSet; }
200
201   //! Currently set marker aspect (can differ from applied).
202   const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
203
204   //! Currently set text aspect (can differ from applied).
205   const OpenGl_AspectText*   AspectText()   const { return myAspectTextSet; }
206
207   //! Assign new line aspect (will be applied within ApplyAspectLine()).
208   Standard_EXPORT const OpenGl_AspectLine*   SetAspectLine   (const OpenGl_AspectLine*   theAspect);
209
210   //! Assign new face aspect (will be applied within ApplyAspectFace()).
211   Standard_EXPORT const OpenGl_AspectFace*   SetAspectFace   (const OpenGl_AspectFace*   theAspect);
212
213   //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
214   Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
215
216   //! Assign new text aspect (will be applied within ApplyAspectText()).
217   Standard_EXPORT const OpenGl_AspectText*   SetAspectText   (const OpenGl_AspectText*   theAspect);
218
219   //! Apply line aspect.
220   //! @return aspect set by SetAspectLine()
221   const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
222
223   //! Apply face aspect.
224   //! @return aspect set by SetAspectFace()
225   Standard_EXPORT const OpenGl_AspectFace*   ApplyAspectFace();
226
227   //! Apply marker aspect.
228   //! @return aspect set by SetAspectMarker()
229   Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
230
231   //! Apply text aspect.
232   //! @return aspect set by SetAspectText()
233   const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
234
235   //! Clear the applied aspect state to default values.
236   void ResetAppliedAspect();
237
238   //! Set filter for restricting rendering of particular elements.
239   //! Filter can be applied for rendering passes used by recursive
240   //! rendering algorithms for rendering elements of groups.
241   //! @param theFilter [in] the filter instance.
242   inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
243   {
244     myRenderFilter = theFilter;
245   }
246
247   //! Get rendering filter.
248   //! @return filter instance.
249   inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
250   {
251     return myRenderFilter;
252   }
253
254   //! @return applied view matrix.
255   inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
256
257   //! @return applied model structure matrix.
258   inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
259
260   //! Returns capping algorithm rendering filter.
261   const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
262   {
263     return myDefaultCappingAlgoFilter;
264   }
265
266   //! Returns face aspect for none culling mode.
267   const OpenGl_AspectFace& NoneCulling() const
268   {
269     return myNoneCulling;
270   }
271
272   //! Returns face aspect for front face culling mode.
273   const OpenGl_AspectFace& FrontCulling() const
274   {
275     return myFrontCulling;
276   }
277
278   //! Sets a new environment texture.
279   void SetEnvironmentTexture (const Handle(OpenGl_TextureSet)& theTexture) { myEnvironmentTexture = theTexture; }
280
281   //! Returns environment texture.
282   const Handle(OpenGl_TextureSet)& EnvironmentTexture() const { return myEnvironmentTexture; }
283
284 protected: //! @name protected fields
285
286   OpenGl_View*                     myView;
287   Handle(OpenGl_Window)            myWindow;
288   Handle(OpenGl_Context)           myGlContext;
289   Standard_Boolean                 myUseZBuffer;
290   Standard_Boolean                 myUseDepthWrite;
291   Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
292   OpenGl_AspectFace                myNoneCulling;
293   OpenGl_AspectFace                myFrontCulling;
294
295 protected: //! @name fields related to status
296
297   Handle(OpenGl_RenderFilter) myRenderFilter;
298   const OpenGl_AspectLine*   myAspectLineSet;
299   const OpenGl_AspectFace*   myAspectFaceSet;
300   Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
301   const OpenGl_AspectMarker* myAspectMarkerSet;
302   Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
303   const OpenGl_AspectText*   myAspectTextSet;
304   Handle(Graphic3d_PresentationAttributes) myAspectFaceAppliedWithHL;
305
306   const OpenGl_Matrix* ViewMatrix_applied;
307   const OpenGl_Matrix* StructureMatrix_applied;
308
309   bool            myToAllowFaceCulling; //!< allow back face culling
310   Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
311
312   OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
313
314   OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
315
316   Handle(OpenGl_TextureSet) myEnvironmentTexture;
317
318 public: //! @name type definition
319
320   DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
321   DEFINE_STANDARD_ALLOC
322
323 };
324
325 #endif // _OpenGl_Workspace_Header