0023000: Improve the way the gradient and textured background is managed in 3d viewer
[occt.git] / src / OpenGl / OpenGl_AspectFace.cxx
1 // File:      OpenGl_AspectFace.cxx
2 // Created:   13 July 2011
3 // Author:    Sergey ZERCHANINOV
4 // Copyright: OPEN CASCADE 2011
5
6 #include <OpenGl_AspectFace.hxx>
7
8 #include <InterfaceGraphic_Graphic3d.hxx>
9 #include <Aspect_PolygonOffsetMode.hxx>
10
11 /*----------------------------------------------------------------------*/
12
13 static const TEL_CONTEXT_FACE myDefaultAspectFace =
14 {
15   Aspect_IS_SOLID,
16   TOn, TEL_HS_SOLID, TOn, TelCullNone,
17   { 0.2F, 0.8F, 0.1F, 0.0F, /* amb, diff, spec, emsv */
18     1.0F, 10.0F, 0.0F, /* trans, shine, env_reflexion */
19     0, /* isphysic */
20     (OPENGL_AMBIENT_MASK | OPENGL_DIFFUSE_MASK | OPENGL_SPECULAR_MASK), /* color_mask */
21     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* ambient color */
22     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* diffuse color */
23     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* specular color */
24     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* emissive color */
25     {{ 1.0F, 1.0F, 1.0F, 1.0F }} /* material color */
26   },
27   { 0.2F, 0.8F, 0.1F, 0.0F, /* amb, diff, spec, emsv */
28     1.0F, 10.0F, 0.0F, /* trans, shine, env_reflexion */
29     0, /* isphysic */
30     (OPENGL_AMBIENT_MASK | OPENGL_DIFFUSE_MASK | OPENGL_SPECULAR_MASK), /* color_mask */
31     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* ambient color */
32     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* diffuse color */
33     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* specular color */
34     {{ 1.0F, 1.0F, 1.0F, 1.0F }}, /* emissive color */
35     {{ 1.0F, 1.0F, 1.0F, 1.0F }} /* material color */
36   },
37   0, -1, { Aspect_POM_Fill, 1.0F, 0.0F }
38 };
39
40 /*----------------------------------------------------------------------*/
41
42 static void ConvertMaterial (const CALL_DEF_MATERIAL &material, OPENGL_SURF_PROP &surface)
43 {
44   /* Cas par cas pour l evaluation */
45   surface.amb  = material.IsAmbient? material.Ambient : 0.F;
46   surface.diff = material.IsDiffuse? material.Diffuse : 0.F;
47   surface.spec = material.IsSpecular? material.Specular : 0.F;
48   surface.emsv = material.IsEmission? material.Emission : 0.F;
49
50   /* type de materiel */
51   surface.isphysic = material.IsPhysic? 1 : 0;
52
53   /* Couleur du materiel */
54   surface.color_mask = 0;
55   if ( material.IsAmbient )
56     surface.color_mask |= OPENGL_AMBIENT_MASK;
57   if ( material.IsDiffuse )
58     surface.color_mask |= OPENGL_DIFFUSE_MASK;
59   if ( material.IsSpecular )
60     surface.color_mask |= OPENGL_SPECULAR_MASK;
61   if ( material.IsEmission )
62     surface.color_mask |= OPENGL_EMISSIVE_MASK;
63
64   /* Couleur eclairage ambient */
65   surface.ambcol.rgb[0] = material.ColorAmb.r;
66   surface.ambcol.rgb[1] = material.ColorAmb.g;
67   surface.ambcol.rgb[2] = material.ColorAmb.b;
68   surface.ambcol.rgb[3] = 1.F;
69
70   /* Couleur eclairage diffus */
71   surface.difcol.rgb[0] = material.ColorDif.r;
72   surface.difcol.rgb[1] = material.ColorDif.g;
73   surface.difcol.rgb[2] = material.ColorDif.b;
74   surface.difcol.rgb[3] = 1.F;
75
76   /* Couleur eclairage speculaire */
77   surface.speccol.rgb[0] = material.ColorSpec.r;
78   surface.speccol.rgb[1] = material.ColorSpec.g;
79   surface.speccol.rgb[2] = material.ColorSpec.b;
80   surface.speccol.rgb[3] = 1.F;
81
82   /* Couleur d emission */
83   surface.emscol.rgb[0] = material.ColorEms.r;
84   surface.emscol.rgb[1] = material.ColorEms.g;
85   surface.emscol.rgb[2] = material.ColorEms.b;
86   surface.emscol.rgb[3] = 1.F;
87
88   surface.shine = ( float )128 * material.Shininess;
89   surface.env_reflexion = material.EnvReflexion;
90
91   /* Dans la couche C++ :
92   * prop->trans = 0. => opaque
93   * prop->trans = 1. => transparent
94   * in OpenGl it is opposite.
95   */
96   surface.trans = 1.0F - material.Transparency;
97 }
98
99 /*----------------------------------------------------------------------*/
100
101 OpenGl_AspectFace::OpenGl_AspectFace ()
102  : myContext(myDefaultAspectFace)
103 {}
104
105 /*----------------------------------------------------------------------*/
106
107 void OpenGl_AspectFace::SetContext (const CALL_DEF_CONTEXTFILLAREA &AContext)
108 {
109   //TelInteriorStyle
110   myContext.InteriorStyle = (Aspect_InteriorStyle) AContext.Style;
111
112   //TelEdgeFlag
113   myContext.Edge = AContext.Edge ? TOn : TOff;
114
115   //TelInteriorStyleIndex
116   switch( AContext.Hatch )
117   {
118     case 0 : /* Aspect_HS_HORIZONTAL */
119       myContext.Hatch = TEL_HS_HORIZONTAL;
120       break;
121     case 1 : /* Aspect_HS_HORIZONTAL_WIDE */
122       myContext.Hatch = TEL_HS_HORIZONTAL_SPARSE;
123       break;
124     case 2 : /* Aspect_HS_VERTICAL */
125       myContext.Hatch = TEL_HS_VERTICAL;
126       break;
127     case 3 : /* Aspect_HS_VERTICAL_WIDE */
128       myContext.Hatch = TEL_HS_VERTICAL_SPARSE;
129       break;
130     case 4 : /* Aspect_HS_DIAGONAL_45 */
131       myContext.Hatch = TEL_HS_DIAG_45;
132       break;
133     case 5 : /* Aspect_HS_DIAGONAL_45_WIDE */
134       myContext.Hatch = TEL_HS_DIAG_45_SPARSE;
135       break;
136     case 6 : /* Aspect_HS_DIAGONAL_135 */
137       myContext.Hatch = TEL_HS_DIAG_135;
138       break;
139     case 7 : /* Aspect_HS_DIAGONAL_135_WIDE */
140       myContext.Hatch = TEL_HS_DIAG_135_SPARSE;
141       break;
142     case 8 : /* Aspect_HS_GRID */
143       myContext.Hatch = TEL_HS_GRID;
144       break;
145     case 9 : /* Aspect_HS_GRID_WIDE */
146       myContext.Hatch = TEL_HS_GRID_SPARSE;
147       break;
148     case 10 : /* Aspect_HS_GRID_DIAGONAL */
149       myContext.Hatch = TEL_HS_CROSS;
150       break;
151     case 11 : /* Aspect_HS_GRID_DIAGONAL_WIDE */
152       myContext.Hatch = TEL_HS_CROSS_SPARSE;
153       break;
154     default :
155       myContext.Hatch = 0;
156       break;
157   }
158
159   //TelFaceDistinguishingMode
160   myContext.DistinguishingMode = AContext.Distinguish ? TOn : TOff;
161
162   //TelFaceCullingMode
163   myContext.CullingMode = AContext.BackFace ? TelCullBack : TelCullNone;
164
165   //TelSurfaceAreaProperties
166   ConvertMaterial(AContext.Front,myContext.IntFront);
167
168   //TelBackSurfaceAreaProperties
169   ConvertMaterial(AContext.Back,myContext.IntBack);
170
171   //TelInteriorColour
172   myContext.IntFront.matcol.rgb[0] = (float) AContext.IntColor.r;
173   myContext.IntFront.matcol.rgb[1] = (float) AContext.IntColor.g;
174   myContext.IntFront.matcol.rgb[2] = (float) AContext.IntColor.b;
175   myContext.IntFront.matcol.rgb[3] = 1.f;
176
177   //TelBackInteriorColour
178   myContext.IntBack.matcol.rgb[0] = (float) AContext.BackIntColor.r;
179   myContext.IntBack.matcol.rgb[1] = (float) AContext.BackIntColor.g;
180   myContext.IntBack.matcol.rgb[2] = (float) AContext.BackIntColor.b;
181   myContext.IntBack.matcol.rgb[3] = 1.f;
182
183   //TelDoTextureMap
184   myContext.doTextureMap = AContext.Texture.doTextureMap;
185
186   //TelTextureId
187   myContext.TexId = AContext.Texture.TexId;
188
189   //TelPolygonOffset
190   myContext.PolygonOffset.mode   = (Aspect_PolygonOffsetMode) AContext.PolygonOffsetMode;
191   myContext.PolygonOffset.factor = AContext.PolygonOffsetFactor;
192   myContext.PolygonOffset.units  = AContext.PolygonOffsetUnits;
193
194   CALL_DEF_CONTEXTLINE anEdgeContext;
195
196   //TelEdgeColour
197   anEdgeContext.Color.r = (float) AContext.EdgeColor.r;
198   anEdgeContext.Color.g = (float) AContext.EdgeColor.g;
199   anEdgeContext.Color.b = (float) AContext.EdgeColor.b;
200
201   //TelEdgeType
202   anEdgeContext.LineType = (Aspect_TypeOfLine) AContext.LineType;
203
204   //TelEdgeWidth
205   anEdgeContext.Width = (float) AContext.Width;
206
207   myAspectEdge.SetContext(anEdgeContext);
208 }
209
210 /*----------------------------------------------------------------------*/
211
212 void OpenGl_AspectFace::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
213 {
214   AWorkspace->SetAspectFace(this);
215 }
216
217 /*----------------------------------------------------------------------*/