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