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