0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[occt.git] / src / OpenGl / OpenGl_Workspace.hxx
index 2b1ba45..9b330cf 100644 (file)
 #ifndef _OpenGl_Workspace_Header
 #define _OpenGl_Workspace_Header
 
-#include <map>
-#include <set>
-
-#include <Handle_OpenGl_Workspace.hxx>
-#include <OpenGl_Window.hxx>
-
-#include <TColStd_Array2OfReal.hxx>
-#include <Quantity_Color.hxx>
-#include <Graphic3d_CView.hxx>
-#include <Graphic3d_TypeOfComposition.hxx>
-#include <Graphic3d_TypeOfTexture.hxx>
-#include <Graphic3d_PtrFrameBuffer.hxx>
 #include <Graphic3d_BufferType.hxx>
-#include <Handle_Graphic3d_TextureParams.hxx>
-
-#include <Aspect_CLayer2d.hxx>
-#include <Aspect_Handle.hxx>
-#include <Aspect_PrintAlgo.hxx>
-#include <Aspect_PolygonOffsetMode.hxx>
-
-#include <InterfaceGraphic_Graphic3d.hxx>
-#include <InterfaceGraphic_Visual3d.hxx>
 
-#include <NCollection_Sequence.hxx>
-
-#include <OpenGl_AspectFace.hxx>
+#include <OpenGl_Aspects.hxx>
+#include <OpenGl_CappingAlgo.hxx>
 #include <OpenGl_FrameBuffer.hxx>
+#include <OpenGl_Material.hxx>
 #include <OpenGl_Matrix.hxx>
-#include <OpenGl_NamedStatus.hxx>
-#include <OpenGl_PrinterContext.hxx>
-#include <OpenGl_SceneGeometry.hxx>
-#include <OpenGl_TextParam.hxx>
-#include <OpenGl_RenderFilter.hxx>
-#include <OpenGl_Vec.hxx>
-#include <OpenGl_LineAttributes.hxx>
-
-#include <Handle_OpenGl_View.hxx>
-#include <Handle_OpenGl_Texture.hxx>
-
 #include <OpenGl_ShaderObject.hxx>
 #include <OpenGl_ShaderProgram.hxx>
+#include <OpenGl_TextParam.hxx>
 #include <OpenGl_TextureBufferArb.hxx>
+#include <OpenGl_RenderFilter.hxx>
+#include <OpenGl_Vec.hxx>
+#include <OpenGl_Window.hxx>
 
-class OpenGl_AspectLine;
-class OpenGl_AspectMarker;
-class OpenGl_AspectText;
-class OpenGl_FrameBuffer;
-class OpenGl_Structure;
-class OpenGl_TriangleSet;
-class OpenGl_Element;
+class OpenGl_View;
 class Image_PixMap;
 
-//! OpenGL material definition
-struct OpenGl_Material
+class OpenGl_Workspace;
+DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient)
+
+//! Rendering workspace.
+//! Provides methods to render primitives and maintain GL state.
+class OpenGl_Workspace : public Standard_Transient
 {
+public:
 
-  OpenGl_Vec4 Ambient;  //!< ambient reflection coefficient
-  OpenGl_Vec4 Diffuse;  //!< diffuse reflection coefficient
-  OpenGl_Vec4 Specular; //!< glossy  reflection coefficient
-  OpenGl_Vec4 Emission; //!< material emission
-  OpenGl_Vec4 Params;   //!< extra packed parameters
+  //! Constructor of rendering workspace.
+  Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow);
 
-  Standard_ShortReal  Shine()              const { return Params.x(); }
-  Standard_ShortReal& ChangeShine()              { return Params.x(); }
+  //! Destructor
+  virtual ~OpenGl_Workspace() {}
 
-  Standard_ShortReal  Transparency()       const { return Params.y(); }
-  Standard_ShortReal& ChangeTransparency()       { return Params.y(); }
+  //! Activate rendering context.
+  Standard_EXPORT Standard_Boolean Activate();
 
-  //! Initialize material
-  void Init (const OPENGL_SURF_PROP& theProps);
+  OpenGl_View* View() const { return myView; }
 
-  //! Returns packed (serialized) representation of material properties
-  const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
-  static Standard_Integer NbOfVec4() { return 5; }
+  const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; }
 
-};
+  Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
 
-DEFINE_STANDARD_HANDLE (OpenGl_RaytraceFilter, OpenGl_RenderFilter)
+  Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo);
 
