0025789: Visualization - get rid of obsolete 2d layers implementation
[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
fc73a202 19#include <map>
20#include <set>
e276548b 21
2166f0fa
SK
22#include <OpenGl_Window.hxx>
23
24#include <TColStd_Array2OfReal.hxx>
25#include <Quantity_Color.hxx>
bf75be98 26#include <Graphic3d_CView.hxx>
2166f0fa 27#include <Graphic3d_TypeOfComposition.hxx>
bf75be98 28#include <Graphic3d_TypeOfTexture.hxx>
29#include <Graphic3d_PtrFrameBuffer.hxx>
30#include <Graphic3d_BufferType.hxx>
2166f0fa 31
2166f0fa
SK
32#include <Aspect_Handle.hxx>
33#include <Aspect_PrintAlgo.hxx>
550f3b8b 34#include <Aspect_PolygonOffsetMode.hxx>
2166f0fa
SK
35
36#include <InterfaceGraphic_Graphic3d.hxx>
37#include <InterfaceGraphic_Visual3d.hxx>
38
1981cb22 39#include <NCollection_Sequence.hxx>
40
bf75be98 41#include <OpenGl_AspectFace.hxx>
fc73a202 42#include <OpenGl_FrameBuffer.hxx>
2166f0fa
SK
43#include <OpenGl_Matrix.hxx>
44#include <OpenGl_NamedStatus.hxx>
a174a3c5 45#include <OpenGl_PrinterContext.hxx>
fc73a202 46#include <OpenGl_SceneGeometry.hxx>
2166f0fa 47#include <OpenGl_TextParam.hxx>
4269bd1b 48#include <OpenGl_RenderFilter.hxx>
0adbd30f 49#include <OpenGl_Vec.hxx>
73192b37 50#include <OpenGl_LineAttributes.hxx>
f8ae3605 51#include <OpenGl_CappingAlgo.hxx>
2166f0fa 52
2166f0fa 53
fc73a202 54#include <OpenGl_ShaderObject.hxx>
55#include <OpenGl_ShaderProgram.hxx>
56#include <OpenGl_TextureBufferArb.hxx>
57
2166f0fa 58class OpenGl_AspectLine;
2166f0fa
SK
59class OpenGl_AspectMarker;
60class OpenGl_AspectText;
61class OpenGl_FrameBuffer;
62class OpenGl_Structure;
b7cd4ba7 63class OpenGl_TriangleSet;
4269bd1b 64class OpenGl_Element;
5b111128 65class OpenGl_View;
692613e5 66class Image_PixMap;
2166f0fa 67
0adbd30f 68//! OpenGL material definition
69struct OpenGl_Material
70{
71
72 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
73 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
74 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
75 OpenGl_Vec4 Emission; //!< material emission
76 OpenGl_Vec4 Params; //!< extra packed parameters
77
78 Standard_ShortReal Shine() const { return Params.x(); }
79 Standard_ShortReal& ChangeShine() { return Params.x(); }
80
81 Standard_ShortReal Transparency() const { return Params.y(); }
82 Standard_ShortReal& ChangeTransparency() { return Params.y(); }
83
84 //! Initialize material
85 void Init (const OPENGL_SURF_PROP& theProps);
86
87 //! Returns packed (serialized) representation of material properties
88 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
89 static Standard_Integer NbOfVec4() { return 5; }
90
91};
92
c04c30b3 93class OpenGl_RaytraceFilter;
a89742cf 94DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
95
25ef750e 96//! Graphical ray-tracing filter.
a89742cf 97//! Filters out all raytracable structures.
98class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
99{
100public:
101
102 //! Default constructor.
103 OpenGl_RaytraceFilter() {}
104
91c60b57 105 //! Returns the previously set filter.
106 const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
107 {
108 return myPrevRenderFilter;
109 }
110
a89742cf 111 //! Remembers the previously set filter.
91c60b57 112 void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
a89742cf 113 {
114 myPrevRenderFilter = theFilter;
115 }
116
117 //! Checks whether the element can be rendered or not.
118 //! @param theElement [in] the element to check.
119 //! @return True if element can be rendered.
120 virtual Standard_Boolean CanRender (const OpenGl_Element* theElement);
121
122private:
123
124 Handle(OpenGl_RenderFilter) myPrevRenderFilter;
125
126public:
127
ec357c5c 128 DEFINE_STANDARD_RTTI(OpenGl_RaytraceFilter, OpenGl_RenderFilter)
a89742cf 129};
130
c04c30b3 131class OpenGl_Workspace;
132DEFINE_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
133
e276548b 134//! Represents window with GL context.
135//! Provides methods to render primitives and maintain GL state.
2166f0fa
SK
136class OpenGl_Workspace : public OpenGl_Window
137{
138public:
139
140 //! Main constructor - prepare GL context for specified window.
62e1beed 141 Standard_EXPORT OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
142 const CALL_DEF_WINDOW& theCWindow,
143 Aspect_RenderingContext theGContext,
144 const Handle(OpenGl_Caps)& theCaps,
145 const Handle(OpenGl_Context)& theShareCtx);
2166f0fa
SK
146
147 //! Destructor
62e1beed 148 Standard_EXPORT virtual ~OpenGl_Workspace();
2166f0fa 149
62e1beed 150 Standard_EXPORT void SetActiveView (const Handle(OpenGl_View)& theView,
151 const Standard_Integer theViewId);
a272ed94 152
153 const Handle(OpenGl_View)& ActiveView() const { return myView; }
154
155 Standard_Integer ActiveViewId() const { return myViewId; }
2166f0fa
SK
156
157 //! Redraw the window.
27f85086 158 Standard_EXPORT virtual void Redraw (const Graphic3d_CView& theCView);
2166f0fa 159
679ecdee 160 Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
27f85086 161 Standard_EXPORT virtual void RedrawImmediate (const Graphic3d_CView& theCView);
679ecdee 162
62e1beed 163 //! Mark cached view content invalid (e.g. complete view redraw should be performed on next frame).
164 void Invalidate() { myBackBufferRestored = Standard_False; }
165
166 //! Return true if view content cache has been invalidated.
167 Standard_Boolean IsInvalidated() const { return !myBackBufferRestored; }
2166f0fa
SK
168
169 //! Special method to perform printing.
170 //! System-specific and currently only Win platform implemented.
a174a3c5 171 Standard_Boolean Print (const Handle(OpenGl_PrinterContext)& thePrintContext,
172 const Graphic3d_CView& theCView,
2166f0fa
SK
173 const Aspect_Handle theHPrintDC,
174 const Standard_Boolean theToShowBackground,
175 const Standard_CString theFileName,
176 const Aspect_PrintAlgo thePrintAlgorithm,
177 const Standard_Real theScaleFactor);
178
a174a3c5 179 const Handle(OpenGl_PrinterContext)& PrinterContext() const
180 {
181 return myPrintContext;
182 }
183
2166f0fa
SK
184 void DisplayCallback (const Graphic3d_CView& theCView, int theReason);
185
2166f0fa
SK
186 Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
187 void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr);
692613e5 188 Standard_Boolean BufferDump (OpenGl_FrameBuffer* theFBOPtr,
189 Image_PixMap& theImage,
190 const Graphic3d_BufferType& theBufferType);
2166f0fa 191
eae454e3 192 //! Setup Z-buffer usage flag (without affecting GL state!).
193 //! Returns previously set flag.
194 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
195 {
196 const Standard_Boolean wasUsed = myUseZBuffer;
197 myUseZBuffer = theToUse;
198 return wasUsed;
199 }
200
201 Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
202 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
203 Standard_Boolean& UseGLLight() { return myUseGLLight; }
2166f0fa 204
73192b37 205 Standard_Integer AntiAliasingMode() const { return myAntiAliasingMode; }
206
2166f0fa
SK
207 //// RELATED TO STATUS ////
208
209 Standard_Integer NamedStatus;
210
2166f0fa
SK
211 const TEL_COLOUR* HighlightColor;
212
2166f0fa
SK
213 const OpenGl_AspectLine* SetAspectLine (const OpenGl_AspectLine* theAspect);
214 const OpenGl_AspectFace* SetAspectFace (const OpenGl_AspectFace* theAspect);
215 const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
216 const OpenGl_AspectText* SetAspectText (const OpenGl_AspectText* theAspect);
217
218 void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
219
3946774d 220 //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
221 Standard_EXPORT const OpenGl_AspectLine* AspectLine (const Standard_Boolean theWithApply);
222 Standard_EXPORT const OpenGl_AspectFace* AspectFace (const Standard_Boolean theWithApply);
223 Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
224 Standard_EXPORT const OpenGl_AspectText* AspectText (const Standard_Boolean theWithApply);
a174a3c5 225 inline const OpenGl_TextParam* AspectTextParams() const
226 {
227 return TextParam_applied;
228 }
2166f0fa 229
34a44cbd 230 //! Clear the applied aspect state.
231 void ResetAppliedAspect();
232
bf75be98 233 Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
234 Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)& theTexture,
235 const Handle(Graphic3d_TextureParams)& theParams = NULL);
8625ef7e 236 const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
bf75be98 237
4269bd1b 238 //! Set filter for restricting rendering of particular elements.
239 //! Filter can be applied for rendering passes used by recursive
240 //! rendering algorithms for rendering elements of groups.
241 //! @param theFilter [in] the filter instance.
242 inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
243 {
5322131b 244 myRenderFilter = theFilter;
4269bd1b 245 }
246
247 //! Get rendering filter.
248 //! @return filter instance.
5322131b 249 inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
250 {
251 return myRenderFilter;
4269bd1b 252 }
253
254 //! @return applied view matrix.
255 inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
256
257 //! @return applied model structure matrix.
258 inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
259
550f3b8b 260 //! Sets and applies current polygon offset.
261 void SetPolygonOffset (int theMode, Standard_ShortReal theFactor, Standard_ShortReal theUnits);
262
263 //! Returns currently applied polygon offset params.
264 const TEL_POFFSET_PARAM& AppliedPolygonOffset() { return PolygonOffset_applied; }
265
b7cd4ba7 266 //! @return true if clipping algorithm enabled
267 inline Standard_Boolean IsCullingEnabled() const { return myIsCullingEnabled; }
268
f8ae3605 269 //! Returns capping algorithm rendering filter.
270 const Handle(OpenGl_CappingAlgoFilter)& DefaultCappingAlgoFilter() const
271 {
272 return myDefaultCappingAlgoFilter;
273 }
274
275 //! Returns face aspect for none culling mode.
276 const OpenGl_AspectFace& NoneCulling() const
277 {
278 return myNoneCulling;
279 }
280
281 //! Returns face aspect for front face culling mode.
282 const OpenGl_AspectFace& FrontCulling() const
283 {
284 return myFrontCulling;
285 }
286
2166f0fa
SK
287protected:
288
679ecdee 289 //! Copy content of Back buffer to the Front buffer
290 void copyBackToFront();
2166f0fa 291
62e1beed 292 //! Initialize blit quad.
293 Standard_EXPORT OpenGl_VertexBuffer* initBlitQuad (const Standard_Boolean theToFlip);
294
38a0206f 295 //! Blit image from/to specified buffers.
62e1beed 296 Standard_EXPORT bool blitBuffers (OpenGl_FrameBuffer* theReadFbo,
297 OpenGl_FrameBuffer* theDrawFbo,
298 const Standard_Boolean theToFlip = Standard_False);
38a0206f 299
62e1beed 300 Standard_EXPORT virtual Standard_Boolean Activate();
2166f0fa 301
38a0206f 302 void redraw1 (const Graphic3d_CView& theCView,
38a0206f 303 OpenGl_FrameBuffer* theReadDrawFbo,
304 const Graphic3d_Camera::Projection theProjection);
305
306 //! Setup default FBO.
307 void bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo = NULL);
308
309 //! Blend together views pair into stereo image.
f978241f 310 void drawStereoPair (const Graphic3d_CView& theCView);
ee51a9fe 311
38a0206f 312 //! Blit snapshot containing main scene (myMainSceneFbos or BackBuffer)
313 //! into presentation buffer (myMainSceneFbos -> offscreen FBO or myMainSceneFbos -> BackBuffer or BackBuffer -> FrontBuffer),
ee51a9fe 314 //! and redraw immediate structures on top.
315 //!
316 //! When scene caching is disabled (myTransientDrawToFront, no double buffer in window, etc.),
317 //! the first step (blitting) will be skipped.
318 //!
319 //! @return false if immediate structures has been rendered directly into FrontBuffer and Buffer Swap should not be called.
320 bool redrawImmediate (const Graphic3d_CView& theCView,
38a0206f 321 OpenGl_FrameBuffer* theReadFbo,
322 const Graphic3d_Camera::Projection theProjection,
323 OpenGl_FrameBuffer* theDrawFbo,
ee51a9fe 324 const Standard_Boolean theIsPartialUpdate = Standard_False);
2166f0fa 325
0adbd30f 326 void updateMaterial (const int theFlag);
2166f0fa 327
bf75be98 328 void setTextureParams (Handle(OpenGl_Texture)& theTexture,
329 const Handle(Graphic3d_TextureParams)& theParams);
2166f0fa 330
91c60b57 331protected: //! @name protected fields
265d4508 332
38a0206f 333 //! Two framebuffers (left and right views) store cached main presentation
334 //! of the view (without presentation of immediate layers).
335 Handle(OpenGl_FrameBuffer) myMainSceneFbos[2];
336 //! Additional buffers for immediate layer in stereo mode.
337 Handle(OpenGl_FrameBuffer) myImmediateSceneFbos[2];
338 //! Special flag which is invalidated when myMainSceneFbos can not be blitted for some reason (e.g. driver bugs).
ee51a9fe 339 Standard_Boolean myHasFboBlit;
b86bb3df 340
62e1beed 341 //! Flag to draw result image upside-down
342 Standard_Boolean myToFlipOutput;
343
b86bb3df 344 //! Vertices for full-screen quad rendering.
345 OpenGl_VertexBuffer myFullScreenQuad;
62e1beed 346 OpenGl_VertexBuffer myFullScreenQuadFlip;
b86bb3df 347
a174a3c5 348 Handle(OpenGl_PrinterContext) myPrintContext;
73192b37 349 Handle(OpenGl_View) myView;
350 Handle(OpenGl_LineAttributes) myLineAttribs;
a272ed94 351 Standard_Integer myViewId;
73192b37 352 Standard_Integer myAntiAliasingMode;
1981cb22 353 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
679ecdee 354 Standard_Boolean myBackBufferRestored;
355 Standard_Boolean myIsImmediateDrawn; //!< flag indicates that immediate mode buffer contains some data
bf75be98 356 Standard_Boolean myUseZBuffer;
eae454e3 357 Standard_Boolean myUseDepthWrite;
bf75be98 358 Standard_Boolean myUseGLLight;
b7cd4ba7 359 Standard_Boolean myIsCullingEnabled; //!< frustum culling flag
2166f0fa 360
a89742cf 361 unsigned int myFrameCounter; //!< redraw counter, for debugging
362
f8ae3605 363 Handle(OpenGl_CappingAlgoFilter) myDefaultCappingAlgoFilter;
364 OpenGl_AspectFace myNoneCulling;
365 OpenGl_AspectFace myFrontCulling;
366
bf75be98 367protected: //! @name fields related to status
368
4269bd1b 369 Handle(OpenGl_RenderFilter) myRenderFilter;
bf75be98 370 Handle(OpenGl_Texture) myTextureBound; //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
2166f0fa
SK
371 const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied;
372 const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied;
373 const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
374 const OpenGl_AspectText *AspectText_set, *AspectText_applied;
375
376 const OpenGl_TextParam *TextParam_set, *TextParam_applied;
377
378 const OpenGl_Matrix* ViewMatrix_applied;
379 const OpenGl_Matrix* StructureMatrix_applied;
380
3b1817a9 381 OpenGl_Material myMatFront; //!< current front material state (cached to reduce GL context updates)
382 OpenGl_Material myMatBack; //!< current back material state
383 OpenGl_Material myMatTmp; //!< temporary variable
384 TelCullMode myCullingMode; //!< back face culling mode, applied from face aspect
0adbd30f 385
550f3b8b 386 OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
0f8c0fb8 387
550f3b8b 388 TEL_POFFSET_PARAM PolygonOffset_applied; //!< Currently applied polygon offset.
2166f0fa 389
550f3b8b 390 OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
bf75be98 391
392public: //! @name type definition
2166f0fa 393
ec357c5c 394 DEFINE_STANDARD_RTTI(OpenGl_Workspace, OpenGl_Window)
1c35b92f 395 DEFINE_STANDARD_ALLOC
2166f0fa
SK
396
397};
398
bf75be98 399#endif // _OpenGl_Workspace_Header