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_RenderFilter.hxx>
27 #include <OpenGl_ShaderObject.hxx>
28 #include <OpenGl_ShaderProgram.hxx>
29 #include <OpenGl_TextParam.hxx>
30 #include <OpenGl_TextureBufferArb.hxx>
31 #include <OpenGl_Vec.hxx>
32 #include <OpenGl_Window.hxx>
37 class OpenGl_RaytraceFilter;
38 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
40 //! Graphical ray-tracing filter.
41 //! Filters out all raytracable structures.
42 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
46 //! Default constructor.
47 OpenGl_RaytraceFilter() {}
49 //! Returns the previously set filter.
50 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
52 return myPrevRenderFilter;
55 //! Remembers the previously set filter.
56 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
58 myPrevRenderFilter = theFilter;
61 //! Checks whether the element can be rendered or not.
62 //! @param theElement [in] the element to check.
63 //! @return True if element can be rendered.
64 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement) Standard_OVERRIDE;
68 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
72 DEFINE_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
75 class OpenGl_Workspace;
76 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
78 //! Rendering workspace.
79 //! Provides methods to render primitives and maintain GL state.
80 class OpenGl_Workspace : public Standard_Transient
84 //! Constructor of rendering workspace.
85 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
88 virtual ~OpenGl_Workspace() {}
90 //! Activate rendering context.
91 Standard_EXPORT Standard_Boolean Activate();
93 OpenGl_View* View() const { return myView; }
95 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
97 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
99 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
101 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
102 Image_PixMap& theImage,
103 const Graphic3d_BufferType& theBufferType);
105 Standard_EXPORT Standard_Integer Width() const;
107 Standard_EXPORT Standard_Integer Height() const;
109 //! Setup Z-buffer usage flag (without affecting GL state!).
110 //! Returns previously set flag.
111 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
113 const Standard_Boolean wasUsed = myUseZBuffer;
114 myUseZBuffer = theToUse;
118 //! @return true if usage of Z buffer is enabled.
119 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
121 //! @return true if depth writing is enabled.
122 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
124 //! @return true if clipping algorithm enabled
125 Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
127 //// RELATED TO STATUS ////
129 //! Return true if active group might activate face culling (e.g. primitives are closed).
130 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
132 //! Allow or disallow face culling.
133 //! This call does NOT affect current state of back face culling;
134 //! ApplyAspectFace() should be called to update state.
135 void SetAllowFaceCulling (bool theToAllow) { myToAllowFaceCulling = theToAllow; }
137 //! Return true if following structures should apply highlight color.
138 bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
140 //! Return highlight style.
141 const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
143 //! Set highlight style.
144 void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) { myHighlightStyle = theStyle; }
146 //! Return line color taking into account highlight flag.
147 const OpenGl_Vec4& LineColor() const
149 return !myHighlightStyle.IsNull()
150 ? myHighlightStyle->ColorRGBA()
151 : myAspectLineSet->Aspect()->ColorRGBA();
154 //! Return edge color taking into account highlight flag.
155 const OpenGl_Vec4& EdgeColor() const
157 return !myHighlightStyle.IsNull()
158 ? myHighlightStyle->ColorRGBA()
159 : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
162 //! Return marker color taking into account highlight flag.
163 const OpenGl_Vec4& MarkerColor() const
165 return !myHighlightStyle.IsNull()
166 ? myHighlightStyle->ColorRGBA()
167 : myAspectMarkerSet->Aspect()->ColorRGBA();
170 //! Return Interior color taking into account highlight flag.
171 const OpenGl_Vec4& InteriorColor() const
173 return !myHighlightStyle.IsNull()
174 ? myHighlightStyle->ColorRGBA()
175 : myAspectFaceSet->Aspect()->InteriorColorRGBA();
178 //! Return text color taking into account highlight flag.
179 const OpenGl_Vec4& TextColor() const
181 return !myHighlightStyle.IsNull()
182 ? myHighlightStyle->ColorRGBA()
183 : myAspectTextSet->Aspect()->ColorRGBA();
186 //! Return text Subtitle color taking into account highlight flag.
187 const OpenGl_Vec4& TextSubtitleColor() const
189 return !myHighlightStyle.IsNull()
190 ? myHighlightStyle->ColorRGBA()
191 : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
194 //! Currently set line aspect (can differ from applied).
195 const OpenGl_AspectLine* AspectLine() const { return myAspectLineSet; }
197 //! Currently set face aspect (can differ from applied).
198 const OpenGl_AspectFace* AspectFace() const { return myAspectFaceSet; }
200 //! Currently set marker aspect (can differ from applied).
201 const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
203 //! Currently set text aspect (can differ from applied).
204 const OpenGl_AspectText* AspectText() const { return myAspectTextSet; }
206 //! Assign new line aspect (will be applied within ApplyAspectLine()).
207 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
209 //! Assign new face aspect (will be applied within ApplyAspectFace()).
210 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
212 //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
213 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
215 //! Assign new text aspect (will be applied within ApplyAspectText()).
216 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
218 //! Apply line aspect.
219 //! @return aspect set by SetAspectLine()
220 const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
222 //! Apply face aspect.
223 //! @return aspect set by SetAspectFace()
224 Standard_EXPORT const OpenGl_AspectFace* ApplyAspectFace();
226 //! Apply marker aspect.
227 //! @return aspect set by SetAspectMarker()
228 Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
230 //! Apply text aspect.
231 //! @return aspect set by SetAspectText()
232 const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
234 //! Clear the applied aspect state to default values.
235 void ResetAppliedAspect();
237 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
238 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
239 const Handle(Graphic3d_TextureParams)& theParams = NULL);
240 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
242 //! Set filter for restricting rendering of particular elements.
243 //! Filter can be applied for rendering passes used by recursive
244 //! rendering algorithms for rendering elements of groups.
245 //! @param theFilter [in] the filter instance.
246 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
248 myRenderFilter = theFilter;
251 //! Get rendering filter.
252 //! @return filter instance.
253 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
255 return myRenderFilter;
258 //! @return applied view matrix.
259 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
261 //! @return applied model structure matrix.
262 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
264 //! Sets and applies current polygon offset.
265 void SetPolygonOffset (const Graphic3d_PolygonOffset& theParams);
267 //! Returns currently applied polygon offset parameters.
268 const Graphic3d_PolygonOffset& AppliedPolygonOffset() { return myPolygonOffsetApplied; }
270 //! Returns capping algorithm rendering filter.
271 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
273 return myDefaultCappingAlgoFilter;
276 //! Returns face aspect for none culling mode.
277 const OpenGl_AspectFace& NoneCulling() const
279 return myNoneCulling;
282 //! Returns face aspect for front face culling mode.
283 const OpenGl_AspectFace& FrontCulling() const
285 return myFrontCulling;
288 //! Sets a new environment texture.
289 void SetEnvironmentTexture (const Handle(OpenGl_Texture)& theTexture)
291 myEnvironmentTexture = theTexture;
294 //! Returns environment texture.
295 const Handle(OpenGl_Texture)& EnvironmentTexture() const
297 return myEnvironmentTexture;
302 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
303 const Handle(Graphic3d_TextureParams)& theParams);
305 protected: //! @name protected fields
308 Handle(OpenGl_Window) myWindow;
309 Handle(OpenGl_Context) myGlContext;
310 Standard_Boolean myUseZBuffer;
311 Standard_Boolean myUseDepthWrite;
312 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
313 OpenGl_AspectFace myNoneCulling;
314 OpenGl_AspectFace myFrontCulling;
316 protected: //! @name fields related to status
318 Handle(OpenGl_RenderFilter) myRenderFilter;
319 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
320 const OpenGl_AspectLine* myAspectLineSet;
321 const OpenGl_AspectFace* myAspectFaceSet;
322 Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
323 const OpenGl_AspectMarker* myAspectMarkerSet;
324 Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
325 const OpenGl_AspectText* myAspectTextSet;
326 Handle(Graphic3d_PresentationAttributes) myAspectFaceAppliedWithHL;
328 const OpenGl_Matrix* ViewMatrix_applied;
329 const OpenGl_Matrix* StructureMatrix_applied;
331 bool myToAllowFaceCulling; //!< allow back face culling
332 Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
334 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
336 Graphic3d_PolygonOffset myPolygonOffsetApplied; //!< currently applied polygon offset
338 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
340 Handle(OpenGl_Texture) myEnvironmentTexture;
342 public: //! @name type definition
344 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
345 DEFINE_STANDARD_ALLOC
349 #endif // _OpenGl_Workspace_Header