-//! Graphical ray-tracing filter.
-//! Filters out all raytracable structures.
-class OpenGl_RaytraceFilter : public OpenGl_RenderFilter
-{
-public:
+  Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
+                               Image_PixMap&                     theImage,
+                               const Graphic3d_BufferType&       theBufferType);
 
-  //! Default constructor.
-  OpenGl_RaytraceFilter() {}
+  Standard_EXPORT Standard_Integer Width()  const;
 
-  //! Returns the previously set filter.
-  const Handle(OpenGl_RenderFilter)& PrevRenderFilter()
-  {
-    return myPrevRenderFilter;
-  }
+  Standard_EXPORT Standard_Integer Height() const;
 
-  //! Remembers the previously set filter.
-  void SetPrevRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
+  //! Setup Z-buffer usage flag (without affecting GL state!).
+  //! Returns previously set flag.
+  Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse)
   {
-    myPrevRenderFilter = theFilter;
+    const Standard_Boolean wasUsed = myUseZBuffer;
+    myUseZBuffer = theToUse;
+    return wasUsed;
   }
 
-  //! Checks whether the element can be rendered or not.
-  //! @param theElement [in] the element to check.
-  //! @return True if element can be rendered.
-  virtual Standard_Boolean CanRender (const OpenGl_Element* theElement);
-
-private:
-
-  Handle(OpenGl_RenderFilter) myPrevRenderFilter;
-
-public:
+  //! @return true if usage of Z buffer is enabled.
+  Standard_Boolean& UseZBuffer() { return myUseZBuffer; }
 
-  DEFINE_STANDARD_RTTI(OpenGl_RaytraceFilter)
-};
+  //! @return true if depth writing is enabled.
+  Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; }
 
-//! Represents window with GL context.
-//! Provides methods to render primitives and maintain GL state.
-class OpenGl_Workspace : public OpenGl_Window
-{
-public:
+  //! Configure default polygon offset parameters.
+  //! Return previous settings.
+  Standard_EXPORT Graphic3d_PolygonOffset SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset);
 
-  //! Main constructor - prepare GL context for specified window.
-  OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
-                    const CALL_DEF_WINDOW&        theCWindow,
-                    Aspect_RenderingContext       theGContext,
-                    const Handle(OpenGl_Caps)&    theCaps,
-                    const Handle(OpenGl_Context)& theShareCtx);
+  //// RELATED TO STATUS ////
 
-  //! Destructor
-  virtual ~OpenGl_Workspace();
+  //! Return true if active group might activate face culling (e.g. primitives are closed).
+  bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
 
-  void SetActiveView (const Handle(OpenGl_View)& theView,
-                      const Standard_Integer     theViewId)
+  //! Allow or disallow face culling.
+  //! This call does NOT affect current state of back face culling;
+  //! ApplyAspectFace() should be called to update state.
+  bool SetAllowFaceCulling (bool theToAllow)
   {
-    myView   = theView;
-    myViewId = theViewId;
+    const bool wasAllowed = myToAllowFaceCulling;
+    myToAllowFaceCulling = theToAllow;
+    return wasAllowed;
   }
 
-  const Handle(OpenGl_View)& ActiveView() const { return myView; }
-
-  Standard_Integer ActiveViewId() const { return myViewId; }
+  //! Return true if following structures should apply highlight color.
+  bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
 
-  //! Redraw the window.
-  void Redraw (const Graphic3d_CView& theCView,
-               const Aspect_CLayer2d& theCUnderLayer,
-               const Aspect_CLayer2d& theCOverLayer);
+  //! Return highlight style.
+  const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; }
 
-  Standard_Boolean SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer);
-  void RedrawImmediate (const Graphic3d_CView& theCView,
-                        const Aspect_CLayer2d& theCUnderLayer,
-                        const Aspect_CLayer2d& theCOverLayer);
+  //! Set highlight style.
+  void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) {  myHighlightStyle = theStyle; }
 
-  void Invalidate (const Graphic3d_CView& /*theCView*/)
+  //! Return edge color taking into account highlight flag.
+  const OpenGl_Vec4& EdgeColor() const
   {
-    myBackBufferRestored = Standard_False;
+    return !myHighlightStyle.IsNull()
+         ?  myHighlightStyle->ColorRGBA()
+         :  myAspectsSet->Aspect()->EdgeColorRGBA();
   }
 
