0028912: Visualization, TKOpenGl - multi-texture support
[occt.git] / src / OpenGl / OpenGl_AspectFace.hxx
1 // Created on: 2011-07-13
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_AspectFace_Header
17 #define _OpenGl_AspectFace_Header
18
19 #include <OpenGl_AspectLine.hxx>
20 #include <OpenGl_TextureSet.hxx>
21 #include <Graphic3d_AspectFillArea3d.hxx>
22 #include <Graphic3d_ShaderProgram.hxx>
23 #include <Graphic3d_TextureMap.hxx>
24 #include <Graphic3d_BSDF.hxx>
25
26 class OpenGl_Texture;
27
28 //! The element holding Graphic3d_AspectFillArea3d.
29 class OpenGl_AspectFace : public OpenGl_Element
30 {
31
32 public:
33
34   //! Empty constructor.
35   Standard_EXPORT OpenGl_AspectFace();
36
37   //! Create and assign parameters.
38   Standard_EXPORT OpenGl_AspectFace (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
39
40   //! Return aspect.
41   const Handle(Graphic3d_AspectFillArea3d)& Aspect() const { return myAspect; }
42
43   //! Assign parameters.
44   Standard_EXPORT void SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
45
46   //! Set edge aspect.
47   void SetAspectEdge (const OpenGl_AspectLine* theAspectEdge) { myAspectEdge = *theAspectEdge; }
48
49   //! @return edge aspect.
50   const OpenGl_AspectLine* AspectEdge() const  { return &myAspectEdge; }
51
52   //! Returns true if lighting should be disabled.
53   bool IsNoLighting() const { return myIsNoLighting; }
54
55   //! Set if lighting should be disabled or not.
56   void SetNoLighting (bool theValue) { myIsNoLighting = theValue; }
57
58   //! Returne textures map.
59   const Handle(OpenGl_TextureSet)& TextureSet (const Handle(OpenGl_Context)& theCtx) const
60   {
61     if (!myResources.IsTextureReady())
62     {
63       myResources.BuildTextures (theCtx, myAspect->TextureSet());
64       myResources.SetTextureReady();
65     }
66     return myResources.TextureSet();
67   }
68
69   //! Init and return OpenGl shader program resource.
70   //! @return shader program resource.
71   const Handle(OpenGl_ShaderProgram)& ShaderProgramRes (const Handle(OpenGl_Context)& theCtx) const
72   {
73     if (!myResources.IsShaderReady())
74     {
75       myResources.BuildShader (theCtx, myAspect->ShaderProgram());
76       myResources.SetShaderReady();
77     }
78
79     return myResources.ShaderProgram;
80   }
81
82   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
83   Standard_EXPORT virtual void Release (OpenGl_Context* theContext);
84
85 protected:
86
87   //! OpenGl resources
88   mutable struct Resources
89   {
90   public:
91     //! Empty constructor.
92     Resources()
93     : myIsTextureReady (Standard_False),
94       myIsShaderReady  (Standard_False) {}
95
96     //! Return TRUE if texture resource is up-to-date.
97     Standard_Boolean IsTextureReady() const { return myIsTextureReady; }
98
99     //! Return TRUE if shader resource is up-to-date.
100     Standard_Boolean IsShaderReady () const { return myIsShaderReady;  }
101
102     //! Set texture resource up-to-date state.
103     void SetTextureReady() { myIsTextureReady = Standard_True; }
104
105     //! Set shader resource up-to-date state.
106     void SetShaderReady () { myIsShaderReady  = Standard_True; }
107
108     //! Reset shader resource up-to-date state.
109     void ResetShaderReadiness () { myIsShaderReady  = Standard_False; }
110
111     //! Return textures array.
112     const Handle(OpenGl_TextureSet)& TextureSet() const { return myTextures; }
113
114     //! Update texture resource up-to-date state.
115     Standard_EXPORT void UpdateTexturesRediness (const Handle(Graphic3d_TextureSet)& theTextures);
116
117     //! Build texture resource.
118     Standard_EXPORT void BuildTextures (const Handle(OpenGl_Context)& theCtx,
119                                         const Handle(Graphic3d_TextureSet)& theTextures);
120
121     //! Build shader resource.
122     Standard_EXPORT void BuildShader  (const Handle(OpenGl_Context)&          theCtx,
123                                        const Handle(Graphic3d_ShaderProgram)& theShader);
124
125     //! Release texture resource.
126     Standard_EXPORT void ReleaseTextures (OpenGl_Context* theCtx);
127
128     Handle(OpenGl_ShaderProgram) ShaderProgram;
129     TCollection_AsciiString      ShaderProgramId;
130
131   private:
132
133     Handle(OpenGl_TextureSet) myTextures;
134     Standard_Boolean myIsTextureReady;
135     Standard_Boolean myIsShaderReady;
136
137   } myResources;
138
139   Handle(Graphic3d_AspectFillArea3d) myAspect;
140   OpenGl_AspectLine                  myAspectEdge;
141   bool                               myIsNoLighting;
142
143 public:
144
145   DEFINE_STANDARD_ALLOC
146
147 };
148
149 #endif //_OpenGl_AspectFace_Header