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