-  //! Special method to perform printing.
-  //! System-specific and currently only Win platform implemented.
-  Standard_Boolean Print (const Handle(OpenGl_PrinterContext)& thePrintContext,
-                          const Graphic3d_CView& theCView,
-                          const Aspect_CLayer2d& theCUnderLayer,
-                          const Aspect_CLayer2d& theCOverLayer,
-                          const Aspect_Handle    theHPrintDC,
-                          const Standard_Boolean theToShowBackground,
-                          const Standard_CString theFileName,
-                          const Aspect_PrintAlgo thePrintAlgorithm,
-                          const Standard_Real    theScaleFactor);
-
-  const Handle(OpenGl_PrinterContext)& PrinterContext() const
+  //! Return Interior color taking into account highlight flag.
+  const OpenGl_Vec4& InteriorColor() const
   {
-    return myPrintContext;
+    return !myHighlightStyle.IsNull()
+         ?  myHighlightStyle->ColorRGBA()
+         :  myAspectsSet->Aspect()->InteriorColorRGBA();
   }
 
-  void DisplayCallback (const Graphic3d_CView& theCView, int theReason);
-
-  Graphic3d_PtrFrameBuffer FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight);
-  void FBORelease (Graphic3d_PtrFrameBuffer theFBOPtr);
-  Standard_Boolean BufferDump (OpenGl_FrameBuffer*         theFBOPtr,
-                               Image_PixMap&               theImage,
-                               const Graphic3d_BufferType& theBufferType);
-
-  Standard_Boolean& UseZBuffer()   { return myUseZBuffer; }
-  Standard_Boolean& UseDepthTest() { return myUseDepthTest; }
-  Standard_Boolean& UseGLLight()   { return myUseGLLight; }
-
-  Standard_Integer AntiAliasingMode() const { return myAntiAliasingMode; }
-
-  //// RELATED TO STATUS ////
-
-  Standard_Integer   NamedStatus;
+  //! Return text color taking into account highlight flag.
+  const OpenGl_Vec4& TextColor() const
+  {
+    return !myHighlightStyle.IsNull()
+         ?  myHighlightStyle->ColorRGBA()
+         :  myAspectsSet->Aspect()->ColorRGBA();
+  }
 
-  const TEL_COLOUR* HighlightColor;
+  //! Return text Subtitle color taking into account highlight flag.
+  const OpenGl_Vec4& TextSubtitleColor() const
+  {
+    return !myHighlightStyle.IsNull()
+         ?  myHighlightStyle->ColorRGBA()
+         :  myAspectsSet->Aspect()->ColorSubTitleRGBA();
+  }
 
-  const OpenGl_AspectLine*   SetAspectLine   (const OpenGl_AspectLine*   theAspect);
-  const OpenGl_AspectFace*   SetAspectFace   (const OpenGl_AspectFace*   theAspect);
-  const OpenGl_AspectMarker* SetAspectMarker (const OpenGl_AspectMarker* theAspect);
-  const OpenGl_AspectText*   SetAspectText   (const OpenGl_AspectText*   theAspect);
+  //! Currently set aspects (can differ from applied).
+  const OpenGl_Aspects* Aspects() const { return myAspectsSet; }
 
-  void SetTextParam (const OpenGl_TextParam* theParam) { TextParam_set = theParam; }
+  //! Assign new aspects (will be applied within ApplyAspects()).
+  Standard_EXPORT const OpenGl_Aspects* SetAspects (const OpenGl_Aspects* theAspect);
 
-  //// THESE METHODS ARE EXPORTED AS THEY PROVIDE STATE INFO TO USERDRAW
-  Standard_EXPORT const OpenGl_AspectLine*   AspectLine   (const Standard_Boolean theWithApply);
-  Standard_EXPORT const OpenGl_AspectFace*   AspectFace   (const Standard_Boolean theWithApply);
-  Standard_EXPORT const OpenGl_AspectMarker* AspectMarker (const Standard_Boolean theWithApply);
-  Standard_EXPORT const OpenGl_AspectText*   AspectText   (const Standard_Boolean theWithApply);
-  inline const OpenGl_TextParam* AspectTextParams() const
-  {
-    return TextParam_applied;
-  }
+  //! Apply aspects.
+  //! @return aspect set by SetAspects()
+  Standard_EXPORT const OpenGl_Aspects* ApplyAspects();
 
-  //! Clear the applied aspect state.
+  //! Clear the applied aspect state to default values.
   void ResetAppliedAspect();
 
