0030700: Visualization, TKOpenGl - support PBR Metallic-Roughness shading model
[occt.git] / src / Graphic3d / Graphic3d_MaterialAspect.cxx
1 // Copyright (c) 1991-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <Graphic3d_MaterialAspect.hxx>
16
17 #include <Graphic3d_MaterialDefinitionError.hxx>
18 #include <Standard_Assert.hxx>
19 #include <Standard_OutOfRange.hxx>
20
21 namespace
22 {
23   //! Raw material for defining list of standard materials
24   struct RawMaterial
25   {
26     const char*              StringName;
27     Graphic3d_BSDF           BSDF;
28     Graphic3d_PBRMaterial    PBRMaterial;
29     Quantity_Color           Colors[Graphic3d_TypeOfReflection_NB];
30     Standard_ShortReal       TransparencyCoef;
31     Standard_ShortReal       RefractionIndex;
32     Standard_ShortReal       Shininess;
33     Standard_ShortReal       AmbientCoef;  //!< coefficient for Graphic3d_MaterialAspect::SetColor()
34     Standard_ShortReal       DiffuseCoef;  //!< coefficient for Graphic3d_MaterialAspect::SetColor()
35     Graphic3d_TypeOfMaterial MaterialType;
36     Graphic3d_NameOfMaterial MaterialName;
37
38     RawMaterial (Graphic3d_NameOfMaterial theName, const char* theStringName);
39
40   };
41
42   //! Name list of standard materials (defined within enumeration).
43   static const RawMaterial THE_MATERIALS[] =
44   {
45     RawMaterial (Graphic3d_NOM_BRASS,       "Brass"),
46     RawMaterial (Graphic3d_NOM_BRONZE,      "Bronze"),
47     RawMaterial (Graphic3d_NOM_COPPER,      "Copper"),
48     RawMaterial (Graphic3d_NOM_GOLD,        "Gold"),
49     RawMaterial (Graphic3d_NOM_PEWTER,      "Pewter"),
50     RawMaterial (Graphic3d_NOM_PLASTER,     "Plastered"),
51     RawMaterial (Graphic3d_NOM_PLASTIC,     "Plastified"),
52     RawMaterial (Graphic3d_NOM_SILVER,      "Silver"),
53     RawMaterial (Graphic3d_NOM_STEEL,       "Steel"),
54     RawMaterial (Graphic3d_NOM_STONE,       "Stone"),
55     RawMaterial (Graphic3d_NOM_SHINY_PLASTIC, "Shiny_plastified"),
56     RawMaterial (Graphic3d_NOM_SATIN,       "Satined"),
57     RawMaterial (Graphic3d_NOM_METALIZED,   "Metalized"),
58     RawMaterial (Graphic3d_NOM_NEON_GNC,    "Ionized"),
59     RawMaterial (Graphic3d_NOM_CHROME,      "Chrome"),
60     RawMaterial (Graphic3d_NOM_ALUMINIUM,   "Aluminium"),
61     RawMaterial (Graphic3d_NOM_OBSIDIAN,    "Obsidian"),
62     RawMaterial (Graphic3d_NOM_NEON_PHC,    "Neon"),
63     RawMaterial (Graphic3d_NOM_JADE,        "Jade"),
64     RawMaterial (Graphic3d_NOM_CHARCOAL,    "Charcoal"),
65     RawMaterial (Graphic3d_NOM_WATER,       "Water"),
66     RawMaterial (Graphic3d_NOM_GLASS,       "Glass"),
67     RawMaterial (Graphic3d_NOM_DIAMOND,     "Diamond"),
68     RawMaterial (Graphic3d_NOM_TRANSPARENT, "Transparent"),
69     RawMaterial (Graphic3d_NOM_DEFAULT,     "Default"),
70     RawMaterial (Graphic3d_NOM_UserDefined, "UserDefined")
71   };
72 }
73
74 // =======================================================================
75 // function : RawMaterial
76 // purpose  :
77 // =======================================================================
78 RawMaterial::RawMaterial (Graphic3d_NameOfMaterial theName, const char* theStringName)
79 : StringName      (theStringName),
80   BSDF            (Graphic3d_BSDF::CreateDiffuse (Graphic3d_Vec3 (0.0f))),
81   TransparencyCoef(0.0f),
82   RefractionIndex (1.0f),
83   Shininess       (0.039f),
84   AmbientCoef     (0.25f),
85   DiffuseCoef     (1.0f),
86   MaterialType    (Graphic3d_MATERIAL_ASPECT),
87   MaterialName    (theName)
88 {
89   switch (theName)
90   {
91     case Graphic3d_NOM_PLASTIC:
92       MaterialType = Graphic3d_MATERIAL_ASPECT;
93
94       Shininess = 0.0078125f;
95       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.25f));
96       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.24f));
97       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.004896f));
98       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
99
100       BSDF.Kd = Graphic3d_Vec3 (0.2f);
101       BSDF.Ks = Graphic3d_Vec4 (0.00784314f, 0.00784314f, 0.00784314f, 0.25f);
102       BSDF.Normalize();
103
104       break;
105     case Graphic3d_NOM_SHINY_PLASTIC:
106       MaterialType = Graphic3d_MATERIAL_ASPECT;
107
108       Shininess = 1.00f;
109       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.22f));
110       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.50f));
111       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (1.0f));
112       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
113
114       BSDF.Kd = Graphic3d_Vec3 (0.2f);
115       BSDF.Ks = Graphic3d_Vec4 (0.145f, 0.145f, 0.145f, 0.17f);
116       BSDF.Normalize();
117       break;
118     case Graphic3d_NOM_SATIN:
119       MaterialType = Graphic3d_MATERIAL_ASPECT;
120
121       Shininess = 0.09375f;
122       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.165f));
123       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.40f));
124       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.162647f));
125       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
126
127       BSDF.Kd = Graphic3d_Vec3 (0.2f);
128       BSDF.Ks = Graphic3d_Vec4 (0.6f);
129
130       break;
131     case Graphic3d_NOM_NEON_GNC:
132       MaterialType = Graphic3d_MATERIAL_ASPECT;
133
134       Shininess = 0.05f;
135       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.0f));
136       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (1.0f));
137       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.342392f));
138       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (1.0f));
139
140       BSDF.Kd = Graphic3d_Vec3 (0.0f);
141       BSDF.Ks = Graphic3d_Vec4 (0.5f, 0.5f, 0.5f, 0.f);
142       BSDF.Le = static_cast<Graphic3d_Vec3> (Colors[Graphic3d_TOR_DIFFUSE]);
143       BSDF.FresnelBase = Graphic3d_Fresnel::CreateDielectric (1.5f);
144       break;
145     case Graphic3d_NOM_METALIZED:
146       MaterialType = Graphic3d_MATERIAL_ASPECT;
147
148       Shininess = 0.13f;
149       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.0f));
150       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.47f));
151       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.170645f));
152       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
153
154       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
155                                              Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.2f)), 0.045f);
156       break;
157     case Graphic3d_NOM_BRASS:
158       MaterialType = Graphic3d_MATERIAL_PHYSIC;
159
160       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
161         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.58f, 0.42f, 0.20f)), 0.045f);
162
163       Shininess = 0.65f;
164       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.088428f, 0.041081f, 0.002090f));
165       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.570482f, 0.283555f, 0.012335f));
166       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.992f, 0.941f, 0.808f));
167       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
168       break;
169     case Graphic3d_NOM_BRONZE:
170       MaterialType = Graphic3d_MATERIAL_PHYSIC;
171
172       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
173         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.65f, 0.35f, 0.15f)), 0.045f);
174
175       Shininess = 0.65f;
176       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.037301f, 0.014931f, 0.004305f));
177       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.468185f, 0.153344f, 0.027491f));
178       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.590f, 0.408f, 0.250f));
179       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
180       break;
181     case Graphic3d_NOM_COPPER:
182       MaterialType = Graphic3d_MATERIAL_PHYSIC;
183
184       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
185         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.955008f, 0.637427f, 0.538163f)), 0.045f);
186
187       Shininess = 0.65f;
188       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.030370f, 0.006451f, 0.001780f));
189       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.323236f, 0.059254f, 0.007584f));
190       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.950f, 0.640f, 0.540f));
191       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
192       break;
193     case Graphic3d_NOM_GOLD:
194       MaterialType = Graphic3d_MATERIAL_PHYSIC;
195
196       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
197         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (1.000000f, 0.765557f, 0.336057f)), 0.045f);
198
199       Shininess = 0.80f;
200       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.073239f, 0.043234f, 0.009264f));
201       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.525643f, 0.295700f, 0.010023f));
202       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (1.000f, 0.710f, 0.290f));
203       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
204       break;
205     case Graphic3d_NOM_PEWTER:
206       MaterialType = Graphic3d_MATERIAL_PHYSIC;
207
208       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
209         Graphic3d_Fresnel::CreateConductor (1.8800f, 3.4900f), 0.045f);
210
211       Shininess = 0.50f;
212       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.010979f, 0.004795f, 0.012335f));
213       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.152583f, 0.188174f, 0.253972f));
214       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.333f, 0.333f, 0.522f));
215       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
216       break;
217     case Graphic3d_NOM_PLASTER:
218       MaterialType = Graphic3d_MATERIAL_ASPECT;
219
220       Shininess = 0.01f;
221       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.13f));
222       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.75f));
223       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.003936f));
224       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
225
226       BSDF.Kd = Graphic3d_Vec3 (0.482353f, 0.482353f, 0.482353f);
227
228       break;
229     case Graphic3d_NOM_SILVER:
230       MaterialType = Graphic3d_MATERIAL_PHYSIC;
231
232       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
233         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.971519f, 0.959915f, 0.915324f)), 0.045f);
234
235       Shininess = 0.75f;
236       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.061465f, 0.061465f, 0.050876f));
237       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.354692f, 0.354692f, 0.354692f));
238       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.950f, 0.930f, 0.880f));
239       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
240       break;
241     case Graphic3d_NOM_STEEL:
242       MaterialType = Graphic3d_MATERIAL_PHYSIC;
243
244       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
245         Graphic3d_Fresnel::CreateConductor (Graphic3d_Vec3 (2.90f, 2.80f, 2.53f), Graphic3d_Vec3 (3.08f, 2.90f, 2.74f)), 0.045f);
246
247       Shininess = 0.90f;
248       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.019607f, 0.019607f, 0.027212f));
249       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.214041f, 0.223414f, 0.233022f));
250       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.560f, 0.570f, 0.580f));
251       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
252       break;
253     case Graphic3d_NOM_STONE:
254       MaterialType = Graphic3d_MATERIAL_PHYSIC;
255
256       // special case for SetColor()
257       AmbientCoef = 0.19f * 0.25f;
258       DiffuseCoef = 0.75f;
259
260       Shininess = 0.17f;
261       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.030074f, 0.020069f, 0.013011f));
262       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.522522f, 0.318547f, 0.183064f));
263       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.98f, 1.0f, 0.60f) * 0.08f);
264       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
265
266       BSDF.Kd = Graphic3d_Vec3 (0.243137f, 0.243137f, 0.243137f);
267       BSDF.Ks = Graphic3d_Vec4 (0.00392157f, 0.00392157f, 0.00392157f, 0.5f);
268
269       break;
270     case Graphic3d_NOM_CHROME:
271       MaterialType = Graphic3d_MATERIAL_PHYSIC;
272
273       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
274         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.549585f, 0.556114f, 0.554256f)), 0.045f);
275
276       Shininess = 0.90f;
277       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.033105f, 0.033105f, 0.041436f));
278       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.263273f, 0.263273f, 0.263273f));
279       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.975f, 0.975f, 0.975f));
280       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
281       break;
282     case Graphic3d_NOM_ALUMINIUM:
283       MaterialType = Graphic3d_MATERIAL_PHYSIC;
284
285       BSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
286         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.913183f, 0.921494f, 0.924524f)), 0.045f);
287
288       Shininess = 0.75f;
289       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.073239f, 0.073239f, 0.073239f));
290       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.318547f, 0.318547f, 0.318547f));
291       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.910f, 0.920f, 0.920f));
292       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
293       break;
294     case Graphic3d_NOM_NEON_PHC:
295       MaterialType = Graphic3d_MATERIAL_PHYSIC;
296
297       Shininess = 0.05f;
298       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.0f));
299       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.0f));
300       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.62f));
301       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f, 0.787412f, 0.142892f));
302
303       BSDF.Kd = Graphic3d_Vec3 (0.0f);
304       BSDF.Ks = Graphic3d_Vec4 (0.5f, 0.5f, 0.5f, 0.f);
305       BSDF.Le = Graphic3d_Vec3 (0.0f, 1.0f, 0.46f);
306       BSDF.FresnelBase = Graphic3d_Fresnel::CreateDielectric (1.5f);
307       break;
308     case Graphic3d_NOM_OBSIDIAN:
309       MaterialType = Graphic3d_MATERIAL_PHYSIC;
310
311       Shininess = 0.3f;
312       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.004305f, 0.003936f, 0.005532f));
313       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.028053f, 0.024515f, 0.041436f));
314       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.333f, 0.329f, 0.346f));
315       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
316
317       BSDF.Kd = Graphic3d_Vec3 (0.023f, 0.f, 0.023f);
318       BSDF.Ks = Graphic3d_Vec4 (0.0156863f, 0.0156863f, 0.0156863f, 0.1f);
319       break;
320     case Graphic3d_NOM_JADE:
321       MaterialType = Graphic3d_MATERIAL_PHYSIC;
322
323       Shininess = 0.10f;
324       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.016338f, 0.040729f, 0.021493f));
325       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.252950f, 0.767769f, 0.354692f));
326       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.316f, 0.316f, 0.316f));
327       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
328
329       BSDF.FresnelBase = Graphic3d_Fresnel::CreateDielectric (1.5f);
330       BSDF.Kd = Graphic3d_Vec3 (0.208658f, 0.415686f, 0.218401f);
331       BSDF.Ks = Graphic3d_Vec4 (0.611765f, 0.611765f, 0.611765f, 0.06f);
332       break;
333     case Graphic3d_NOM_CHARCOAL:
334       MaterialType = Graphic3d_MATERIAL_PHYSIC;
335
336       Shininess = 0.01f;
337       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.003936f));
338       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.019607f));
339       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.0f));
340       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
341
342       BSDF.Kd = Graphic3d_Vec3 (0.02f, 0.02f, 0.02f);
343       BSDF.Ks = Graphic3d_Vec4 (0.1f, 0.1f, 0.1f, 0.3f);
344       break;
345     case Graphic3d_NOM_WATER:
346       MaterialType = Graphic3d_MATERIAL_PHYSIC;
347
348       RefractionIndex  = 1.33f;
349       BSDF             = Graphic3d_BSDF::CreateGlass (Graphic3d_Vec3 (1.f),
350                                                       Graphic3d_Vec3 (0.7f, 0.75f, 0.85f),
351                                                       0.05f,
352                                                       RefractionIndex);
353       TransparencyCoef = 0.80f;
354
355       Shininess = 0.90f;
356       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.170645f, 0.170645f, 0.191627f));
357       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.003936f, 0.003936f, 0.006571f));
358       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.380f, 0.380f, 0.380f));
359       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
360       break;
361     case Graphic3d_NOM_GLASS:
362       MaterialType = Graphic3d_MATERIAL_PHYSIC;
363
364       RefractionIndex  = 1.62f;
365       BSDF             = Graphic3d_BSDF::CreateGlass (Graphic3d_Vec3 (1.f),
366                                                       Graphic3d_Vec3 (0.75f, 0.95f, 0.9f),
367                                                       0.05f,
368                                                       RefractionIndex);
369       TransparencyCoef = 0.80f;
370
371       Shininess = 0.50f;
372       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.263273f, 0.290143f, 0.290143f));
373       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.003936f, 0.006571f, 0.006571f));
374       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.920f, 0.920f, 0.920f));
375       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
376       break;
377     case Graphic3d_NOM_DIAMOND:
378       MaterialType = Graphic3d_MATERIAL_PHYSIC;
379
380       RefractionIndex  = 2.42f;
381       BSDF             = Graphic3d_BSDF::CreateGlass (Graphic3d_Vec3 (1.f),
382                                                       Graphic3d_Vec3 (0.95f, 0.95f, 0.95f),
383                                                       0.05f,
384                                                       RefractionIndex);
385       TransparencyCoef = 0.80f;
386
387       Shininess = 0.90f;
388       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.263273f));
389       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.010023f));
390       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.970f));
391       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
392       break;
393
394     case Graphic3d_NOM_TRANSPARENT:
395       MaterialType = Graphic3d_MATERIAL_PHYSIC;
396
397       RefractionIndex = 1.0f;
398
399       BSDF.Kd = Graphic3d_Vec3 (0.1f);
400       BSDF.Kt = Graphic3d_Vec3 (0.9f);
401       BSDF.FresnelBase = Graphic3d_Fresnel::CreateConstant (0.0f);
402       TransparencyCoef = 0.80f;
403
404       Shininess = 0.90f;
405       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.263273f));
406       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.010023f));
407       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.970f));
408       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
409       break;
410     case Graphic3d_NOM_UserDefined:
411       MaterialType = Graphic3d_MATERIAL_PHYSIC;
412       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.1f));
413       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.6f));
414       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.2f));
415       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
416       break;
417     case Graphic3d_NOM_DEFAULT:
418       MaterialType = Graphic3d_MATERIAL_ASPECT;
419
420       Colors[Graphic3d_TOR_AMBIENT]  = Quantity_Color (Graphic3d_Vec3 (0.15f));
421       Colors[Graphic3d_TOR_DIFFUSE]  = Quantity_Color (Graphic3d_Vec3 (0.65f));
422       Colors[Graphic3d_TOR_SPECULAR] = Quantity_Color (Graphic3d_Vec3 (0.0f));
423       Colors[Graphic3d_TOR_EMISSION] = Quantity_Color (Graphic3d_Vec3 (0.0f));
424       break;
425   }
426   PBRMaterial.SetBSDF (BSDF);
427 }
428
429 // =======================================================================
430 // function : Graphic3d_MaterialAspect
431 // purpose  :
432 // =======================================================================
433 Graphic3d_MaterialAspect::Graphic3d_MaterialAspect()
434 : myRequestedMaterialName (Graphic3d_NOM_DEFAULT)
435 {
436   init (Graphic3d_NOM_DEFAULT);
437 }
438
439 // =======================================================================
440 // function : Graphic3d_MaterialAspect
441 // purpose  :
442 // =======================================================================
443 Graphic3d_MaterialAspect::Graphic3d_MaterialAspect (const Graphic3d_NameOfMaterial theName)
444 : myRequestedMaterialName (theName)
445 {
446   init (theName);
447 }
448
449 // =======================================================================
450 // function : init
451 // purpose  :
452 // =======================================================================
453 void Graphic3d_MaterialAspect::init (const Graphic3d_NameOfMaterial theName)
454 {
455   const RawMaterial& aMat = THE_MATERIALS[theName];
456   myBSDF        = aMat.BSDF;
457   myPBRMaterial = aMat.PBRMaterial;
458   myStringName  = aMat.StringName;
459   myColors[Graphic3d_TOR_AMBIENT]     = aMat.Colors[Graphic3d_TOR_AMBIENT];
460   myColors[Graphic3d_TOR_DIFFUSE]     = aMat.Colors[Graphic3d_TOR_DIFFUSE];
461   myColors[Graphic3d_TOR_SPECULAR]    = aMat.Colors[Graphic3d_TOR_SPECULAR];
462   myColors[Graphic3d_TOR_EMISSION]    = aMat.Colors[Graphic3d_TOR_EMISSION];
463   myTransparencyCoef = aMat.TransparencyCoef;
464   myRefractionIndex  = aMat.RefractionIndex;
465   myShininess        = aMat.Shininess;
466   myMaterialType     = aMat.MaterialType;
467   myMaterialName     = theName;
468   myRequestedMaterialName = theName;
469 }
470
471 // =======================================================================
472 // function : IncreaseShine
473 // purpose  :
474 // =======================================================================
475 void Graphic3d_MaterialAspect::IncreaseShine (const Standard_ShortReal theDelta)
476 {
477   const Standard_ShortReal anOldShine = myShininess;
478   myShininess = myShininess + myShininess * theDelta / 100.0f;
479   if (myShininess > 1.0f || myShininess < 0.0f)
480   {
481     myShininess = anOldShine;
482   }
483 }
484
485 // =======================================================================
486 // function : SetMaterialType
487 // purpose  :
488 // =======================================================================
489 void Graphic3d_MaterialAspect::SetMaterialType (const Graphic3d_TypeOfMaterial theType)
490 {
491   myMaterialType = theType;
492   if (theType != myMaterialType)
493   {
494     setUserMaterial();
495   }
496 }
497
498 // =======================================================================
499 // function : SetColor
500 // purpose  :
501 // =======================================================================
502 void Graphic3d_MaterialAspect::SetColor (const Quantity_Color& theColor)
503 {
504   if (myMaterialType == Graphic3d_MATERIAL_ASPECT)
505   {
506     return;
507   }
508
509   myPBRMaterial.SetColor (theColor);
510
511   const RawMaterial& aSrcMat = THE_MATERIALS[myRequestedMaterialName];
512   const Quantity_Color anAmbient((Graphic3d_Vec3 )theColor * aSrcMat.AmbientCoef);
513   const Quantity_Color aDiffuse ((Graphic3d_Vec3 )theColor * aSrcMat.DiffuseCoef);
514   if (myMaterialName != Graphic3d_NOM_UserDefined
515    && (!myColors[Graphic3d_TOR_AMBIENT].IsEqual (anAmbient)
516     || !myColors[Graphic3d_TOR_DIFFUSE].IsEqual (aDiffuse)))
517   {
518     setUserMaterial();
519   }
520   myColors[Graphic3d_TOR_AMBIENT] = anAmbient;
521   myColors[Graphic3d_TOR_DIFFUSE] = aDiffuse;
522 }
523
524 // =======================================================================
525 // function : SetAmbientColor
526 // purpose  :
527 // =======================================================================
528 void Graphic3d_MaterialAspect::SetAmbientColor (const Quantity_Color& theColor)
529 {
530   if (myMaterialType == Graphic3d_MATERIAL_PHYSIC
531   &&  myMaterialName != Graphic3d_NOM_UserDefined
532   && !myColors[Graphic3d_TOR_AMBIENT].IsEqual (theColor))
533   {
534     setUserMaterial();
535   }
536   myColors[Graphic3d_TOR_AMBIENT] = theColor;
537 }
538
539 // =======================================================================
540 // function : SetDiffuseColor
541 // purpose  :
542 // =======================================================================
543 void Graphic3d_MaterialAspect::SetDiffuseColor (const Quantity_Color& theColor)
544 {
545   if (myMaterialType == Graphic3d_MATERIAL_PHYSIC
546   &&  myMaterialName != Graphic3d_NOM_UserDefined
547   && !myColors[Graphic3d_TOR_DIFFUSE].IsEqual (theColor))
548   {
549     setUserMaterial();
550   }
551   myColors[Graphic3d_TOR_DIFFUSE] = theColor;
552 }
553
554 // =======================================================================
555 // function : SetSpecularColor
556 // purpose  :
557 // =======================================================================
558 void Graphic3d_MaterialAspect::SetSpecularColor (const Quantity_Color& theColor)
559 {
560   if (myMaterialType == Graphic3d_MATERIAL_PHYSIC
561   &&  myMaterialName != Graphic3d_NOM_UserDefined
562   && !myColors[Graphic3d_TOR_SPECULAR].IsEqual (theColor))
563   {
564     setUserMaterial();
565   }
566   myColors[Graphic3d_TOR_SPECULAR] = theColor;
567 }
568
569 // =======================================================================
570 // function : SetEmissiveColor
571 // purpose  :
572 // =======================================================================
573 void Graphic3d_MaterialAspect::SetEmissiveColor (const Quantity_Color& theColor)
574 {
575   if (myMaterialType == Graphic3d_MATERIAL_PHYSIC
576   &&  myMaterialName != Graphic3d_NOM_UserDefined
577   && !myColors[Graphic3d_TOR_EMISSION].IsEqual (theColor))
578   {
579     setUserMaterial();
580   }
581   myColors[Graphic3d_TOR_EMISSION] = theColor;
582 }
583
584 // =======================================================================
585 // function : SetTransparency
586 // purpose  :
587 // =======================================================================
588 void Graphic3d_MaterialAspect::SetTransparency (const Standard_ShortReal theValue)
589 {
590   if (theValue < 0.0f
591    || theValue > 1.0f)
592   {
593     throw Graphic3d_MaterialDefinitionError("Bad value for SetTransparency < 0. or > 1.0");
594   }
595
596   myTransparencyCoef = theValue;
597   myPBRMaterial.SetAlpha (1.0f - theValue);
598 }
599
600 // =======================================================================
601 // function : SetRefractionIndex
602 // purpose  :
603 // =======================================================================
604 void Graphic3d_MaterialAspect::SetRefractionIndex (const Standard_ShortReal theValue)
605 {
606   if (theValue < 1.0f)
607   {
608     throw Graphic3d_MaterialDefinitionError("Bad value for refraction index < 1.0");
609   }
610
611   myRefractionIndex = theValue;
612 }
613
614 // =======================================================================
615 // function : SetShininess
616 // purpose  :
617 // =======================================================================
618 void Graphic3d_MaterialAspect::SetShininess (const Standard_ShortReal theValue)
619 {
620   if (theValue < 0.0f
621    || theValue > 1.0f)
622   {
623     throw Graphic3d_MaterialDefinitionError("Bad value for Shininess < 0. or > 1.0");
624   }
625
626   if (myShininess != theValue)
627   {
628     myShininess = theValue;
629     setUserMaterial();
630   }
631 }
632
633 // =======================================================================
634 // function : MaterialName
635 // purpose  :
636 // =======================================================================
637 Standard_CString Graphic3d_MaterialAspect::MaterialName (const Standard_Integer theRank)
638 {
639   if (theRank < 1 || theRank > NumberOfMaterials())
640   {
641     throw Standard_OutOfRange("BAD index of material");
642   }
643   const RawMaterial& aMat = THE_MATERIALS[theRank - 1];
644   return aMat.StringName;
645 }
646
647 // =======================================================================
648 // function : MaterialFromName
649 // purpose  :
650 // =======================================================================
651 Standard_Boolean Graphic3d_MaterialAspect::MaterialFromName (const Standard_CString theName,
652                                                              Graphic3d_NameOfMaterial& theMat)
653 {
654   TCollection_AsciiString aName (theName);
655   aName.LowerCase();
656   aName.Capitalize();
657   const Standard_Integer aNbMaterials = Graphic3d_MaterialAspect::NumberOfMaterials();
658   for (Standard_Integer aMatIter = 0; aMatIter <= aNbMaterials; ++aMatIter)
659   {
660     const RawMaterial& aMat = THE_MATERIALS[aMatIter];
661     if (aName == aMat.StringName)
662     {
663       theMat = Graphic3d_NameOfMaterial(aMatIter);
664       return Standard_True;
665     }
666   }
667
668   // parse aliases
669   if (aName == "Plastic")            // Plastified
670   {
671     theMat = Graphic3d_NOM_PLASTIC;
672     return Standard_True;
673   }
674   else if (aName == "Shiny_plastic") // Shiny_plastified
675   {
676     theMat = Graphic3d_NOM_SHINY_PLASTIC;
677     return Standard_True;
678   }
679   else if (aName == "Plaster")       // Plastered
680   {
681     theMat = Graphic3d_NOM_PLASTER;
682     return Standard_True;
683   }
684   else if (aName == "Satin")         // Satined
685   {
686     theMat = Graphic3d_NOM_SATIN;
687     return Standard_True;
688   }
689   else if (aName == "Neon_gnc")      // Ionized
690   {
691     theMat = Graphic3d_NOM_NEON_GNC;
692     return Standard_True;
693   }
694   else if (aName == "Neon_phc") // Neon
695   {
696     theMat = Graphic3d_NOM_NEON_PHC;
697     return Standard_True;
698   }
699   return Standard_False;
700 }
701
702 // =======================================================================
703 // function : MaterialType
704 // purpose  :
705 // =======================================================================
706 Graphic3d_TypeOfMaterial Graphic3d_MaterialAspect::MaterialType (const Standard_Integer theRank)
707 {
708   if (theRank < 1 || theRank > NumberOfMaterials())
709   {
710     throw Standard_OutOfRange("BAD index of material");
711   }
712   const RawMaterial& aMat = THE_MATERIALS[theRank - 1];
713   return aMat.MaterialType;
714 }