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