0025695: Visualization, AIS_InteractiveContext - define default HilightMode
[occt.git] / src / OpenGl / OpenGl_Workspace.hxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
1981cb22 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
2166f0fa
SK
16#ifndef _OpenGl_Workspace_Header
17#define _OpenGl_Workspace_Header
18
bf75be98 19#include <Graphic3d_BufferType.hxx>
2166f0fa 20
bf75be98 21#include <OpenGl_AspectFace.hxx>
c357e426 22#include <OpenGl_CappingAlgo.hxx>
fc73a202 23#include <OpenGl_FrameBuffer.hxx>
2166f0fa
SK
24#include <OpenGl_Matrix.hxx>
25#include <OpenGl_NamedStatus.hxx>
4269bd1b 26#include <OpenGl_RenderFilter.hxx>
fc73a202 27#include <OpenGl_ShaderObject.hxx>
28#include <OpenGl_ShaderProgram.hxx>
c357e426 29#include <OpenGl_TextParam.hxx>
fc73a202 30#include <OpenGl_TextureBufferArb.hxx>
c357e426 31#include <OpenGl_Vec.hxx>
32#include <OpenGl_Window.hxx>
fc73a202 33
5b111128 34class OpenGl_View;
692613e5 35class Image_PixMap;
2166f0fa 36
0adbd30f 37//! OpenGL material definition
38struct OpenGl_Material
39{
40
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
46
47 Standard_ShortReal Shine() const { return Params.x(); }
48 Standard_ShortReal& ChangeShine() { return Params.x(); }
49
50 Standard_ShortReal Transparency() const { return Params.y(); }
51 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
52
f9ba5c4d 53 //! Set material color.
54 void SetColor (const OpenGl_Vec4& theColor)
55 {
56 // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
57 Ambient.xyz() = theColor.rgb() * 0.25f;
58 Diffuse.xyz() = theColor.rgb();
59 }
60
0adbd30f 61 //! Initialize material
b6472664 62 void Init (const Graphic3d_MaterialAspect& theProp,
63 const Quantity_Color& theInteriorColor);
0adbd30f 64
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; }
68
69};
70
c04c30b3 71class OpenGl_RaytraceFilter;
a89742cf 72DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
73
25ef750e 74//! Graphical ray-tracing filter.
a89742cf 75//! Filters out all raytracable structures.
76class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
77{
78public:
79
80 //! Default constructor.
81 OpenGl_RaytraceFilter() {}
82
91c60b57 83 //! Returns the previously set filter.
84 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
85 {
86 return myPrevRenderFilter;
87 }
88
a89742cf 89 //! Remembers the previously set filter.
91c60b57 90 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
a89742cf 91 {
92 myPrevRenderFilter = theFilter;
93 }
94
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.
79104795 98 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement) Standard_OVERRIDE;
a89742cf 99
100private:
101
102 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
103
104public:
105
92efcf78 106 DEFINE_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
a89742cf 107};
108
c04c30b3 109class OpenGl_Workspace;
c357e426 110DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
c04c30b3 111
c357e426 112//! Rendering workspace.
e276548b 113//! Provides methods to render primitives and maintain GL state.
c357e426 114class OpenGl_Workspace : public Standard_Transient
2166f0fa
SK
115{
116public:
117
c357e426 118 //! Constructor of rendering workspace.
119 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
2166f0fa
SK
120
121 //! Destructor
6d0e6be5 122 virtual ~OpenGl_Workspace() {}
2166f0fa 123
c357e426 124 //! Activate rendering context.
125 Standard_EXPORT Standard_Boolean Activate();
a272ed94 126
c357e426 127 OpenGl_View* View() const { return myView; }
2166f0fa 128
c357e426 129 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
2166f0fa 130
b128c892 131 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
679ecdee 132
b128c892 133 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
a174a3c5 134
b128c892 135 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
136 Image_PixMap& theImage,
137 const Graphic3d_BufferType& theBufferType);
2166f0fa 138
c357e426 139 Standard_EXPORT Standard_Integer Width() const;
140
141 Standard_EXPORT Standard_Integer Height() const;
142
eae454e3 143 //! Setup Z-buffer usage flag (without affecting GL state!).
144 //! Returns previously set flag.
145 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
146 {
147 const Standard_Boolean wasUsed = myUseZBuffer;
148 myUseZBuffer = theToUse;
149 return wasUsed;
150 }
151
c357e426 152 //! @return true if usage of Z buffer is enabled.
153 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
154
155 //! @return true if depth writing is enabled.
eae454e3 156 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
2166f0fa 157
c357e426 158 //! @return true if clipping algorithm enabled
159 Standard_EXPORT Standard_Boolean IsCullingEnabled() const;
160
161 Standard_Integer NamedStatus;
162
163 //// RELATED TO STATUS ////
2166f0fa 164
b6472664 165 //! Return true if active group might activate face culling (e.g. primitives are closed).
166 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
167
168 //! Allow or disallow face culling.
169 //! This call does NOT affect current state of back face culling;
170 //! ApplyAspectFace() should be called to update state.
171 void SetAllowFaceCulling (bool theToAllow) { myToAllowFaceCulling = theToAllow; }
2166f0fa 172
f9ba5c4d 173 //! Return true if following structures should apply highlight color.
f838dac4 174 bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
175
176 //! Return highlight style.
177 const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
f9ba5c4d 178
f838dac4 179 //! Set highlight style.
180 void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) { myHighlightStyle = theStyle; }
f9ba5c4d 181
182 //! Return line color taking into account highlight flag.
b6472664 183 const OpenGl_Vec4& LineColor() const
f9ba5c4d 184 {
f838dac4 185 return !myHighlightStyle.IsNull()
186 ? myHighlightStyle->ColorRGBA()
187 : myAspectLineSet->Aspect()->ColorRGBA();
f9ba5c4d 188 }
189
190 //! Return edge color taking into account highlight flag.
b6472664 191 const OpenGl_Vec4& EdgeColor() const
f9ba5c4d 192 {
f838dac4 193 return !myHighlightStyle.IsNull()
194 ? myHighlightStyle->ColorRGBA()
195 : myAspectFaceSet->AspectEdge()->Aspect()->ColorRGBA();
f9ba5c4d 196 }
197
198 //! Return marker color taking into account highlight flag.
b6472664 199 const OpenGl_Vec4& MarkerColor() const
f9ba5c4d 200 {
f838dac4 201 return !myHighlightStyle.IsNull()
202 ? myHighlightStyle->ColorRGBA()
203 : myAspectMarkerSet->Aspect()->ColorRGBA();
f9ba5c4d 204 }
205
206 //! Return Interior color taking into account highlight flag.
b6472664 207 const OpenGl_Vec4& InteriorColor() const
f9ba5c4d 208 {
f838dac4 209 return !myHighlightStyle.IsNull()
210 ? myHighlightStyle->ColorRGBA()
211 : myAspectFaceSet->Aspect()->InteriorColorRGBA();
f9ba5c4d 212 }
213
214 //! Return text color taking into account highlight flag.
b6472664 215 const OpenGl_Vec4& TextColor() const
f9ba5c4d 216 {
f838dac4 217 return !myHighlightStyle.IsNull()
218 ? myHighlightStyle->ColorRGBA()
219 : myAspectTextSet->Aspect()->ColorRGBA();
f9ba5c4d 220 }
221
222 //! Return text Subtitle color taking into account highlight flag.
b6472664 223 const OpenGl_Vec4& TextSubtitleColor() const
f9ba5c4d 224 {
f838dac4 225 return !myHighlightStyle.IsNull()
226 ? myHighlightStyle->ColorRGBA()
227 : myAspectTextSet->Aspect()->ColorSubTitleRGBA();
f9ba5c4d 228 }
229
230 //! Currently set line aspect (can differ from applied).
231 const OpenGl_AspectLine* AspectLine() const { return myAspectLineSet; }
232
233 //! Currently set face aspect (can differ from applied).
234 const OpenGl_AspectFace* AspectFace() const { return myAspectFaceSet; }
235
236 //! Currently set marker aspect (can differ from applied).
237 const OpenGl_AspectMarker* AspectMarker() const { return myAspectMarkerSet; }
238
239 //! Currently set text aspect (can differ from applied).
240 const OpenGl_AspectText* AspectText() const { return myAspectTextSet; }
241
242 //! Assign new line aspect (will be applied within ApplyAspectLine()).
71c6e1e5 243 Standard_EXPORT const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
f9ba5c4d 244
245 //! Assign new face aspect (will be applied within ApplyAspectFace()).
71c6e1e5 246 Standard_EXPORT const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
f9ba5c4d 247
248 //! Assign new marker aspect (will be applied within ApplyAspectMarker()).
71c6e1e5 249 Standard_EXPORT const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
f9ba5c4d 250
251 //! Assign new text aspect (will be applied within ApplyAspectText()).
71c6e1e5 252 Standard_EXPORT const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
2166f0fa 253
f9ba5c4d 254 //! Apply line aspect.
255 //! @return aspect set by SetAspectLine()
256 const OpenGl_AspectLine* ApplyAspectLine() { return myAspectLineSet; }
257
258 //! Apply face aspect.
259 //! @return aspect set by SetAspectFace()
260 Standard_EXPORT const OpenGl_AspectFace* ApplyAspectFace();
261
262 //! Apply marker aspect.
263 //! @return aspect set by SetAspectMarker()
264 Standard_EXPORT const OpenGl_AspectMarker* ApplyAspectMarker();
265
266 //! Apply text aspect.
267 //! @return aspect set by SetAspectText()
268 const OpenGl_AspectText* ApplyAspectText() { return myAspectTextSet; }
2166f0fa 269
f9ba5c4d 270 //! Clear the applied aspect state to default values.
34a44cbd 271 void ResetAppliedAspect();
272
bf75be98 273 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
274 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
275 const Handle(Graphic3d_TextureParams)& theParams = NULL);
8625ef7e 276 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
bf75be98 277
4269bd1b 278 //! Set filter for restricting rendering of particular elements.
279 //! Filter can be applied for rendering passes used by recursive
280 //! rendering algorithms for rendering elements of groups.
281 //! @param theFilter [in] the filter instance.
282 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
283 {
5322131b 284 myRenderFilter = theFilter;
4269bd1b 285 }
286
287 //! Get rendering filter.
288 //! @return filter instance.
5322131b 289 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
290 {
291 return myRenderFilter;
4269bd1b 292 }
293
294 //! @return applied view matrix.
295 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
296
297 //! @return applied model structure matrix.
298 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
299
550f3b8b 300 //! Sets and applies current polygon offset.
b6472664 301 void SetPolygonOffset (const Graphic3d_PolygonOffset& theParams);
550f3b8b 302
6d0e6be5 303 //! Returns currently applied polygon offset parameters.
b6472664 304 const Graphic3d_PolygonOffset& AppliedPolygonOffset() { return myPolygonOffsetApplied; }
550f3b8b 305
f8ae3605 306 //! Returns capping algorithm rendering filter.
307 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
308 {
309 return myDefaultCappingAlgoFilter;
310 }
311
312 //! Returns face aspect for none culling mode.
313 const OpenGl_AspectFace& NoneCulling() const
314 {
315 return myNoneCulling;
316 }
317
318 //! Returns face aspect for front face culling mode.
319 const OpenGl_AspectFace& FrontCulling() const
320 {
321 return myFrontCulling;
322 }
323
83da37b1 324 //! Sets a new environment texture.
325 void SetEnvironmentTexture (const Handle(OpenGl_Texture)& theTexture)
326 {
327 myEnvironmentTexture = theTexture;
328 }
329
330 //! Returns environment texture.
331 const Handle(OpenGl_Texture)& EnvironmentTexture() const
332 {
333 return myEnvironmentTexture;
334 }
335
2166f0fa
SK
336protected:
337
b6472664 338 enum
339 {
340 TEL_FRONT_MATERIAL = 1,
341 TEL_BACK_MATERIAL = 2
342 };
343
0adbd30f 344 void updateMaterial (const int theFlag);
2166f0fa 345
bf75be98 346 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
347 const Handle(Graphic3d_TextureParams)& theParams);
2166f0fa 348
91c60b57 349protected: //! @name protected fields
265d4508 350
c357e426 351 OpenGl_View* myView;
352 Handle(OpenGl_Window) myWindow;
353 Handle(OpenGl_Context) myGlContext;
c357e426 354 Standard_Boolean myUseZBuffer;
355 Standard_Boolean myUseDepthWrite;
f8ae3605 356 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
357 OpenGl_AspectFace myNoneCulling;
358 OpenGl_AspectFace myFrontCulling;
359
bf75be98 360protected: //! @name fields related to status
361
4269bd1b 362 Handle(OpenGl_RenderFilter) myRenderFilter;
bf75be98 363 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
f9ba5c4d 364 const OpenGl_AspectLine* myAspectLineSet;
365 const OpenGl_AspectFace* myAspectFaceSet;
b6472664 366 Handle(Graphic3d_AspectFillArea3d) myAspectFaceApplied;
f9ba5c4d 367 const OpenGl_AspectMarker* myAspectMarkerSet;
b6472664 368 Handle(Graphic3d_AspectMarker3d) myAspectMarkerApplied;
f9ba5c4d 369 const OpenGl_AspectText* myAspectTextSet;
370 bool myAspectFaceAppliedWithHL;
2166f0fa 371
2166f0fa
SK
372 const OpenGl_Matrix* ViewMatrix_applied;
373 const OpenGl_Matrix* StructureMatrix_applied;
374
3b1817a9 375 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
376 OpenGl_Material myMatBack; //!< current back material state
377 OpenGl_Material myMatTmp; //!< temporary variable
b6472664 378 bool myToAllowFaceCulling; //!< allow back face culling
f838dac4 379 Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
0adbd30f 380
550f3b8b 381 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
0f8c0fb8 382
b6472664 383 Graphic3d_PolygonOffset myPolygonOffsetApplied; //!< currently applied polygon offset
2166f0fa 384
550f3b8b 385 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
bf75be98 386
83da37b1 387 Handle(OpenGl_Texture) myEnvironmentTexture;
388
bf75be98 389public: //! @name type definition
2166f0fa 390
92efcf78 391 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
1c35b92f 392 DEFINE_STANDARD_ALLOC
2166f0fa
SK
393
394};
395
bf75be98 396#endif // _OpenGl_Workspace_Header