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