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