-  Standard_EXPORT Handle(OpenGl_Texture) DisableTexture();
-  Standard_EXPORT Handle(OpenGl_Texture) EnableTexture (const Handle(OpenGl_Texture)&          theTexture,
-                                                        const Handle(Graphic3d_TextureParams)& theParams = NULL);
-  const Handle(OpenGl_Texture)& ActiveTexture() const { return myTextureBound; }
+  //! Get rendering filter.
+  //! @sa ShouldRender()
+  Standard_Integer RenderFilter() const { return myRenderFilter; }
 
   //! Set filter for restricting rendering of particular elements.
-  //! Filter can be applied for rendering passes used by recursive
-  //! rendering algorithms for rendering elements of groups.
-  //! @param theFilter [in] the filter instance.
-  inline void SetRenderFilter (const Handle(OpenGl_RenderFilter)& theFilter)
-  {
-    myRenderFilter = theFilter;
-  }
+  //! @sa ShouldRender()
+  void SetRenderFilter (Standard_Integer theFilter) { myRenderFilter = theFilter; }
 
-  //! Get rendering filter.
-  //! @return filter instance.
-  inline const Handle(OpenGl_RenderFilter)& GetRenderFilter() const
-  {
-    return myRenderFilter;
-  }
+  //! Checks whether the element can be rendered or not.
+  //! @param theElement [in] the element to check
+  //! @return True if element can be rendered
+  bool ShouldRender (const OpenGl_Element* theElement);
+
+  //! Return the number of skipped transparent elements within active OpenGl_RenderFilter_OpaqueOnly filter.
+  //! @sa OpenGl_LayerList::Render()
+  Standard_Integer NbSkippedTransparentElements() { return myNbSkippedTranspElems; }
+
+  //! Reset skipped transparent elements counter.
+  //! @sa OpenGl_LayerList::Render()
+  void ResetSkippedCounter() { myNbSkippedTranspElems = 0; }
 
   //! @return applied view matrix.
   inline const OpenGl_Matrix* ViewMatrix() const { return ViewMatrix_applied; }
@@ -256,101 +183,54 @@ public:
   //! @return applied model structure matrix.
   inline const OpenGl_Matrix* ModelMatrix() const { return StructureMatrix_applied; }
 
-  //! Sets and applies current polygon offset.
-  void SetPolygonOffset (int theMode, Standard_ShortReal theFactor, Standard_ShortReal theUnits);
-
-  //! Returns currently applied polygon offset params.
-  const TEL_POFFSET_PARAM& AppliedPolygonOffset() { return PolygonOffset_applied; }
+  //! Returns face aspect for none culling mode.
+  const OpenGl_Aspects& NoneCulling() const { return myNoneCulling; }
 
-  //! @return true if clipping algorithm enabled
-  inline Standard_Boolean IsCullingEnabled() const { return myIsCullingEnabled; }
+  //! Returns face aspect for front face culling mode.
+  const OpenGl_Aspects& FrontCulling() const { return myFrontCulling; }
 
-  //! Returns framebuffer storing cached main presentation of the view.
-  const Handle(OpenGl_FrameBuffer)& ResultFBO() const { return myResultFBO; }
+  //! Sets a new environment texture.
+  void SetEnvironmentTexture (const Handle(OpenGl_TextureSet)& theTexture) { myEnvironmentTexture = theTexture; }
 
-protected:
-
-  //! Copy content of Back buffer to the Front buffer
-  void copyBackToFront();
-
-  virtual Standard_Boolean Activate();
-
-  void redraw1 (const Graphic3d_CView& theCView,
-                const Aspect_CLayer2d& theCUnderLayer,
-                const Aspect_CLayer2d& theCOverLayer);
-
-  //! Blit snapshot containing main scene (myResultFBO or BackBuffer)
-  //! into presentation buffer (myResultFBO->offscreen FBO or myResultFBO->BackBuffer or BackBuffer->FrontBuffer),
-  //! and redraw immediate structures on top.
-  //!
-  //! When scene caching is disabled (myTransientDrawToFront, no double buffer in window, etc.),
-  //! the first step (blitting) will be skipped.
-  //!
-  //! @return false if immediate structures has been rendered directly into FrontBuffer and Buffer Swap should not be called.
-  bool redrawImmediate (const Graphic3d_CView& theCView,
-                        const Aspect_CLayer2d& theCUnderLayer,
-                        const Aspect_CLayer2d& theCOverLayer,
-                        OpenGl_FrameBuffer*    theTargetFBO,
-                        const Standard_Boolean theIsPartialUpdate = Standard_False);
-
-  void updateMaterial (const int theFlag);
-
-  void setTextureParams (Handle(OpenGl_Texture)&                theTexture,
-                         const Handle(Graphic3d_TextureParams)& theParams);
+  //! Returns environment texture.
+  const Handle(OpenGl_TextureSet)& EnvironmentTexture() const { return myEnvironmentTexture; }
 
 protected: //! @name protected fields
 
