0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[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 <InterfaceGraphic_telem.hxx>
20 #include <Aspect_InteriorStyle.hxx>
21 #include <Aspect_PolygonOffsetMode.hxx>
22 #include <TCollection_AsciiString.hxx>
23
24 #include <OpenGl_AspectLine.hxx>
25 #include <OpenGl_Element.hxx>
26
27 #include <Graphic3d_AspectFillArea3d.hxx>
28 #include <Graphic3d_CAspectFillArea.hxx>
29 #include <Graphic3d_ShaderProgram.hxx>
30 #include <Graphic3d_TextureMap.hxx>
31 #include <Graphic3d_BSDF.hxx>
32
33 class OpenGl_Texture;
34
35 #define OPENGL_AMBIENT_MASK  (1<<0)
36 #define OPENGL_DIFFUSE_MASK  (1<<1)
37 #define OPENGL_SPECULAR_MASK (1<<2)
38 #define OPENGL_EMISSIVE_MASK (1<<3)
39
40 static const TEL_POFFSET_PARAM THE_DEFAULT_POFFSET = { Aspect_POM_Fill, 1.0F, 0.0F };
41
42 struct OPENGL_SURF_PROP
43 {
44   Standard_ShortReal amb;
45   Standard_ShortReal diff;
46   Standard_ShortReal spec;
47   Standard_ShortReal emsv;
48
49   Standard_ShortReal trans;
50   Standard_ShortReal shine;
51   Standard_ShortReal index;
52
53   Standard_ShortReal env_reflexion;
54   Standard_Integer   isphysic;
55
56   unsigned int color_mask;
57
58   TEL_COLOUR speccol;
59   TEL_COLOUR difcol;
60   TEL_COLOUR ambcol;
61   TEL_COLOUR emscol;
62   TEL_COLOUR matcol;
63
64   Graphic3d_BSDF BSDF;
65
66   DEFINE_STANDARD_ALLOC
67 };
68
69 class OpenGl_AspectFace : public OpenGl_Element
70 {
71
72 public:
73
74   Standard_EXPORT OpenGl_AspectFace();
75
76   //! Copy parameters
77   Standard_EXPORT void SetAspect (const CALL_DEF_CONTEXTFILLAREA& theAspect);
78   Standard_EXPORT void SetAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect);
79
80   //! Set edge aspect.
81   void SetAspectEdge (const OpenGl_AspectLine* theAspectEdge)
82   {
83     myAspectEdge = *theAspectEdge;
84   }
85
86   //! @return edge aspect.
87   const OpenGl_AspectLine* AspectEdge() const 
88   {
89     return &myAspectEdge;
90   }
91
92   //! @return interior style
93   Aspect_InteriorStyle InteriorStyle() const
94   {
95     return myInteriorStyle;
96   }
97
98   Aspect_InteriorStyle& ChangeInteriorStyle()
99   {
100     return myInteriorStyle;
101   }
102
103   //! @return edge on flag.
104   int Edge() const
105   {
106     return myEdge;
107   }
108
109   //! @return edge on flag.
110   int& ChangeEdge()
111   {
112     return myEdge;
113   }
114
115   //! @return hatch type.
116   int Hatch() const
117   {
118     return myHatch;
119   }
120
121   //! @return hatch type variable.
122   int& ChangeHatch()
123   {
124     return myHatch;
125   }
126
127   //! @return distinguishing mode.
128   int DistinguishingMode() const
129   {
130     return myDistinguishingMode;
131   }
132
133   //! @return distinguishing mode.
134   int& ChangeDistinguishingMode()
135   {
136     return myDistinguishingMode;
137   }
138
139   //! @return culling mode.
140   int CullingMode() const
141   {
142     return myCullingMode;
143   }
144
145   //! @return culling mode.
146   int& ChangeCullingMode()
147   {
148     return myCullingMode;
149   }
150
151   //! @return front material properties.
152   const OPENGL_SURF_PROP& IntFront() const
153   {
154     return myIntFront;
155   }
156
157   //! @return front material properties.
158   OPENGL_SURF_PROP& ChangeIntFront()
159   {
160     return myIntFront;
161   }
162
163   //! @return back material properties.
164   const OPENGL_SURF_PROP& IntBack() const
165   {
166     return myIntBack;
167   }
168
169   //! @return back material properties.
170   OPENGL_SURF_PROP& ChangeIntBack()
171   {
172     return myIntBack;
173   }
174
175   //! @return polygon offset parameters.
176   const TEL_POFFSET_PARAM& PolygonOffset() const
177   {
178     return myPolygonOffset;
179   }
180
181   //! @return polygon offset parameters.
182   TEL_POFFSET_PARAM& ChangePolygonOffset()
183   {
184     return myPolygonOffset;
185   }
186
187   //! @return texture mapping flag.
188   bool DoTextureMap() const
189   {
190     return myDoTextureMap;
191   }
192
193   //! @return texture mapping flag.
194   bool& ChangeDoTextureMap()
195   {
196     return myDoTextureMap;
197   }
198
199   //! @return texture mapping parameters.
200   const Handle(Graphic3d_TextureParams)& TextureParams() const
201   {
202     return myTexture->GetParams();
203   }
204
205   //! @return texture map.
206   const Handle(OpenGl_Texture)& TextureRes (const Handle(OpenGl_Context)& theCtx) const
207   {
208     if (!myResources.IsTextureReady())
209     {
210       myResources.BuildTexture (theCtx, myTexture);
211       myResources.SetTextureReady();
212     }
213
214     return myResources.Texture;
215   }
216
217   //! Init and return OpenGl shader program resource.
218   //! @return shader program resource.
219   const Handle(OpenGl_ShaderProgram)& ShaderProgramRes (const Handle(OpenGl_Context)& theCtx) const
220   {
221     if (!myResources.IsShaderReady())
222     {
223       myResources.BuildShader (theCtx, myShaderProgram);
224       myResources.SetShaderReady();
225     }
226
227     return myResources.ShaderProgram;
228   }
229
230   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
231   Standard_EXPORT virtual void Release (OpenGl_Context* theContext);
232
233 protected:
234
235   Standard_EXPORT void convertMaterial (const CALL_DEF_MATERIAL& theMat,
236                                         OPENGL_SURF_PROP&        theSurf);
237
238 protected: //! @name ordinary aspect properties
239
240   Aspect_InteriorStyle            myInteriorStyle;
241   int                             myEdge;
242   int                             myHatch;
243   int                             myDistinguishingMode;
244   int                             myCullingMode;
245   OPENGL_SURF_PROP                myIntFront;
246   OPENGL_SURF_PROP                myIntBack;
247   TEL_POFFSET_PARAM               myPolygonOffset;
248   bool                            myDoTextureMap;
249   Handle(Graphic3d_TextureMap)    myTexture;
250   Handle(Graphic3d_ShaderProgram) myShaderProgram;
251
252 protected:
253
254   //! OpenGl resources
255   mutable struct Resources
256   {
257   public:
258     Resources()
259       : myIsTextureReady (Standard_False),
260         myIsShaderReady  (Standard_False) {}
261
262     Standard_Boolean IsTextureReady() const { return myIsTextureReady; }
263     Standard_Boolean IsShaderReady () const { return myIsShaderReady;  }
264     void SetTextureReady() { myIsTextureReady = Standard_True; }
265     void SetShaderReady () { myIsShaderReady  = Standard_True; }
266     void ResetTextureReadiness() { myIsTextureReady = Standard_False; }
267     void ResetShaderReadiness () { myIsShaderReady  = Standard_False; }
268
269     Standard_EXPORT void BuildTexture (const Handle(OpenGl_Context)&          theCtx,
270                                        const Handle(Graphic3d_TextureMap)&    theTexture);
271     Standard_EXPORT void BuildShader  (const Handle(OpenGl_Context)&          theCtx,
272                                        const Handle(Graphic3d_ShaderProgram)& theShader);
273
274     Handle(OpenGl_Texture)       Texture;
275     TCollection_AsciiString      TextureId;
276     Handle(OpenGl_ShaderProgram) ShaderProgram;
277     TCollection_AsciiString      ShaderProgramId;
278
279   private:
280
281     Standard_Boolean myIsTextureReady;
282     Standard_Boolean myIsShaderReady;
283
284   } myResources;
285
286 protected:
287
288   OpenGl_AspectLine               myAspectEdge;
289
290 public:
291
292   DEFINE_STANDARD_ALLOC
293
294 };
295
296 #endif //_OpenGl_AspectFace_Header