0031172: Unexpected result of BRepTools::OuterWire()
[occt.git] / src / OpenGl / OpenGl_View.hxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 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_View_Header
17#define _OpenGl_View_Header
18
25e59720 19#include <Standard_Transient.hxx>
ec357c5c 20#include <Standard_Type.hxx>
2166f0fa
SK
21
22#include <TColStd_Array2OfReal.hxx>
23#include <NCollection_List.hxx>
189f85a3 24#include <math_BullardGenerator.hxx>
2166f0fa
SK
25
26#include <Quantity_NameOfColor.hxx>
27#include <Aspect_FillMethod.hxx>
28#include <Aspect_GradientFillMethod.hxx>
2166f0fa 29
2166f0fa 30#include <Graphic3d_CView.hxx>
d325cb7f 31#include <Graphic3d_CullingTool.hxx>
a79f67f8 32#include <Graphic3d_GraduatedTrihedron.hxx>
51b10cd4 33#include <Graphic3d_SequenceOfHClipPlane.hxx>
eb85ed36 34#include <Graphic3d_ToneMappingMethod.hxx>
c357e426 35#include <Graphic3d_TypeOfShadingModel.hxx>
825aa485 36#include <Graphic3d_WorldViewProjState.hxx>
c5751993 37#include <Graphic3d_ZLayerSettings.hxx>
2166f0fa 38
bf5f0ca2 39#include <OpenGl_Aspects.hxx>
0b0320e7 40#include <OpenGl_BackgroundArray.hxx>
c357e426 41#include <OpenGl_Context.hxx>
42#include <OpenGl_FrameBuffer.hxx>
15669413 43#include <OpenGl_FrameStatsPrs.hxx>
c357e426 44#include <OpenGl_GraduatedTrihedron.hxx>
59f45b7c 45#include <OpenGl_LayerList.hxx>
73192b37 46#include <OpenGl_LineAttributes.hxx>
c357e426 47#include <OpenGl_SceneGeometry.hxx>
48#include <OpenGl_Structure.hxx>
c357e426 49#include <OpenGl_Window.hxx>
50#include <OpenGl_Workspace.hxx>
3a9b5dc8 51#include <OpenGl_TileSampler.hxx>
2166f0fa 52
c357e426 53#include <map>
54#include <set>
2166f0fa 55
b5ac8292 56struct OpenGl_Matrix;
2166f0fa 57
c357e426 58class Graphic3d_StructureManager;
59class OpenGl_GraphicDriver;
67312b79 60class OpenGl_PBREnvironment;
c357e426 61class OpenGl_StateCounter;
d84e8669 62class OpenGl_ShadowMap;
63class OpenGl_ShadowMapArray;
c357e426 64class OpenGl_TriangleSet;
65class OpenGl_Workspace;
c04c30b3 66class OpenGl_View;
d84e8669 67
c357e426 68DEFINE_STANDARD_HANDLE(OpenGl_View,Graphic3d_CView)
c04c30b3 69
c357e426 70//! Implementation of OpenGl view.
71class OpenGl_View : public Graphic3d_CView
2166f0fa 72{
c357e426 73
74public:
75
76 //! Constructor.
77 Standard_EXPORT OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
78 const Handle(OpenGl_GraphicDriver)& theDriver,
79 const Handle(OpenGl_Caps)& theCaps,
c357e426 80 OpenGl_StateCounter* theCounter);
81
82 //! Default destructor.
83 Standard_EXPORT virtual ~OpenGl_View();
2166f0fa 84
ba00aab7 85 //! Release OpenGL resources.
62e1beed 86 Standard_EXPORT void ReleaseGlResources (const Handle(OpenGl_Context)& theCtx);
bf75be98 87
c357e426 88 //! Deletes and erases the view.
89 Standard_EXPORT virtual void Remove() Standard_OVERRIDE;
90
91 //! @param theDrawToFrontBuffer Advanced option to modify rendering mode:
92 //! 1. TRUE. Drawing immediate mode structures directly to the front buffer over the scene image.
93 //! Fast, so preferred for interactive work (used by default).
94 //! However these extra drawings will be missed in image dump since it is performed from back buffer.
95 //! Notice that since no pre-buffering used the V-Sync will be ignored and rendering could be seen
96 //! in run-time (in case of slow hardware) and/or tearing may appear.
97 //! So this is strongly recommended to draw only simple (fast) structures.
98 //! 2. FALSE. Drawing immediate mode structures to the back buffer.
99 //! The complete scene is redrawn first, so this mode is slower if scene contains complex data and/or V-Sync
100 //! is turned on. But it works in any case and is especially useful for view dump because the dump image is read
101 //! from the back buffer.
102 //! @return previous mode.
103 Standard_EXPORT Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer) Standard_OVERRIDE;
104
105 //! Creates and maps rendering window to the view.
c357e426 106 //! @param theWindow [in] the window.
107 //! @param theContext [in] the rendering context. If NULL the context will be created internally.
a521d90d 108 Standard_EXPORT virtual void SetWindow (const Handle(Aspect_Window)& theWindow,
109 const Aspect_RenderingContext theContext) Standard_OVERRIDE;
c357e426 110
111 //! Returns window associated with the view.
79104795 112 virtual Handle(Aspect_Window) Window() const Standard_OVERRIDE
113 { return myWindow->PlatformWindow(); }
c357e426 114
115 //! Returns True if the window associated to the view is defined.
79104795 116 virtual Standard_Boolean IsDefined() const Standard_OVERRIDE
117 { return !myWindow.IsNull(); }
c357e426 118
119 //! Handle changing size of the rendering window.
120 Standard_EXPORT virtual void Resized() Standard_OVERRIDE;
121
122 //! Redraw content of the view.
123 Standard_EXPORT virtual void Redraw() Standard_OVERRIDE;
124
125 //! Redraw immediate content of the view.
126 Standard_EXPORT virtual void RedrawImmediate() Standard_OVERRIDE;
127
128 //! Marks BVH tree for given priority list as dirty and marks primitive set for rebuild.
129 Standard_EXPORT virtual void Invalidate() Standard_OVERRIDE;
130
131 //! Return true if view content cache has been invalidated.
d2eddacc 132 virtual Standard_Boolean IsInvalidated() Standard_OVERRIDE { return !myBackBufferRestored; }
c357e426 133
c357e426 134 //! Dump active rendering buffer into specified memory buffer.
38d90bb3 135 //! In Ray-Tracing allow to get a raw HDR buffer using Graphic3d_BT_RGB_RayTraceHdrLeft buffer type,
136 //! only Left view will be dumped ignoring stereoscopic parameter.
c357e426 137 Standard_EXPORT virtual Standard_Boolean BufferDump (Image_PixMap& theImage,
138 const Graphic3d_BufferType& theBufferType) Standard_OVERRIDE;
139
c357e426 140 //! Marks BVH tree and the set of BVH primitives of correspondent priority list with id theLayerId as outdated.
8f138407 141 Standard_EXPORT virtual void InvalidateBVHData (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
c357e426 142
1c728f2d 143 //! Add a layer to the view.
144 //! @param theNewLayerId [in] id of new layer, should be > 0 (negative values are reserved for default layers).
145 //! @param theSettings [in] new layer settings
146 //! @param theLayerAfter [in] id of layer to append new layer before
147 Standard_EXPORT virtual void InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
148 const Graphic3d_ZLayerSettings& theSettings,
149 const Graphic3d_ZLayerId theLayerAfter) Standard_OVERRIDE;
150
151 //! Add a layer to the view.
152 //! @param theNewLayerId [in] id of new layer, should be > 0 (negative values are reserved for default layers).
153 //! @param theSettings [in] new layer settings
154 //! @param theLayerBefore [in] id of layer to append new layer after
155 Standard_EXPORT virtual void InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
156 const Graphic3d_ZLayerSettings& theSettings,
157 const Graphic3d_ZLayerId theLayerBefore) Standard_OVERRIDE;
c357e426 158
159 //! Remove a z layer with the given ID.
160 Standard_EXPORT virtual void RemoveZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
2166f0fa 161
c357e426 162 //! Sets the settings for a single Z layer of specified view.
163 Standard_EXPORT virtual void SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
164 const Graphic3d_ZLayerSettings& theSettings) Standard_OVERRIDE;
b5ac8292 165
50d06d8f 166 //! Returns the maximum Z layer ID.
167 //! First layer ID is Graphic3d_ZLayerId_Default, last ID is ZLayerMax().
168 Standard_EXPORT virtual Standard_Integer ZLayerMax() const Standard_OVERRIDE;
169
1c728f2d 170 //! Returns the list of layers.
171 Standard_EXPORT virtual const NCollection_List<Handle(Graphic3d_Layer)>& Layers() const Standard_OVERRIDE;
50d06d8f 172
1c728f2d 173 //! Returns layer with given ID or NULL if undefined.
174 Standard_EXPORT virtual Handle(Graphic3d_Layer) Layer (const Graphic3d_ZLayerId theLayerId) const Standard_OVERRIDE;
175
176 //! Returns the bounding box of all structures displayed in the view.
177 //! If theToIncludeAuxiliary is TRUE, then the boundary box also includes minimum and maximum limits
178 //! of graphical elements forming parts of infinite and other auxiliary structures.
3fe9ce0e 179 //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
180 //! @return computed bounding box
1c728f2d 181 Standard_EXPORT virtual Bnd_Box MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const Standard_OVERRIDE;
50d06d8f 182
c357e426 183 //! Returns pointer to an assigned framebuffer object.
b128c892 184 Standard_EXPORT virtual Handle(Standard_Transient) FBO() const Standard_OVERRIDE;
2166f0fa 185
c357e426 186 //! Sets framebuffer object for offscreen rendering.
b128c892 187 Standard_EXPORT virtual void SetFBO (const Handle(Standard_Transient)& theFbo) Standard_OVERRIDE;
2166f0fa 188
c357e426 189 //! Generate offscreen FBO in the graphic library.
190 //! If not supported on hardware returns NULL.
b128c892 191 Standard_EXPORT virtual Handle(Standard_Transient) FBOCreate (const Standard_Integer theWidth,
192 const Standard_Integer theHeight) Standard_OVERRIDE;
2166f0fa 193
c357e426 194 //! Remove offscreen FBO from the graphic library
b128c892 195 Standard_EXPORT virtual void FBORelease (Handle(Standard_Transient)& theFbo) Standard_OVERRIDE;
2166f0fa 196
c357e426 197 //! Read offscreen FBO configuration.
b128c892 198 Standard_EXPORT virtual void FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 199 Standard_Integer& theWidth,
200 Standard_Integer& theHeight,
201 Standard_Integer& theWidthMax,
202 Standard_Integer& theHeightMax) Standard_OVERRIDE;
203
204 //! Change offscreen FBO viewport.
b128c892 205 Standard_EXPORT virtual void FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 206 const Standard_Integer theWidth,
207 const Standard_Integer theHeight) Standard_OVERRIDE;
2166f0fa 208
c357e426 209public:
2166f0fa 210
c357e426 211 //! Returns gradient background fill colors.
212 Standard_EXPORT virtual Aspect_GradientBackground GradientBackground() const Standard_OVERRIDE;
2166f0fa 213
c357e426 214 //! Sets gradient background fill colors.
215 Standard_EXPORT virtual void SetGradientBackground (const Aspect_GradientBackground& theBackground) Standard_OVERRIDE;
216
99289bed 217 //! Returns background image texture map.
218 virtual Handle(Graphic3d_TextureMap) BackgroundImage() Standard_OVERRIDE { return myBackgroundImage; }
c357e426 219
99289bed 220 //! Sets image texture or environment cubemap as backround.
221 //! @param theTextureMap [in] source to set a background;
222 //! should be either Graphic3d_Texture2D or Graphic3d_CubeMap
223 //! @param theToUpdatePBREnv [in] defines whether IBL maps will be generated or not
224 //! (see GeneratePBREnvironment())
225 Standard_EXPORT virtual void SetBackgroundImage (const Handle(Graphic3d_TextureMap)& theTextureMap,
226 Standard_Boolean theToUpdatePBREnv = Standard_True) Standard_OVERRIDE;
c357e426 227
228 //! Returns background image fill style.
229 Standard_EXPORT virtual Aspect_FillMethod BackgroundImageStyle() const Standard_OVERRIDE;
230
231 //! Sets background image fill style.
232 Standard_EXPORT virtual void SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle) Standard_OVERRIDE;
233
077a220c 234 //! Returns cubemap being set last time on background.
235 Standard_EXPORT Handle(Graphic3d_CubeMap) BackgroundCubeMap() const Standard_OVERRIDE;
236
67312b79 237 //! Generates PBR specular probe and irradiance map
238 //! in order to provide environment indirect illumination in PBR shading model (Image Based Lighting).
239 //! The source of environment data is background cubemap.
240 //! If PBR is unavailable it does nothing.
241 //! If PBR is available but there is no cubemap being set to background it clears all IBL maps (see 'ClearPBREnvironment').
242 virtual void GeneratePBREnvironment() Standard_OVERRIDE { myPBREnvRequest = OpenGl_PBREnvRequest_BAKE; }
243
244 //! Fills PBR specular probe and irradiance map with white color.
245 //! So that environment indirect illumination will be constant
246 //! and will be fully controlled by ambient light sources.
247 //! If PBR is unavailable it does nothing.
248 virtual void ClearPBREnvironment() Standard_OVERRIDE { myPBREnvRequest = OpenGl_PBREnvRequest_CLEAR; }
249
250 //! Returns number of mipmap levels used in specular IBL map.
251 //! 0 if PBR environment is not created.
252 Standard_EXPORT unsigned int SpecIBLMapLevels() const;
077a220c 253
c357e426 254 //! Returns environment texture set for the view.
0f57ab75 255 virtual Handle(Graphic3d_TextureEnv) TextureEnv() const Standard_OVERRIDE { return myTextureEnvData; }
c357e426 256
257 //! Sets environment texture for the view.
258 Standard_EXPORT virtual void SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv) Standard_OVERRIDE;
259
c357e426 260 //! Return backfacing model used for the view.
261 virtual Graphic3d_TypeOfBackfacingModel BackfacingModel() const Standard_OVERRIDE { return myBackfacing; }
262
263 //! Sets backfacing model for the view.
264 virtual void SetBackfacingModel (const Graphic3d_TypeOfBackfacingModel theModel) Standard_OVERRIDE { myBackfacing = theModel; }
265
7c3ef2f7 266 //! Returns local camera origin currently set for rendering, might be modified during rendering.
267 const gp_XYZ& LocalOrigin() const { return myLocalOrigin; }
268
269 //! Setup local camera origin currently set for rendering.
270 Standard_EXPORT void SetLocalOrigin (const gp_XYZ& theOrigin);
271
c357e426 272 //! Returns list of lights of the view.
992ed6b3 273 virtual const Handle(Graphic3d_LightSet)& Lights() const Standard_OVERRIDE { return myLights; }
c357e426 274
275 //! Sets list of lights for the view.
992ed6b3 276 virtual void SetLights (const Handle(Graphic3d_LightSet)& theLights) Standard_OVERRIDE
c357e426 277 {
278 myLights = theLights;
279 myCurrLightSourceState = myStateCounter->Increment();
280 }
281
282 //! Returns list of clip planes set for the view.
3202bf1e 283 virtual const Handle(Graphic3d_SequenceOfHClipPlane)& ClipPlanes() const Standard_OVERRIDE { return myClipPlanes; }
c357e426 284
285 //! Sets list of clip planes for the view.
3202bf1e 286 virtual void SetClipPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) Standard_OVERRIDE { myClipPlanes = thePlanes; }
c357e426 287
26d9c835 288 //! Fill in the dictionary with diagnostic info.
289 //! Should be called within rendering thread.
290 //!
291 //! This API should be used only for user output or for creating automated reports.
292 //! The format of returned information (e.g. key-value layout)
293 //! is NOT part of this API and can be changed at any time.
294 //! Thus application should not parse returned information to weed out specific parameters.
295 Standard_EXPORT virtual void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
296 Graphic3d_DiagnosticInfo theFlags) const Standard_OVERRIDE;
297
79b544e6 298 //! Returns string with statistic performance info.
299 Standard_EXPORT virtual TCollection_AsciiString StatisticInformation() const Standard_OVERRIDE;
300
301 //! Fills in the dictionary with statistic performance info.
302 Standard_EXPORT virtual void StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const Standard_OVERRIDE;
303
c357e426 304public:
305
306 //! Returns background color.
b6472664 307 const Quantity_ColorRGBA& BackgroundColor() const { return myBgColor; }
c357e426 308
c357e426 309 //! Change graduated trihedron.
310 OpenGl_GraduatedTrihedron& ChangeGraduatedTrihedron() { return myGraduatedTrihedron; }
311
312 void SetTextureEnv (const Handle(OpenGl_Context)& theCtx,
313 const Handle(Graphic3d_TextureEnv)& theTexture);
314
2166f0fa 315 void SetBackgroundTextureStyle (const Aspect_FillMethod FillStyle);
c357e426 316
2166f0fa 317 void SetBackgroundGradient (const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod AType);
2166f0fa 318
c357e426 319 void SetBackgroundGradientType (const Aspect_GradientFillMethod AType);
2166f0fa 320
e276548b 321 //! Returns list of OpenGL Z-layers.
322 const OpenGl_LayerList& LayerList() const { return myZLayers; }
323
c357e426 324 //! Returns OpenGL window implementation.
5e30547b 325 const Handle(OpenGl_Window)& GlWindow() const { return myWindow; }
e276548b 326
c357e426 327 //! Returns OpenGL environment map.
cc8cbabe 328 const Handle(OpenGl_TextureSet)& GlTextureEnv() const { return myTextureEnv; }
e276548b 329
b7cd4ba7 330 //! Returns selector for BVH tree, providing a possibility to store information
331 //! about current view volume and to detect which objects are overlapping it.
d325cb7f 332 const Graphic3d_CullingTool& BVHTreeSelector() const { return myBVHSelector; }
b7cd4ba7 333
a1954302 334 //! Returns true if there are immediate structures to display
335 bool HasImmediateStructures() const
336 {
c3282ec1 337 return myZLayers.NbImmediateStructures() != 0;
a1954302 338 }
339
d325cb7f 340public: //! @name obsolete Graduated Trihedron functionality
341
342 //! Displays Graduated Trihedron.
343 Standard_EXPORT virtual void GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData) Standard_OVERRIDE;
344
345 //! Erases Graduated Trihedron.
346 Standard_EXPORT virtual void GraduatedTrihedronErase() Standard_OVERRIDE;
347
348 //! Sets minimum and maximum points of scene bounding box for Graduated Trihedron stored in graphic view object.
349 //! @param theMin [in] the minimum point of scene.
350 //! @param theMax [in] the maximum point of scene.
351 Standard_EXPORT virtual void GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax) Standard_OVERRIDE;
352
c357e426 353protected: //! @name Internal methods for managing GL resources
354
355 //! Initializes OpenGl resource for environment texture.
356 void initTextureEnv (const Handle(OpenGl_Context)& theContext);
357
a521d90d 358protected: //! @name low-level redrawing sub-routines
c357e426 359
4b7fee8f 360 //! Prepare frame buffers for rendering.
361 Standard_EXPORT virtual bool prepareFrameBuffers (Graphic3d_Camera::Projection& theProj);
362
c357e426 363 //! Redraws view for the given monographic camera projection, or left/right eye.
a521d90d 364 Standard_EXPORT virtual void redraw (const Graphic3d_Camera::Projection theProjection,
a1073ae2 365 OpenGl_FrameBuffer* theReadDrawFbo,
366 OpenGl_FrameBuffer* theOitAccumFbo);
2166f0fa 367
c357e426 368 //! Redraws view for the given monographic camera projection, or left/right eye.
369 //!
370 //! Method will blit snapshot containing main scene (myMainSceneFbos or BackBuffer)
371 //! into presentation buffer (myMainSceneFbos -> offscreen FBO or
372 //! myMainSceneFbos -> BackBuffer or BackBuffer -> FrontBuffer),
373 //! and redraw immediate structures on top.
374 //!
375 //! When scene caching is disabled (myTransientDrawToFront, no double buffer in window, etc.),
376 //! the first step (blitting) will be skipped.
377 //!
378 //! @return false if immediate structures has been rendered directly into FrontBuffer
379 //! and Buffer Swap should not be called.
a521d90d 380 Standard_EXPORT virtual bool redrawImmediate (const Graphic3d_Camera::Projection theProjection,
381 OpenGl_FrameBuffer* theReadFbo,
382 OpenGl_FrameBuffer* theDrawFbo,
a1073ae2 383 OpenGl_FrameBuffer* theOitAccumFbo,
a521d90d 384 const Standard_Boolean theIsPartialUpdate = Standard_False);
b5ac8292 385
c357e426 386 //! Blit image from/to specified buffers.
a521d90d 387 Standard_EXPORT bool blitBuffers (OpenGl_FrameBuffer* theReadFbo,
388 OpenGl_FrameBuffer* theDrawFbo,
389 const Standard_Boolean theToFlip = Standard_False);
2166f0fa 390
c357e426 391 //! Setup default FBO.
a521d90d 392 Standard_EXPORT void bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo = NULL);
2166f0fa 393
c357e426 394protected: //! @name Rendering of GL graphics (with prepared drawing buffer).
2166f0fa 395
d84e8669 396 //! Renders the graphical contents of the view into the preprepared shadowmap framebuffer.
397 //! @param theShadowMap [in] the framebuffer for rendering shadowmap.
398 Standard_EXPORT virtual void renderShadowMap (const Handle(OpenGl_ShadowMap)& theShadowMap);
399
c357e426 400 //! Renders the graphical contents of the view into the preprepared window or framebuffer.
401 //! @param theProjection [in] the projection that should be used for rendering.
402 //! @param theReadDrawFbo [in] the framebuffer for rendering graphics.
a1073ae2 403 //! @param theOitAccumFbo [in] the framebuffer for accumulating color and coverage for OIT process.
c357e426 404 //! @param theToDrawImmediate [in] the flag indicates whether the rendering performs in immediate mode.
a521d90d 405 Standard_EXPORT virtual void render (Graphic3d_Camera::Projection theProjection,
406 OpenGl_FrameBuffer* theReadDrawFbo,
a1073ae2 407 OpenGl_FrameBuffer* theOitAccumFbo,
a521d90d 408 const Standard_Boolean theToDrawImmediate);
2166f0fa 409
c357e426 410 //! Renders the graphical scene.
bf02aa7d 411 //! @param theProjection [in] the projection that is used for rendering.
c357e426 412 //! @param theReadDrawFbo [in] the framebuffer for rendering graphics.
a1073ae2 413 //! @param theOitAccumFbo [in] the framebuffer for accumulating color and coverage for OIT process.
c357e426 414 //! @param theToDrawImmediate [in] the flag indicates whether the rendering performs in immediate mode.
bf02aa7d 415 Standard_EXPORT virtual void renderScene (Graphic3d_Camera::Projection theProjection,
416 OpenGl_FrameBuffer* theReadDrawFbo,
a1073ae2 417 OpenGl_FrameBuffer* theOitAccumFbo,
a521d90d 418 const Standard_Boolean theToDrawImmediate);
419
420 //! Draw background (gradient / image)
cdc54fb0 421 Standard_EXPORT virtual void drawBackground (const Handle(OpenGl_Workspace)& theWorkspace,
422 Graphic3d_Camera::Projection theProjection);
2166f0fa 423
c357e426 424 //! Render set of structures presented in the view.
bf02aa7d 425 //! @param theProjection [in] the projection that is used for rendering.
c357e426 426 //! @param theReadDrawFbo [in] the framebuffer for rendering graphics.
a1073ae2 427 //! @param theOitAccumFbo [in] the framebuffer for accumulating color and coverage for OIT process.
c357e426 428 //! @param theToDrawImmediate [in] the flag indicates whether the rendering performs in immediate mode.
bf02aa7d 429 Standard_EXPORT virtual void renderStructs (Graphic3d_Camera::Projection theProjection,
430 OpenGl_FrameBuffer* theReadDrawFbo,
a1073ae2 431 OpenGl_FrameBuffer* theOitAccumFbo,
a521d90d 432 const Standard_Boolean theToDrawImmediate);
2166f0fa 433
c357e426 434 //! Renders trihedron.
435 void renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace);
2166f0fa 436
15669413 437 //! Renders frame statistics.
438 void renderFrameStats();
439
c357e426 440private:
392ac980 441
c357e426 442 //! Adds the structure to display lists of the view.
443 Standard_EXPORT virtual void displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
444 const Standard_Integer thePriority) Standard_OVERRIDE;
445
446 //! Erases the structure from display lists of the view.
447 Standard_EXPORT virtual void eraseStructure (const Handle(Graphic3d_CStructure)& theStructure) Standard_OVERRIDE;
448
449 //! Change Z layer of a structure already presented in view.
450 Standard_EXPORT virtual void changeZLayer (const Handle(Graphic3d_CStructure)& theCStructure,
451 const Graphic3d_ZLayerId theNewLayerId) Standard_OVERRIDE;
452
453 //! Changes the priority of a structure within its Z layer in the specified view.
454 Standard_EXPORT virtual void changePriority (const Handle(Graphic3d_CStructure)& theCStructure,
455 const Standard_Integer theNewPriority) Standard_OVERRIDE;
456
a521d90d 457private:
458
ba00aab7 459 //! Release sRGB resources (frame-buffers, textures, etc.).
460 void releaseSrgbResources (const Handle(OpenGl_Context)& theCtx);
461
a521d90d 462 //! Copy content of Back buffer to the Front buffer.
a0b49de4 463 bool copyBackToFront();
a521d90d 464
465 //! Initialize blit quad.
466 OpenGl_VertexBuffer* initBlitQuad (const Standard_Boolean theToFlip);
467
468 //! Blend together views pair into stereo image.
3c4b62a4 469 void drawStereoPair (OpenGl_FrameBuffer* theDrawFbo);
a521d90d 470
a1073ae2 471 //! Check and update OIT compatibility with current OpenGL context's state.
472 bool checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
473 const Standard_Boolean theMSAA);
474
475 //! Chooses compatible internal color format for OIT frame buffer.
476 bool chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
477 const Standard_Integer theConfigIndex,
478 OpenGl_ColorFormats& theFormats);
479
c357e426 480protected:
481
482 OpenGl_GraphicDriver* myDriver;
483 Handle(OpenGl_Window) myWindow;
484 Handle(OpenGl_Workspace) myWorkspace;
485 Handle(OpenGl_Caps) myCaps;
c357e426 486 Standard_Boolean myWasRedrawnGL;
487
c357e426 488 Graphic3d_TypeOfBackfacingModel myBackfacing;
3202bf1e 489 Handle(Graphic3d_SequenceOfHClipPlane) myClipPlanes;
7c3ef2f7 490 gp_XYZ myLocalOrigin;
b128c892 491 Handle(OpenGl_FrameBuffer) myFBO;
c357e426 492 Standard_Boolean myToShowGradTrihedron;
99289bed 493 Handle(Graphic3d_TextureMap) myBackgroundImage;
c357e426 494 Handle(Graphic3d_TextureEnv) myTextureEnvData;
495 Graphic3d_GraduatedTrihedron myGTrihedronData;
496
992ed6b3 497 Handle(Graphic3d_LightSet) myNoShadingLight;
498 Handle(Graphic3d_LightSet) myLights;
c357e426 499 OpenGl_LayerList myZLayers; //!< main list of displayed structure, sorted by layers
500
501 Graphic3d_WorldViewProjState myWorldViewProjState; //!< camera modification state
502 OpenGl_StateCounter* myStateCounter;
503 Standard_Size myCurrLightSourceState;
992ed6b3 504 Standard_Size myLightsRevision;
392ac980 505
506 typedef std::pair<Standard_Size, Standard_Size> StateInfo;
507
508 StateInfo myLastOrientationState;
509 StateInfo myLastViewMappingState;
510 StateInfo myLastLightSourceState;
30f0ad28 511
b7cd4ba7 512 //! Is needed for selection of overlapping objects and storage of the current view volume
d325cb7f 513 Graphic3d_CullingTool myBVHSelector;
b7cd4ba7 514
c357e426 515 OpenGl_GraduatedTrihedron myGraduatedTrihedron;
15669413 516 OpenGl_FrameStatsPrs myFrameStatsPrs;
c357e426 517
cc8cbabe 518 Handle(OpenGl_TextureSet) myTextureEnv;
c357e426 519
3a9b5dc8 520 //! Framebuffers for OpenGL output.
521 Handle(OpenGl_FrameBuffer) myOpenGlFBO;
522 Handle(OpenGl_FrameBuffer) myOpenGlFBO2;
523
c357e426 524protected: //! @name Rendering properties
525
526 //! Two framebuffers (left and right views) store cached main presentation
527 //! of the view (without presentation of immediate layers).
ba00aab7 528 Standard_Integer mySRgbState; //!< track sRGB state
3c4b62a4 529 GLint myFboColorFormat; //!< sized format for color attachments
530 GLint myFboDepthFormat; //!< sized format for depth-stencil attachments
a1073ae2 531 OpenGl_ColorFormats myFboOitColorConfig; //!< selected color format configuration for OIT color attachments
c357e426 532 Handle(OpenGl_FrameBuffer) myMainSceneFbos[2];
a1073ae2 533 Handle(OpenGl_FrameBuffer) myMainSceneFbosOit[2]; //!< Additional buffers for transparent draw of main layer.
534 Handle(OpenGl_FrameBuffer) myImmediateSceneFbos[2]; //!< Additional buffers for immediate layer in stereo mode.
535 Handle(OpenGl_FrameBuffer) myImmediateSceneFbosOit[2]; //!< Additional buffers for transparency draw of immediate layer.
b40cdc2b 536 Handle(OpenGl_FrameBuffer) myXrSceneFbo; //!< additional FBO (without MSAA) for submitting to XR
d84e8669 537 Handle(OpenGl_ShadowMapArray) myShadowMaps; //!< additional FBOs for shadow map rendering
c357e426 538 OpenGl_VertexBuffer myFullScreenQuad; //!< Vertices for full-screen quad rendering.
539 OpenGl_VertexBuffer myFullScreenQuadFlip;
540 Standard_Boolean myToFlipOutput; //!< Flag to draw result image upside-down
541 unsigned int myFrameCounter; //!< redraw counter, for debugging
7ccf8676 542 Standard_Boolean myHasFboBlit; //!< disable FBOs on failure
a1073ae2 543 Standard_Boolean myToDisableOIT; //!< disable OIT on failure
544 Standard_Boolean myToDisableOITMSAA; //!< disable OIT with MSAA on failure
7ccf8676 545 Standard_Boolean myToDisableMSAA; //!< disable MSAA after failure
c357e426 546 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
547 Standard_Boolean myBackBufferRestored;
548 Standard_Boolean myIsImmediateDrawn; //!< flag indicates that immediate mode buffer contains some data
549
0b0320e7 550protected: //! @name Background parameters
551
077a220c 552 OpenGl_Aspects* myTextureParams; //!< Stores texture and its parameters for textured background
553 OpenGl_Aspects* myCubeMapParams; //!< Stores cubemap and its parameters for cubemap background
67312b79 554 Handle(Graphic3d_CubeMap) myBackgroundCubeMap; //!< Cubemap has been set as background
555 Graphic3d_TypeOfBackground myBackgroundType; //!< Current type of background
077a220c 556 OpenGl_BackgroundArray* myBackgrounds[Graphic3d_TypeOfBackground_NB]; //!< Array of primitive arrays of different background types
0b0320e7 557
67312b79 558protected: //! @name methods related to PBR
559
560 //! Checks whether PBR is available.
561 Standard_EXPORT Standard_Boolean checkPBRAvailability() const;
562
563 //! Generates IBL maps used in PBR pipeline.
564 //! If background cubemap is not set clears all IBL maps.
565 Standard_EXPORT void bakePBREnvironment (const Handle(OpenGl_Context)& theCtx);
566
567 //! Fills all IBL maps with white color.
568 //! So that environment lighting is considered to be constant and is completely controls by ambient light sources.
569 Standard_EXPORT void clearPBREnvironment (const Handle(OpenGl_Context)& theCtx);
570
571 //! Process requests to generate or to clear PBR environment.
572 Standard_EXPORT void processPBREnvRequest (const Handle(OpenGl_Context)& theCtx);
573
574protected: //! @name fields and types related to PBR
575
576 //! State of PBR environment.
577 enum PBREnvironmentState
578 {
579 OpenGl_PBREnvState_NONEXISTENT,
580 OpenGl_PBREnvState_UNAVAILABLE, // indicates failed try to create PBR environment
581 OpenGl_PBREnvState_CREATED
582 };
583
584 //! Type of action which can be done with PBR environment.
585 enum PBREnvironmentRequest
586 {
587 OpenGl_PBREnvRequest_NONE,
588 OpenGl_PBREnvRequest_BAKE,
589 OpenGl_PBREnvRequest_CLEAR
590 };
591
592 Handle(OpenGl_PBREnvironment) myPBREnvironment; //!< manager of IBL maps used in PBR pipeline
593 PBREnvironmentState myPBREnvState; //!< state of PBR environment
594 PBREnvironmentRequest myPBREnvRequest; //!< type of action is requested to be done with PBR environment
595
91c60b57 596protected: //! @name data types related to ray-tracing
597
598 //! Result of OpenGL shaders initialization.
599 enum RaytraceInitStatus
600 {
601 OpenGl_RT_NONE,
602 OpenGl_RT_INIT,
603 OpenGl_RT_FAIL
604 };
605
606 //! Describes update mode (state).
607 enum RaytraceUpdateMode
608 {
609 OpenGl_GUM_CHECK, //!< check geometry state
610 OpenGl_GUM_PREPARE, //!< collect unchanged objects
611 OpenGl_GUM_REBUILD //!< rebuild changed and new objects
612 };
613
614 //! Defines frequently used shader variables.
615 enum ShaderVariableIndex
616 {
617 OpenGl_RT_aPosition,
618
189f85a3 619 // camera position
91c60b57 620 OpenGl_RT_uOriginLT,
621 OpenGl_RT_uOriginLB,
622 OpenGl_RT_uOriginRT,
623 OpenGl_RT_uOriginRB,
624 OpenGl_RT_uDirectLT,
625 OpenGl_RT_uDirectLB,
626 OpenGl_RT_uDirectRT,
627 OpenGl_RT_uDirectRB,
3a9b5dc8 628 OpenGl_RT_uViewPrMat,
91c60b57 629 OpenGl_RT_uUnviewMat,
630
189f85a3 631 // 3D scene params
91c60b57 632 OpenGl_RT_uSceneRad,
633 OpenGl_RT_uSceneEps,
634 OpenGl_RT_uLightAmbnt,
635 OpenGl_RT_uLightCount,
636
189f85a3 637 // background params
638 OpenGl_RT_uBackColorTop,
639 OpenGl_RT_uBackColorBot,
91c60b57 640
189f85a3 641 // ray-tracing params
642 OpenGl_RT_uShadowsEnabled,
643 OpenGl_RT_uReflectEnabled,
89d855ba 644 OpenGl_RT_uEnvMapEnabled,
645 OpenGl_RT_uEnvMapForBack,
189f85a3 646 OpenGl_RT_uTexSamplersArray,
8c820969 647 OpenGl_RT_uBlockedRngEnabled,
189f85a3 648
3a9b5dc8 649 // size of render window
650 OpenGl_RT_uWinSizeX,
651 OpenGl_RT_uWinSizeY,
652
189f85a3 653 // sampled frame params
383c6c9f 654 OpenGl_RT_uAccumSamples,
189f85a3 655 OpenGl_RT_uFrameRndSeed,
656
657 // adaptive FSAA params
91c60b57 658 OpenGl_RT_uOffsetX,
659 OpenGl_RT_uOffsetY,
660 OpenGl_RT_uSamples,
91c60b57 661
b09447ed 662 // images used by ISS mode
3a9b5dc8 663 OpenGl_RT_uRenderImage,
e084dbbc 664 OpenGl_RT_uTilesImage,
3a9b5dc8 665 OpenGl_RT_uOffsetImage,
66d1cdc6 666 OpenGl_RT_uTileSize,
667 OpenGl_RT_uVarianceScaleFactor,
3a9b5dc8 668
b09447ed 669 // maximum radiance value
670 OpenGl_RT_uMaxRadiance,
671
91c60b57 672 OpenGl_RT_NbVariables // special field
673 };
674
3a9b5dc8 675 //! Defines OpenGL image samplers.
676 enum ShaderImageNames
677 {
66d1cdc6 678 OpenGl_RT_OutputImage = 0,
679 OpenGl_RT_VisualErrorImage = 1,
680 OpenGl_RT_TileOffsetsImage = 2,
e084dbbc 681 OpenGl_RT_TileSamplesImage = 3
91c60b57 682 };
683
684 //! Tool class for management of shader sources.
685 class ShaderSource
686 {
687 public:
688
189f85a3 689 //! Default shader prefix - empty string.
690 static const TCollection_AsciiString EMPTY_PREFIX;
691
91c60b57 692 //! Creates new uninitialized shader source.
693 ShaderSource()
694 {
695 //
696 }
697
73722cc9 698 public:
189f85a3 699
73722cc9 700 //! Returns error description in case of load fail.
701 const TCollection_AsciiString& ErrorDescription() const
702 {
703 return myError;
91c60b57 704 }
705
91c60b57 706 //! Returns prefix to insert before the source.
707 const TCollection_AsciiString& Prefix() const
708 {
709 return myPrefix;
710 }
711
712 //! Sets prefix to insert before the source.
713 void SetPrefix (const TCollection_AsciiString& thePrefix)
714 {
715 myPrefix = thePrefix;
716 }
717
718 //! Returns shader source combined with prefix.
719 TCollection_AsciiString Source() const;
720
721 //! Loads shader source from specified files.
ee5befae 722 Standard_Boolean LoadFromFiles (const TCollection_AsciiString* theFileNames, const TCollection_AsciiString& thePrefix = EMPTY_PREFIX);
723
724 //! Loads shader source from specified strings.
725 Standard_Boolean LoadFromStrings (const TCollection_AsciiString* theStrings, const TCollection_AsciiString& thePrefix = EMPTY_PREFIX);
91c60b57 726
727 private:
728
729 TCollection_AsciiString mySource; //!< Source string of the shader object
730 TCollection_AsciiString myPrefix; //!< Prefix to insert before the source
73722cc9 731 TCollection_AsciiString myError; //!< error state
91c60b57 732
733 };
734
735 //! Default ray-tracing depth.
736 static const Standard_Integer THE_DEFAULT_NB_BOUNCES = 3;
737
738 //! Default size of traversal stack.
f2474958 739 static const Standard_Integer THE_DEFAULT_STACK_SIZE = 10;
91c60b57 740
741 //! Compile-time ray-tracing parameters.
742 struct RaytracingParams
743 {
744 //! Actual size of traversal stack in shader program.
745 Standard_Integer StackSize;
746
747 //! Actual ray-tracing depth (number of ray bounces).
748 Standard_Integer NbBounces;
749
f483f2ed 750 //! Enables/disables light propagation through transparent media.
91c60b57 751 Standard_Boolean TransparentShadows;
752
189f85a3 753 //! Enables/disables global illumination (GI) effects.
754 Standard_Boolean GlobalIllumination;
755
f483f2ed 756 //! Enables/disables the use of OpenGL bindless textures.
757 Standard_Boolean UseBindlessTextures;
758
b4327ba8 759 //! Enables/disables two-sided BSDF models instead of one-sided.
760 Standard_Boolean TwoSidedBsdfModels;
761
3a9b5dc8 762 //! Enables/disables adaptive screen sampling for path tracing.
763 Standard_Boolean AdaptiveScreenSampling;
764
e084dbbc 765 //! Enables/disables 1-pass atomic mode for AdaptiveScreenSampling.
766 Standard_Boolean AdaptiveScreenSamplingAtomic;
767
b09447ed 768 //! Enables/disables environment map for background.
769 Standard_Boolean UseEnvMapForBackground;
770
78607702 771 //! Enables/disables normal map ignoring during path tracing.
772 Standard_Boolean ToIgnoreNormalMap;
773
b09447ed 774 //! Maximum radiance value used for clamping radiance estimation.
775 Standard_ShortReal RadianceClampingValue;
eb85ed36 776
b27ab03d 777 //! Enables/disables depth-of-field effect (path tracing, perspective camera).
778 Standard_Boolean DepthOfField;
779
89d855ba 780 //! Enables/disables cubemap backgraund.
781 Standard_Boolean CubemapForBack;
782
eb85ed36 783 //! Tone mapping method for path tracing.
784 Graphic3d_ToneMappingMethod ToneMappingMethod;
4eaaf9d8 785
91c60b57 786 //! Creates default compile-time ray-tracing parameters.
787 RaytracingParams()
b09447ed 788 : StackSize (THE_DEFAULT_STACK_SIZE),
789 NbBounces (THE_DEFAULT_NB_BOUNCES),
b4327ba8 790 TransparentShadows (Standard_False),
791 GlobalIllumination (Standard_False),
792 UseBindlessTextures (Standard_False),
793 TwoSidedBsdfModels (Standard_False),
b09447ed 794 AdaptiveScreenSampling (Standard_False),
e084dbbc 795 AdaptiveScreenSamplingAtomic (Standard_False),
b09447ed 796 UseEnvMapForBackground (Standard_False),
78607702 797 ToIgnoreNormalMap (Standard_False),
4eaaf9d8 798 RadianceClampingValue (30.0),
b27ab03d 799 DepthOfField (Standard_False),
89d855ba 800 CubemapForBack (Standard_False),
eb85ed36 801 ToneMappingMethod (Graphic3d_ToneMappingMethod_Disabled) { }
91c60b57 802 };
803
d4aaad5b 804 //! Describes state of OpenGL structure.
805 struct StructState
806 {
807 Standard_Size StructureState;
808 Standard_Size InstancedState;
809
810 //! Creates new structure state.
811 StructState (const Standard_Size theStructureState = 0,
812 const Standard_Size theInstancedState = 0)
813 : StructureState (theStructureState),
814 InstancedState (theInstancedState)
815 {
816 //
817 }
818
819 //! Creates new structure state.
820 StructState (const OpenGl_Structure* theStructure)
821 {
822 StructureState = theStructure->ModificationState();
823
824 InstancedState = theStructure->InstancedStructure() != NULL ?
825 theStructure->InstancedStructure()->ModificationState() : 0;
826 }
827 };
828
91c60b57 829protected: //! @name methods related to ray-tracing
830
831 //! Updates 3D scene geometry for ray-tracing.
832 Standard_Boolean updateRaytraceGeometry (const RaytraceUpdateMode theMode,
833 const Standard_Integer theViewId,
834 const Handle(OpenGl_Context)& theGlContext);
835
836 //! Updates 3D scene light sources for ray-tracing.
837 Standard_Boolean updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext);
838
91c60b57 839 //! Checks to see if the OpenGL structure is modified.
840 Standard_Boolean toUpdateStructure (const OpenGl_Structure* theStructure);
841
842 //! Adds OpenGL structure to ray-traced scene geometry.
843 Standard_Boolean addRaytraceStructure (const OpenGl_Structure* theStructure,
844 const Handle(OpenGl_Context)& theGlContext);
845
846 //! Adds OpenGL groups to ray-traced scene geometry.
8c820969 847 Standard_Boolean addRaytraceGroups (const OpenGl_Structure* theStructure,
848 const OpenGl_RaytraceMaterial& theStructMat,
7f24b768 849 const Handle(TopLoc_Datum3D)& theTrsf,
8c820969 850 const Handle(OpenGl_Context)& theGlContext);
91c60b57 851
852 //! Creates ray-tracing material properties.
bf5f0ca2 853 OpenGl_RaytraceMaterial convertMaterial (const OpenGl_Aspects* theAspect,
91c60b57 854 const Handle(OpenGl_Context)& theGlContext);
855
856 //! Adds OpenGL primitive array to ray-traced scene geometry.
f5b72419 857 Handle(OpenGl_TriangleSet) addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
858 const Standard_Integer theMatID,
859 const OpenGl_Mat4* theTrans);
91c60b57 860
861 //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
862 Standard_Boolean addRaytraceVertexIndices (OpenGl_TriangleSet& theSet,
863 const Standard_Integer theMatID,
864 const Standard_Integer theCount,
865 const Standard_Integer theOffset,
866 const OpenGl_PrimitiveArray& theArray);
867
868 //! Adds OpenGL triangle array to ray-traced scene geometry.
869 Standard_Boolean addRaytraceTriangleArray (OpenGl_TriangleSet& theSet,
870 const Standard_Integer theMatID,
871 const Standard_Integer theCount,
872 const Standard_Integer theOffset,
873 const Handle(Graphic3d_IndexBuffer)& theIndices);
874
875 //! Adds OpenGL triangle fan array to ray-traced scene geometry.
876 Standard_Boolean addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet,
877 const Standard_Integer theMatID,
878 const Standard_Integer theCount,
879 const Standard_Integer theOffset,
880 const Handle(Graphic3d_IndexBuffer)& theIndices);
881
882 //! Adds OpenGL triangle strip array to ray-traced scene geometry.
883 Standard_Boolean addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet,
884 const Standard_Integer theMatID,
885 const Standard_Integer theCount,
886 const Standard_Integer theOffset,
887 const Handle(Graphic3d_IndexBuffer)& theIndices);
888
889 //! Adds OpenGL quadrangle array to ray-traced scene geometry.
890 Standard_Boolean addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet,
891 const Standard_Integer theMatID,
892 const Standard_Integer theCount,
893 const Standard_Integer theOffset,
894 const Handle(Graphic3d_IndexBuffer)& theIndices);
895
896 //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
897 Standard_Boolean addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet,
898 const Standard_Integer theMatID,
899 const Standard_Integer theCount,
900 const Standard_Integer theOffset,
901 const Handle(Graphic3d_IndexBuffer)& theIndices);
902
903 //! Adds OpenGL polygon array to ray-traced scene geometry.
904 Standard_Boolean addRaytracePolygonArray (OpenGl_TriangleSet& theSet,
905 const Standard_Integer theMatID,
906 const Standard_Integer theCount,
907 const Standard_Integer theOffset,
908 const Handle(Graphic3d_IndexBuffer)& theIndices);
909
910 //! Uploads ray-trace data to the GPU.
911 Standard_Boolean uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext);
912
913 //! Generates shader prefix based on current ray-tracing options.
914 TCollection_AsciiString generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const;
915
916 //! Performs safe exit when shaders initialization fails.
917 Standard_Boolean safeFailBack (const TCollection_ExtendedString& theMessage,
918 const Handle(OpenGl_Context)& theGlContext);
919
920 //! Loads and compiles shader object from specified source.
921 Handle(OpenGl_ShaderObject) initShader (const GLenum theType,
922 const ShaderSource& theSource,
923 const Handle(OpenGl_Context)& theGlContext);
924
189f85a3 925 //! Creates shader program from the given vertex and fragment shaders.
926 Handle(OpenGl_ShaderProgram) initProgram (const Handle(OpenGl_Context)& theGlContext,
927 const Handle(OpenGl_ShaderObject)& theVertShader,
d95f5ce1 928 const Handle(OpenGl_ShaderObject)& theFragShader,
929 const TCollection_AsciiString& theName);
189f85a3 930
91c60b57 931 //! Initializes OpenGL/GLSL shader programs.
66d1cdc6 932 Standard_Boolean initRaytraceResources (const Standard_Integer theSizeX,
933 const Standard_Integer theSizeY,
934 const Handle(OpenGl_Context)& theGlContext);
91c60b57 935
936 //! Releases OpenGL/GLSL shader programs.
d877e610 937 void releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext,
938 const Standard_Boolean theToRebuild = Standard_False);
91c60b57 939
bf02aa7d 940 //! Updates auxiliary OpenGL frame buffers.
941 Standard_Boolean updateRaytraceBuffers (const Standard_Integer theSizeX,
91c60b57 942 const Standard_Integer theSizeY,
943 const Handle(OpenGl_Context)& theGlContext);
944
945 //! Generates viewing rays for corners of screen quad.
b27ab03d 946 //! (ray tracing; path tracing for orthographic camera)
91c60b57 947 void updateCamera (const OpenGl_Mat4& theOrientation,
948 const OpenGl_Mat4& theViewMapping,
949 OpenGl_Vec3* theOrigins,
950 OpenGl_Vec3* theDirects,
1d865689 951 OpenGl_Mat4& theView,
91c60b57 952 OpenGl_Mat4& theUnView);
953
b27ab03d 954 //! Generate viewing rays (path tracing, perspective camera).
955 void updatePerspCameraPT(const OpenGl_Mat4& theOrientation,
956 const OpenGl_Mat4& theViewMapping,
957 Graphic3d_Camera::Projection theProjection,
958 OpenGl_Mat4& theViewPr,
959 OpenGl_Mat4& theUnview,
960 const int theWinSizeX,
961 const int theWinSizeY);
962
91c60b57 963 //! Binds ray-trace textures to corresponding texture units.
66d1cdc6 964 void bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext,
965 int theStereoView);
91c60b57 966
967 //! Unbinds ray-trace textures from corresponding texture unit.
968 void unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext);
969
970 //! Sets uniform state for the given ray-tracing shader program.
3a9b5dc8 971 Standard_Boolean setUniformState (const Standard_Integer theProgramId,
972 const Standard_Integer theSizeX,
973 const Standard_Integer theSizeY,
b27ab03d 974 Graphic3d_Camera::Projection theProjection,
91c60b57 975 const Handle(OpenGl_Context)& theGlContext);
976
977 //! Runs ray-tracing shader programs.
c357e426 978 Standard_Boolean runRaytraceShaders (const Standard_Integer theSizeX,
91c60b57 979 const Standard_Integer theSizeY,
bf02aa7d 980 Graphic3d_Camera::Projection theProjection,
38a0206f 981 OpenGl_FrameBuffer* theReadDrawFbo,
91c60b57 982 const Handle(OpenGl_Context)& theGlContext);
983
3a9b5dc8 984 //! Runs classical (Whitted-style) ray-tracing kernel.
985 Standard_Boolean runRaytrace (const Standard_Integer theSizeX,
986 const Standard_Integer theSizeY,
987 Graphic3d_Camera::Projection theProjection,
988 OpenGl_FrameBuffer* theReadDrawFbo,
989 const Handle(OpenGl_Context)& theGlContext);
990
991 //! Runs path tracing (global illumination) kernel.
4eaaf9d8 992 Standard_Boolean runPathtrace (const Standard_Integer theSizeX,
993 const Standard_Integer theSizeY,
994 Graphic3d_Camera::Projection theProjection,
4eaaf9d8 995 const Handle(OpenGl_Context)& theGlContext);
3a9b5dc8 996
66d1cdc6 997 //! Runs path tracing (global illumination) kernel.
998 Standard_Boolean runPathtraceOut (Graphic3d_Camera::Projection theProjection,
999 OpenGl_FrameBuffer* theReadDrawFbo,
1000 const Handle(OpenGl_Context)& theGlContext);
1001
3a9b5dc8 1002 //! Redraws the window using OpenGL/GLSL ray-tracing or path tracing.
c357e426 1003 Standard_Boolean raytrace (const Standard_Integer theSizeX,
91c60b57 1004 const Standard_Integer theSizeY,
bf02aa7d 1005 Graphic3d_Camera::Projection theProjection,
38a0206f 1006 OpenGl_FrameBuffer* theReadDrawFbo,
91c60b57 1007 const Handle(OpenGl_Context)& theGlContext);
1008
1009protected: //! @name fields related to ray-tracing
1010
3a9b5dc8 1011 //! Result of RT/PT shaders initialization.
91c60b57 1012 RaytraceInitStatus myRaytraceInitStatus;
1013
3a9b5dc8 1014 //! Is ray-tracing geometry data valid?
91c60b57 1015 Standard_Boolean myIsRaytraceDataValid;
1016
3a9b5dc8 1017 //! True if warning about missing extension GL_ARB_bindless_texture has been displayed.
91c60b57 1018 Standard_Boolean myIsRaytraceWarnTextures;
1019
1020 //! 3D scene geometry data for ray-tracing.
1021 OpenGl_RaytraceGeometry myRaytraceGeometry;
1022
f5b72419 1023 //! Builder for triangle set.
1024 opencascade::handle<BVH_Builder<Standard_ShortReal, 3> > myRaytraceBVHBuilder;
1025
91c60b57 1026 //! Compile-time ray-tracing parameters.
1027 RaytracingParams myRaytraceParameters;
1028
1029 //! Radius of bounding sphere of the scene.
1030 Standard_ShortReal myRaytraceSceneRadius;
1031 //! Scene epsilon to prevent self-intersections.
1032 Standard_ShortReal myRaytraceSceneEpsilon;
1033
1034 //! OpenGL/GLSL source of ray-tracing fragment shader.
1035 ShaderSource myRaytraceShaderSource;
1036 //! OpenGL/GLSL source of adaptive-AA fragment shader.
1037 ShaderSource myPostFSAAShaderSource;
3a9b5dc8 1038 //! OpenGL/GLSL source of RT/PT display fragment shader.
1039 ShaderSource myOutImageShaderSource;
91c60b57 1040
1041 //! OpenGL/GLSL ray-tracing fragment shader.
1042 Handle(OpenGl_ShaderObject) myRaytraceShader;
1043 //! OpenGL/GLSL adaptive-AA fragment shader.
1044 Handle(OpenGl_ShaderObject) myPostFSAAShader;
3a9b5dc8 1045 //! OpenGL/GLSL ray-tracing display fragment shader.
1046 Handle(OpenGl_ShaderObject) myOutImageShader;
91c60b57 1047
1048 //! OpenGL/GLSL ray-tracing shader program.
1049 Handle(OpenGl_ShaderProgram) myRaytraceProgram;
1050 //! OpenGL/GLSL adaptive-AA shader program.
1051 Handle(OpenGl_ShaderProgram) myPostFSAAProgram;
189f85a3 1052 //! OpenGL/GLSL program for displaying texture.
1053 Handle(OpenGl_ShaderProgram) myOutImageProgram;
91c60b57 1054
1055 //! Texture buffer of data records of bottom-level BVH nodes.
1056 Handle(OpenGl_TextureBufferArb) mySceneNodeInfoTexture;
1057 //! Texture buffer of minimum points of bottom-level BVH nodes.
1058 Handle(OpenGl_TextureBufferArb) mySceneMinPointTexture;
1059 //! Texture buffer of maximum points of bottom-level BVH nodes.
1060 Handle(OpenGl_TextureBufferArb) mySceneMaxPointTexture;
1061 //! Texture buffer of transformations of high-level BVH nodes.
1062 Handle(OpenGl_TextureBufferArb) mySceneTransformTexture;
1063
1064 //! Texture buffer of vertex coords.
1065 Handle(OpenGl_TextureBufferArb) myGeometryVertexTexture;
1066 //! Texture buffer of vertex normals.
1067 Handle(OpenGl_TextureBufferArb) myGeometryNormalTexture;
1068 //! Texture buffer of vertex UV coords.
1069 Handle(OpenGl_TextureBufferArb) myGeometryTexCrdTexture;
1070 //! Texture buffer of triangle indices.
1071 Handle(OpenGl_TextureBufferArb) myGeometryTriangTexture;
1072
1073 //! Texture buffer of material properties.
1074 Handle(OpenGl_TextureBufferArb) myRaytraceMaterialTexture;
1075 //! Texture buffer of light source properties.
1076 Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture;
1077
1078 //! 1st framebuffer (FBO) to perform adaptive FSAA.
3a9b5dc8 1079 //! Used in compatibility mode (no adaptive sampling).
bf02aa7d 1080 Handle(OpenGl_FrameBuffer) myRaytraceFBO1[2];
91c60b57 1081 //! 2nd framebuffer (FBO) to perform adaptive FSAA.
3a9b5dc8 1082 //! Used in compatibility mode (no adaptive sampling).
bf02aa7d 1083 Handle(OpenGl_FrameBuffer) myRaytraceFBO2[2];
3a9b5dc8 1084
1085 //! Output textures (2 textures are used in stereo mode).
1086 //! Used if adaptive screen sampling is activated.
1087 Handle(OpenGl_Texture) myRaytraceOutputTexture[2];
1088
b27ab03d 1089 //! Texture containing per-tile visual error estimation (2 textures are used in stereo mode).
3a9b5dc8 1090 //! Used if adaptive screen sampling is activated.
b27ab03d 1091 Handle(OpenGl_Texture) myRaytraceVisualErrorTexture[2];
1092 //! Texture containing offsets of sampled screen tiles (2 textures are used in stereo mode).
3a9b5dc8 1093 //! Used if adaptive screen sampling is activated.
b27ab03d 1094 Handle(OpenGl_Texture) myRaytraceTileOffsetsTexture[2];
e084dbbc 1095 //! Texture containing amount of extra per-tile samples (2 textures are used in stereo mode).
1096 //! Used if adaptive screen sampling is activated.
1097 Handle(OpenGl_Texture) myRaytraceTileSamplesTexture[2];
91c60b57 1098
1099 //! Vertex buffer (VBO) for drawing dummy quad.
1100 OpenGl_VertexBuffer myRaytraceScreenQuad;
1101
1102 //! Cached locations of frequently used uniform variables.
1103 Standard_Integer myUniformLocations[2][OpenGl_RT_NbVariables];
1104
1105 //! State of OpenGL structures reflected to ray-tracing.
d4aaad5b 1106 std::map<const OpenGl_Structure*, StructState> myStructureStates;
91c60b57 1107
1108 //! PrimitiveArray to TriangleSet map for scene partial update.
1109 std::map<Standard_Size, OpenGl_TriangleSet*> myArrayToTrianglesMap;
1110
189f85a3 1111 //! Set of IDs of non-raytracable elements (to detect updates).
1112 std::set<Standard_Integer> myNonRaytraceStructureIDs;
1113
91c60b57 1114 //! Marks if environment map should be updated.
1115 Standard_Boolean myToUpdateEnvironmentMap;
1116
1117 //! State of OpenGL layer list.
bd6a8454 1118 Standard_Size myRaytraceLayerListState;
189f85a3 1119
1120 //! Number of accumulated frames (for progressive rendering).
1121 Standard_Integer myAccumFrames;
1122
1123 //! Stored ray origins used for detection of camera movements.
1124 OpenGl_Vec3 myPreviousOrigins[3];
1125
1126 //! Bullard RNG to produce random sequence.
1127 math_BullardGenerator myRNG;
91c60b57 1128
3a9b5dc8 1129 //! Tool object for sampling screen tiles in PT mode.
1130 OpenGl_TileSampler myTileSampler;
1131
b27ab03d 1132 //! Camera position used for projective mode
1133 OpenGl_Vec3 myEyeOrig;
1134
1135 //! Camera view direction used for projective mode
1136 OpenGl_Vec3 myEyeView;
1137
1138 //! Camera's screen vertical direction used for projective mode
1139 OpenGl_Vec3 myEyeVert;
1140
1141 //! Camera's screen horizontal direction used for projective mode
1142 OpenGl_Vec3 myEyeSide;
1143
1144 //! Camera's screen size used for projective mode
1145 OpenGl_Vec2 myEyeSize;
1146
1147 //! Aperture radius of camera on previous frame used for depth-of-field (path tracing)
1148 float myPrevCameraApertureRadius;
1149
1150 //! Focal distance of camera on previous frame used for depth-of-field (path tracing)
1151 float myPrevCameraFocalPlaneDist;
1152
679ecdee 1153public:
1154
1c35b92f 1155 DEFINE_STANDARD_ALLOC
92efcf78 1156 DEFINE_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView) // Type definition
679ecdee 1157
c357e426 1158 friend class OpenGl_GraphicDriver;
a89742cf 1159 friend class OpenGl_Workspace;
a1073ae2 1160 friend class OpenGl_LayerList;
15669413 1161 friend class OpenGl_FrameStats;
2166f0fa
SK
1162};
1163
679ecdee 1164#endif // _OpenGl_View_Header