0027670: Visualization - avoid duplication of structures defining primitive array...
[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 <Graphic3d_AspectFillArea3d.hxx>
21 #include <Graphic3d_ShaderProgram.hxx>
22 #include <Graphic3d_TextureMap.hxx>
23 #include <Graphic3d_BSDF.hxx>
24
25 class OpenGl_Texture;
26
27 //! The element holding Graphic3d_AspectFillArea3d.
28 class OpenGl_AspectFace : public OpenGl_Element
29 {
30
31 public:
32
33   //! Empty constructor.
34   Standard_EXPORT OpenGl_AspectFace();
35
36   //! Create and assign parameters.
37   Standard_EXPORT OpenGl_AspectFace (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
38
39   //! Return aspect.
40   const Handle(Graphic3d_AspectFillArea3d)& Aspect() const { return myAspect; }
41
42   //! Assign parameters.
43   Standard_EXPORT void SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
44
45   //! Set edge aspect.
46   void SetAspectEdge (const OpenGl_AspectLine* theAspectEdge) { myAspectEdge = *theAspectEdge; }
47
48   //! @return edge aspect.
49   const OpenGl_AspectLine* AspectEdge() const  { return &myAspectEdge; }
50
51   //! Returns true if lighting should be disabled.
52   bool IsNoLighting() const { return myIsNoLighting; }
53
54   //! Set if lighting should be disabled or not.
55   void SetNoLighting (bool theValue) { myIsNoLighting = theValue; }
56
57   //! @return texture mapping parameters.
58   const Handle(Graphic3d_TextureParams)& TextureParams() const
59   {
60     return myAspect->TextureMap()->GetParams();
61   }
62
63   //! @return texture map.
64   const Handle(OpenGl_Texture)& TextureRes (const Handle(OpenGl_Context)& theCtx) const
65   {
66     if (!myResources.IsTextureReady())
67     {
68       myResources.BuildTexture (theCtx, myAspect->TextureMap());
69       myResources.SetTextureReady();
70     }
71
72     return myResources.Texture;
73   }
74
75   //! Init and return OpenGl shader program resource.
76   //! @return shader program resource.
77   const Handle(OpenGl_ShaderProgram)& ShaderProgramRes (const Handle(OpenGl_Context)& theCtx) const
78   {
79     if (!myResources.IsShaderReady())
80     {
81       myResources.BuildShader (theCtx, myAspect->ShaderProgram());
82       myResources.SetShaderReady();
83     }
84
85     return myResources.ShaderProgram;
86   }
87
88   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
89   Standard_EXPORT virtual void Release (OpenGl_Context* theContext);
90
91 protected:
92
93   //! OpenGl resources
94   mutable struct Resources
95   {
96   public:
97     Resources()
98       : myIsTextureReady (Standard_False),
99         myIsShaderReady  (Standard_False) {}
100
101     Standard_Boolean IsTextureReady() const { return myIsTextureReady; }
102     Standard_Boolean IsShaderReady () const { return myIsShaderReady;  }
103     void SetTextureReady() { myIsTextureReady = Standard_True; }
104     void SetShaderReady () { myIsShaderReady  = Standard_True; }
105     void ResetTextureReadiness() { myIsTextureReady = Standard_False; }
106     void ResetShaderReadiness () { myIsShaderReady  = Standard_False; }
107
108     Standard_EXPORT void BuildTexture (const Handle(OpenGl_Context)&          theCtx,
109                                        const Handle(Graphic3d_TextureMap)&    theTexture);
110     Standard_EXPORT void BuildShader  (const Handle(OpenGl_Context)&          theCtx,
111                                        const Handle(Graphic3d_ShaderProgram)& theShader);
112
113     Handle(OpenGl_Texture)       Texture;
114     TCollection_AsciiString      TextureId;
115     Handle(OpenGl_ShaderProgram) ShaderProgram;
116     TCollection_AsciiString      ShaderProgramId;
117
118   private:
119
120     Standard_Boolean myIsTextureReady;
121     Standard_Boolean myIsShaderReady;
122
123   } myResources;
124
125   Handle(Graphic3d_AspectFillArea3d) myAspect;
126   OpenGl_AspectLine                  myAspectEdge;
127   bool                               myIsNoLighting;
128
129 public:
130
131   DEFINE_STANDARD_ALLOC
132
133 };
134
135 #endif //_OpenGl_AspectFace_Header