0024192: Adding support for shaders to OCCT visualization toolkit
[occt.git] / src / OpenGl / OpenGl_AspectFace.hxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 #ifndef _OpenGl_AspectFace_Header
21 #define _OpenGl_AspectFace_Header
22
23 #include <InterfaceGraphic_telem.hxx>
24 #include <Aspect_InteriorStyle.hxx>
25 #include <TCollection_AsciiString.hxx>
26
27 #include <Handle_Graphic3d_TextureParams.hxx>
28 #include <Handle_OpenGl_ShaderProgram.hxx>
29 #include <Handle_OpenGl_Texture.hxx>
30 #include <OpenGl_AspectLine.hxx>
31 #include <OpenGl_Element.hxx>
32
33 #include <Graphic3d_AspectFillArea3d.hxx>
34 #include <Graphic3d_ShaderProgram_Handle.hxx>
35 #include <Graphic3d_TextureMap.hxx>
36
37 #define OPENGL_AMBIENT_MASK  (1<<0)
38 #define OPENGL_DIFFUSE_MASK  (1<<1)
39 #define OPENGL_SPECULAR_MASK (1<<2)
40 #define OPENGL_EMISSIVE_MASK (1<<3)
41
42 class CALL_DEF_CONTEXTFILLAREA;
43
44 struct OPENGL_SURF_PROP
45 {
46   float        amb, diff, spec, emsv;
47   float        trans, shine;
48   float        env_reflexion;
49   int          isphysic;
50   unsigned int color_mask;
51   TEL_COLOUR speccol, difcol, ambcol, emscol, matcol;
52   DEFINE_STANDARD_ALLOC
53 };
54
55 class OpenGl_AspectFace : public OpenGl_Element
56 {
57
58 public:
59
60   OpenGl_AspectFace();
61
62   //! Copy parameters
63   void SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect);
64   void SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
65
66   //! Set edge aspect.
67   void SetAspectEdge (const OpenGl_AspectLine* theAspectEdge)
68   {
69     myAspectEdge = *theAspectEdge;
70   }
71
72   //! @return edge aspect.
73   const OpenGl_AspectLine* AspectEdge() const 
74   {
75     return &myAspectEdge;
76   }
77
78   //! @return interior style
79   const Aspect_InteriorStyle InteriorStyle() const
80   {
81     return myInteriorStyle;
82   }
83
84   Aspect_InteriorStyle& ChangeInteriorStyle()
85   {
86     return myInteriorStyle;
87   }
88
89   //! @return edge on flag.
90   int Edge() const
91   {
92     return myEdge;
93   }
94
95   //! @return edge on flag.
96   int& ChangeEdge()
97   {
98     return myEdge;
99   }
100
101   //! @return hatch type.
102   int Hatch() const
103   {
104     return myHatch;
105   }
106
107   //! @return hatch type variable.
108   int& ChangeHatch()
109   {
110     return myHatch;
111   }
112
113   //! @return distinguishing mode.
114   int DistinguishingMode() const
115   {
116     return myDistinguishingMode;
117   }
118
119   //! @return distinguishing mode.
120   int& ChangeDistinguishingMode()
121   {
122     return myDistinguishingMode;
123   }
124
125   //! @return culling mode.
126   int CullingMode() const
127   {
128     return myCullingMode;
129   }
130
131   //! @return culling mode.
132   int& ChangeCullingMode()
133   {
134     return myCullingMode;
135   }
136
137   //! @return front material properties.
138   const OPENGL_SURF_PROP& IntFront() const
139   {
140     return myIntFront;
141   }
142
143   //! @return front material properties.
144   OPENGL_SURF_PROP& ChangeIntFront()
145   {
146     return myIntFront;
147   }
148
149   //! @return back material properties.
150   const OPENGL_SURF_PROP& IntBack() const
151   {
152     return myIntBack;
153   }
154
155   //! @return back material properties.
156   OPENGL_SURF_PROP& ChangeIntBack()
157   {
158     return myIntBack;
159   }
160
161   //! @return polygon offset parameters.
162   const TEL_POFFSET_PARAM& PolygonOffset() const
163   {
164     return myPolygonOffset;
165   }
166
167   //! @return polygon offset parameters.
168   TEL_POFFSET_PARAM& ChangePolygonOffset()
169   {
170     return myPolygonOffset;
171   }
172
173   //! @return texture mapping flag.
174   bool DoTextureMap() const
175   {
176     return myDoTextureMap;
177   }
178
179   //! @return texture mapping flag.
180   bool& ChangeDoTextureMap()
181   {
182     return myDoTextureMap;
183   }
184
185   //! @return texture mapping parameters.
186   const Handle(Graphic3d_TextureParams)& TextureParams() const
187   {
188     return myTexture->GetParams();
189   }
190
191   //! @return texture map.
192   const Handle(OpenGl_Texture)& TextureRes (const Handle(OpenGl_Workspace)& theWorkspace) const
193   {
194     if (!myResources.IsTextureReady())
195     {
196       myResources.BuildTexture (theWorkspace, myTexture);
197       myResources.SetTextureReady();
198     }
199
200     return myResources.Texture;
201   }
202
203   //! Init and return OpenGl shader program resource.
204   //! @return shader program resource.
205   const Handle(OpenGl_ShaderProgram)& ShaderProgramRes (const Handle(OpenGl_Workspace)& theWorkspace) const 
206   {
207     if (!myResources.IsShaderReady())
208     {
209       myResources.BuildShader (theWorkspace, myShaderProgram);
210       myResources.SetShaderReady();
211     }
212
213     return myResources.ShaderProgram;
214   }
215
216   virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
217   virtual void Release (const Handle(OpenGl_Context)&   theContext);
218
219 protected:
220
221   void convertMaterial (const CALL_DEF_MATERIAL& theMat,
222                         OPENGL_SURF_PROP&        theSurf);
223
224 protected: //! @name ordinary aspect properties
225
226   Aspect_InteriorStyle            myInteriorStyle;
227   int                             myEdge;
228   int                             myHatch;
229   int                             myDistinguishingMode;
230   int                             myCullingMode;
231   OPENGL_SURF_PROP                myIntFront;
232   OPENGL_SURF_PROP                myIntBack;
233   TEL_POFFSET_PARAM               myPolygonOffset;
234   bool                            myDoTextureMap;
235   Handle(Graphic3d_TextureMap)    myTexture;
236   Handle(Graphic3d_ShaderProgram) myShaderProgram;
237
238 protected:
239
240   //! OpenGl resources
241   mutable struct Resources
242   {
243   public:
244     Resources()
245       : myIsTextureReady (Standard_False),
246         myIsShaderReady  (Standard_False) {}
247
248     Standard_Boolean IsTextureReady() const { return myIsTextureReady; }
249     Standard_Boolean IsShaderReady () const { return myIsShaderReady;  }
250     void SetTextureReady() { myIsTextureReady = Standard_True; }
251     void SetShaderReady () { myIsShaderReady  = Standard_True; }
252     void ResetTexture() { myIsTextureReady = Standard_False; }
253     void ResetShader () { myIsShaderReady  = Standard_False; }
254
255     void BuildTexture (const Handle(OpenGl_Workspace)& theWS, const Handle(Graphic3d_TextureMap)& theTexture);
256     void BuildShader  (const Handle(OpenGl_Workspace)& theWS, const Handle(Graphic3d_ShaderProgram)& theShader);
257
258     Handle(OpenGl_Texture)  Texture;
259     TCollection_AsciiString TextureId;
260     Handle(OpenGl_ShaderProgram) ShaderProgram;
261     TCollection_AsciiString      ShaderProgramId;
262
263   private:
264
265     Standard_Boolean myIsTextureReady;
266     Standard_Boolean myIsShaderReady;
267
268   } myResources;
269
270 protected:
271
272   OpenGl_AspectLine               myAspectEdge;
273
274 public:
275
276   DEFINE_STANDARD_ALLOC
277
278 };
279
280 #endif //_OpenGl_AspectFace_Header