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_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>
37 class OpenGl_Workspace;
38 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
40 //! Rendering workspace.
41 //! Provides methods to render primitives and maintain GL state.
42 class OpenGl_Workspace : public Standard_Transient
46 //! Constructor of rendering workspace.
47 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
50 virtual ~OpenGl_Workspace() {}
52 //! Activate rendering context.
53 Standard_EXPORT Standard_Boolean Activate();
55 OpenGl_View* View() const { return myView; }
57 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
59 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
61 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
63 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
64 Image_PixMap& theImage,
65 const Graphic3d_BufferType& theBufferType);
67 Standard_EXPORT Standard_Integer Width() const;
69 Standard_EXPORT Standard_Integer Height() const;
71 //! Setup Z-buffer usage flag (without affecting GL state!).
72 //! Returns previously set flag.
73 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
75 const Standard_Boolean wasUsed = myUseZBuffer;
76 myUseZBuffer = theToUse;
80 //! @return true if usage of Z buffer is enabled.
81 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
83 //! @return true if depth writing is enabled.
84 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
86 //! @return true if frustum culling algorithm is enabled
87 Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
89 //! Configure default polygon offset parameters.
90 //! Return previous settings.
91 Standard_EXPORT Graphic3d_PolygonOffset SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset);
93 //// RELATED TO STATUS ////
95 //! Return true if active group might activate face culling (e.g. primitives are closed).
96 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
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)
103 const bool wasAllowed = myToAllowFaceCulling;
104 myToAllowFaceCulling = theToAllow;
108 //! Return true if following structures should apply highlight color.
109 bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
111 //! Return highlight style.
112 const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
114 //! Set highlight style.
115 void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) { myHighlightStyle = theStyle; }
117 //! Return line color taking into account highlight flag.
118 const OpenGl_Vec4& LineColor() const
120 return !myHighlightStyle.IsNull()
121 ? myHighlightStyle->ColorRGBA()
122 : myAspectLineSet->Aspect()->ColorRGBA();
125 //! Return edge color taking into account highlight flag.
126 const OpenGl_Vec4& EdgeColor() const
128 return !myHighlightStyle.IsNull()
129 ? myHighlightStyle->ColorRGBA()
130 : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
133 //! Return marker color taking into account highlight flag.
134 const OpenGl_Vec4& MarkerColor() const
136 return !myHighlightStyle.IsNull()
137 ? myHighlightStyle->ColorRGBA()
138 : myAspectMarkerSet->Aspect()->ColorRGBA();
141 //! Return Interior color taking into account highlight flag.
142 const OpenGl_Vec4& InteriorColor() const
144 return !myHighlightStyle.IsNull()
145 ? myHighlightStyle->ColorRGBA()
146 : myAspectFaceSet->Aspect()->InteriorColorRGBA();
149 //! Return text color taking into account highlight flag.
150 const OpenGl_Vec4& TextColor() const
152 return !myHighlightStyle.IsNull()
153 ? myHighlightStyle->ColorRGBA()
154 : myAspectTextSet->Aspect()->ColorRGBA();
157 //! Return text Subtitle color taking into account highlight flag.
158 const OpenGl_Vec4& TextSubtitleColor() const
160 return !myHighlightStyle.IsNull()
161 ? myHighlightStyle->ColorRGBA()
162 : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
165 //! Currently set line aspect (can differ from applied).
166 const OpenGl_AspectLine* AspectLine() const { return myAspectLineSet; }
168 //! Currently set face aspect (can differ from applied).
169 const OpenGl_AspectFace* AspectFace() const { return myAspectFaceSet; }
171 //! Currently set marker aspect (can differ from applied).
172 const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
174 //! Currently set text aspect (can differ from applied).
175 const OpenGl_AspectText* AspectText() const { return myAspectTextSet; }
177 //! Assign new line aspect (will be applied within ApplyAspectLine()).
178 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
180 //! Assign new face aspect (will be applied within ApplyAspectFace()).
181 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
183 //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
184 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
186 //! Assign new text aspect (will be applied within ApplyAspectText()).
187 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
189 //! Apply line aspect.
190 //! @return aspect set by SetAspectLine()
191 const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
193 //! Apply face aspect.
194 //! @return aspect set by SetAspectFace()
195 Standard_EXPORT const OpenGl_AspectFace* ApplyAspectFace();
197 //! Apply marker aspect.
198 //! @return aspect set by SetAspectMarker()
199 Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
201 //! Apply text aspect.
202 //! @return aspect set by SetAspectText()
203 const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
205 //! Clear the applied aspect state to default values.
206 void ResetAppliedAspect();
208 //! Get rendering filter.
209 //! @sa ShouldRender()
210 Standard_Integer RenderFilter() const { return myRenderFilter; }
212 //! Set filter for restricting rendering of particular elements.
213 //! @sa ShouldRender()
214 void SetRenderFilter (Standard_Integer theFilter) { myRenderFilter = theFilter; }
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);
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; }
225 //! Reset skipped transparent elements counter.
226 //! @sa OpenGl_LayerList::Render()
227 void ResetSkippedCounter() { myNbSkippedTranspElems = 0; }
229 //! @return applied view matrix.
230 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
232 //! @return applied model structure matrix.
233 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
235 //! Returns face aspect for textured font rendering.
236 const OpenGl_AspectFace& FontFaceAspect() const { return myFontFaceAspect; }
238 //! Returns face aspect for none culling mode.
239 const OpenGl_AspectFace& NoneCulling() const { return myNoneCulling; }
241 //! Returns face aspect for front face culling mode.
242 const OpenGl_AspectFace& FrontCulling() const { return myFrontCulling; }
244 //! Sets a new environment texture.
245 void SetEnvironmentTexture (const Handle(OpenGl_TextureSet)& theTexture) { myEnvironmentTexture = theTexture; }
247 //! Returns environment texture.
248 const Handle(OpenGl_TextureSet)& EnvironmentTexture() const { return myEnvironmentTexture; }
250 protected: //! @name protected fields
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;
261 protected: //! @name fields related to status
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)
266 OpenGl_AspectLine myDefaultAspectLine;
267 OpenGl_AspectFace myDefaultAspectFace;
268 OpenGl_AspectMarker myDefaultAspectMarker;
269 OpenGl_AspectText myDefaultAspectText;
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;
279 const OpenGl_Matrix* ViewMatrix_applied;
280 const OpenGl_Matrix* StructureMatrix_applied;
282 bool myToAllowFaceCulling; //!< allow back face culling
283 Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
285 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
287 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
289 Handle(OpenGl_TextureSet) myEnvironmentTexture;
291 public: //! @name type definition
293 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
294 DEFINE_STANDARD_ALLOC
298 #endif // _OpenGl_Workspace_Header