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 <InterfaceGraphic_Graphic3d.hxx>
23 #include <OpenGl_AspectFace.hxx>
24 #include <OpenGl_CappingAlgo.hxx>
25 #include <OpenGl_FrameBuffer.hxx>
26 #include <OpenGl_LineAttributes.hxx>
27 #include <OpenGl_Matrix.hxx>
28 #include <OpenGl_NamedStatus.hxx>
29 #include <OpenGl_PrinterContext.hxx>
30 #include <OpenGl_RenderFilter.hxx>
31 #include <OpenGl_ShaderObject.hxx>
32 #include <OpenGl_ShaderProgram.hxx>
33 #include <OpenGl_TextParam.hxx>
34 #include <OpenGl_TextureBufferArb.hxx>
35 #include <OpenGl_Vec.hxx>
36 #include <OpenGl_Window.hxx>
38 class OpenGl_AspectLine;
39 class OpenGl_AspectMarker;
40 class OpenGl_AspectText;
45 //! OpenGL material definition
46 struct OpenGl_Material
49 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
50 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
51 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
52 OpenGl_Vec4 Emission; //!< material emission
53 OpenGl_Vec4 Params; //!< extra packed parameters
55 Standard_ShortReal Shine() const { return Params.x(); }
56 Standard_ShortReal& ChangeShine() { return Params.x(); }
58 Standard_ShortReal Transparency() const { return Params.y(); }
59 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
61 //! Initialize material
62 void Init (const OPENGL_SURF_PROP& theProps);
64 //! Returns packed (serialized) representation of material properties
65 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
66 static Standard_Integer NbOfVec4() { return 5; }
70 class OpenGl_RaytraceFilter;
71 DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
73 //! Graphical ray-tracing filter.
74 //! Filters out all raytracable structures.
75 class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
79 //! Default constructor.
80 OpenGl_RaytraceFilter() {}
82 //! Returns the previously set filter.
83 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
85 return myPrevRenderFilter;
88 //! Remembers the previously set filter.
89 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
91 myPrevRenderFilter = theFilter;
94 //! Checks whether the element can be rendered or not.
95 //! @param theElement [in] the element to check.
96 //! @return True if element can be rendered.
97 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement) Standard_OVERRIDE;
101 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
105 DEFINE_STANDARD_RTTI(OpenGl_RaytraceFilter, OpenGl_RenderFilter)
108 class OpenGl_Workspace;
109 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
111 //! Rendering workspace.
112 //! Provides methods to render primitives and maintain GL state.
113 class OpenGl_Workspace : public Standard_Transient
117 //! Constructor of rendering workspace.
118 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
121 Standard_EXPORT virtual ~OpenGl_Workspace();
123 //! Activate rendering context.
124 Standard_EXPORT Standard_Boolean Activate();
126 OpenGl_View* View() const { return myView; }
128 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
130 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
132 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
134 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
135 Image_PixMap& theImage,
136 const Graphic3d_BufferType& theBufferType);
138 Standard_EXPORT Standard_Integer Width() const;
140 Standard_EXPORT Standard_Integer Height() const;
142 //! Setup Z-buffer usage flag (without affecting GL state!).
143 //! Returns previously set flag.
144 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
146 const Standard_Boolean wasUsed = myUseZBuffer;
147 myUseZBuffer = theToUse;
151 Handle(OpenGl_PrinterContext)& PrinterContext() { return myPrintContext; }
153 //! @return true if usage of Z buffer is enabled.
154 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
156 //! @return true if depth writing is enabled.
157 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
159 //! @return true if usage of GL light is enabled.
160 Standard_EXPORT Standard_Boolean UseGLLight() const;
162 //! @return true if antialiasing is enabled.
163 Standard_EXPORT Standard_Integer AntiAliasingMode() 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 TEL_COLOUR* HighlightColor;
174 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
175 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
176 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
177 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
179 //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
180 Standard_EXPORT const OpenGl_AspectLine* AspectLine (const Standard_Boolean theWithApply);
181 Standard_EXPORT const OpenGl_AspectFace* AspectFace (const Standard_Boolean theWithApply);
182 Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
183 Standard_EXPORT const OpenGl_AspectText* AspectText (const Standard_Boolean theWithApply);
185 //! Clear the applied aspect state.
186 void ResetAppliedAspect();
188 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
189 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
190 const Handle(Graphic3d_TextureParams)& theParams = NULL);
191 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
193 //! Set filter for restricting rendering of particular elements.
194 //! Filter can be applied for rendering passes used by recursive
195 //! rendering algorithms for rendering elements of groups.
196 //! @param theFilter [in] the filter instance.
197 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
199 myRenderFilter = theFilter;
202 //! Get rendering filter.
203 //! @return filter instance.
204 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
206 return myRenderFilter;
209 //! @return applied view matrix.
210 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
212 //! @return applied model structure matrix.
213 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
215 //! Sets and applies current polygon offset.
216 void SetPolygonOffset (int theMode, Standard_ShortReal theFactor, Standard_ShortReal theUnits);
218 //! Returns currently applied polygon offset params.
219 const TEL_POFFSET_PARAM& AppliedPolygonOffset() { return PolygonOffset_applied; }
221 //! Returns capping algorithm rendering filter.
222 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
224 return myDefaultCappingAlgoFilter;
227 //! Returns face aspect for none culling mode.
228 const OpenGl_AspectFace& NoneCulling() const
230 return myNoneCulling;
233 //! Returns face aspect for front face culling mode.
234 const OpenGl_AspectFace& FrontCulling() const
236 return myFrontCulling;
241 void updateMaterial (const int theFlag);
243 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
244 const Handle(Graphic3d_TextureParams)& theParams);
246 protected: //! @name protected fields
249 Handle(OpenGl_Window) myWindow;
250 Handle(OpenGl_Context) myGlContext;
251 Handle(OpenGl_PrinterContext) myPrintContext;
252 Handle(OpenGl_LineAttributes) myLineAttribs;
253 Standard_Integer myAntiAliasingMode;
254 Standard_Boolean myUseZBuffer;
255 Standard_Boolean myUseDepthWrite;
256 Standard_Boolean myUseGLLight;
257 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
258 OpenGl_AspectFace myNoneCulling;
259 OpenGl_AspectFace myFrontCulling;
261 protected: //! @name fields related to status
263 Handle(OpenGl_RenderFilter) myRenderFilter;
264 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
265 const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied;
266 const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied;
267 const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
268 const OpenGl_AspectText *AspectText_set, *AspectText_applied;
270 const OpenGl_Matrix* ViewMatrix_applied;
271 const OpenGl_Matrix* StructureMatrix_applied;
273 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
274 OpenGl_Material myMatBack; //!< current back material state
275 OpenGl_Material myMatTmp; //!< temporary variable
276 TelCullMode myCullingMode; //!< back face culling mode, applied from face aspect
278 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
280 TEL_POFFSET_PARAM PolygonOffset_applied; //!< Currently applied polygon offset.
282 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
284 public: //! @name type definition
286 DEFINE_STANDARD_RTTI(OpenGl_Workspace, Standard_Transient)
287 DEFINE_STANDARD_ALLOC
291 #endif // _OpenGl_Workspace_Header