1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_Workspace_Header
17 #define _OpenGl_Workspace_Header
19 #include <Graphic3d_BufferType.hxx>
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>
36 class OpenGl_Workspace;
37 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
39 //! Rendering workspace.
40 //! Provides methods to render primitives and maintain GL state.
41 class OpenGl_Workspace : public Standard_Transient
45 //! Constructor of rendering workspace.
46 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
49 virtual ~OpenGl_Workspace() {}
51 //! Activate rendering context.
52 Standard_EXPORT Standard_Boolean Activate();
54 OpenGl_View* View() const { return myView; }
56 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
58 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
60 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
62 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
63 Image_PixMap& theImage,
64 const Graphic3d_BufferType& theBufferType);
66 Standard_EXPORT Standard_Integer Width() const;
68 Standard_EXPORT Standard_Integer Height() const;
70 //! Setup Z-buffer usage flag (without affecting GL state!).
71 //! Returns previously set flag.
72 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
74 const Standard_Boolean wasUsed = myUseZBuffer;
75 myUseZBuffer = theToUse;
79 //! @return true if usage of Z buffer is enabled.
80 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
82 //! @return true if depth writing is enabled.
83 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
85 //! Configure default polygon offset parameters.
86 //! Return previous settings.
87 Standard_EXPORT Graphic3d_PolygonOffset SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset);
89 //// RELATED TO STATUS ////
91 //! Return true if active group might activate face culling (e.g. primitives are closed).
92 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
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)
99 const bool wasAllowed = myToAllowFaceCulling;
100 myToAllowFaceCulling = theToAllow;
104 //! Return true if following structures should apply highlight color.
105 bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
107 //! Return highlight style.
108 const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
110 //! Set highlight style.
111 void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) { myHighlightStyle = theStyle; }
113 //! Return edge color taking into account highlight flag.
114 const OpenGl_Vec4& EdgeColor() const
116 return !myHighlightStyle.IsNull()
117 ? myHighlightStyle->ColorRGBA()
118 : myAspectsSet->Aspect()->EdgeColorRGBA();
121 //! Return Interior color taking into account highlight flag.
122 const OpenGl_Vec4& InteriorColor() const
124 return !myHighlightStyle.IsNull()
125 ? myHighlightStyle->ColorRGBA()
126 : myAspectsSet->Aspect()->InteriorColorRGBA();
129 //! Return text color taking into account highlight flag.
130 const OpenGl_Vec4& TextColor() const
132 return !myHighlightStyle.IsNull()
133 ? myHighlightStyle->ColorRGBA()
134 : myAspectsSet->Aspect()->ColorRGBA();
137 //! Return text Subtitle color taking into account highlight flag.
138 const OpenGl_Vec4& TextSubtitleColor() const
140 return !myHighlightStyle.IsNull()
141 ? myHighlightStyle->ColorRGBA()
142 : myAspectsSet->Aspect()->ColorSubTitleRGBA();
145 //! Currently set aspects (can differ from applied).
146 const OpenGl_Aspects* Aspects() const { return myAspectsSet; }
148 //! Assign new aspects (will be applied within ApplyAspects()).
149 Standard_EXPORT const OpenGl_Aspects* SetAspects (const OpenGl_Aspects* theAspect);
151 //! Return TextureSet from set Aspects or Environment texture.
152 const Handle(OpenGl_TextureSet)& TextureSet() const
154 const Handle(OpenGl_TextureSet)& aTextureSet = myAspectsSet->TextureSet (myGlContext, ToHighlight());
155 return !aTextureSet.IsNull()
156 || myAspectsSet->Aspect()->ToMapTexture()
158 : myEnvironmentTexture;
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);
166 //! Clear the applied aspect state to default values.
167 void ResetAppliedAspect();
169 //! Get rendering filter.
170 //! @sa ShouldRender()
171 Standard_Integer RenderFilter() const { return myRenderFilter; }
173 //! Set filter for restricting rendering of particular elements.
174 //! @sa ShouldRender()
175 void SetRenderFilter (Standard_Integer theFilter) { myRenderFilter = theFilter; }
177 //! Checks whether the element can be rendered or not.
178 //! @param theElement [in] the element to check
179 //! @param theGroup [in] the group containing the element
180 //! @return True if element can be rendered
181 bool ShouldRender (const OpenGl_Element* theElement, const OpenGl_Group* theGroup);
183 //! Return the number of skipped transparent elements within active OpenGl_RenderFilter_OpaqueOnly filter.
184 //! @sa OpenGl_LayerList::Render()
185 Standard_Integer NbSkippedTransparentElements() { return myNbSkippedTranspElems; }
187 //! Reset skipped transparent elements counter.
188 //! @sa OpenGl_LayerList::Render()
189 void ResetSkippedCounter() { myNbSkippedTranspElems = 0; }
191 //! @return applied view matrix.
192 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
194 //! @return applied model structure matrix.
195 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
197 //! Returns face aspect for none culling mode.
198 const OpenGl_Aspects& NoneCulling() const { return myNoneCulling; }
200 //! Returns face aspect for front face culling mode.
201 const OpenGl_Aspects& FrontCulling() const { return myFrontCulling; }
203 //! Sets a new environment texture.
204 void SetEnvironmentTexture (const Handle(OpenGl_TextureSet)& theTexture) { myEnvironmentTexture = theTexture; }
206 //! Returns environment texture.
207 const Handle(OpenGl_TextureSet)& EnvironmentTexture() const { return myEnvironmentTexture; }
209 //! Dumps the content of me into the stream
210 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
212 protected: //! @name protected fields
215 Handle(OpenGl_Window) myWindow;
216 Handle(OpenGl_Context) myGlContext;
217 Standard_Boolean myUseZBuffer;
218 Standard_Boolean myUseDepthWrite;
219 OpenGl_Aspects myNoneCulling;
220 OpenGl_Aspects myFrontCulling;
222 protected: //! @name fields related to status
224 Standard_Integer myNbSkippedTranspElems; //!< counter of skipped transparent elements for OpenGl_LayerList two rendering passes method
225 Standard_Integer myRenderFilter; //!< active filter for skipping rendering of elements by some criteria (multiple render passes)
227 OpenGl_Aspects myDefaultAspects;
228 const OpenGl_Aspects* myAspectsSet;
229 Handle(Graphic3d_Aspects) myAspectsApplied;
231 Handle(Graphic3d_PresentationAttributes) myAspectFaceAppliedWithHL;
233 const OpenGl_Matrix* ViewMatrix_applied;
234 const OpenGl_Matrix* StructureMatrix_applied;
236 bool myToAllowFaceCulling; //!< allow back face culling
237 Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
239 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
241 OpenGl_Aspects myAspectFaceHl; //!< Hiddenline aspect
243 Handle(OpenGl_TextureSet) myEnvironmentTexture;
245 public: //! @name type definition
247 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
248 DEFINE_STANDARD_ALLOC
252 #endif // _OpenGl_Workspace_Header