Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-02 09:13:33

0001 // Created on: 2011-09-20
0002 // Created by: Sergey ZERCHANINOV
0003 // Copyright (c) 2011-2013 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef OpenGl_Workspace_HeaderFile
0017 #define OpenGl_Workspace_HeaderFile
0018 
0019 #include <Graphic3d_BufferType.hxx>
0020 #include <Graphic3d_PresentationAttributes.hxx>
0021 
0022 #include <OpenGl_Aspects.hxx>
0023 #include <OpenGl_Vec.hxx>
0024 
0025 class OpenGl_FrameBuffer;
0026 class OpenGl_Group;
0027 class OpenGl_View;
0028 class OpenGl_Window;
0029 class Image_PixMap;
0030 
0031 //! Rendering workspace.
0032 //! Provides methods to render primitives and maintain GL state.
0033 class OpenGl_Workspace : public Standard_Transient
0034 {
0035 public:
0036   //! Constructor of rendering workspace.
0037   Standard_EXPORT OpenGl_Workspace(OpenGl_View*                      theView,
0038                                    const occ::handle<OpenGl_Window>& theWindow);
0039 
0040   //! Destructor
0041   ~OpenGl_Workspace() override = default;
0042 
0043   //! Activate rendering context.
0044   Standard_EXPORT bool Activate();
0045 
0046   OpenGl_View* View() const { return myView; }
0047 
0048   const occ::handle<OpenGl_Context>& GetGlContext() { return myGlContext; }
0049 
0050   Standard_EXPORT occ::handle<OpenGl_FrameBuffer> FBOCreate(const int theWidth,
0051                                                             const int theHeight);
0052 
0053   Standard_EXPORT void FBORelease(occ::handle<OpenGl_FrameBuffer>& theFbo);
0054 
0055   bool BufferDump(const occ::handle<OpenGl_FrameBuffer>& theFbo,
0056                   Image_PixMap&                          theImage,
0057                   const Graphic3d_BufferType&            theBufferType);
0058 
0059   Standard_EXPORT int Width() const;
0060 
0061   Standard_EXPORT int Height() const;
0062 
0063   //! Setup Z-buffer usage flag (without affecting GL state!).
0064   //! Returns previously set flag.
0065   bool SetUseZBuffer(const bool theToUse)
0066   {
0067     const bool wasUsed = myUseZBuffer;
0068     myUseZBuffer       = theToUse;
0069     return wasUsed;
0070   }
0071 
0072   //! @return true if usage of Z buffer is enabled.
0073   bool& UseZBuffer() { return myUseZBuffer; }
0074 
0075   //! @return true if depth writing is enabled.
0076   bool& UseDepthWrite() { return myUseDepthWrite; }
0077 
0078   //! Configure default polygon offset parameters.
0079   //! Return previous settings.
0080   Standard_EXPORT Graphic3d_PolygonOffset
0081     SetDefaultPolygonOffset(const Graphic3d_PolygonOffset& theOffset);
0082 
0083   //// RELATED TO STATUS ////
0084 
0085   //! Return true if active group might activate face culling (e.g. primitives are closed).
0086   bool ToAllowFaceCulling() const { return myToAllowFaceCulling; }
0087 
0088   //! Allow or disallow face culling.
0089   //! This call does NOT affect current state of back face culling;
0090   //! ApplyAspectFace() should be called to update state.
0091   bool SetAllowFaceCulling(bool theToAllow)
0092   {
0093     const bool wasAllowed = myToAllowFaceCulling;
0094     myToAllowFaceCulling  = theToAllow;
0095     return wasAllowed;
0096   }
0097 
0098   //! Return true if following structures should apply highlight color.
0099   bool ToHighlight() const { return !myHighlightStyle.IsNull(); }
0100 
0101   //! Return highlight style.
0102   const occ::handle<Graphic3d_PresentationAttributes>& HighlightStyle() const
0103   {
0104     return myHighlightStyle;
0105   }
0106 
0107   //! Set highlight style.
0108   void SetHighlightStyle(const occ::handle<Graphic3d_PresentationAttributes>& theStyle)
0109   {
0110     myHighlightStyle = theStyle;
0111   }
0112 
0113   //! Return edge color taking into account highlight flag.
0114   const NCollection_Vec4<float>& EdgeColor() const
0115   {
0116     return !myHighlightStyle.IsNull() ? myHighlightStyle->ColorRGBA()
0117                                       : myAspectsSet->Aspect()->EdgeColorRGBA();
0118   }
0119 
0120   //! Return Interior color taking into account highlight flag.
0121   const NCollection_Vec4<float>& InteriorColor() const
0122   {
0123     return !myHighlightStyle.IsNull() ? myHighlightStyle->ColorRGBA()
0124                                       : myAspectsSet->Aspect()->InteriorColorRGBA();
0125   }
0126 
0127   //! Return back interior color taking into account highlight and distinguish flags.
0128   const NCollection_Vec4<float>& BackInteriorColor() const
0129   {
0130     return !myHighlightStyle.IsNull()              ? myHighlightStyle->ColorRGBA()
0131            : myAspectsSet->Aspect()->Distinguish() ? myAspectsSet->Aspect()->BackInteriorColorRGBA()
0132                                                    : myAspectsSet->Aspect()->InteriorColorRGBA();
0133   }
0134 
0135   //! Return text color taking into account highlight flag.
0136   const NCollection_Vec4<float>& TextColor() const
0137   {
0138     return !myHighlightStyle.IsNull() ? myHighlightStyle->ColorRGBA()
0139                                       : myAspectsSet->Aspect()->ColorRGBA();
0140   }
0141 
0142   //! Return text Subtitle color taking into account highlight flag.
0143   const NCollection_Vec4<float>& TextSubtitleColor() const
0144   {
0145     return !myHighlightStyle.IsNull() ? myHighlightStyle->ColorRGBA()
0146                                       : myAspectsSet->Aspect()->ColorSubTitleRGBA();
0147   }
0148 
0149   //! Currently set aspects (can differ from applied).
0150   const OpenGl_Aspects* Aspects() const { return myAspectsSet; }
0151 
0152   //! Assign new aspects (will be applied within ApplyAspects()).
0153   Standard_EXPORT const OpenGl_Aspects* SetAspects(const OpenGl_Aspects* theAspect);
0154 
0155   //! Return TextureSet from set Aspects or Environment texture.
0156   const occ::handle<OpenGl_TextureSet>& TextureSet() const
0157   {
0158     const occ::handle<OpenGl_TextureSet>& aTextureSet =
0159       myAspectsSet->TextureSet(myGlContext, ToHighlight());
0160     return !aTextureSet.IsNull() || myAspectsSet->Aspect()->ToMapTexture() ? aTextureSet
0161                                                                            : myEnvironmentTexture;
0162   }
0163 
0164   //! Apply aspects.
0165   //! @param theToBindTextures flag to bind texture set defined by applied aspect
0166   //! @return aspect set by SetAspects()
0167   Standard_EXPORT const OpenGl_Aspects* ApplyAspects(bool theToBindTextures = true);
0168 
0169   //! Clear the applied aspect state to default values.
0170   void ResetAppliedAspect();
0171 
0172   //! Get rendering filter.
0173   //! @sa ShouldRender()
0174   int RenderFilter() const { return myRenderFilter; }
0175 
0176   //! Set filter for restricting rendering of particular elements.
0177   //! @sa ShouldRender()
0178   void SetRenderFilter(int theFilter) { myRenderFilter = theFilter; }
0179 
0180   //! Checks whether the element can be rendered or not.
0181   //! @param[in] theElement  the element to check
0182   //! @param[in] theGroup    the group containing the element
0183   //! @return True if element can be rendered
0184   bool ShouldRender(const OpenGl_Element* theElement, const OpenGl_Group* theGroup);
0185 
0186   //! Return the number of skipped transparent elements within active OpenGl_RenderFilter_OpaqueOnly
0187   //! filter.
0188   //! @sa OpenGl_LayerList::Render()
0189   int NbSkippedTransparentElements() { return myNbSkippedTranspElems; }
0190 
0191   //! Reset skipped transparent elements counter.
0192   //! @sa OpenGl_LayerList::Render()
0193   void ResetSkippedCounter() { myNbSkippedTranspElems = 0; }
0194 
0195   //! Returns face aspect for none culling mode.
0196   const OpenGl_Aspects& NoneCulling() const { return myNoneCulling; }
0197 
0198   //! Returns face aspect for front face culling mode.
0199   const OpenGl_Aspects& FrontCulling() const { return myFrontCulling; }
0200 
0201   //! Sets a new environment texture.
0202   void SetEnvironmentTexture(const occ::handle<OpenGl_TextureSet>& theTexture)
0203   {
0204     myEnvironmentTexture = theTexture;
0205   }
0206 
0207   //! Returns environment texture.
0208   const occ::handle<OpenGl_TextureSet>& EnvironmentTexture() const { return myEnvironmentTexture; }
0209 
0210   //! Dumps the content of me into the stream
0211   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, int theDepth = -1) const;
0212 
0213 protected: //! @name protected fields
0214   OpenGl_View*                myView;
0215   occ::handle<OpenGl_Window>  myWindow;
0216   occ::handle<OpenGl_Context> myGlContext;
0217   bool                        myUseZBuffer;
0218   bool                        myUseDepthWrite;
0219   OpenGl_Aspects              myNoneCulling;
0220   OpenGl_Aspects              myFrontCulling;
0221 
0222 protected:           //! @name fields related to status
0223                      // clang-format off
0224   int myNbSkippedTranspElems; //!< counter of skipped transparent elements for OpenGl_LayerList two rendering passes method
0225   int myRenderFilter;         //!< active filter for skipping rendering of elements by some criteria (multiple render passes)
0226                      // clang-format on
0227 
0228   OpenGl_Aspects                 myDefaultAspects;
0229   const OpenGl_Aspects*          myAspectsSet;
0230   occ::handle<Graphic3d_Aspects> myAspectsApplied;
0231 
0232   occ::handle<Graphic3d_PresentationAttributes> myAspectFaceAppliedWithHL;
0233 
0234   bool                                          myToAllowFaceCulling; //!< allow back face culling
0235   occ::handle<Graphic3d_PresentationAttributes> myHighlightStyle;     //!< active highlight style
0236 
0237   OpenGl_Aspects myAspectFaceHl; //!< Hiddenline aspect
0238 
0239   occ::handle<OpenGl_TextureSet> myEnvironmentTexture;
0240 
0241 public: //! @name type definition
0242   DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace, Standard_Transient)
0243   DEFINE_STANDARD_ALLOC
0244 };
0245 
0246 #endif // _OpenGl_Workspace_Header