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_Matrix.hxx>
25 #include <OpenGl_NamedStatus.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 //! OpenGL material definition
38 struct OpenGl_Material
41 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
42 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
43 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
44 OpenGl_Vec4 Emission; //!< material emission
45 OpenGl_Vec4 Params; //!< extra packed parameters
47 Standard_ShortReal Shine() const { return Params.x(); }
48 Standard_ShortReal& ChangeShine() { return Params.x(); }
50 Standard_ShortReal Transparency() const { return Params.y(); }
51 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
53 //! Set material color.
54 void SetColor (const OpenGl_Vec4& theColor)
56 // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
57 Ambient.xyz() = theColor.rgb() * 0.25f;
58 Diffuse.xyz() = theColor.rgb();
61 //! Initialize material
62 void Init (const Graphic3d_MaterialAspect& theProp,
63 const Quantity_Color& theInteriorColor);
65 //! Returns packed (serialized) representation of material properties
66 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
67 static Standard_Integer NbOfVec4() { return 5; }
71 class OpenGl_RaytraceFilter;
72 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
74 //! Graphical ray-tracing filter.
75 //! Filters out all raytracable structures.
76 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
80 //! Default constructor.
81 OpenGl_RaytraceFilter() {}
83 //! Returns the previously set filter.
84 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
86 return myPrevRenderFilter;
89 //! Remembers the previously set filter.
90 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
92 myPrevRenderFilter = theFilter;
95 //! Checks whether the element can be rendered or not.
96 //! @param theElement [in] the element to check.
97 //! @return True if element can be rendered.
98 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement) Standard_OVERRIDE;
102 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
106 DEFINE_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
109 class OpenGl_Workspace;
110 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
112 //! Rendering workspace.
113 //! Provides methods to render primitives and maintain GL state.
114 class OpenGl_Workspace : public Standard_Transient
118 //! Constructor of rendering workspace.
119 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
122 virtual ~OpenGl_Workspace() {}
124 //! Activate rendering context.
125 Standard_EXPORT Standard_Boolean Activate();
127 OpenGl_View* View() const { return myView; }
129 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
131 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
133 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
135 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
136 Image_PixMap& theImage,
137 const Graphic3d_BufferType& theBufferType);
139 Standard_EXPORT Standard_Integer Width() const;
141 Standard_EXPORT Standard_Integer Height() const;
143 //! Setup Z-buffer usage flag (without affecting GL state!).
144 //! Returns previously set flag.
145 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
147 const Standard_Boolean wasUsed = myUseZBuffer;
148 myUseZBuffer = theToUse;
152 //! @return true if usage of Z buffer is enabled.
153 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
155 //! @return true if depth writing is enabled.
156 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
158 //! @return true if clipping algorithm enabled
159 Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
161 Standard_Integer NamedStatus;
163 //// RELATED TO STATUS ////
165 const OpenGl_Vec4* HighlightColor;
167 //! Return true if active group might activate face culling (e.g. primitives are closed).
168 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
170 //! Allow or disallow face culling.
171 //! This call does NOT affect current state of back face culling;
172 //! ApplyAspectFace() should be called to update state.
173 void SetAllowFaceCulling (bool theToAllow) { myToAllowFaceCulling = theToAllow; }
175 //! Return true if following structures should apply highlight color.
176 bool ToHighlight() const { return myToHighlight; }
179 void SetHighlight (bool theToHighlight) { myToHighlight = theToHighlight; }
181 //! Return line color taking into account highlight flag.
182 const OpenGl_Vec4& LineColor() const
186 : myAspectLineSet->Aspect()->ColorRGBA();
189 //! Return edge color taking into account highlight flag.
190 const OpenGl_Vec4& EdgeColor() const
194 : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
197 //! Return marker color taking into account highlight flag.
198 const OpenGl_Vec4& MarkerColor() const
202 : myAspectMarkerSet->Aspect()->ColorRGBA();
205 //! Return Interior color taking into account highlight flag.
206 const OpenGl_Vec4& InteriorColor() const
210 : myAspectFaceSet->Aspect()->InteriorColorRGBA();
213 //! Return text color taking into account highlight flag.
214 const OpenGl_Vec4& TextColor() const
218 : myAspectTextSet->Aspect()->ColorRGBA();
221 //! Return text Subtitle color taking into account highlight flag.
222 const OpenGl_Vec4& TextSubtitleColor() const
226 : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
229 //! Currently set line aspect (can differ from applied).
230 const OpenGl_AspectLine* AspectLine() const { return myAspectLineSet; }
232 //! Currently set face aspect (can differ from applied).
233 const OpenGl_AspectFace* AspectFace() const { return myAspectFaceSet; }
235 //! Currently set marker aspect (can differ from applied).
236 const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
238 //! Currently set text aspect (can differ from applied).
239 const OpenGl_AspectText* AspectText() const { return myAspectTextSet; }
241 //! Assign new line aspect (will be applied within ApplyAspectLine()).
242 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
244 //! Assign new face aspect (will be applied within ApplyAspectFace()).
245 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
247 //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
248 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
250 //! Assign new text aspect (will be applied within ApplyAspectText()).
251 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
253 //! Apply line aspect.
254 //! @return aspect set by SetAspectLine()
255 const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
257 //! Apply face aspect.
258 //! @return aspect set by SetAspectFace()
259 Standard_EXPORT const OpenGl_AspectFace* ApplyAspectFace();
261 //! Apply marker aspect.
262 //! @return aspect set by SetAspectMarker()
263 Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
265 //! Apply text aspect.
266 //! @return aspect set by SetAspectText()
267 const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
269 //! Clear the applied aspect state to default values.
270 void ResetAppliedAspect();
272 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
273 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
274 const Handle(Graphic3d_TextureParams)& theParams = NULL);
275 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
277 //! Set filter for restricting rendering of particular elements.
278 //! Filter can be applied for rendering passes used by recursive
279 //! rendering algorithms for rendering elements of groups.
280 //! @param theFilter [in] the filter instance.
281 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
283 myRenderFilter = theFilter;
286 //! Get rendering filter.
287 //! @return filter instance.
288 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
290 return myRenderFilter;
293 //! @return applied view matrix.
294 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
296 //! @return applied model structure matrix.
297 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
299 //! Sets and applies current polygon offset.
300 void SetPolygonOffset (const Graphic3d_PolygonOffset& theParams);
302 //! Returns currently applied polygon offset parameters.
303 const Graphic3d_PolygonOffset& AppliedPolygonOffset() { return myPolygonOffsetApplied; }
305 //! Returns capping algorithm rendering filter.
306 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
308 return myDefaultCappingAlgoFilter;
311 //! Returns face aspect for none culling mode.
312 const OpenGl_AspectFace& NoneCulling() const
314 return myNoneCulling;
317 //! Returns face aspect for front face culling mode.
318 const OpenGl_AspectFace& FrontCulling() const
320 return myFrontCulling;
323 //! Sets a new environment texture.
324 void SetEnvironmentTexture (const Handle(OpenGl_Texture)& theTexture)
326 myEnvironmentTexture = theTexture;
329 //! Returns environment texture.
330 const Handle(OpenGl_Texture)& EnvironmentTexture() const
332 return myEnvironmentTexture;
339 TEL_FRONT_MATERIAL = 1,
340 TEL_BACK_MATERIAL = 2
343 void updateMaterial (const int theFlag);
345 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
346 const Handle(Graphic3d_TextureParams)& theParams);
348 protected: //! @name protected fields
351 Handle(OpenGl_Window) myWindow;
352 Handle(OpenGl_Context) myGlContext;
353 Standard_Boolean myUseZBuffer;
354 Standard_Boolean myUseDepthWrite;
355 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
356 OpenGl_AspectFace myNoneCulling;
357 OpenGl_AspectFace myFrontCulling;
359 protected: //! @name fields related to status
361 Handle(OpenGl_RenderFilter) myRenderFilter;
362 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
363 const OpenGl_AspectLine* myAspectLineSet;
364 const OpenGl_AspectFace* myAspectFaceSet;
365 Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
366 const OpenGl_AspectMarker* myAspectMarkerSet;
367 Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
368 const OpenGl_AspectText* myAspectTextSet;
369 bool myAspectFaceAppliedWithHL;
371 const OpenGl_Matrix* ViewMatrix_applied;
372 const OpenGl_Matrix* StructureMatrix_applied;
374 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
375 OpenGl_Material myMatBack; //!< current back material state
376 OpenGl_Material myMatTmp; //!< temporary variable
377 bool myToAllowFaceCulling; //!< allow back face culling
378 bool myToHighlight; //!< flag indicating highlighting mode
380 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
382 Graphic3d_PolygonOffset myPolygonOffsetApplied; //!< currently applied polygon offset
384 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
386 Handle(OpenGl_Texture) myEnvironmentTexture;
388 public: //! @name type definition
390 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
391 DEFINE_STANDARD_ALLOC
395 #endif // _OpenGl_Workspace_Header