-  //! Framebuffer stores cached main presentation of the view (without presentation of immediate layers).
-  Handle(OpenGl_FrameBuffer) myResultFBO;
-  //! Special flag which is invalidated when myResultFBO can not be blitted for some reason (e.g. driver bugs).
-  Standard_Boolean           myHasFboBlit;
-
-  //! Vertices for full-screen quad rendering.
-  OpenGl_VertexBuffer        myFullScreenQuad;
-
-  Handle(OpenGl_PrinterContext) myPrintContext;
-  Handle(OpenGl_View)           myView;
-  Handle(OpenGl_LineAttributes) myLineAttribs;
-  Standard_Integer       myViewId;
-  Standard_Integer       myAntiAliasingMode;
-  Standard_Boolean       myTransientDrawToFront; //!< optimization flag for immediate mode (to render directly to the front buffer)
-  Standard_Boolean       myBackBufferRestored;
-  Standard_Boolean       myIsImmediateDrawn;     //!< flag indicates that immediate mode buffer contains some data
+  OpenGl_View*           myView;
+  Handle(OpenGl_Window)  myWindow;
+  Handle(OpenGl_Context) myGlContext;
   Standard_Boolean       myUseZBuffer;
-  Standard_Boolean       myUseDepthTest;
-  Standard_Boolean       myUseGLLight;
-  Standard_Boolean       myIsCullingEnabled;     //!< frustum culling flag
-
-  unsigned int           myFrameCounter;         //!< redraw counter, for debugging
+  Standard_Boolean       myUseDepthWrite;
+  OpenGl_Aspects         myNoneCulling;
+  OpenGl_Aspects         myFrontCulling;
 
 protected: //! @name fields related to status
 
-  Handle(OpenGl_RenderFilter) myRenderFilter;
-  Handle(OpenGl_Texture) myTextureBound;    //!< currently bound texture (managed by OpenGl_AspectFace and OpenGl_View environment texture)
-  const OpenGl_AspectLine *AspectLine_set, *AspectLine_applied;
-  const OpenGl_AspectFace *AspectFace_set, *AspectFace_applied;
-  const OpenGl_AspectMarker *AspectMarker_set, *AspectMarker_applied;
-  const OpenGl_AspectText *AspectText_set, *AspectText_applied;
+  Standard_Integer myNbSkippedTranspElems; //!< counter of skipped transparent elements for OpenGl_LayerList two rendering passes method
+  Standard_Integer myRenderFilter;         //!< active filter for skipping rendering of elements by some criteria (multiple render passes)
+
+  OpenGl_Aspects   myDefaultAspects;
+  const OpenGl_Aspects*      myAspectsSet;
+  Handle(Graphic3d_Aspects)  myAspectsApplied;
 
-  const OpenGl_TextParam *TextParam_set, *TextParam_applied;
+  Handle(Graphic3d_PresentationAttributes) myAspectFaceAppliedWithHL;
 
   const OpenGl_Matrix* ViewMatrix_applied;
   const OpenGl_Matrix* StructureMatrix_applied;
 
-  OpenGl_Material myMatFront;    //!< current front material state (cached to reduce GL context updates)
-  OpenGl_Material myMatBack;     //!< current back  material state
-  OpenGl_Material myMatTmp;      //!< temporary variable
-  TelCullMode     myCullingMode; //!< back face culling mode, applied from face aspect
+  bool            myToAllowFaceCulling; //!< allow back face culling
+  Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style
 
   OpenGl_Matrix myModelViewMatrix; //!< Model matrix with applied structure transformations
 
-  TEL_POFFSET_PARAM PolygonOffset_applied; //!< Currently applied polygon offset.
+  OpenGl_Aspects myAspectFaceHl; //!< Hiddenline aspect
 
-  OpenGl_AspectFace myAspectFaceHl; //!< Hiddenline aspect
+  Handle(OpenGl_TextureSet) myEnvironmentTexture;
 
 public: //! @name type definition
 
-  DEFINE_STANDARD_RTTI(OpenGl_Workspace)
+  DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
   DEFINE_STANDARD_ALLOC
 
 };