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