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_LineAttributes.hxx>
25 #include <OpenGl_Matrix.hxx>
26 #include <OpenGl_NamedStatus.hxx>
27 #include <OpenGl_PrinterContext.hxx>
28 #include <OpenGl_RenderFilter.hxx>
29 #include <OpenGl_ShaderObject.hxx>
30 #include <OpenGl_ShaderProgram.hxx>
31 #include <OpenGl_TextParam.hxx>
32 #include <OpenGl_TextureBufferArb.hxx>
33 #include <OpenGl_Vec.hxx>
34 #include <OpenGl_Window.hxx>
39 //! OpenGL material definition
40 struct OpenGl_Material
43 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
44 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
45 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
46 OpenGl_Vec4 Emission; //!< material emission
47 OpenGl_Vec4 Params; //!< extra packed parameters
49 Standard_ShortReal Shine() const { return Params.x(); }
50 Standard_ShortReal& ChangeShine() { return Params.x(); }
52 Standard_ShortReal Transparency() const { return Params.y(); }
53 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
55 //! Set material color.
56 void SetColor (const OpenGl_Vec4& theColor)
58 // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
59 Ambient.xyz() = theColor.rgb() * 0.25f;
60 Diffuse.xyz() = theColor.rgb();
63 //! Initialize material
64 void Init (const Graphic3d_MaterialAspect& theProp,
65 const Quantity_Color& theInteriorColor);
67 //! Returns packed (serialized) representation of material properties
68 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
69 static Standard_Integer NbOfVec4() { return 5; }
73 class OpenGl_RaytraceFilter;
74 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
76 //! Graphical ray-tracing filter.
77 //! Filters out all raytracable structures.
78 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
82 //! Default constructor.
83 OpenGl_RaytraceFilter() {}
85 //! Returns the previously set filter.
86 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
88 return myPrevRenderFilter;
91 //! Remembers the previously set filter.
92 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
94 myPrevRenderFilter = theFilter;
97 //! Checks whether the element can be rendered or not.
98 //! @param theElement [in] the element to check.
99 //! @return True if element can be rendered.
100 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement) Standard_OVERRIDE;
104 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
108 DEFINE_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
111 class OpenGl_Workspace;
112 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
114 //! Rendering workspace.
115 //! Provides methods to render primitives and maintain GL state.
116 class OpenGl_Workspace : public Standard_Transient
120 //! Constructor of rendering workspace.
121 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
124 Standard_EXPORT virtual ~OpenGl_Workspace();
126 //! Activate rendering context.
127 Standard_EXPORT Standard_Boolean Activate();
129 OpenGl_View* View() const { return myView; }
131 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
133 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
135 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
137 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
138 Image_PixMap& theImage,
139 const Graphic3d_BufferType& theBufferType);
141 Standard_EXPORT Standard_Integer Width() const;
143 Standard_EXPORT Standard_Integer Height() const;
145 //! Setup Z-buffer usage flag (without affecting GL state!).
146 //! Returns previously set flag.
147 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
149 const Standard_Boolean wasUsed = myUseZBuffer;
150 myUseZBuffer = theToUse;
154 Handle(OpenGl_PrinterContext)& PrinterContext() { return myPrintContext; }
156 //! @return true if usage of Z buffer is enabled.
157 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
159 //! @return true if depth writing is enabled.
160 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
162 //! @return true if usage of GL light is enabled.
163 Standard_EXPORT Standard_Boolean UseGLLight() const;
165 //! @return true if clipping algorithm enabled
166 Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
168 Standard_Integer NamedStatus;
170 //// RELATED TO STATUS ////
172 const OpenGl_Vec4* HighlightColor;
174 //! Return true if active group might activate face culling (e.g. primitives are closed).
175 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
177 //! Allow or disallow face culling.
178 //! This call does NOT affect current state of back face culling;
179 //! ApplyAspectFace() should be called to update state.
180 void SetAllowFaceCulling (bool theToAllow) { myToAllowFaceCulling = theToAllow; }
182 //! Return true if following structures should apply highlight color.
183 bool ToHighlight() const { return myToHighlight; }
186 void SetHighlight (bool theToHighlight) { myToHighlight = theToHighlight; }
188 //! Return line color taking into account highlight flag.
189 const OpenGl_Vec4& LineColor() const
193 : myAspectLineSet->Aspect()->ColorRGBA();
196 //! Return edge color taking into account highlight flag.
197 const OpenGl_Vec4& EdgeColor() const
201 : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
204 //! Return marker color taking into account highlight flag.
205 const OpenGl_Vec4& MarkerColor() const
209 : myAspectMarkerSet->Aspect()->ColorRGBA();
212 //! Return Interior color taking into account highlight flag.
213 const OpenGl_Vec4& InteriorColor() const
217 : myAspectFaceSet->Aspect()->InteriorColorRGBA();
220 //! Return text color taking into account highlight flag.
221 const OpenGl_Vec4& TextColor() const
225 : myAspectTextSet->Aspect()->ColorRGBA();
228 //! Return text Subtitle color taking into account highlight flag.
229 const OpenGl_Vec4& TextSubtitleColor() const
233 : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
236 //! Currently set line aspect (can differ from applied).
237 const OpenGl_AspectLine* AspectLine() const { return myAspectLineSet; }
239 //! Currently set face aspect (can differ from applied).
240 const OpenGl_AspectFace* AspectFace() const { return myAspectFaceSet; }
242 //! Currently set marker aspect (can differ from applied).
243 const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
245 //! Currently set text aspect (can differ from applied).
246 const OpenGl_AspectText* AspectText() const { return myAspectTextSet; }
248 //! Assign new line aspect (will be applied within ApplyAspectLine()).
249 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
251 //! Assign new face aspect (will be applied within ApplyAspectFace()).
252 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
254 //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
255 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
257 //! Assign new text aspect (will be applied within ApplyAspectText()).
258 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
260 //! Apply line aspect.
261 //! @return aspect set by SetAspectLine()
262 const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
264 //! Apply face aspect.
265 //! @return aspect set by SetAspectFace()
266 Standard_EXPORT const OpenGl_AspectFace* ApplyAspectFace();
268 //! Apply marker aspect.
269 //! @return aspect set by SetAspectMarker()
270 Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
272 //! Apply text aspect.
273 //! @return aspect set by SetAspectText()
274 const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
276 //! Clear the applied aspect state to default values.
277 void ResetAppliedAspect();
279 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
280 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
281 const Handle(Graphic3d_TextureParams)& theParams = NULL);
282 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
284 //! Set filter for restricting rendering of particular elements.
285 //! Filter can be applied for rendering passes used by recursive
286 //! rendering algorithms for rendering elements of groups.
287 //! @param theFilter [in] the filter instance.
288 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
290 myRenderFilter = theFilter;
293 //! Get rendering filter.
294 //! @return filter instance.
295 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
297 return myRenderFilter;
300 //! @return applied view matrix.
301 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
303 //! @return applied model structure matrix.
304 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
306 //! Sets and applies current polygon offset.
307 void SetPolygonOffset (const Graphic3d_PolygonOffset& theParams);
309 //! Returns currently applied polygon offset params.
310 const Graphic3d_PolygonOffset& AppliedPolygonOffset() { return myPolygonOffsetApplied; }
312 //! Returns capping algorithm rendering filter.
313 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
315 return myDefaultCappingAlgoFilter;
318 //! Returns face aspect for none culling mode.
319 const OpenGl_AspectFace& NoneCulling() const
321 return myNoneCulling;
324 //! Returns face aspect for front face culling mode.
325 const OpenGl_AspectFace& FrontCulling() const
327 return myFrontCulling;
330 //! Sets a new environment texture.
331 void SetEnvironmentTexture (const Handle(OpenGl_Texture)& theTexture)
333 myEnvironmentTexture = theTexture;
336 //! Returns environment texture.
337 const Handle(OpenGl_Texture)& EnvironmentTexture() const
339 return myEnvironmentTexture;
346 TEL_FRONT_MATERIAL = 1,
347 TEL_BACK_MATERIAL = 2
350 void updateMaterial (const int theFlag);
352 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
353 const Handle(Graphic3d_TextureParams)& theParams);
355 protected: //! @name protected fields
358 Handle(OpenGl_Window) myWindow;
359 Handle(OpenGl_Context) myGlContext;
360 Handle(OpenGl_PrinterContext) myPrintContext;
361 Handle(OpenGl_LineAttributes) myLineAttribs;
362 Standard_Boolean myUseZBuffer;
363 Standard_Boolean myUseDepthWrite;
364 Standard_Boolean myUseGLLight;
365 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
366 OpenGl_AspectFace myNoneCulling;
367 OpenGl_AspectFace myFrontCulling;
369 protected: //! @name fields related to status
371 Handle(OpenGl_RenderFilter) myRenderFilter;
372 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
373 const OpenGl_AspectLine* myAspectLineSet;
374 const OpenGl_AspectFace* myAspectFaceSet;
375 Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
376 const OpenGl_AspectMarker* myAspectMarkerSet;
377 Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
378 const OpenGl_AspectText* myAspectTextSet;
379 bool myAspectFaceAppliedWithHL;
381 const OpenGl_Matrix* ViewMatrix_applied;
382 const OpenGl_Matrix* StructureMatrix_applied;
384 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
385 OpenGl_Material myMatBack; //!< current back material state
386 OpenGl_Material myMatTmp; //!< temporary variable
387 bool myToAllowFaceCulling; //!< allow back face culling
388 bool myToHighlight; //!< flag indicating highlighting mode
390 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
392 Graphic3d_PolygonOffset myPolygonOffsetApplied; //!< currently applied polygon offset
394 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
396 Handle(OpenGl_Texture) myEnvironmentTexture;
398 public: //! @name type definition
400 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
401 DEFINE_STANDARD_ALLOC
405 #endif // _OpenGl_Workspace_Header