aa41691979f952643b5fb9e1b0f72de5093a9dd4
[occt.git] / src / OpenGl / OpenGl_View.hxx
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_DefineHandle.hxx>
21
22 #include <TColStd_Array2OfReal.hxx>
23 #include <NCollection_List.hxx>
24
25 #include <Quantity_NameOfColor.hxx>
26 #include <Aspect_FillMethod.hxx>
27 #include <Aspect_GradientFillMethod.hxx>
28 #include <Aspect_TypeOfTriedronPosition.hxx>
29 #include <Aspect_CLayer2d.hxx>
30
31 #include <InterfaceGraphic_Graphic3d.hxx>
32 #include <InterfaceGraphic_Visual3d.hxx>
33
34 #include <Graphic3d_CView.hxx>
35 #include <Graphic3d_GraduatedTrihedron.hxx>
36 #include <Graphic3d_SequenceOfHClipPlane.hxx>
37 #include <Graphic3d_ZLayerSettings.hxx>
38 #include <Visual3d_TypeOfSurfaceDetail.hxx>
39 #include <Visual3d_TypeOfModel.hxx>
40
41 #include <OpenGl_AspectFace.hxx>
42 #include <OpenGl_BackgroundArray.hxx>
43 #include <OpenGl_BVHTreeSelector.hxx>
44 #include <OpenGl_LayerList.hxx>
45 #include <OpenGl_Light.hxx>
46 #include <OpenGl_LineAttributes.hxx>
47 #include <OpenGl_Trihedron.hxx>
48 #include <OpenGl_GraduatedTrihedron.hxx>
49
50 #include <Handle_OpenGl_Context.hxx>
51 #include <Handle_OpenGl_GraphicDriver.hxx>
52 #include <Handle_OpenGl_Workspace.hxx>
53 #include <Handle_OpenGl_View.hxx>
54 #include <Handle_OpenGl_Texture.hxx>
55
56 struct OPENGL_ZCLIP
57 {
58   struct {
59     Standard_Boolean   IsOn;
60     Standard_ShortReal Limit; /* in the range [0., 1.] */
61   } Back;
62   struct {
63     Standard_Boolean   IsOn;
64     Standard_ShortReal Limit; /* in the range [0., 1.] */
65   } Front;
66 };
67
68 struct OPENGL_FOG
69 {
70   Standard_Boolean   IsOn;
71   Standard_ShortReal Front; /* in the range [0., 1.] */
72   Standard_ShortReal Back; /* in the range [0., 1.] */
73   TEL_COLOUR         Color;
74 };
75
76 struct OpenGl_Matrix;
77 class OpenGl_Structure;
78 class Handle(OpenGl_PrinterContext);
79 class OpenGl_StateCounter;
80
81 class OpenGl_View : public MMgt_TShared
82 {
83  public:
84   OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext, OpenGl_StateCounter* theCounter);
85   virtual ~OpenGl_View ();
86
87   void ReleaseGlResources (const Handle(OpenGl_Context)& theCtx);
88
89   void SetTextureEnv (const Handle(OpenGl_Context)&       theCtx,
90                       const Handle(Graphic3d_TextureEnv)& theTexture);
91   void SetSurfaceDetail (const Visual3d_TypeOfSurfaceDetail AMode);
92   void SetBackfacing (const Standard_Integer AMode);
93   void SetLights (const CALL_DEF_VIEWCONTEXT &AContext);
94   void SetAntiAliasing (const Standard_Boolean AMode) { myAntiAliasing = AMode; }
95   void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane &thePlanes) { myClipPlanes = thePlanes; }
96   void SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext);
97
98   const Handle(Graphic3d_Camera)& Camera() { return myCamera; }
99   void SetCamera (const Handle(Graphic3d_Camera)& theCamera) { myCamera = theCamera; }
100
101   void SetClipLimit (const Graphic3d_CView& theCView);
102
103   void SetFog (const Graphic3d_CView& theCView, const Standard_Boolean theFlag);
104
105   OpenGl_Trihedron& ChangeTrihedron() { return myTrihedron; }
106   void TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition,
107                         const Quantity_NameOfColor          theColor,
108                         const Standard_Real                 theScale,
109                         const Standard_Boolean              theAsWireframe);
110   void TriedronErase (const Handle(OpenGl_Context)& theCtx);
111
112   OpenGl_GraduatedTrihedron& ChangeGraduatedTrihedron() { return myGraduatedTrihedron; }
113   void GraduatedTrihedronDisplay (const Handle(OpenGl_Context)&        theCtx,
114                                   const Graphic3d_GraduatedTrihedron& theCubic);
115   void GraduatedTrihedronErase (const Handle(OpenGl_Context)& theCtx);
116
117   Standard_Real Height () const { return myCamera->ViewDimensions().X(); }
118   Standard_Real Width () const { return myCamera->ViewDimensions().Y(); }
119
120   Standard_Integer Backfacing () const { return myBackfacing; }
121
122   const TEL_TRANSFORM_PERSISTENCE * BeginTransformPersistence (const Handle(OpenGl_Context)& theCtx,
123                                                                const TEL_TRANSFORM_PERSISTENCE *theTransPers,
124                                                                Standard_Integer theWidth,
125                                                                Standard_Integer theHeight);
126   void EndTransformPersistence (const Handle(OpenGl_Context)& theCtx);
127
128   //! Add structure to display list with specified priority.
129   //! The structure will be added to associated with it z layer.
130   //! If the z layer is not presented in the view, the structure will
131   //! be displayed in default bottom-level z layer.
132   void DisplayStructure (const Handle(Graphic3d_Structure)& theStructure,
133                          const Standard_Integer             thePriority);
134
135   //! Erase structure from display list.
136   void EraseStructure (const Handle(Graphic3d_Structure)& theStructure);
137
138   //! Add structure to the list of immediate structures.
139   void DisplayImmediateStructure (const Handle(Graphic3d_Structure)& theStructure);
140
141   //! Erase structure from display list.
142   void EraseImmediateStructure (const OpenGl_Structure* theStructure);
143
144   //! Insert a new top-level z layer with ID <theLayerId>
145   void AddZLayer (const Graphic3d_ZLayerId theLayerId);
146
147   //! Remove a z layer with ID <theLayerId>
148   void RemoveZLayer (const Graphic3d_ZLayerId theLayerId);
149
150   //! Display structure in z layer with ID <theNewLayerId>
151   //! If the layer with ID <theNewLayerId> is not presented in the view,
152   //! the structure will be displayed in default bottom-level layer.
153   void ChangeZLayer (const OpenGl_Structure*  theStructure,
154                      const Graphic3d_ZLayerId theNewLayerId);
155
156   //! Sets the settings for a single Z layer of specified view.
157   void SetZLayerSettings (const Graphic3d_ZLayerId        theLayerId,
158                           const Graphic3d_ZLayerSettings& theSettings);
159
160   //! Changes the priority of a structure within its ZLayer
161   void ChangePriority (const OpenGl_Structure* theStructure,
162                        const Standard_Integer  theNewPriority);
163
164   void CreateBackgroundTexture (const Standard_CString AFileName,
165                                 const Aspect_FillMethod AFillStyle);
166
167   void SetBackgroundTextureStyle (const Aspect_FillMethod FillStyle);
168   void SetBackgroundGradient (const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod AType);
169   void SetBackgroundGradientType (const Aspect_GradientFillMethod AType);
170
171   void Render (const Handle(OpenGl_PrinterContext)& thePrintContext,
172                const Handle(OpenGl_Workspace)&      theWorkspace,
173                OpenGl_FrameBuffer*                  theReadDrawFbo,
174                Graphic3d_Camera::Projection         theProjection,
175                const Graphic3d_CView&               theCView,
176                const Aspect_CLayer2d&               theCUnderLayer,
177                const Aspect_CLayer2d&               theCOverLayer,
178                const Standard_Boolean               theToDrawImmediate);
179
180
181   void DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace);
182
183   //! Returns list of OpenGL Z-layers.
184   const OpenGl_LayerList& LayerList() const { return myZLayers; }
185
186   //! Returns list of openGL light sources.
187   const OpenGl_ListOfLight& LightList() const { return myLights; }
188
189   //! Returns OpenGL environment map.
190   const Handle(OpenGl_Texture)& TextureEnv() const { return myTextureEnv; }
191
192   //! Returns visualization mode for objects in the view.
193   Visual3d_TypeOfSurfaceDetail SurfaceDetail() const { return mySurfaceDetail; }
194
195   //! Returns selector for BVH tree, providing a possibility to store information
196   //! about current view volume and to detect which objects are overlapping it.
197   OpenGl_BVHTreeSelector& BVHTreeSelector() { return myBVHSelector; }
198
199   //! Marks BVH tree for given priority list as dirty and
200   //! marks primitive set for rebuild.
201   void InvalidateBVHData (const Standard_Integer theLayerId);
202
203   //! Returns list of immediate structures rendered on top of main presentation
204   const OpenGl_IndexedMapOfStructure& ImmediateStructures() const
205   {
206     return myImmediateList;
207   }
208
209   //! Returns true if there are immediate structures to display
210   bool HasImmediateStructures() const
211   {
212     return !myImmediateList.IsEmpty()
213          || myZLayers.NbImmediateStructures() != 0;
214   }
215
216 protected:
217
218   void RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace,
219                       OpenGl_FrameBuffer*             theReadDrawFbo,
220                       const Graphic3d_CView&          theCView,
221                       const Standard_Boolean          theToDrawImmediate);
222
223   void RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext,
224                       const Handle(OpenGl_Workspace)&      theWorkspace,
225                       const Graphic3d_CView&               theCView,
226                       const Aspect_CLayer2d&               theCLayer);
227
228   void RedrawTrihedron (const Handle(OpenGl_Workspace) &theWorkspace);
229
230   //! Redraw contents of model scene: clipping planes,
231   //! lights, structures. The peculiar properties of "scene" is that
232   //! it requires empty Z-Buffer and uses projection and orientation
233   //! matrices supplied by 3d view.
234   void RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext,
235                     const Handle(OpenGl_Workspace)&      theWorkspace,
236                     OpenGl_FrameBuffer*                  theReadDrawFbo,
237                     const Graphic3d_CView&               theCView,
238                     const Standard_Boolean               theToDrawImmediate);
239
240   Handle(OpenGl_LineAttributes) myLineAttribs;
241   Handle(OpenGl_Texture)        myTextureEnv;
242   Visual3d_TypeOfSurfaceDetail  mySurfaceDetail;
243   Standard_Integer              myBackfacing;
244
245   OPENGL_ZCLIP   myZClip;
246
247   Graphic3d_SequenceOfHClipPlane myClipPlanes;
248   
249   Handle(Graphic3d_Camera) myCamera;
250
251   OPENGL_FOG myFog;
252   OpenGl_Trihedron           myTrihedron;
253   OpenGl_GraduatedTrihedron  myGraduatedTrihedron;
254   bool                       myToShowTrihedron;
255   bool                       myToShowGradTrihedron;
256
257   //View_LABViewContext
258   int myVisualization;
259   Visual3d_TypeOfModel       myShadingModel;  //!< lighting shading model
260
261   //View_LABLight
262   OpenGl_ListOfLight myLights;
263
264   //View_LABPlane
265   //View_LABAliasing
266   Standard_Boolean myAntiAliasing;
267
268   //View_LABDepthCueing - fixed index used
269
270   OpenGl_LayerList             myZLayers;       //!< main list of displayed structure, sorted by layers
271   OpenGl_IndexedMapOfStructure myImmediateList; //!< list of immediate structures rendered on top of main presentation
272
273   const TEL_TRANSFORM_PERSISTENCE *myTransPers;
274   Standard_Boolean myIsTransPers;
275
276   //! Modification state
277   Standard_Size myProjectionState;
278   Standard_Size myModelViewState;
279   OpenGl_StateCounter* myStateCounter;
280
281   Standard_Size myCurrLightSourceState;
282
283   typedef std::pair<Standard_Size, Standard_Size> StateInfo;
284
285   StateInfo myLastOrientationState;
286   StateInfo myLastViewMappingState;
287   StateInfo myLastLightSourceState;
288
289   //! Is needed for selection of overlapping objects and storage of the current view volume
290   OpenGl_BVHTreeSelector myBVHSelector;
291
292 protected: //! @name Background parameters
293
294   OpenGl_AspectFace*      myTextureParams;   //!< Stores texture and its parameters for textured background
295   OpenGl_BackgroundArray* myBgGradientArray; //!< Primitive array for gradient background
296   OpenGl_BackgroundArray* myBgTextureArray;  //!< Primitive array for texture  background
297
298 protected: //! @name data types related to ray-tracing
299
300   //! Result of OpenGL shaders initialization.
301   enum RaytraceInitStatus
302   {
303     OpenGl_RT_NONE,
304     OpenGl_RT_INIT,
305     OpenGl_RT_FAIL
306   };
307
308   //! Describes update mode (state).
309   enum RaytraceUpdateMode
310   {
311     OpenGl_GUM_CHECK,   //!< check geometry state
312     OpenGl_GUM_PREPARE, //!< collect unchanged objects
313     OpenGl_GUM_REBUILD  //!< rebuild changed and new objects
314   };
315
316   //! Defines frequently used shader variables.
317   enum ShaderVariableIndex
318   {
319     OpenGl_RT_aPosition,
320
321     OpenGl_RT_uOriginLT,
322     OpenGl_RT_uOriginLB,
323     OpenGl_RT_uOriginRT,
324     OpenGl_RT_uOriginRB,
325     OpenGl_RT_uDirectLT,
326     OpenGl_RT_uDirectLB,
327     OpenGl_RT_uDirectRT,
328     OpenGl_RT_uDirectRB,
329     OpenGl_RT_uUnviewMat,
330
331     OpenGl_RT_uSceneRad,
332     OpenGl_RT_uSceneEps,
333     OpenGl_RT_uLightAmbnt,
334     OpenGl_RT_uLightCount,
335
336     OpenGl_RT_uShadEnabled,
337     OpenGl_RT_uReflEnabled,
338     OpenGl_RT_uEnvMapEnable,
339
340     OpenGl_RT_uOffsetX,
341     OpenGl_RT_uOffsetY,
342     OpenGl_RT_uSamples,
343     OpenGl_RT_uWinSizeX,
344     OpenGl_RT_uWinSizeY,
345
346     OpenGl_RT_uTextures,
347
348     OpenGl_RT_NbVariables // special field
349   };
350
351   //! Defines texture samplers.
352   enum ShaderSamplerNames
353   {
354     OpenGl_RT_SceneNodeInfoTexture  = 0,
355     OpenGl_RT_SceneMinPointTexture  = 1,
356     OpenGl_RT_SceneMaxPointTexture  = 2,
357     OpenGl_RT_SceneTransformTexture = 3,
358
359     OpenGl_RT_GeometryVertexTexture = 4,
360     OpenGl_RT_GeometryNormalTexture = 5,
361     OpenGl_RT_GeometryTexCrdTexture = 6,
362     OpenGl_RT_GeometryTriangTexture = 7,
363
364     OpenGl_RT_EnvironmentMapTexture = 8,
365
366     OpenGl_RT_RaytraceMaterialTexture = 9,
367     OpenGl_RT_RaytraceLightSrcTexture = 10,
368
369     OpenGl_RT_FSAAInputTexture = 11,
370
371     OpenGl_RT_OpenGlColorTexture = 12,
372     OpenGl_RT_OpenGlDepthTexture = 13
373   };
374
375   //! Tool class for management of shader sources.
376   class ShaderSource
377   {
378   public:
379
380     //! Creates new uninitialized shader source.
381     ShaderSource()
382     {
383       //
384     }
385
386     //! Creates new shader source from specified file.
387     ShaderSource (const TCollection_AsciiString& theFileName)
388     {
389       Load (&theFileName, 1);
390     }
391
392   public:
393
394     //! Returns prefix to insert before the source.
395     const TCollection_AsciiString& Prefix() const
396     {
397       return myPrefix;
398     }
399
400     //! Sets prefix to insert before the source.
401     void SetPrefix (const TCollection_AsciiString& thePrefix)
402     {
403       myPrefix = thePrefix;
404     }
405
406     //! Returns shader source combined with prefix.
407     TCollection_AsciiString Source() const;
408
409     //! Loads shader source from specified files.
410     void Load (const TCollection_AsciiString* theFileNames, const Standard_Integer theCount);
411
412   private:
413
414     TCollection_AsciiString mySource; //!< Source string of the shader object
415     TCollection_AsciiString myPrefix; //!< Prefix to insert before the source
416
417   };
418
419   //! Default ray-tracing depth.
420   static const Standard_Integer THE_DEFAULT_NB_BOUNCES = 3;
421
422   //! Default size of traversal stack.
423   static const Standard_Integer THE_DEFAULT_STACK_SIZE = 24;
424
425   //! Compile-time ray-tracing parameters.
426   struct RaytracingParams
427   {
428     //! Actual size of traversal stack in shader program.
429     Standard_Integer StackSize;
430
431     //! Actual ray-tracing depth (number of ray bounces).
432     Standard_Integer NbBounces;
433
434     //! Enables/disables light propagation through transparent media.
435     Standard_Boolean TransparentShadows;
436
437     //! Enables/disables the use of OpenGL bindless textures.
438     Standard_Boolean UseBindlessTextures;
439
440     //! Creates default compile-time ray-tracing parameters.
441     RaytracingParams()
442     : StackSize (THE_DEFAULT_STACK_SIZE),
443       NbBounces (THE_DEFAULT_NB_BOUNCES),
444       TransparentShadows (Standard_False),
445       UseBindlessTextures (Standard_False)
446     {
447       //
448     }
449   };
450
451   //! Describes state of OpenGL structure.
452   struct StructState
453   {
454     Standard_Size StructureState;
455     Standard_Size InstancedState;
456
457     //! Creates new structure state.
458     StructState (const Standard_Size theStructureState = 0,
459                  const Standard_Size theInstancedState = 0)
460     : StructureState (theStructureState),
461       InstancedState (theInstancedState)
462     {
463       //
464     }
465
466     //! Creates new structure state.
467     StructState (const OpenGl_Structure* theStructure)
468     {
469       StructureState = theStructure->ModificationState();
470
471       InstancedState = theStructure->InstancedStructure() != NULL ?
472         theStructure->InstancedStructure()->ModificationState() : 0;
473     }
474   };
475
476 protected: //! @name methods related to ray-tracing
477
478   //! Updates 3D scene geometry for ray-tracing.
479   Standard_Boolean updateRaytraceGeometry (const RaytraceUpdateMode      theMode,
480                                            const Standard_Integer        theViewId,
481                                            const Handle(OpenGl_Context)& theGlContext);
482
483   //! Updates 3D scene light sources for ray-tracing.
484   Standard_Boolean updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext);
485
486   //! Updates environment map for ray-tracing.
487   Standard_Boolean updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext);
488
489   //! Checks to see if the OpenGL structure is modified.
490   Standard_Boolean toUpdateStructure (const OpenGl_Structure* theStructure);
491
492   //! Adds OpenGL structure to ray-traced scene geometry.
493   Standard_Boolean addRaytraceStructure (const OpenGl_Structure*       theStructure,
494                                          const Handle(OpenGl_Context)& theGlContext);
495
496   //! Adds OpenGL groups to ray-traced scene geometry.
497   Standard_Boolean addRaytraceGroups (const OpenGl_Structure*       theStructure,
498                                       const Standard_Integer        theStructMat,
499                                       const Standard_ShortReal*     theTransform,
500                                       const Handle(OpenGl_Context)& theGlContext);
501
502   //! Creates ray-tracing material properties.
503   OpenGl_RaytraceMaterial convertMaterial (const OpenGl_AspectFace*      theAspect,
504                                            const Handle(OpenGl_Context)& theGlContext);
505
506   //! Adds OpenGL primitive array to ray-traced scene geometry.
507   OpenGl_TriangleSet* addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
508                                                  const Standard_Integer       theMatID,
509                                                  const OpenGl_Mat4*           theTrans);
510
511   //! Adds vertex indices from OpenGL primitive array to ray-traced scene geometry.
512   Standard_Boolean addRaytraceVertexIndices (OpenGl_TriangleSet&                  theSet,
513                                              const Standard_Integer               theMatID,
514                                              const Standard_Integer               theCount,
515                                              const Standard_Integer               theOffset,
516                                              const OpenGl_PrimitiveArray&         theArray);
517
518   //! Adds OpenGL triangle array to ray-traced scene geometry.
519   Standard_Boolean addRaytraceTriangleArray (OpenGl_TriangleSet&                  theSet,
520                                              const Standard_Integer               theMatID,
521                                              const Standard_Integer               theCount,
522                                              const Standard_Integer               theOffset,
523                                              const Handle(Graphic3d_IndexBuffer)& theIndices);
524
525   //! Adds OpenGL triangle fan array to ray-traced scene geometry.
526   Standard_Boolean addRaytraceTriangleFanArray (OpenGl_TriangleSet&                  theSet,
527                                                 const Standard_Integer               theMatID,
528                                                 const Standard_Integer               theCount,
529                                                 const Standard_Integer               theOffset,
530                                                 const Handle(Graphic3d_IndexBuffer)& theIndices);
531
532   //! Adds OpenGL triangle strip array to ray-traced scene geometry.
533   Standard_Boolean addRaytraceTriangleStripArray (OpenGl_TriangleSet&                  theSet,
534                                                   const Standard_Integer               theMatID,
535                                                   const Standard_Integer               theCount,
536                                                   const Standard_Integer               theOffset,
537                                                   const Handle(Graphic3d_IndexBuffer)& theIndices);
538
539   //! Adds OpenGL quadrangle array to ray-traced scene geometry.
540   Standard_Boolean addRaytraceQuadrangleArray (OpenGl_TriangleSet&                  theSet,
541                                                const Standard_Integer               theMatID,
542                                                const Standard_Integer               theCount,
543                                                const Standard_Integer               theOffset,
544                                                const Handle(Graphic3d_IndexBuffer)& theIndices);
545
546   //! Adds OpenGL quadrangle strip array to ray-traced scene geometry.
547   Standard_Boolean addRaytraceQuadrangleStripArray (OpenGl_TriangleSet&                  theSet,
548                                                     const Standard_Integer               theMatID,
549                                                     const Standard_Integer               theCount,
550                                                     const Standard_Integer               theOffset,
551                                                     const Handle(Graphic3d_IndexBuffer)& theIndices);
552
553   //! Adds OpenGL polygon array to ray-traced scene geometry.
554   Standard_Boolean addRaytracePolygonArray (OpenGl_TriangleSet&                  theSet,
555                                             const Standard_Integer               theMatID,
556                                             const Standard_Integer               theCount,
557                                             const Standard_Integer               theOffset,
558                                             const Handle(Graphic3d_IndexBuffer)& theIndices);
559
560   //! Uploads ray-trace data to the GPU.
561   Standard_Boolean uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext);
562
563   //! Generates shader prefix based on current ray-tracing options.
564   TCollection_AsciiString generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const;
565
566   //! Performs safe exit when shaders initialization fails.
567   Standard_Boolean safeFailBack (const TCollection_ExtendedString& theMessage,
568                                  const Handle(OpenGl_Context)&     theGlContext);
569
570   //! Loads and compiles shader object from specified source.
571   Handle(OpenGl_ShaderObject) initShader (const GLenum                  theType,
572                                           const ShaderSource&           theSource,
573                                           const Handle(OpenGl_Context)& theGlContext);
574
575   //! Initializes OpenGL/GLSL shader programs.
576   Standard_Boolean initRaytraceResources (const Graphic3d_CView&        theCView,
577                                           const Handle(OpenGl_Context)& theGlContext);
578
579   //! Releases OpenGL/GLSL shader programs.
580   void releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext);
581
582   //! Resizes OpenGL frame buffers.
583   Standard_Boolean resizeRaytraceBuffers (const Standard_Integer        theSizeX,
584                                           const Standard_Integer        theSizeY,
585                                           const Handle(OpenGl_Context)& theGlContext);
586
587   //! Generates viewing rays for corners of screen quad.
588   void updateCamera (const OpenGl_Mat4& theOrientation,
589                      const OpenGl_Mat4& theViewMapping,
590                      OpenGl_Vec3*       theOrigins,
591                      OpenGl_Vec3*       theDirects,
592                      OpenGl_Mat4&       theUnView);
593
594   //! Binds ray-trace textures to corresponding texture units.
595   void bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext);
596
597   //! Unbinds ray-trace textures from corresponding texture unit.
598   void unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext);
599
600   //! Sets uniform state for the given ray-tracing shader program.
601   Standard_Boolean setUniformState (const Graphic3d_CView&        theCView,
602                                     const OpenGl_Vec3*            theOrigins,
603                                     const OpenGl_Vec3*            theDirects,
604                                     const OpenGl_Mat4&            theUnviewMat,
605                                     const Standard_Integer        theProgramId,
606                                     const Handle(OpenGl_Context)& theGlContext);
607
608   //! Runs ray-tracing shader programs.
609   Standard_Boolean runRaytraceShaders (const Graphic3d_CView&        theCView,
610                                        const Standard_Integer        theSizeX,
611                                        const Standard_Integer        theSizeY,
612                                        const OpenGl_Vec3*            theOrigins,
613                                        const OpenGl_Vec3*            theDirects,
614                                        const OpenGl_Mat4&            theUnviewMat,
615                                        OpenGl_FrameBuffer*           theReadDrawFbo,
616                                        const Handle(OpenGl_Context)& theGlContext);
617
618   //! Redraws the window using OpenGL/GLSL ray-tracing.
619   Standard_Boolean raytrace (const Graphic3d_CView&        theCView,
620                              const Standard_Integer        theSizeX,
621                              const Standard_Integer        theSizeY,
622                              OpenGl_FrameBuffer*           theReadDrawFbo,
623                              const Handle(OpenGl_Context)& theGlContext);
624
625 protected: //! @name fields related to ray-tracing
626
627   //! Result of shaders initialization.
628   RaytraceInitStatus myRaytraceInitStatus;
629
630   //! Is geometry data valid?
631   Standard_Boolean myIsRaytraceDataValid;
632
633   //! Warning about missing extension GL_ARB_bindless_texture has been displayed?
634   Standard_Boolean myIsRaytraceWarnTextures;
635
636   //! 3D scene geometry data for ray-tracing.
637   OpenGl_RaytraceGeometry myRaytraceGeometry;
638
639   //! Compile-time ray-tracing parameters.
640   RaytracingParams myRaytraceParameters;
641
642   //! Radius of bounding sphere of the scene.
643   Standard_ShortReal myRaytraceSceneRadius;
644   //! Scene epsilon to prevent self-intersections.
645   Standard_ShortReal myRaytraceSceneEpsilon;
646
647   //! OpenGL/GLSL source of ray-tracing fragment shader.
648   ShaderSource myRaytraceShaderSource;
649   //! OpenGL/GLSL source of adaptive-AA fragment shader.
650   ShaderSource myPostFSAAShaderSource;
651
652   //! OpenGL/GLSL ray-tracing fragment shader.
653   Handle(OpenGl_ShaderObject) myRaytraceShader;
654   //! OpenGL/GLSL adaptive-AA fragment shader.
655   Handle(OpenGl_ShaderObject) myPostFSAAShader;
656
657   //! OpenGL/GLSL ray-tracing shader program.
658   Handle(OpenGl_ShaderProgram) myRaytraceProgram;
659   //! OpenGL/GLSL adaptive-AA shader program.
660   Handle(OpenGl_ShaderProgram) myPostFSAAProgram;
661
662   //! Texture buffer of data records of bottom-level BVH nodes.
663   Handle(OpenGl_TextureBufferArb) mySceneNodeInfoTexture;
664   //! Texture buffer of minimum points of bottom-level BVH nodes.
665   Handle(OpenGl_TextureBufferArb) mySceneMinPointTexture;
666   //! Texture buffer of maximum points of bottom-level BVH nodes.
667   Handle(OpenGl_TextureBufferArb) mySceneMaxPointTexture;
668   //! Texture buffer of transformations of high-level BVH nodes.
669   Handle(OpenGl_TextureBufferArb) mySceneTransformTexture;
670
671   //! Texture buffer of vertex coords.
672   Handle(OpenGl_TextureBufferArb) myGeometryVertexTexture;
673   //! Texture buffer of vertex normals.
674   Handle(OpenGl_TextureBufferArb) myGeometryNormalTexture;
675   //! Texture buffer of vertex UV coords.
676   Handle(OpenGl_TextureBufferArb) myGeometryTexCrdTexture;
677   //! Texture buffer of triangle indices.
678   Handle(OpenGl_TextureBufferArb) myGeometryTriangTexture;
679
680   //! Texture buffer of material properties.
681   Handle(OpenGl_TextureBufferArb) myRaytraceMaterialTexture;
682   //! Texture buffer of light source properties.
683   Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture;
684
685   //! 1st framebuffer (FBO) to perform adaptive FSAA.
686   Handle(OpenGl_FrameBuffer) myRaytraceFBO1;
687   //! 2nd framebuffer (FBO) to perform adaptive FSAA.
688   Handle(OpenGl_FrameBuffer) myRaytraceFBO2;
689   //! Framebuffer (FBO) for preliminary OpenGL output.
690   Handle(OpenGl_FrameBuffer) myOpenGlFBO;
691
692   //! Vertex buffer (VBO) for drawing dummy quad.
693   OpenGl_VertexBuffer myRaytraceScreenQuad;
694
695   //! Cached locations of frequently used uniform variables.
696   Standard_Integer myUniformLocations[2][OpenGl_RT_NbVariables];
697
698   //! State of OpenGL structures reflected to ray-tracing.
699   std::map<const OpenGl_Structure*, StructState> myStructureStates;
700
701   //! PrimitiveArray to TriangleSet map for scene partial update.
702   std::map<Standard_Size, OpenGl_TriangleSet*> myArrayToTrianglesMap;
703
704   //! Graphical ray-tracing filter to filter out all raytracable structures.
705   Handle(OpenGl_RaytraceFilter) myRaytraceFilter;
706
707   //! Marks if environment map should be updated.
708   Standard_Boolean myToUpdateEnvironmentMap;
709
710   //! State of OpenGL layer list.
711   Standard_Size myLayersModificationStatus;
712
713 public:
714
715   DEFINE_STANDARD_ALLOC
716   DEFINE_STANDARD_RTTI(OpenGl_View) // Type definition
717
718   friend class OpenGl_Workspace;
719
720 };
721
722 #endif // _OpenGl_View_Header