1 // Created on: 2011-09-20 2 // Created by: Sergey ZERCHANINOV 3 // Copyright (c) 2011-2013 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_Workspace_HeaderFile 17 #define OpenGl_Workspace_HeaderFile 18 19 #include <Graphic3d_BufferType.hxx> 20 #include <Graphic3d_PresentationAttributes.hxx> 21 22 #include <OpenGl_Aspects.hxx> 23 #include <OpenGl_Vec.hxx> 24 25 class OpenGl_FrameBuffer; 26 class OpenGl_Group; 27 class OpenGl_View; 28 class OpenGl_Window; 29 class Image_PixMap; 30 31 class OpenGl_Workspace; 32 DEFINE_STANDARD_HANDLE(OpenGl_Workspace,Standard_Transient) 33 34 //! Rendering workspace. 35 //! Provides methods to render primitives and maintain GL state. 36 class OpenGl_Workspace : public Standard_Transient 37 { 38 public: 39 40 //! Constructor of rendering workspace. 41 Standard_EXPORT OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow); 42 43 //! Destructor ~OpenGl_Workspace()44 virtual ~OpenGl_Workspace() {} 45 46 //! Activate rendering context. 47 Standard_EXPORT Standard_Boolean Activate(); 48 View() const49 OpenGl_View* View() const { return myView; } 50 Handle(OpenGl_Context)51 const Handle(OpenGl_Context)& GetGlContext() { return myGlContext; } 52 53 Standard_EXPORT Handle(OpenGl_FrameBuffer) FBOCreate (const Standard_Integer theWidth, const Standard_Integer theHeight); 54 55 Standard_EXPORT void FBORelease (Handle(OpenGl_FrameBuffer)& theFbo); 56 57 Standard_Boolean BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo, 58 Image_PixMap& theImage, 59 const Graphic3d_BufferType& theBufferType); 60 61 Standard_EXPORT Standard_Integer Width() const; 62 63 Standard_EXPORT Standard_Integer Height() const; 64 65 //! Setup Z-buffer usage flag (without affecting GL state!). 66 //! Returns previously set flag. SetUseZBuffer(const Standard_Boolean theToUse)67 Standard_Boolean SetUseZBuffer (const Standard_Boolean theToUse) 68 { 69 const Standard_Boolean wasUsed = myUseZBuffer; 70 myUseZBuffer = theToUse; 71 return wasUsed; 72 } 73 74 //! @return true if usage of Z buffer is enabled. UseZBuffer()75 Standard_Boolean& UseZBuffer() { return myUseZBuffer; } 76 77 //! @return true if depth writing is enabled. UseDepthWrite()78 Standard_Boolean& UseDepthWrite() { return myUseDepthWrite; } 79 80 //! Configure default polygon offset parameters. 81 //! Return previous settings. 82 Standard_EXPORT Graphic3d_PolygonOffset SetDefaultPolygonOffset (const Graphic3d_PolygonOffset& theOffset); 83 84 //// RELATED TO STATUS //// 85 86 //! Return true if active group might activate face culling (e.g. primitives are closed). ToAllowFaceCulling() const87 bool ToAllowFaceCulling() const { return myToAllowFaceCulling; } 88 89 //! Allow or disallow face culling. 90 //! This call does NOT affect current state of back face culling; 91 //! ApplyAspectFace() should be called to update state. SetAllowFaceCulling(bool theToAllow)92 bool SetAllowFaceCulling (bool theToAllow) 93 { 94 const bool wasAllowed = myToAllowFaceCulling; 95 myToAllowFaceCulling = theToAllow; 96 return wasAllowed; 97 } 98 99 //! Return true if following structures should apply highlight color. ToHighlight() const100 bool ToHighlight() const { return !myHighlightStyle.IsNull(); } 101 102 //! Return highlight style. Handle(Graphic3d_PresentationAttributes)103 const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myHighlightStyle; } 104 105 //! Set highlight style. SetHighlightStyle(const Handle (Graphic3d_PresentationAttributes)& theStyle)106 void SetHighlightStyle (const Handle(Graphic3d_PresentationAttributes)& theStyle) { myHighlightStyle = theStyle; } 107 108 //! Return edge color taking into account highlight flag. EdgeColor() const109 const OpenGl_Vec4& EdgeColor() const 110 { 111 return !myHighlightStyle.IsNull() 112 ? myHighlightStyle->ColorRGBA() 113 : myAspectsSet->Aspect()->EdgeColorRGBA(); 114 } 115 116 //! Return Interior color taking into account highlight flag. InteriorColor() const117 const OpenGl_Vec4& InteriorColor() const 118 { 119 return !myHighlightStyle.IsNull() 120 ? myHighlightStyle->ColorRGBA() 121 : myAspectsSet->Aspect()->InteriorColorRGBA(); 122 } 123 124 //! Return text color taking into account highlight flag. TextColor() const125 const OpenGl_Vec4& TextColor() const 126 { 127 return !myHighlightStyle.IsNull() 128 ? myHighlightStyle->ColorRGBA() 129 : myAspectsSet->Aspect()->ColorRGBA(); 130 } 131 132 //! Return text Subtitle color taking into account highlight flag. TextSubtitleColor() const133 const OpenGl_Vec4& TextSubtitleColor() const 134 { 135 return !myHighlightStyle.IsNull() 136 ? myHighlightStyle->ColorRGBA() 137 : myAspectsSet->Aspect()->ColorSubTitleRGBA(); 138 } 139 140 //! Currently set aspects (can differ from applied). Aspects() const141 const OpenGl_Aspects* Aspects() const { return myAspectsSet; } 142 143 //! Assign new aspects (will be applied within ApplyAspects()). 144 Standard_EXPORT const OpenGl_Aspects* SetAspects (const OpenGl_Aspects* theAspect); 145 146 //! Return TextureSet from set Aspects or Environment texture. Handle(OpenGl_TextureSet)147 const Handle(OpenGl_TextureSet)& TextureSet() const 148 { 149 const Handle(OpenGl_TextureSet)& aTextureSet = myAspectsSet->TextureSet (myGlContext, ToHighlight()); 150 return !aTextureSet.IsNull() 151 || myAspectsSet->Aspect()->ToMapTexture() 152 ? aTextureSet 153 : myEnvironmentTexture; 154 } 155 156 //! Apply aspects. 157 //! @param theToBindTextures flag to bind texture set defined by applied aspect 158 //! @return aspect set by SetAspects() 159 Standard_EXPORT const OpenGl_Aspects* ApplyAspects (bool theToBindTextures = true); 160 161 //! Clear the applied aspect state to default values. 162 void ResetAppliedAspect(); 163 164 //! Get rendering filter. 165 //! @sa ShouldRender() RenderFilter() const166 Standard_Integer RenderFilter() const { return myRenderFilter; } 167 168 //! Set filter for restricting rendering of particular elements. 169 //! @sa ShouldRender() SetRenderFilter(Standard_Integer theFilter)170 void SetRenderFilter (Standard_Integer theFilter) { myRenderFilter = theFilter; } 171 172 //! Checks whether the element can be rendered or not. 173 //! @param theElement [in] the element to check 174 //! @param theGroup [in] the group containing the element 175 //! @return True if element can be rendered 176 bool ShouldRender (const OpenGl_Element* theElement, const OpenGl_Group* theGroup); 177 178 //! Return the number of skipped transparent elements within active OpenGl_RenderFilter_OpaqueOnly filter. 179 //! @sa OpenGl_LayerList::Render() NbSkippedTransparentElements()180 Standard_Integer NbSkippedTransparentElements() { return myNbSkippedTranspElems; } 181 182 //! Reset skipped transparent elements counter. 183 //! @sa OpenGl_LayerList::Render() ResetSkippedCounter()184 void ResetSkippedCounter() { myNbSkippedTranspElems = 0; } 185 186 //! Returns face aspect for none culling mode. NoneCulling() const187 const OpenGl_Aspects& NoneCulling() const { return myNoneCulling; } 188 189 //! Returns face aspect for front face culling mode. FrontCulling() const190 const OpenGl_Aspects& FrontCulling() const { return myFrontCulling; } 191 192 //! Sets a new environment texture. SetEnvironmentTexture(const Handle (OpenGl_TextureSet)& theTexture)193 void SetEnvironmentTexture (const Handle(OpenGl_TextureSet)& theTexture) { myEnvironmentTexture = theTexture; } 194 195 //! Returns environment texture. Handle(OpenGl_TextureSet)196 const Handle(OpenGl_TextureSet)& EnvironmentTexture() const { return myEnvironmentTexture; } 197 198 //! Dumps the content of me into the stream 199 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const; 200 201 protected: //! @name protected fields 202 203 OpenGl_View* myView; 204 Handle(OpenGl_Window) myWindow; 205 Handle(OpenGl_Context) myGlContext; 206 Standard_Boolean myUseZBuffer; 207 Standard_Boolean myUseDepthWrite; 208 OpenGl_Aspects myNoneCulling; 209 OpenGl_Aspects myFrontCulling; 210 211 protected: //! @name fields related to status 212 213 Standard_Integer myNbSkippedTranspElems; //!< counter of skipped transparent elements for OpenGl_LayerList two rendering passes method 214 Standard_Integer myRenderFilter; //!< active filter for skipping rendering of elements by some criteria (multiple render passes) 215 216 OpenGl_Aspects myDefaultAspects; 217 const OpenGl_Aspects* myAspectsSet; 218 Handle(Graphic3d_Aspects) myAspectsApplied; 219 220 Handle(Graphic3d_PresentationAttributes) myAspectFaceAppliedWithHL; 221 222 bool myToAllowFaceCulling; //!< allow back face culling 223 Handle(Graphic3d_PresentationAttributes) myHighlightStyle; //!< active highlight style 224 225 OpenGl_Aspects myAspectFaceHl; //!< Hiddenline aspect 226 227 Handle(OpenGl_TextureSet) myEnvironmentTexture; 228 229 public: //! @name type definition 230 231 DEFINE_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient) 232 DEFINE_STANDARD_ALLOC 233 234 }; 235 236 #endif // _OpenGl_Workspace_Header 237