1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_View_Header
17 #define _OpenGl_View_Header
19 #include <MMgt_TShared.hxx>
20 #include <Standard_Type.hxx>
22 #include <TColStd_Array2OfReal.hxx>
23 #include <NCollection_List.hxx>
24 #include <math_BullardGenerator.hxx>
26 #include <Quantity_NameOfColor.hxx>
27 #include <Aspect_FillMethod.hxx>
28 #include <Aspect_GradientFillMethod.hxx>
29 #include <Aspect_TypeOfTriedronPosition.hxx>
31 #include <Graphic3d_CView.hxx>
32 #include <Graphic3d_GraduatedTrihedron.hxx>
33 #include <Graphic3d_SequenceOfHClipPlane.hxx>
34 #include <Graphic3d_TypeOfShadingModel.hxx>
35 #include <Graphic3d_WorldViewProjState.hxx>
36 #include <Graphic3d_ZLayerSettings.hxx>
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_GraduatedTrihedron.hxx>
44 #include <OpenGl_LayerList.hxx>
45 #include <OpenGl_Light.hxx>
46 #include <OpenGl_LineAttributes.hxx>
47 #include <OpenGl_SceneGeometry.hxx>
48 #include <OpenGl_Structure.hxx>
49 #include <OpenGl_Trihedron.hxx>
50 #include <OpenGl_Window.hxx>
51 #include <OpenGl_Workspace.hxx>
58 class Graphic3d_StructureManager;
59 class OpenGl_GraphicDriver;
60 class OpenGl_StateCounter;
61 class OpenGl_RaytraceMaterial;
62 class OpenGl_TriangleSet;
63 class OpenGl_Workspace;
65 DEFINE_STANDARD_HANDLE(OpenGl_View,Graphic3d_CView)
67 //! Implementation of OpenGl view.
68 class OpenGl_View : public Graphic3d_CView
74 Standard_EXPORT OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
75 const Handle(OpenGl_GraphicDriver)& theDriver,
76 const Handle(OpenGl_Caps)& theCaps,
77 Standard_Boolean& theDeviceLostFlag,
78 OpenGl_StateCounter* theCounter);
80 //! Default destructor.
81 Standard_EXPORT virtual ~OpenGl_View();
83 Standard_EXPORT void ReleaseGlResources (const Handle(OpenGl_Context)& theCtx);
85 //! Deletes and erases the view.
86 Standard_EXPORT virtual void Remove() Standard_OVERRIDE;
88 //! @param theDrawToFrontBuffer Advanced option to modify rendering mode:
89 //! 1. TRUE. Drawing immediate mode structures directly to the front buffer over the scene image.
90 //! Fast, so preferred for interactive work (used by default).
91 //! However these extra drawings will be missed in image dump since it is performed from back buffer.
92 //! Notice that since no pre-buffering used the V-Sync will be ignored and rendering could be seen
93 //! in run-time (in case of slow hardware) and/or tearing may appear.
94 //! So this is strongly recommended to draw only simple (fast) structures.
95 //! 2. FALSE. Drawing immediate mode structures to the back buffer.
96 //! The complete scene is redrawn first, so this mode is slower if scene contains complex data and/or V-Sync
97 //! is turned on. But it works in any case and is especially useful for view dump because the dump image is read
98 //! from the back buffer.
99 //! @return previous mode.
100 Standard_EXPORT Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer) Standard_OVERRIDE;
102 //! Creates and maps rendering window to the view.
103 //! @param theWindow [in] the window.
104 //! @param theContext [in] the rendering context. If NULL the context will be created internally.
105 Standard_EXPORT virtual void SetWindow (const Handle(Aspect_Window)& theWindow,
106 const Aspect_RenderingContext theContext) Standard_OVERRIDE;
108 //! Returns window associated with the view.
109 virtual Handle(Aspect_Window) Window() const Standard_OVERRIDE
110 { return myWindow->PlatformWindow(); }
112 //! Returns True if the window associated to the view is defined.
113 virtual Standard_Boolean IsDefined() const Standard_OVERRIDE
114 { return !myWindow.IsNull(); }
116 //! Handle changing size of the rendering window.
117 Standard_EXPORT virtual void Resized() Standard_OVERRIDE;
119 //! Redraw content of the view.
120 Standard_EXPORT virtual void Redraw() Standard_OVERRIDE;
122 //! Redraw immediate content of the view.
123 Standard_EXPORT virtual void RedrawImmediate() Standard_OVERRIDE;
125 //! Marks BVH tree for given priority list as dirty and marks primitive set for rebuild.
126 Standard_EXPORT virtual void Invalidate() Standard_OVERRIDE;
128 //! Return true if view content cache has been invalidated.
129 virtual Standard_Boolean IsInvalidated() Standard_OVERRIDE { return !myBackBufferRestored; }
131 //! Displays z-buffer trihedron.
132 Standard_EXPORT virtual void TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition = Aspect_TOTP_CENTER,
133 const Quantity_NameOfColor theColor = Quantity_NOC_WHITE,
134 const Standard_Real theScale = 0.02,
135 const Standard_Boolean theAsWireframe = Standard_True) Standard_OVERRIDE;
137 //! Erases z-buffer trihedron.
138 Standard_EXPORT virtual void TriedronErase() Standard_OVERRIDE;
140 //! Setup parameters of z-buffer trihedron.
141 Standard_EXPORT virtual void ZBufferTriedronSetup (const Quantity_NameOfColor theXColor = Quantity_NOC_RED,
142 const Quantity_NameOfColor theYColor = Quantity_NOC_GREEN,
143 const Quantity_NameOfColor theZColor = Quantity_NOC_BLUE1,
144 const Standard_Real theSizeRatio = 0.8,
145 const Standard_Real theAxisDiametr = 0.05,
146 const Standard_Integer theNbFacettes = 12) Standard_OVERRIDE;
148 //! Displays trihedron echo.
149 Standard_EXPORT virtual void TriedronEcho (const Aspect_TypeOfTriedronEcho theType = Aspect_TOTE_NONE) Standard_OVERRIDE;
151 //! Returns data of a graduated trihedron
152 const Graphic3d_GraduatedTrihedron& GetGraduatedTrihedron() Standard_OVERRIDE
153 { return myGTrihedronData; }
155 //! Displays Graduated Trihedron.
156 Standard_EXPORT virtual void GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData) Standard_OVERRIDE;
158 //! Erases Graduated Trihedron.
159 Standard_EXPORT virtual void GraduatedTrihedronErase() Standard_OVERRIDE;
161 //! Sets minimum and maximum points of scene bounding box for Graduated Trihedron stored in graphic view object.
162 //! @param theMin [in] the minimum point of scene.
163 //! @param theMax [in] the maximum point of scene.
164 Standard_EXPORT virtual void GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax) Standard_OVERRIDE;
166 //! Dump active rendering buffer into specified memory buffer.
167 Standard_EXPORT virtual Standard_Boolean BufferDump (Image_PixMap& theImage,
168 const Graphic3d_BufferType& theBufferType) Standard_OVERRIDE;
170 //! Export scene into the one of the Vector graphics formats (SVG, PS, PDF...).
171 //! In contrast to Bitmaps, Vector graphics is scalable (so you may got quality benefits
172 //! on printing to laser printer). Notice however that results may differ a lot and
173 //! do not contain some elements.
174 Standard_EXPORT virtual Standard_Boolean Export (const Standard_CString theFileName,
175 const Graphic3d_ExportFormat theFormat,
176 const Graphic3d_SortType theSortType = Graphic3d_ST_BSP_Tree) Standard_OVERRIDE;
178 //! Marks BVH tree and the set of BVH primitives of correspondent priority list with id theLayerId as outdated.
179 Standard_EXPORT virtual void InvalidateBVHData (const Standard_Integer theLayerId) Standard_OVERRIDE;
181 //! Insert a new top-level z layer with the given ID.
182 Standard_EXPORT virtual void AddZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
184 //! Remove a z layer with the given ID.
185 Standard_EXPORT virtual void RemoveZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
187 //! Sets the settings for a single Z layer of specified view.
188 Standard_EXPORT virtual void SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
189 const Graphic3d_ZLayerSettings& theSettings) Standard_OVERRIDE;
191 //! Returns the maximum Z layer ID.
192 //! First layer ID is Graphic3d_ZLayerId_Default, last ID is ZLayerMax().
193 Standard_EXPORT virtual Standard_Integer ZLayerMax() const Standard_OVERRIDE;
195 //! Returns the bounding box of all structures displayed in the Z layer.
196 //! Never fails. If Z layer does not exist nothing happens.
197 Standard_EXPORT virtual void InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const Standard_OVERRIDE;
199 //! Returns the bounding box of all structures displayed in the Z layer.
200 //! If Z layer does not exist the empty box is returned.
201 //! @param theLayerId layer identifier
202 //! @param theCamera camera definition
203 //! @param theWindowWidth viewport width (for applying transformation-persistence)
204 //! @param theWindowHeight viewport height (for applying transformation-persistence)
205 //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
206 //! @return computed bounding box
207 Standard_EXPORT virtual Graphic3d_BndBox4f ZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId,
208 const Handle(Graphic3d_Camera)& theCamera,
209 const Standard_Integer theWindowWidth,
210 const Standard_Integer theWindowHeight,
211 const Standard_Boolean theToIncludeAuxiliary) const Standard_OVERRIDE;
213 //! Returns pointer to an assigned framebuffer object.
214 Standard_EXPORT virtual Handle(Standard_Transient) FBO() const Standard_OVERRIDE;
216 //! Sets framebuffer object for offscreen rendering.
217 Standard_EXPORT virtual void SetFBO (const Handle(Standard_Transient)& theFbo) Standard_OVERRIDE;
219 //! Generate offscreen FBO in the graphic library.
220 //! If not supported on hardware returns NULL.
221 Standard_EXPORT virtual Handle(Standard_Transient) FBOCreate (const Standard_Integer theWidth,
222 const Standard_Integer theHeight) Standard_OVERRIDE;
224 //! Remove offscreen FBO from the graphic library
225 Standard_EXPORT virtual void FBORelease (Handle(Standard_Transient)& theFbo) Standard_OVERRIDE;
227 //! Read offscreen FBO configuration.
228 Standard_EXPORT virtual void FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
229 Standard_Integer& theWidth,
230 Standard_Integer& theHeight,
231 Standard_Integer& theWidthMax,
232 Standard_Integer& theHeightMax) Standard_OVERRIDE;
234 //! Change offscreen FBO viewport.
235 Standard_EXPORT virtual void FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
236 const Standard_Integer theWidth,
237 const Standard_Integer theHeight) Standard_OVERRIDE;
241 //! Returns background fill color.
242 Standard_EXPORT virtual Aspect_Background Background() const Standard_OVERRIDE;
244 //! Sets background fill color.
245 Standard_EXPORT virtual void SetBackground (const Aspect_Background& theBackground) Standard_OVERRIDE;
247 //! Returns gradient background fill colors.
248 Standard_EXPORT virtual Aspect_GradientBackground GradientBackground() const Standard_OVERRIDE;
250 //! Sets gradient background fill colors.
251 Standard_EXPORT virtual void SetGradientBackground (const Aspect_GradientBackground& theBackground) Standard_OVERRIDE;
253 //! Returns background image texture file path.
254 Standard_EXPORT virtual TCollection_AsciiString BackgroundImage() Standard_OVERRIDE { return myBackgroundImagePath; }
256 //! Sets background image texture file path.
257 Standard_EXPORT virtual void SetBackgroundImage (const TCollection_AsciiString& theFilePath) Standard_OVERRIDE;
259 //! Returns background image fill style.
260 Standard_EXPORT virtual Aspect_FillMethod BackgroundImageStyle() const Standard_OVERRIDE;
262 //! Sets background image fill style.
263 Standard_EXPORT virtual void SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle) Standard_OVERRIDE;
265 //! Returns environment texture set for the view.
266 Standard_EXPORT virtual Handle(Graphic3d_TextureEnv) TextureEnv() const Standard_OVERRIDE { return myTextureEnvData; }
268 //! Sets environment texture for the view.
269 Standard_EXPORT virtual void SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv) Standard_OVERRIDE;
271 //! Returns the state of frustum culling optimization.
272 virtual Standard_Boolean IsCullingEnabled() const Standard_OVERRIDE { return myCulling; }
274 //! Enables or disables frustum culling optimization.
275 virtual void SetCullingEnabled (const Standard_Boolean theIsEnabled) Standard_OVERRIDE { myCulling = theIsEnabled; }
277 //! Returns shading model of the view.
278 virtual Graphic3d_TypeOfShadingModel ShadingModel() const Standard_OVERRIDE { return myShadingModel; }
280 //! Sets shading model of the view.
281 virtual void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel) Standard_OVERRIDE { myShadingModel = theModel; }
283 //! Return backfacing model used for the view.
284 virtual Graphic3d_TypeOfBackfacingModel BackfacingModel() const Standard_OVERRIDE { return myBackfacing; }
286 //! Sets backfacing model for the view.
287 virtual void SetBackfacingModel (const Graphic3d_TypeOfBackfacingModel theModel) Standard_OVERRIDE { myBackfacing = theModel; }
289 //! Returns camera object of the view.
290 virtual const Handle(Graphic3d_Camera)& Camera() const Standard_OVERRIDE { return myCamera; }
292 //! Sets camera used by the view.
293 virtual void SetCamera (const Handle(Graphic3d_Camera)& theCamera) Standard_OVERRIDE { myCamera = theCamera; }
295 //! Returns true if GL lighting is enabled.
296 virtual Standard_Boolean IsGLLightEnabled() const Standard_OVERRIDE { return myUseGLLight; }
298 //! Sets GL lighting enabled or disable state.
299 virtual void SetGLLightEnabled (const Standard_Boolean theIsEnabled) Standard_OVERRIDE { myUseGLLight = theIsEnabled; }
301 //! Returns list of lights of the view.
302 virtual const Graphic3d_ListOfCLight& Lights() const Standard_OVERRIDE { return myLights; }
304 //! Sets list of lights for the view.
305 virtual void SetLights (const Graphic3d_ListOfCLight& theLights) Standard_OVERRIDE
307 myLights = theLights;
308 myCurrLightSourceState = myStateCounter->Increment();
311 //! Returns list of clip planes set for the view.
312 virtual const Handle(Graphic3d_SequenceOfHClipPlane)& ClipPlanes() const Standard_OVERRIDE { return myClipPlanes; }
314 //! Sets list of clip planes for the view.
315 virtual void SetClipPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) Standard_OVERRIDE { myClipPlanes = thePlanes; }
317 //! Fill in the dictionary with diagnostic info.
318 //! Should be called within rendering thread.
320 //! This API should be used only for user output or for creating automated reports.
321 //! The format of returned information (e.g. key-value layout)
322 //! is NOT part of this API and can be changed at any time.
323 //! Thus application should not parse returned information to weed out specific parameters.
324 Standard_EXPORT virtual void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
325 Graphic3d_DiagnosticInfo theFlags) const Standard_OVERRIDE;
329 //! Returns background color.
330 const Quantity_ColorRGBA& BackgroundColor() const { return myBgColor; }
332 //! Change trihedron.
333 OpenGl_Trihedron& ChangeTrihedron() { return myTrihedron; }
335 //! Change graduated trihedron.
336 OpenGl_GraduatedTrihedron& ChangeGraduatedTrihedron() { return myGraduatedTrihedron; }
338 void SetTextureEnv (const Handle(OpenGl_Context)& theCtx,
339 const Handle(Graphic3d_TextureEnv)& theTexture);
341 void SetBackgroundTextureStyle (const Aspect_FillMethod FillStyle);
343 void SetBackgroundGradient (const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod AType);
345 void SetBackgroundGradientType (const Aspect_GradientFillMethod AType);
347 //! Returns list of OpenGL Z-layers.
348 const OpenGl_LayerList& LayerList() const { return myZLayers; }
350 //! Returns list of openGL light sources.
351 const OpenGl_ListOfLight& LightList() const { return myLights; }
353 //! Returns OpenGL window implementation.
354 const Handle(OpenGl_Window) GlWindow() const { return myWindow; }
356 //! Returns OpenGL environment map.
357 const Handle(OpenGl_Texture)& GlTextureEnv() const { return myTextureEnv; }
359 //! Returns selector for BVH tree, providing a possibility to store information
360 //! about current view volume and to detect which objects are overlapping it.
361 OpenGl_BVHTreeSelector& BVHTreeSelector() { return myBVHSelector; }
363 //! Returns true if there are immediate structures to display
364 bool HasImmediateStructures() const
366 return myZLayers.NbImmediateStructures() != 0;
369 protected: //! @name Internal methods for managing GL resources
371 //! Initializes OpenGl resource for environment texture.
372 void initTextureEnv (const Handle(OpenGl_Context)& theContext);
374 protected: //! @name low-level redrawing sub-routines
376 //! Redraws view for the given monographic camera projection, or left/right eye.
377 Standard_EXPORT virtual void redraw (const Graphic3d_Camera::Projection theProjection,
378 OpenGl_FrameBuffer* theReadDrawFbo);
380 //! Redraws view for the given monographic camera projection, or left/right eye.
382 //! Method will blit snapshot containing main scene (myMainSceneFbos or BackBuffer)
383 //! into presentation buffer (myMainSceneFbos -> offscreen FBO or
384 //! myMainSceneFbos -> BackBuffer or BackBuffer -> FrontBuffer),
385 //! and redraw immediate structures on top.
387 //! When scene caching is disabled (myTransientDrawToFront, no double buffer in window, etc.),
388 //! the first step (blitting) will be skipped.
390 //! @return false if immediate structures has been rendered directly into FrontBuffer
391 //! and Buffer Swap should not be called.
392 Standard_EXPORT virtual bool redrawImmediate (const Graphic3d_Camera::Projection theProjection,
393 OpenGl_FrameBuffer* theReadFbo,
394 OpenGl_FrameBuffer* theDrawFbo,
395 const Standard_Boolean theIsPartialUpdate = Standard_False);
397 //! Blit image from/to specified buffers.
398 Standard_EXPORT bool blitBuffers (OpenGl_FrameBuffer* theReadFbo,
399 OpenGl_FrameBuffer* theDrawFbo,
400 const Standard_Boolean theToFlip = Standard_False);
402 //! Setup default FBO.
403 Standard_EXPORT void bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo = NULL);
405 protected: //! @name Rendering of GL graphics (with prepared drawing buffer).
407 //! Renders the graphical contents of the view into the preprepared window or framebuffer.
408 //! @param theProjection [in] the projection that should be used for rendering.
409 //! @param theReadDrawFbo [in] the framebuffer for rendering graphics.
410 //! @param theToDrawImmediate [in] the flag indicates whether the rendering performs in immediate mode.
411 Standard_EXPORT virtual void render (Graphic3d_Camera::Projection theProjection,
412 OpenGl_FrameBuffer* theReadDrawFbo,
413 const Standard_Boolean theToDrawImmediate);
415 //! Renders the graphical scene.
416 //! @param theProjection [in] the projection that is used for rendering.
417 //! @param theReadDrawFbo [in] the framebuffer for rendering graphics.
418 //! @param theToDrawImmediate [in] the flag indicates whether the rendering performs in immediate mode.
419 Standard_EXPORT virtual void renderScene (Graphic3d_Camera::Projection theProjection,
420 OpenGl_FrameBuffer* theReadDrawFbo,
421 const Standard_Boolean theToDrawImmediate);
423 //! Draw background (gradient / image)
424 Standard_EXPORT virtual void drawBackground (const Handle(OpenGl_Workspace)& theWorkspace);
426 //! Render set of structures presented in the view.
427 //! @param theProjection [in] the projection that is used for rendering.
428 //! @param theReadDrawFbo [in] the framebuffer for rendering graphics.
429 //! @param theToDrawImmediate [in] the flag indicates whether the rendering performs in immediate mode.
430 Standard_EXPORT virtual void renderStructs (Graphic3d_Camera::Projection theProjection,
431 OpenGl_FrameBuffer* theReadDrawFbo,
432 const Standard_Boolean theToDrawImmediate);
434 //! Renders trihedron.
435 void renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace);
439 //! Adds the structure to display lists of the view.
440 Standard_EXPORT virtual void displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
441 const Standard_Integer thePriority) Standard_OVERRIDE;
443 //! Erases the structure from display lists of the view.
444 Standard_EXPORT virtual void eraseStructure (const Handle(Graphic3d_CStructure)& theStructure) Standard_OVERRIDE;
446 //! Change Z layer of a structure already presented in view.
447 Standard_EXPORT virtual void changeZLayer (const Handle(Graphic3d_CStructure)& theCStructure,
448 const Graphic3d_ZLayerId theNewLayerId) Standard_OVERRIDE;
450 //! Changes the priority of a structure within its Z layer in the specified view.
451 Standard_EXPORT virtual void changePriority (const Handle(Graphic3d_CStructure)& theCStructure,
452 const Standard_Integer theNewPriority) Standard_OVERRIDE;
454 //! Returns zoom-scale factor.
455 Standard_EXPORT virtual Standard_Real considerZoomPersistenceObjects (const Graphic3d_ZLayerId theLayerId,
456 const Handle(Graphic3d_Camera)& theCamera,
457 const Standard_Integer theWindowWidth,
458 const Standard_Integer theWindowHeight) const Standard_OVERRIDE;
462 //! Copy content of Back buffer to the Front buffer.
463 void copyBackToFront();
465 //! Initialize blit quad.
466 OpenGl_VertexBuffer* initBlitQuad (const Standard_Boolean theToFlip);
468 //! Blend together views pair into stereo image.
469 void drawStereoPair (OpenGl_FrameBuffer* theDrawFbo);
473 OpenGl_GraphicDriver* myDriver;
474 Handle(OpenGl_Window) myWindow;
475 Handle(OpenGl_Workspace) myWorkspace;
476 Handle(OpenGl_Caps) myCaps;
477 Standard_Boolean& myDeviceLostFlag;
478 Standard_Boolean myWasRedrawnGL;
480 Standard_Boolean myCulling;
481 Graphic3d_TypeOfShadingModel myShadingModel;
482 Graphic3d_TypeOfBackfacingModel myBackfacing;
483 Quantity_ColorRGBA myBgColor;
484 Handle(Graphic3d_SequenceOfHClipPlane) myClipPlanes;
485 Handle(Graphic3d_Camera) myCamera;
486 Handle(OpenGl_FrameBuffer) myFBO;
487 Standard_Boolean myUseGLLight;
488 Standard_Boolean myToShowTrihedron;
489 Standard_Boolean myToShowGradTrihedron;
490 TCollection_AsciiString myBackgroundImagePath;
491 Handle(Graphic3d_TextureEnv) myTextureEnvData;
492 Graphic3d_GraduatedTrihedron myGTrihedronData;
494 OpenGl_ListOfLight myNoShadingLight;
495 OpenGl_ListOfLight myLights;
496 OpenGl_LayerList myZLayers; //!< main list of displayed structure, sorted by layers
498 Graphic3d_WorldViewProjState myWorldViewProjState; //!< camera modification state
499 OpenGl_StateCounter* myStateCounter;
500 Standard_Size myCurrLightSourceState;
502 typedef std::pair<Standard_Size, Standard_Size> StateInfo;
504 StateInfo myLastOrientationState;
505 StateInfo myLastViewMappingState;
506 StateInfo myLastLightSourceState;
508 //! Is needed for selection of overlapping objects and storage of the current view volume
509 OpenGl_BVHTreeSelector myBVHSelector;
511 OpenGl_Trihedron myTrihedron;
512 OpenGl_GraduatedTrihedron myGraduatedTrihedron;
514 Handle(OpenGl_Texture) myTextureEnv;
516 protected: //! @name Rendering properties
518 //! Two framebuffers (left and right views) store cached main presentation
519 //! of the view (without presentation of immediate layers).
520 GLint myFboColorFormat; //!< sized format for color attachments
521 GLint myFboDepthFormat; //!< sized format for depth-stencil attachments
522 Handle(OpenGl_FrameBuffer) myMainSceneFbos[2];
523 Handle(OpenGl_FrameBuffer) myImmediateSceneFbos[2]; //!< Additional buffers for immediate layer in stereo mode.
524 OpenGl_VertexBuffer myFullScreenQuad; //!< Vertices for full-screen quad rendering.
525 OpenGl_VertexBuffer myFullScreenQuadFlip;
526 Standard_Boolean myToFlipOutput; //!< Flag to draw result image upside-down
527 unsigned int myFrameCounter; //!< redraw counter, for debugging
528 Standard_Boolean myHasFboBlit; //!< disable FBOs on failure
529 Standard_Boolean myToDisableMSAA; //!< disable MSAA after failure
530 Standard_Boolean myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
531 Standard_Boolean myBackBufferRestored;
532 Standard_Boolean myIsImmediateDrawn; //!< flag indicates that immediate mode buffer contains some data
534 protected: //! @name Background parameters
536 OpenGl_AspectFace* myTextureParams; //!< Stores texture and its parameters for textured background
537 OpenGl_BackgroundArray* myBgGradientArray; //!< Primitive array for gradient background
538 OpenGl_BackgroundArray* myBgTextureArray; //!< Primitive array for texture background
540 protected: //! @name data types related to ray-tracing
542 //! Result of OpenGL shaders initialization.
543 enum RaytraceInitStatus
550 //! Describes update mode (state).
551 enum RaytraceUpdateMode
553 OpenGl_GUM_CHECK, //!< check geometry state
554 OpenGl_GUM_PREPARE, //!< collect unchanged objects
555 OpenGl_GUM_REBUILD //!< rebuild changed and new objects
558 //! Defines frequently used shader variables.
559 enum ShaderVariableIndex
573 OpenGl_RT_uUnviewMat,
578 OpenGl_RT_uLightAmbnt,
579 OpenGl_RT_uLightCount,
582 OpenGl_RT_uBackColorTop,
583 OpenGl_RT_uBackColorBot,
585 // ray-tracing params
586 OpenGl_RT_uShadowsEnabled,
587 OpenGl_RT_uReflectEnabled,
588 OpenGl_RT_uSphereMapEnabled,
589 OpenGl_RT_uSphereMapForBack,
590 OpenGl_RT_uTexSamplersArray,
591 OpenGl_RT_uBlockedRngEnabled,
593 // sampled frame params
594 OpenGl_RT_uSampleWeight,
595 OpenGl_RT_uFrameRndSeed,
597 // adaptive FSAA params
602 OpenGl_RT_NbVariables // special field
605 //! Defines texture samplers.
606 enum ShaderSamplerNames
608 OpenGl_RT_SceneNodeInfoTexture = 0,
609 OpenGl_RT_SceneMinPointTexture = 1,
610 OpenGl_RT_SceneMaxPointTexture = 2,
611 OpenGl_RT_SceneTransformTexture = 3,
613 OpenGl_RT_GeometryVertexTexture = 4,
614 OpenGl_RT_GeometryNormalTexture = 5,
615 OpenGl_RT_GeometryTexCrdTexture = 6,
616 OpenGl_RT_GeometryTriangTexture = 7,
618 OpenGl_RT_EnvironmentMapTexture = 8,
620 OpenGl_RT_RaytraceMaterialTexture = 9,
621 OpenGl_RT_RaytraceLightSrcTexture = 10,
623 OpenGl_RT_FsaaInputTexture = 11,
624 OpenGl_RT_PrevAccumTexture = 12,
625 OpenGl_RT_DepthTexture = 13,
627 OpenGl_RT_OpenGlColorTexture = 14,
628 OpenGl_RT_OpenGlDepthTexture = 15
631 //! Tool class for management of shader sources.
636 //! Default shader prefix - empty string.
637 static const TCollection_AsciiString EMPTY_PREFIX;
639 //! Creates new uninitialized shader source.
647 //! Returns error description in case of load fail.
648 const TCollection_AsciiString& ErrorDescription() const
653 //! Returns prefix to insert before the source.
654 const TCollection_AsciiString& Prefix() const
659 //! Sets prefix to insert before the source.
660 void SetPrefix (const TCollection_AsciiString& thePrefix)
662 myPrefix = thePrefix;
665 //! Returns shader source combined with prefix.
666 TCollection_AsciiString Source() const;
668 //! Loads shader source from specified files.
669 Standard_Boolean Load (const TCollection_AsciiString* theFileNames, const TCollection_AsciiString& thePrefix = EMPTY_PREFIX);
673 TCollection_AsciiString mySource; //!< Source string of the shader object
674 TCollection_AsciiString myPrefix; //!< Prefix to insert before the source
675 TCollection_AsciiString myError; //!< error state
679 //! Default ray-tracing depth.
680 static const Standard_Integer THE_DEFAULT_NB_BOUNCES = 3;
682 //! Default size of traversal stack.
683 static const Standard_Integer THE_DEFAULT_STACK_SIZE = 10;
685 //! Compile-time ray-tracing parameters.
686 struct RaytracingParams
688 //! Actual size of traversal stack in shader program.
689 Standard_Integer StackSize;
691 //! Actual ray-tracing depth (number of ray bounces).
692 Standard_Integer NbBounces;
694 //! Enables/disables light propagation through transparent media.
695 Standard_Boolean TransparentShadows;
697 //! Enables/disables global illumination (GI) effects.
698 Standard_Boolean GlobalIllumination;
700 //! Enables/disables the use of OpenGL bindless textures.
701 Standard_Boolean UseBindlessTextures;
703 //! Creates default compile-time ray-tracing parameters.
705 : StackSize (THE_DEFAULT_STACK_SIZE),
706 NbBounces (THE_DEFAULT_NB_BOUNCES),
707 TransparentShadows (Standard_False),
708 GlobalIllumination (Standard_False),
709 UseBindlessTextures (Standard_False)
715 //! Describes state of OpenGL structure.
718 Standard_Size StructureState;
719 Standard_Size InstancedState;
721 //! Creates new structure state.
722 StructState (const Standard_Size theStructureState = 0,
723 const Standard_Size theInstancedState = 0)
724 : StructureState (theStructureState),
725 InstancedState (theInstancedState)
730 //! Creates new structure state.
731 StructState (const OpenGl_Structure* theStructure)
733 StructureState = theStructure->ModificationState();
735 InstancedState = theStructure->InstancedStructure() != NULL ?
736 theStructure->InstancedStructure()->ModificationState() : 0;
740 protected: //! @name methods related to ray-tracing
742 //! Updates 3D scene geometry for ray-tracing.
743 Standard_Boolean updateRaytraceGeometry (const RaytraceUpdateMode theMode,
744 const Standard_Integer theViewId,
745 const Handle(OpenGl_Context)& theGlContext);
747 //! Updates 3D scene light sources for ray-tracing.
748 Standard_Boolean updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext);
750 //! Updates environment map for ray-tracing.
751 Standard_Boolean updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext);
753 //! Checks to see if the OpenGL structure is modified.
754 Standard_Boolean toUpdateStructure (const OpenGl_Structure* theStructure);
756 //! Adds OpenGL structure to ray-traced scene geometry.
757 Standard_Boolean addRaytraceStructure (const OpenGl_Structure* theStructure,
758 const Handle(OpenGl_Context)& theGlContext);
760 //! Adds OpenGL groups to ray-traced scene geometry.
761 Standard_Boolean addRaytraceGroups (const OpenGl_Structure* theStructure,
762 const OpenGl_RaytraceMaterial& theStructMat,
763 const Graphic3d_Mat4* theTransform,
764 const Handle(OpenGl_Context)& theGlContext);
766 //! Creates ray-tracing material properties.
767 OpenGl_RaytraceMaterial convertMaterial (const OpenGl_AspectFace* theAspect,
768 const Handle(OpenGl_Context)& theGlContext);
770 //! Adds OpenGL primitive array to ray-traced scene geometry.
771 OpenGl_TriangleSet* addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
772 const Standard_Integer theMatID,
773 const OpenGl_Mat4* theTrans);
775 //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
776 Standard_Boolean addRaytraceVertexIndices (OpenGl_TriangleSet& theSet,
777 const Standard_Integer theMatID,
778 const Standard_Integer theCount,
779 const Standard_Integer theOffset,
780 const OpenGl_PrimitiveArray& theArray);
782 //! Adds OpenGL triangle array to ray-traced scene geometry.
783 Standard_Boolean addRaytraceTriangleArray (OpenGl_TriangleSet& theSet,
784 const Standard_Integer theMatID,
785 const Standard_Integer theCount,
786 const Standard_Integer theOffset,
787 const Handle(Graphic3d_IndexBuffer)& theIndices);
789 //! Adds OpenGL triangle fan array to ray-traced scene geometry.
790 Standard_Boolean addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet,
791 const Standard_Integer theMatID,
792 const Standard_Integer theCount,
793 const Standard_Integer theOffset,
794 const Handle(Graphic3d_IndexBuffer)& theIndices);
796 //! Adds OpenGL triangle strip array to ray-traced scene geometry.
797 Standard_Boolean addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet,
798 const Standard_Integer theMatID,
799 const Standard_Integer theCount,
800 const Standard_Integer theOffset,
801 const Handle(Graphic3d_IndexBuffer)& theIndices);
803 //! Adds OpenGL quadrangle array to ray-traced scene geometry.
804 Standard_Boolean addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet,
805 const Standard_Integer theMatID,
806 const Standard_Integer theCount,
807 const Standard_Integer theOffset,
808 const Handle(Graphic3d_IndexBuffer)& theIndices);
810 //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
811 Standard_Boolean addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet,
812 const Standard_Integer theMatID,
813 const Standard_Integer theCount,
814 const Standard_Integer theOffset,
815 const Handle(Graphic3d_IndexBuffer)& theIndices);
817 //! Adds OpenGL polygon array to ray-traced scene geometry.
818 Standard_Boolean addRaytracePolygonArray (OpenGl_TriangleSet& theSet,
819 const Standard_Integer theMatID,
820 const Standard_Integer theCount,
821 const Standard_Integer theOffset,
822 const Handle(Graphic3d_IndexBuffer)& theIndices);
824 //! Uploads ray-trace data to the GPU.
825 Standard_Boolean uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext);
827 //! Generates shader prefix based on current ray-tracing options.
828 TCollection_AsciiString generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const;
830 //! Performs safe exit when shaders initialization fails.
831 Standard_Boolean safeFailBack (const TCollection_ExtendedString& theMessage,
832 const Handle(OpenGl_Context)& theGlContext);
834 //! Loads and compiles shader object from specified source.
835 Handle(OpenGl_ShaderObject) initShader (const GLenum theType,
836 const ShaderSource& theSource,
837 const Handle(OpenGl_Context)& theGlContext);
839 //! Creates shader program from the given vertex and fragment shaders.
840 Handle(OpenGl_ShaderProgram) initProgram (const Handle(OpenGl_Context)& theGlContext,
841 const Handle(OpenGl_ShaderObject)& theVertShader,
842 const Handle(OpenGl_ShaderObject)& theFragShader);
844 //! Initializes OpenGL/GLSL shader programs.
845 Standard_Boolean initRaytraceResources (const Handle(OpenGl_Context)& theGlContext);
847 //! Releases OpenGL/GLSL shader programs.
848 void releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext);
850 //! Updates auxiliary OpenGL frame buffers.
851 Standard_Boolean updateRaytraceBuffers (const Standard_Integer theSizeX,
852 const Standard_Integer theSizeY,
853 const Handle(OpenGl_Context)& theGlContext);
855 //! Generates viewing rays for corners of screen quad.
856 void updateCamera (const OpenGl_Mat4& theOrientation,
857 const OpenGl_Mat4& theViewMapping,
858 OpenGl_Vec3* theOrigins,
859 OpenGl_Vec3* theDirects,
860 OpenGl_Mat4& theView,
861 OpenGl_Mat4& theUnView);
863 //! Binds ray-trace textures to corresponding texture units.
864 void bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext);
866 //! Unbinds ray-trace textures from corresponding texture unit.
867 void unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext);
869 //! Sets uniform state for the given ray-tracing shader program.
870 Standard_Boolean setUniformState (const OpenGl_Vec3* theOrigins,
871 const OpenGl_Vec3* theDirects,
872 const OpenGl_Mat4& theViewMat,
873 const OpenGl_Mat4& theUnviewMat,
874 const Standard_Integer theProgramId,
875 const Handle(OpenGl_Context)& theGlContext);
877 //! Runs ray-tracing shader programs.
878 Standard_Boolean runRaytraceShaders (const Standard_Integer theSizeX,
879 const Standard_Integer theSizeY,
880 const OpenGl_Vec3* theOrigins,
881 const OpenGl_Vec3* theDirects,
882 const OpenGl_Mat4& theViewMat,
883 const OpenGl_Mat4& theUnviewMat,
884 Graphic3d_Camera::Projection theProjection,
885 OpenGl_FrameBuffer* theReadDrawFbo,
886 const Handle(OpenGl_Context)& theGlContext);
888 //! Redraws the window using OpenGL/GLSL ray-tracing.
889 Standard_Boolean raytrace (const Standard_Integer theSizeX,
890 const Standard_Integer theSizeY,
891 Graphic3d_Camera::Projection theProjection,
892 OpenGl_FrameBuffer* theReadDrawFbo,
893 const Handle(OpenGl_Context)& theGlContext);
895 protected: //! @name fields related to ray-tracing
897 //! Result of shaders initialization.
898 RaytraceInitStatus myRaytraceInitStatus;
900 //! Is geometry data valid?
901 Standard_Boolean myIsRaytraceDataValid;
903 //! Warning about missing extension GL_ARB_bindless_texture has been displayed?
904 Standard_Boolean myIsRaytraceWarnTextures;
906 //! 3D scene geometry data for ray-tracing.
907 OpenGl_RaytraceGeometry myRaytraceGeometry;
909 //! Compile-time ray-tracing parameters.
910 RaytracingParams myRaytraceParameters;
912 //! Radius of bounding sphere of the scene.
913 Standard_ShortReal myRaytraceSceneRadius;
914 //! Scene epsilon to prevent self-intersections.
915 Standard_ShortReal myRaytraceSceneEpsilon;
917 //! OpenGL/GLSL source of ray-tracing fragment shader.
918 ShaderSource myRaytraceShaderSource;
919 //! OpenGL/GLSL source of adaptive-AA fragment shader.
920 ShaderSource myPostFSAAShaderSource;
922 //! OpenGL/GLSL ray-tracing fragment shader.
923 Handle(OpenGl_ShaderObject) myRaytraceShader;
924 //! OpenGL/GLSL adaptive-AA fragment shader.
925 Handle(OpenGl_ShaderObject) myPostFSAAShader;
927 //! OpenGL/GLSL ray-tracing shader program.
928 Handle(OpenGl_ShaderProgram) myRaytraceProgram;
929 //! OpenGL/GLSL adaptive-AA shader program.
930 Handle(OpenGl_ShaderProgram) myPostFSAAProgram;
931 //! OpenGL/GLSL program for displaying texture.
932 Handle(OpenGl_ShaderProgram) myOutImageProgram;
934 //! Texture buffer of data records of bottom-level BVH nodes.
935 Handle(OpenGl_TextureBufferArb) mySceneNodeInfoTexture;
936 //! Texture buffer of minimum points of bottom-level BVH nodes.
937 Handle(OpenGl_TextureBufferArb) mySceneMinPointTexture;
938 //! Texture buffer of maximum points of bottom-level BVH nodes.
939 Handle(OpenGl_TextureBufferArb) mySceneMaxPointTexture;
940 //! Texture buffer of transformations of high-level BVH nodes.
941 Handle(OpenGl_TextureBufferArb) mySceneTransformTexture;
943 //! Texture buffer of vertex coords.
944 Handle(OpenGl_TextureBufferArb) myGeometryVertexTexture;
945 //! Texture buffer of vertex normals.
946 Handle(OpenGl_TextureBufferArb) myGeometryNormalTexture;
947 //! Texture buffer of vertex UV coords.
948 Handle(OpenGl_TextureBufferArb) myGeometryTexCrdTexture;
949 //! Texture buffer of triangle indices.
950 Handle(OpenGl_TextureBufferArb) myGeometryTriangTexture;
952 //! Texture buffer of material properties.
953 Handle(OpenGl_TextureBufferArb) myRaytraceMaterialTexture;
954 //! Texture buffer of light source properties.
955 Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture;
957 //! 1st framebuffer (FBO) to perform adaptive FSAA.
958 Handle(OpenGl_FrameBuffer) myRaytraceFBO1[2];
959 //! 2nd framebuffer (FBO) to perform adaptive FSAA.
960 Handle(OpenGl_FrameBuffer) myRaytraceFBO2[2];
961 //! Framebuffer (FBO) for preliminary OpenGL output.
962 Handle(OpenGl_FrameBuffer) myOpenGlFBO;
963 Handle(OpenGl_FrameBuffer) myOpenGlFBO2;
965 //! Vertex buffer (VBO) for drawing dummy quad.
966 OpenGl_VertexBuffer myRaytraceScreenQuad;
968 //! Cached locations of frequently used uniform variables.
969 Standard_Integer myUniformLocations[2][OpenGl_RT_NbVariables];
971 //! State of OpenGL structures reflected to ray-tracing.
972 std::map<const OpenGl_Structure*, StructState> myStructureStates;
974 //! PrimitiveArray to TriangleSet map for scene partial update.
975 std::map<Standard_Size, OpenGl_TriangleSet*> myArrayToTrianglesMap;
977 //! Set of IDs of non-raytracable elements (to detect updates).
978 std::set<Standard_Integer> myNonRaytraceStructureIDs;
980 //! Render filter to filter out all raytracable structures.
981 Handle(OpenGl_RaytraceFilter) myRaytraceFilter;
983 //! Marks if environment map should be updated.
984 Standard_Boolean myToUpdateEnvironmentMap;
986 //! State of OpenGL layer list.
987 Standard_Size myLayerListState;
989 //! Number of accumulated frames (for progressive rendering).
990 Standard_Integer myAccumFrames;
992 //! Stored ray origins used for detection of camera movements.
993 OpenGl_Vec3 myPreviousOrigins[3];
995 //! Bullard RNG to produce random sequence.
996 math_BullardGenerator myRNG;
1000 DEFINE_STANDARD_ALLOC
1001 DEFINE_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView) // Type definition
1003 friend class OpenGl_GraphicDriver;
1004 friend class OpenGl_Workspace;
1007 #endif // _OpenGl_View_Header