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