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 //! Configure default polygon offset parameters.
87 //! Return previous settings.
88 Standard_EXPORT Graphic3d_PolygonOffset SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset);
90 //// RELATED TO STATUS ////
92 //! Return true if active group might activate face culling (e.g. primitives are closed).
93 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
95 //! Allow or disallow face culling.
96 //! This call does NOT affect current state of back face culling;
97 //! ApplyAspectFace() should be called to update state.
98 bool SetAllowFaceCulling (bool theToAllow)
100 const bool wasAllowed = myToAllowFaceCulling;
101 myToAllowFaceCulling = theToAllow;
105 //! Return true if following structures should apply highlight color.
106 bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
108 //! Return highlight style.
109 const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
111 //! Set highlight style.
112 void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) { myHighlightStyle = theStyle; }
114 //! Return line color taking into account highlight flag.
115 const OpenGl_Vec4& LineColor() const
117 return !myHighlightStyle.IsNull()
118 ? myHighlightStyle->ColorRGBA()
119 : myAspectLineSet->Aspect()->ColorRGBA();
122 //! Return edge color taking into account highlight flag.
123 const OpenGl_Vec4& EdgeColor() const
125 return !myHighlightStyle.IsNull()
126 ? myHighlightStyle->ColorRGBA()
127 : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
130 //! Return marker color taking into account highlight flag.
131 const OpenGl_Vec4& MarkerColor() const
133 return !myHighlightStyle.IsNull()
134 ? myHighlightStyle->ColorRGBA()
135 : myAspectMarkerSet->Aspect()->ColorRGBA();
138 //! Return Interior color taking into account highlight flag.
139 const OpenGl_Vec4& InteriorColor() const
141 return !myHighlightStyle.IsNull()
142 ? myHighlightStyle->ColorRGBA()
143 : myAspectFaceSet->Aspect()->InteriorColorRGBA();
146 //! Return text color taking into account highlight flag.
147 const OpenGl_Vec4& TextColor() const
149 return !myHighlightStyle.IsNull()
150 ? myHighlightStyle->ColorRGBA()
151 : myAspectTextSet->Aspect()->ColorRGBA();
154 //! Return text Subtitle color taking into account highlight flag.
155 const OpenGl_Vec4& TextSubtitleColor() const
157 return !myHighlightStyle.IsNull()
158 ? myHighlightStyle->ColorRGBA()
159 : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
162 //! Currently set line aspect (can differ from applied).
163 const OpenGl_AspectLine* AspectLine() const { return myAspectLineSet; }
165 //! Currently set face aspect (can differ from applied).
166 const OpenGl_AspectFace* AspectFace() const { return myAspectFaceSet; }
168 //! Currently set marker aspect (can differ from applied).
169 const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
171 //! Currently set text aspect (can differ from applied).
172 const OpenGl_AspectText* AspectText() const { return myAspectTextSet; }
174 //! Assign new line aspect (will be applied within ApplyAspectLine()).
175 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
177 //! Assign new face aspect (will be applied within ApplyAspectFace()).
178 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
180 //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
181 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
183 //! Assign new text aspect (will be applied within ApplyAspectText()).
184 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
186 //! Apply line aspect.
187 //! @return aspect set by SetAspectLine()
188 const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
190 //! Apply face aspect.
191 //! @return aspect set by SetAspectFace()
192 Standard_EXPORT const OpenGl_AspectFace* ApplyAspectFace();
194 //! Apply marker aspect.
195 //! @return aspect set by SetAspectMarker()
196 Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
198 //! Apply text aspect.
199 //! @return aspect set by SetAspectText()
200 const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
202 //! Clear the applied aspect state to default values.
203 void ResetAppliedAspect();
205 //! Get rendering filter.
206 //! @sa ShouldRender()
207 Standard_Integer RenderFilter() const { return myRenderFilter; }
209 //! Set filter for restricting rendering of particular elements.
210 //! @sa ShouldRender()
211 void SetRenderFilter (Standard_Integer theFilter) { myRenderFilter = theFilter; }
213 //! Checks whether the element can be rendered or not.
214 //! @param theElement [in] the element to check
215 //! @return True if element can be rendered
216 bool ShouldRender (const OpenGl_Element* theElement);
218 //! Return the number of skipped transparent elements within active OpenGl_RenderFilter_OpaqueOnly filter.
219 //! @sa OpenGl_LayerList::Render()
220 Standard_Integer NbSkippedTransparentElements() { return myNbSkippedTranspElems; }
222 //! Reset skipped transparent elements counter.
223 //! @sa OpenGl_LayerList::Render()
224 void ResetSkippedCounter() { myNbSkippedTranspElems = 0; }
226 //! @return applied view matrix.
227 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
229 //! @return applied model structure matrix.
230 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
232 //! Returns face aspect for textured font rendering.
233 const OpenGl_AspectFace& FontFaceAspect() const { return myFontFaceAspect; }
235 //! Returns face aspect for none culling mode.
236 const OpenGl_AspectFace& NoneCulling() const { return myNoneCulling; }
238 //! Returns face aspect for front face culling mode.
239 const OpenGl_AspectFace& FrontCulling() const { return myFrontCulling; }
241 //! Sets a new environment texture.
242 void SetEnvironmentTexture (const Handle(OpenGl_TextureSet)& theTexture) { myEnvironmentTexture = theTexture; }
244 //! Returns environment texture.
245 const Handle(OpenGl_TextureSet)& EnvironmentTexture() const { return myEnvironmentTexture; }
247 protected: //! @name protected fields
250 Handle(OpenGl_Window) myWindow;
251 Handle(OpenGl_Context) myGlContext;
252 Standard_Boolean myUseZBuffer;
253 Standard_Boolean myUseDepthWrite;
254 OpenGl_AspectFace myNoneCulling;
255 OpenGl_AspectFace myFrontCulling;
256 OpenGl_AspectFace myFontFaceAspect;
258 protected: //! @name fields related to status
260 Standard_Integer myNbSkippedTranspElems; //!< counter of skipped transparent elements for OpenGl_LayerList two rendering passes method
261 Standard_Integer myRenderFilter; //!< active filter for skipping rendering of elements by some criteria (multiple render passes)
263 OpenGl_AspectLine myDefaultAspectLine;
264 OpenGl_AspectFace myDefaultAspectFace;
265 OpenGl_AspectMarker myDefaultAspectMarker;
266 OpenGl_AspectText myDefaultAspectText;
268 const OpenGl_AspectLine* myAspectLineSet;
269 const OpenGl_AspectFace* myAspectFaceSet;
270 Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
271 const OpenGl_AspectMarker* myAspectMarkerSet;
272 Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
273 const OpenGl_AspectText* myAspectTextSet;
274 Handle(Graphic3d_PresentationAttributes) myAspectFaceAppliedWithHL;
276 const OpenGl_Matrix* ViewMatrix_applied;
277 const OpenGl_Matrix* StructureMatrix_applied;
279 bool myToAllowFaceCulling; //!< allow back face culling
280 Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
282 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
284 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
286 Handle(OpenGl_TextureSet) myEnvironmentTexture;
288 public: //! @name type definition
290 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
291 DEFINE_STANDARD_ALLOC
295 #endif // _OpenGl_Workspace_Header