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