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