f908c939aa9e1c259c3eeeb4f3b5ceb747b04d6c
[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
16 #include <Graphic3d_MaterialAspect.hxx>
17 #include <Graphic3d_MaterialDefinitionError.hxx>
18 #include <Quantity_Color.hxx>
19 #include <Standard_Assert.hxx>
20 #include <Standard_OutOfRange.hxx>
21
22 // =======================================================================
23 // function : Graphic3d_MaterialAspect
24 // purpose  :
25 // =======================================================================
26 Graphic3d_MaterialAspect::Graphic3d_MaterialAspect()
27 : myDiffuseColor (0.2, 0.2, 0.2, Quantity_TOC_RGB),
28   myAmbientColor (0.2, 0.2, 0.2, Quantity_TOC_RGB)
29 {
30   myRequestedMaterialName = Graphic3d_NOM_DEFAULT;
31   Init (myRequestedMaterialName);
32 }
33
34 // =======================================================================
35 // function : Graphic3d_MaterialAspect
36 // purpose  :
37 // =======================================================================
38 Graphic3d_MaterialAspect::Graphic3d_MaterialAspect (const Graphic3d_NameOfMaterial theName)
39 : myDiffuseColor (0.2, 0.2, 0.2, Quantity_TOC_RGB),
40   myAmbientColor (0.2, 0.2, 0.2, Quantity_TOC_RGB)
41 {
42   myRequestedMaterialName = theName;
43   Init (myRequestedMaterialName);
44 }
45
46 // =======================================================================
47 // function : Init
48 // purpose  :
49 // =======================================================================
50 void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
51 {
52   myMaterialType     = Graphic3d_MATERIAL_ASPECT;
53   myTransparencyCoef = 0.0f;
54   myRefractionIndex  = 1.0f;
55   myDiffuseActivity  = Standard_True;
56   myDiffuseCoef      = 0.65f;
57   myAmbientActivity  = Standard_True;
58   myAmbientCoef      = 0.3f;
59   mySpecularActivity = Standard_True;
60   mySpecularCoef     = 0.0f;
61   myEmissiveActivity = Standard_False;
62   myEmissiveCoef     = 0.0f;
63   myEnvReflexion     = 0.0f;
64   myShininess        = 0.039f;
65   myDiffuseColor .SetValues (0.2, 0.2, 0.2, Quantity_TOC_RGB);
66   mySpecularColor.SetValues (1.0, 1.0, 1.0, Quantity_TOC_RGB);
67   myMaterialName     = theName;
68
69   myBSDF = Graphic3d_BSDF::CreateDiffuse (Graphic3d_Vec3 (0.2f, 0.2f, 0.2f));
70
71   Standard_Integer index = Standard_Integer (theName);
72   if (index < NumberOfMaterials())
73   {
74     myStringName = MaterialName (index + 1);
75   }
76
77   switch (theName)
78   {
79     case Graphic3d_NOM_PLASTIC:
80       myShininess    = Standard_ShortReal (0.0078125);
81       myAmbientCoef  = Standard_ShortReal (0.5);
82       myDiffuseCoef  = Standard_ShortReal (0.24);
83       mySpecularCoef = Standard_ShortReal (0.06);
84
85       myBSDF.Kd = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
86                                   static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
87                                   static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
88       myBSDF.Ks = Graphic3d_Vec3 (0.00784314f, 0.00784314f, 0.00784314f);
89       myBSDF.Normalize();
90       myBSDF.Roughness = 32;
91       break;
92     case Graphic3d_NOM_SHINY_PLASTIC:
93       myShininess    = Standard_ShortReal (1.0);
94       myAmbientCoef  = Standard_ShortReal (0.44);
95       myDiffuseCoef  = Standard_ShortReal (0.5);
96       mySpecularCoef = Standard_ShortReal (1.0);
97
98       myBSDF.Kd = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
99                                   static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
100                                   static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
101       myBSDF.Ks = Graphic3d_Vec3 (0.0156863f, 0.0156863f, 0.0156863f);
102       myBSDF.Normalize();
103       myBSDF.Roughness = 64.f;
104       break;
105     case Graphic3d_NOM_SATIN :
106       myShininess    = Standard_ShortReal (0.09375);
107       myAmbientCoef  = Standard_ShortReal (0.33);
108       myDiffuseCoef  = Standard_ShortReal (0.4);
109       mySpecularCoef = Standard_ShortReal (0.44);
110
111       myBSDF.Kd = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
112                                   static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
113                                   static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
114       myBSDF.Ks = Graphic3d_Vec3 (0.0313726f, 0.0313726f, 0.0313726f);
115       myBSDF.Roughness = 16.f;
116       myBSDF.Normalize();
117       break;
118     case Graphic3d_NOM_NEON_GNC:
119       myShininess    = Standard_ShortReal (0.05);
120       myAmbientCoef  = Standard_ShortReal (1.0);
121       myDiffuseCoef  = Standard_ShortReal (1.0);
122       mySpecularCoef = Standard_ShortReal (0.62);
123       myEmissiveCoef = Standard_ShortReal (1.0);
124       myEmissiveActivity = Standard_True;
125       myAmbientActivity  = Standard_False;
126
127       myBSDF.Kr = Graphic3d_Vec3 (0.207843f, 0.207843f, 0.207843f);
128       myBSDF.Le = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
129                                   static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
130                                   static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
131       myBSDF.Fresnel = Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.3f, 0.3f, 0.3f));
132       break;
133     case Graphic3d_NOM_METALIZED:
134       myShininess    = Standard_ShortReal (0.13);
135       myAmbientCoef  = Standard_ShortReal (0.9);
136       myDiffuseCoef  = Standard_ShortReal (0.47);
137       mySpecularCoef = Standard_ShortReal (0.45);
138       myAmbientActivity  = Standard_False;
139
140       {
141         Graphic3d_Vec3 aColor (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
142                                static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
143                                static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
144
145         myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
146           Graphic3d_Fresnel::CreateSchlick (aColor), 1024.f);
147       }
148       break;
149     // Ascending Compatibility physical materials. The same definition is taken as in the next constructor.
150     case Graphic3d_NOM_BRASS:
151       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
152
153       myShininess    = 0.65f;
154       myAmbientCoef  = 1.00f;
155       myDiffuseCoef  = 1.00f;
156       mySpecularCoef = 1.00f;
157
158       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
159         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.58f, 0.42f, 0.20f)), 1024.f);
160
161       // Color resulting from ambient
162       myAmbientColor .SetValues (0.329f, 0.224f, 0.027f, Quantity_TOC_RGB);
163       // Color resulting from dispersed
164       myDiffuseColor .SetValues (0.780f, 0.569f, 0.114f, Quantity_TOC_RGB);
165       // Color resulting from specular
166       mySpecularColor.SetValues (0.992f, 0.941f, 0.808f, Quantity_TOC_RGB);
167       break;
168     case Graphic3d_NOM_BRONZE:
169       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
170
171       myShininess    = 0.65f;
172       myAmbientCoef  = 1.00f;
173       myDiffuseCoef  = 1.00f;
174       mySpecularCoef = 1.00f;
175
176       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
177         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.65f, 0.35f, 0.15f)), 1024.f);
178
179       // Color resulting from ambient
180       myAmbientColor .SetValues (0.213f, 0.128f, 0.054f, Quantity_TOC_RGB);
181       // Color resulting from dispersed
182       myDiffuseColor .SetValues (0.714f, 0.428f, 0.181f, Quantity_TOC_RGB);
183       // Color resulting from specular
184       mySpecularColor.SetValues (0.590f, 0.408f, 0.250f, Quantity_TOC_RGB);
185       break;
186     case Graphic3d_NOM_COPPER:
187       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
188
189       myShininess    = 0.65f;
190       myAmbientCoef  = 1.00f;
191       myDiffuseCoef  = 1.00f;
192       mySpecularCoef = 1.00f;
193
194       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
195         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.955008f, 0.637427f, 0.538163f)), 1024.f);
196
197       // Color resulting from ambient
198       myAmbientColor .SetValues (0.191f, 0.074f, 0.023f, Quantity_TOC_RGB);
199       // Color resulting from dispersed
200       myDiffuseColor .SetValues (0.604f, 0.270f, 0.083f, Quantity_TOC_RGB);
201       // Color resulting from specular
202       mySpecularColor.SetValues (0.950f, 0.640f, 0.540f, Quantity_TOC_RGB);
203       break;
204     case Graphic3d_NOM_GOLD:
205       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
206
207       myShininess    = 0.80f;
208       myAmbientCoef  = 1.00f;
209       myDiffuseCoef  = 1.00f;
210       mySpecularCoef = 1.00f;
211
212       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
213         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (1.000000f, 0.765557f, 0.336057f)), 1024.f);
214
215       // Color resulting from ambient
216       myAmbientColor .SetValues (0.300f, 0.230f, 0.095f, Quantity_TOC_RGB);
217       // Color resulting from dispersed
218       myDiffuseColor .SetValues (0.752f, 0.580f, 0.100f, Quantity_TOC_RGB);
219       // Color resulting from specular
220       mySpecularColor.SetValues (1.000f, 0.710f, 0.290f, Quantity_TOC_RGB);
221       break;
222     case Graphic3d_NOM_PEWTER:
223       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
224
225       myShininess    = 0.50f;
226       myAmbientCoef  = 1.00f;
227       myDiffuseCoef  = 1.00f;
228       mySpecularCoef = 1.00f;
229
230       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
231         Graphic3d_Fresnel::CreateConductor (1.8800f, 3.4900f), 1024.f);
232
233       // Color resulting from ambient
234       myAmbientColor .SetValues (0.106f, 0.059f, 0.114f, Quantity_TOC_RGB);
235       // Color resulting from dispersed
236       myDiffuseColor .SetValues (0.427f, 0.471f, 0.541f, Quantity_TOC_RGB);
237       // Color resulting from specular
238       mySpecularColor.SetValues (0.333f, 0.333f, 0.522f, Quantity_TOC_RGB);
239       break;
240     case Graphic3d_NOM_PLASTER:
241       myShininess    = 0.01f;
242       myAmbientCoef  = 0.26f;
243       myDiffuseCoef  = 0.75f;
244       mySpecularCoef = 0.05f;
245
246       // Color resulting from ambient
247       myAmbientColor .SetValues (0.192f, 0.192f, 0.192f, Quantity_TOC_RGB);
248       // Color resulting from dispersed
249       myDiffuseColor .SetValues (0.508f, 0.508f, 0.508f, Quantity_TOC_RGB);
250       // Color resulting from specular
251       mySpecularColor.SetValues (0.508f, 0.508f, 0.508f, Quantity_TOC_RGB);
252
253       myBSDF.Kd = Graphic3d_Vec3 (0.482353f, 0.482353f, 0.482353f);
254
255       break;
256     case Graphic3d_NOM_SILVER:
257       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
258
259       myShininess    = 0.75f;
260       myAmbientCoef  = 1.00f;
261       myDiffuseCoef  = 1.00f;
262       mySpecularCoef = 1.00f;
263
264       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
265         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.971519f, 0.959915f, 0.915324f)), 1024.f);
266
267       // Color resulting from ambient
268       myAmbientColor .SetValues (0.275f, 0.275f, 0.250f, Quantity_TOC_RGB);
269       // Color resulting from dispersed
270       myDiffuseColor .SetValues (0.630f, 0.630f, 0.630f, Quantity_TOC_RGB);
271       // Color resulting from specular
272       mySpecularColor.SetValues (0.950f, 0.930f, 0.880f, Quantity_TOC_RGB);
273       break;
274     case Graphic3d_NOM_STEEL:
275       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
276
277       myShininess    = 0.90f;
278       myAmbientCoef  = 1.00f;
279       myDiffuseCoef  = 1.00f;
280       mySpecularCoef = 1.00f;
281
282       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
283         Graphic3d_Fresnel::CreateConductor (Graphic3d_Vec3 (2.90f, 2.80f, 2.53f), Graphic3d_Vec3 (3.08f, 2.90f, 2.74f)), 1024.f);
284
285       // Color resulting from ambient
286       myAmbientColor .SetValues (0.150f, 0.150f, 0.180f, Quantity_TOC_RGB);
287       // Color resulting from dispersed
288       myDiffuseColor .SetValues (0.500f, 0.510f, 0.520f, Quantity_TOC_RGB);
289       // Color resulting from specular
290       mySpecularColor.SetValues (0.560f, 0.570f, 0.580f, Quantity_TOC_RGB);
291       break;
292     case Graphic3d_NOM_STONE:
293       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
294
295       myShininess    = 0.17f;
296       myAmbientCoef  = 0.19f;
297       myDiffuseCoef  = 0.75f;
298       mySpecularCoef = 0.08f;
299
300       // Color resulting from ambient
301       myAmbientColor .SetValues (1.0,  0.8, 0.62, Quantity_TOC_RGB);
302       // Color resulting from dispersed
303       myDiffuseColor .SetValues (1.0,  0.8, 0.62, Quantity_TOC_RGB);
304       // Color resulting from specular
305       mySpecularColor.SetValues (0.98, 1.0, 0.60, Quantity_TOC_RGB);
306
307       myBSDF.Kd = Graphic3d_Vec3 (0.243137f, 0.243137f, 0.243137f);
308       myBSDF.Ks = Graphic3d_Vec3 (0.00392157f, 0.00392157f, 0.00392157f);
309
310       break;
311     // Ascending Compatibility of physical materials. Takes the same definition as in the next constructor. New materials
312     case Graphic3d_NOM_CHROME:
313       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
314
315       myShininess    = 0.90f;
316       myAmbientCoef  = 1.00f;
317       myDiffuseCoef  = 1.00f;
318       mySpecularCoef = 1.00f;
319
320       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
321         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.549585f, 0.556114f, 0.554256f)), 1024.f);
322
323       // Color resulting from ambient
324       myAmbientColor .SetValues (0.200f, 0.200f, 0.225f, Quantity_TOC_RGB);
325       // Color resulting from dispersed
326       myDiffuseColor .SetValues (0.550f, 0.550f, 0.550f, Quantity_TOC_RGB);
327       // Color resulting from specular
328       mySpecularColor.SetValues (0.975f, 0.975f, 0.975f, Quantity_TOC_RGB);
329       break;
330     case Graphic3d_NOM_ALUMINIUM:
331       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
332
333       myShininess    = 0.75f;
334       myAmbientCoef  = 1.00f;
335       myDiffuseCoef  = 1.00f;
336       mySpecularCoef = 1.00f;
337
338       myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
339         Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.913183f, 0.921494f, 0.924524f)), 1024.f);
340
341       // Color resulting from ambient
342       myAmbientColor .SetValues (0.300f, 0.300f, 0.300f, Quantity_TOC_RGB);
343       // Color resulting from dispersed
344       myDiffuseColor .SetValues (0.600f, 0.600f, 0.600f, Quantity_TOC_RGB);
345       // Color resulting from specular
346       mySpecularColor.SetValues (0.910f, 0.920f, 0.920f, Quantity_TOC_RGB);
347       break;
348     case Graphic3d_NOM_NEON_PHC:
349       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
350
351       myShininess    = 0.05f;
352       myAmbientCoef  = 1.00f;
353       myDiffuseCoef  = 1.00f;
354       mySpecularCoef = 0.62f;
355       myEmissiveCoef = 0.90f;
356       myAmbientActivity  = Standard_False;
357       myDiffuseActivity  = Standard_False;
358       myEmissiveActivity = Standard_True;
359
360       // Color resulting from ambient
361       myAmbientColor .SetValues (1.0, 1.0, 1.0,  Quantity_TOC_RGB);
362       // Color resulting from dispersed
363       myDiffuseColor .SetValues (1.0, 1.0, 1.0,  Quantity_TOC_RGB);
364       // Color resulting from specular
365       mySpecularColor.SetValues (1.0, 1.0, 1.0,  Quantity_TOC_RGB);
366       // Color resulting from specular
367       myEmissiveColor.SetValues (0.0, 1.0, 0.46, Quantity_TOC_RGB);
368
369       myBSDF.Kr = Graphic3d_Vec3 (0.207843f, 0.207843f, 0.207843f);
370       myBSDF.Le = Graphic3d_Vec3 (0.0f, 1.0f, 0.46f);
371       myBSDF.Fresnel = Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.3f, 0.3f, 0.3f));
372       break;
373     case Graphic3d_NOM_OBSIDIAN:
374       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
375
376       myShininess    = 0.3f;
377       myAmbientCoef  = 1.0f;
378       myDiffuseCoef  = 1.0f;
379       mySpecularCoef = 1.0f;
380
381       // Color resulting from ambient
382       myAmbientColor .SetValues (0.054f, 0.050f, 0.066f, Quantity_TOC_RGB);
383       // Color resulting from dispersed
384       myDiffuseColor .SetValues (0.183f, 0.170f, 0.225f, Quantity_TOC_RGB);
385       // Color resulting from specular
386       mySpecularColor.SetValues (0.333f, 0.329f, 0.346f, Quantity_TOC_RGB);
387
388       myBSDF.Kd = Graphic3d_Vec3 (0.0156863f, 0.f, 0.0155017f);
389       myBSDF.Ks = Graphic3d_Vec3 (0.0156863f, 0.0156863f, 0.0156863f);
390       myBSDF.Roughness = 1024.f;
391       break;
392     case Graphic3d_NOM_JADE:
393       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
394
395       myShininess    = 0.10f;
396       myAmbientCoef  = 1.00f;
397       myDiffuseCoef  = 1.00f;
398       mySpecularCoef = 1.00f;
399
400       // Color resulting from ambient
401       myAmbientColor .SetValues (0.135f, 0.223f, 0.158f, Quantity_TOC_RGB);
402       // Color resulting from dispersed
403       myDiffuseColor .SetValues (0.540f, 0.890f, 0.630f, Quantity_TOC_RGB);
404       // Color resulting from specular
405       mySpecularColor.SetValues (0.316f, 0.316f, 0.316f, Quantity_TOC_RGB);
406
407       myBSDF.Fresnel = Graphic3d_Fresnel::CreateDielectric (1.5f);
408       myBSDF.Kd = Graphic3d_Vec3 (0.208658f, 0.415686f, 0.218401f);
409       myBSDF.Ks = Graphic3d_Vec3 (0.611765f, 0.611765f, 0.611765f);
410       myBSDF.Roughness = 512.f;
411       break;
412     case Graphic3d_NOM_CHARCOAL:
413       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
414
415       myShininess    = 0.01f;
416       myAmbientCoef  = 1.00f;
417       myDiffuseCoef  = 1.00f;
418       mySpecularCoef = 1.00f;
419
420       // Color resulting from ambient
421       myAmbientColor .SetValues (0.050f, 0.050f, 0.050f, Quantity_TOC_RGB);
422       // Color resulting from dispersed
423       myDiffuseColor .SetValues (0.150f, 0.150f, 0.150f, Quantity_TOC_RGB);
424       // Color resulting from specular
425       mySpecularColor.SetValues (0.000f, 0.000f, 0.000f, Quantity_TOC_RGB);
426
427       myBSDF.Kd = Graphic3d_Vec3 (0.0196078f, 0.0196078f, 0.0196078f);
428       myBSDF.Ks = Graphic3d_Vec3 (0.0196078f, 0.0196078f, 0.0196078f);
429       myBSDF.Roughness = 8;
430       break;
431     case Graphic3d_NOM_WATER:
432       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
433
434       myShininess    = 0.90f;
435       myAmbientCoef  = 1.00f;
436       myDiffuseCoef  = 1.00f;
437       mySpecularCoef = 1.00f;
438       myRefractionIndex  = 1.33f;
439       myBSDF             = Graphic3d_BSDF::CreateGlass (Graphic3d_Vec3 (1.f),
440                                                         Graphic3d_Vec3 (0.7f, 0.75f, 0.85f),
441                                                         0.05f,
442                                                         myRefractionIndex);
443       myTransparencyCoef = 0.80f;
444
445       // Color resulting from ambient
446       myAmbientColor.SetValues (0.450f, 0.450f, 0.475f, Quantity_TOC_RGB);
447       // Color resulting from dispersed
448       myDiffuseColor.SetValues (0.050f, 0.050f, 0.075f, Quantity_TOC_RGB);
449       // Color resulting from specular
450       mySpecularColor.SetValues (0.380f, 0.380f, 0.380f, Quantity_TOC_RGB);
451       break;
452     case Graphic3d_NOM_GLASS:
453       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
454
455       myShininess    = 0.50f;
456       myAmbientCoef  = 1.00f;
457       myDiffuseCoef  = 1.00f;
458       mySpecularCoef = 1.00f;
459       myRefractionIndex  = 1.62f;
460       myBSDF             = Graphic3d_BSDF::CreateGlass (Graphic3d_Vec3 (1.f),
461                                                         Graphic3d_Vec3 (0.75f, 0.95f, 0.9f),
462                                                         0.05f,
463                                                         myRefractionIndex);
464       myTransparencyCoef = 0.80f;
465
466       // Color resulting from ambient
467       myAmbientColor.SetValues (0.550f, 0.575f, 0.575f, Quantity_TOC_RGB);
468       // Color resulting from dispersed
469       myDiffuseColor.SetValues (0.050f, 0.075f, 0.075f, Quantity_TOC_RGB);
470       // Color resulting from specular
471       mySpecularColor.SetValues (0.920f, 0.920f, 0.920f, Quantity_TOC_RGB);
472       break;
473     case Graphic3d_NOM_DIAMOND:
474       myMaterialType = Graphic3d_MATERIAL_PHYSIC;
475
476       myShininess    = 0.90f;
477       myAmbientCoef  = 1.00f;
478       myDiffuseCoef  = 1.00f;
479       mySpecularCoef = 1.00f;
480       myRefractionIndex  = 2.42f;
481       myBSDF             = Graphic3d_BSDF::CreateGlass (Graphic3d_Vec3 (1.f),
482                                                         Graphic3d_Vec3 (0.95f, 0.95f, 0.95f),
483                                                         0.05f,
484                                                         myRefractionIndex);
485       myTransparencyCoef = 0.80f;
486
487       // Color resulting from ambient
488       myAmbientColor.SetValues (0.550f, 0.550f, 0.550f, Quantity_TOC_RGB);
489       // Color resulting from dispersed
490       myDiffuseColor.SetValues (0.100f, 0.100f, 0.100f, Quantity_TOC_RGB);
491       // Color resulting from specular
492       mySpecularColor.SetValues (0.970f, 0.970f, 0.970f, Quantity_TOC_RGB);
493       break;
494     case Graphic3d_NOM_UserDefined:
495       myStringName = "UserDefined";
496       break;
497     case Graphic3d_NOM_DEFAULT:
498     default:
499       myStringName = "Default";
500       break;
501   }
502 }
503
504 // =======================================================================
505 // function : IncreaseShine
506 // purpose  :
507 // =======================================================================
508 void Graphic3d_MaterialAspect::IncreaseShine (const Standard_Real theDelta)
509 {
510   Standard_ShortReal anOldShine = myShininess;
511   myShininess = Standard_ShortReal(myShininess + myShininess * theDelta / 100.0);
512   if (myShininess > 1.0 || myShininess < 0.0)
513   {
514     myShininess = anOldShine;
515   }
516 }
517
518 // =======================================================================
519 // function : SetMaterialType
520 // purpose  :
521 // =======================================================================
522 void Graphic3d_MaterialAspect::SetMaterialType (const Graphic3d_TypeOfMaterial theType)
523 {
524   myMaterialType = theType;
525   if (theType != myMaterialType)
526   {
527     SetMaterialName ("UserDefined");
528   }
529 }
530
531 // =======================================================================
532 // function : SetAmbient
533 // purpose  :
534 // =======================================================================
535 void Graphic3d_MaterialAspect::SetAmbient (const Standard_Real theValue)
536 {
537   if (theValue < 0.0
538    || theValue > 1.0)
539   {
540     Graphic3d_MaterialDefinitionError::Raise ("Bad value for SetAmbient < 0. or > 1.0");
541   }
542
543   myAmbientCoef = Standard_ShortReal (theValue);
544   if (myAmbientActivity
545    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
546   {
547     SetMaterialName ("UserDefined");
548   }
549 }
550
551 // =======================================================================
552 // function : SetColor
553 // purpose  :
554 // =======================================================================
555 void Graphic3d_MaterialAspect::SetColor (const Quantity_Color& theColor)
556 {
557   const Standard_ShortReal anAmbientCoeff = 0.25f;
558   myAmbientColor.SetValues (theColor.Red()   * anAmbientCoeff,
559                             theColor.Green() * anAmbientCoeff,
560                             theColor.Blue()  * anAmbientCoeff, Quantity_TOC_RGB);
561   myDiffuseColor = theColor;
562   if (myAmbientActivity && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
563   {
564     SetMaterialName ("UserDefined");
565   }
566 }
567
568 // =======================================================================
569 // function : SetAmbientColor
570 // purpose  :
571 // =======================================================================
572 void Graphic3d_MaterialAspect::SetAmbientColor (const Quantity_Color& theColor)
573 {
574   myAmbientColor = theColor;
575   if (myAmbientActivity
576    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
577   {
578     SetMaterialName ("UserDefined");
579   }
580 }
581
582 // =======================================================================
583 // function : SetDiffuseColor
584 // purpose  :
585 // =======================================================================
586 void Graphic3d_MaterialAspect::SetDiffuseColor (const Quantity_Color& theColor)
587 {
588   myDiffuseColor = theColor;
589   if (myDiffuseActivity
590    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
591   {
592     SetMaterialName ("UserDefined");
593   }
594 }
595
596 // =======================================================================
597 // function : SetSpecularColor
598 // purpose  :
599 // =======================================================================
600 void Graphic3d_MaterialAspect::SetSpecularColor (const Quantity_Color& theColor)
601 {
602   mySpecularColor = theColor;
603   if (mySpecularActivity
604    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
605   {
606     SetMaterialName ("UserDefined");
607   }
608 }
609
610 // =======================================================================
611 // function : SetEmissiveColor
612 // purpose  :
613 // =======================================================================
614 void Graphic3d_MaterialAspect::SetEmissiveColor (const Quantity_Color& theColor)
615 {
616   myEmissiveColor = theColor;
617   if (myEmissiveActivity
618    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
619   {
620     SetMaterialName ("UserDefined");
621   }
622 }
623
624 // =======================================================================
625 // function : SetDiffuse
626 // purpose  :
627 // =======================================================================
628 void Graphic3d_MaterialAspect::SetDiffuse (const Standard_Real theValue)
629 {
630   if (theValue < 0.0
631    || theValue > 1.0)
632   {
633     Graphic3d_MaterialDefinitionError::Raise ("Bad value for SetDiffuse < 0. or > 1.0");
634   }
635
636   myDiffuseCoef  = Standard_ShortReal (theValue);
637   if (myDiffuseActivity
638    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
639   {
640     SetMaterialName ("UserDefined");
641   }
642 }
643
644 // =======================================================================
645 // function : SetEmissive
646 // purpose  :
647 // =======================================================================
648 void Graphic3d_MaterialAspect::SetEmissive (const Standard_Real theValue)
649 {
650   if (theValue < 0.0
651    || theValue > 1.0)
652   {
653     Graphic3d_MaterialDefinitionError::Raise ("Bad value for SetEmissive < 0. or > 1.0");
654   }
655
656   myEmissiveCoef = Standard_ShortReal (theValue);
657   if (myDiffuseActivity
658    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
659   {
660     SetMaterialName ("UserDefined");
661   }
662 }
663
664 // =======================================================================
665 // function : SetReflectionModeOn
666 // purpose  :
667 // =======================================================================
668 void Graphic3d_MaterialAspect::SetReflectionModeOn (const Graphic3d_TypeOfReflection theType)
669 {
670   switch (theType)
671   {
672     case Graphic3d_TOR_AMBIENT:
673       myAmbientActivity  = Standard_True;
674       break;
675     case Graphic3d_TOR_DIFFUSE:
676       myDiffuseActivity  = Standard_True;
677       break;
678     case Graphic3d_TOR_SPECULAR:
679       mySpecularActivity = Standard_True;
680       break;
681     case Graphic3d_TOR_EMISSION:
682       myEmissiveActivity = Standard_True;
683       break;
684   }
685   if (myMaterialType == Graphic3d_MATERIAL_PHYSIC)
686   {
687     SetMaterialName ("UserDefined");
688   }
689 }
690
691 // =======================================================================
692 // function : SetReflectionModeOff
693 // purpose  :
694 // =======================================================================
695 void Graphic3d_MaterialAspect::SetReflectionModeOff (const Graphic3d_TypeOfReflection theType)
696 {
697   switch (theType)
698   {
699     case Graphic3d_TOR_AMBIENT:
700       myAmbientActivity  = Standard_False;
701       break;
702     case Graphic3d_TOR_DIFFUSE:
703       myDiffuseActivity  = Standard_False;
704       break;
705     case Graphic3d_TOR_SPECULAR:
706       mySpecularActivity = Standard_False;
707       break;
708     case Graphic3d_TOR_EMISSION:
709       myEmissiveActivity = Standard_False;
710       break;
711   }
712   if (myMaterialType == Graphic3d_MATERIAL_PHYSIC)
713   {
714     SetMaterialName ("UserDefined");
715   }
716 }
717
718 // =======================================================================
719 // function : SetSpecular
720 // purpose  :
721 // =======================================================================
722 void Graphic3d_MaterialAspect::SetSpecular (const Standard_Real theValue)
723 {
724   if (theValue < 0.0
725    || theValue > 1.0)
726   {
727     Graphic3d_MaterialDefinitionError::Raise ("Bad value for SetSpecular < 0. or > 1.0");
728   }
729
730   mySpecularCoef  = Standard_ShortReal (theValue);
731   if (mySpecularActivity
732    && myMaterialType == Graphic3d_MATERIAL_PHYSIC)
733   {
734     SetMaterialName ("UserDefined");
735   }
736 }
737
738 // =======================================================================
739 // function : SetTransparency
740 // purpose  :
741 // =======================================================================
742 void Graphic3d_MaterialAspect::SetTransparency (const Standard_Real theValue)
743 {
744   if (theValue < 0.0
745    || theValue > 1.0)
746   {
747     Graphic3d_MaterialDefinitionError::Raise ("Bad value for SetTransparency < 0. or > 1.0");
748   }
749
750   myTransparencyCoef  = Standard_ShortReal (theValue);
751 }
752
753 // =======================================================================
754 // function : SetRefractionIndex
755 // purpose  :
756 // =======================================================================
757 void Graphic3d_MaterialAspect::SetRefractionIndex (const Standard_Real theValue)
758 {
759   if (theValue < 1.0)
760   {
761     Graphic3d_MaterialDefinitionError::Raise ("Bad value for refraction index < 1.0");
762   }
763
764   myRefractionIndex = static_cast<Standard_ShortReal> (theValue);
765 }
766
767 // =======================================================================
768 // function : SetBSDF
769 // purpose  :
770 // =======================================================================
771 void Graphic3d_MaterialAspect::SetBSDF (const Graphic3d_BSDF& theBSDF)
772 {
773   myBSDF = theBSDF;
774 }
775
776 // =======================================================================
777 // function : Color
778 // purpose  :
779 // =======================================================================
780 const Quantity_Color& Graphic3d_MaterialAspect::Color() const
781 {
782   // It is generally accepted to consider diffuse color as
783   // "general" color of a material when light shines on it
784   return myDiffuseColor;
785 }
786
787 // =======================================================================
788 // function : AmbientColor
789 // purpose  :
790 // =======================================================================
791 const Quantity_Color& Graphic3d_MaterialAspect::AmbientColor() const
792 {
793   return myAmbientColor;
794 }
795
796 // =======================================================================
797 // function : DiffuseColor
798 // purpose  :
799 // =======================================================================
800 const Quantity_Color& Graphic3d_MaterialAspect::DiffuseColor() const
801 {
802   return myDiffuseColor;
803 }
804
805 // =======================================================================
806 // function : SpecularColor
807 // purpose  :
808 // =======================================================================
809 const Quantity_Color& Graphic3d_MaterialAspect::SpecularColor() const
810 {
811   return mySpecularColor;
812 }
813
814 // =======================================================================
815 // function : EmissiveColor
816 // purpose  :
817 // =======================================================================
818 const Quantity_Color& Graphic3d_MaterialAspect::EmissiveColor() const
819 {
820   return myEmissiveColor;
821 }
822
823 // =======================================================================
824 // function : MaterialType
825 // purpose  :
826 // =======================================================================
827 Standard_Boolean Graphic3d_MaterialAspect::MaterialType (const Graphic3d_TypeOfMaterial theType) const
828 {
829   return myMaterialType == theType;
830 }
831
832 // =======================================================================
833 // function : ReflectionMode
834 // purpose  :
835 // =======================================================================
836 Standard_Boolean Graphic3d_MaterialAspect::ReflectionMode (const Graphic3d_TypeOfReflection theType) const
837 {
838   switch (theType)
839   {
840     case Graphic3d_TOR_AMBIENT:  return myAmbientActivity;
841     case Graphic3d_TOR_DIFFUSE:  return myDiffuseActivity;
842     case Graphic3d_TOR_SPECULAR: return mySpecularActivity;
843     case Graphic3d_TOR_EMISSION: return myEmissiveActivity;
844   }
845   return Standard_False;
846 }
847
848 // =======================================================================
849 // function : Ambient
850 // purpose  :
851 // =======================================================================
852 Standard_Real Graphic3d_MaterialAspect::Ambient() const
853 {
854   return Standard_Real (myAmbientCoef);
855 }
856
857 // =======================================================================
858 // function : Diffuse
859 // purpose  :
860 // =======================================================================
861 Standard_Real Graphic3d_MaterialAspect::Diffuse() const
862 {
863   return Standard_Real (myDiffuseCoef);
864 }
865
866 // =======================================================================
867 // function : Emissive
868 // purpose  :
869 // =======================================================================
870 Standard_Real Graphic3d_MaterialAspect::Emissive() const
871 {
872   return Standard_Real (myEmissiveCoef);
873 }
874
875 // =======================================================================
876 // function : Specular
877 // purpose  :
878 // =======================================================================
879 Standard_Real Graphic3d_MaterialAspect::Specular() const
880 {
881   return Standard_Real (mySpecularCoef);
882 }
883
884 // =======================================================================
885 // function : Transparency
886 // purpose  :
887 // =======================================================================
888 Standard_Real Graphic3d_MaterialAspect::Transparency() const
889 {
890   return Standard_Real (myTransparencyCoef);
891 }
892
893 // =======================================================================
894 // function : RefractionIndex
895 // purpose  :
896 // =======================================================================
897 Standard_Real Graphic3d_MaterialAspect::RefractionIndex() const
898 {
899   return myRefractionIndex;
900 }
901
902 // =======================================================================
903 // function : BSDF
904 // purpose  :
905 // =======================================================================
906 const Graphic3d_BSDF& Graphic3d_MaterialAspect::BSDF() const
907 {
908   return myBSDF;
909 }
910
911 // =======================================================================
912 // function : Shininess
913 // purpose  :
914 // =======================================================================
915 Standard_Real Graphic3d_MaterialAspect::Shininess() const
916 {
917   return Standard_Real (myShininess);
918 }
919
920 // =======================================================================
921 // function : SetShininess
922 // purpose  :
923 // =======================================================================
924 void Graphic3d_MaterialAspect::SetShininess (const Standard_Real theValue)
925 {
926   if (theValue < 0.0
927    || theValue > 1.0)
928   {
929     Graphic3d_MaterialDefinitionError::Raise ("Bad value for Shininess < 0. or > 1.0");
930   }
931
932   myShininess = Standard_ShortReal (theValue);
933   SetMaterialName ("UserDefined");
934 }
935
936 // =======================================================================
937 // function : SetEnvReflexion
938 // purpose  :
939 // =======================================================================
940 void Graphic3d_MaterialAspect::SetEnvReflexion (const Standard_ShortReal theValue)
941 {
942   myEnvReflexion = theValue;
943 }
944
945 // =======================================================================
946 // function : EnvReflexion
947 // purpose  :
948 // =======================================================================
949 Standard_ShortReal Graphic3d_MaterialAspect::EnvReflexion() const
950 {
951   return myEnvReflexion;
952 }
953
954 // =======================================================================
955 // function : Name
956 // purpose  :
957 // =======================================================================
958 Graphic3d_NameOfMaterial Graphic3d_MaterialAspect::Name() const
959 {
960   return myMaterialName;
961 }
962
963 // =======================================================================
964 // function : Reset
965 // purpose  :
966 // =======================================================================
967 void Graphic3d_MaterialAspect::Reset()
968 {
969   Init (myRequestedMaterialName);
970 }
971
972 // =======================================================================
973 // function : IsEqual
974 // purpose  :
975 // =======================================================================
976 Standard_Boolean Graphic3d_MaterialAspect::IsEqual (const Graphic3d_MaterialAspect& theOther) const
977 {
978   return myAmbientCoef      == theOther.myAmbientCoef
979       && myDiffuseCoef      == theOther.myDiffuseCoef
980       && mySpecularCoef     == theOther.mySpecularCoef
981       && myEmissiveCoef     == theOther.myEmissiveCoef
982       && myTransparencyCoef == theOther.myTransparencyCoef
983       && myRefractionIndex  == theOther.myRefractionIndex
984       && myBSDF             == theOther.myBSDF
985       && myShininess        == theOther.myShininess
986       && myEnvReflexion     == theOther.myEnvReflexion
987       && myAmbientColor     == theOther.myAmbientColor
988       && myDiffuseColor     == theOther.myDiffuseColor
989       && mySpecularColor    == theOther.mySpecularColor
990       && myEmissiveColor    == theOther.myEmissiveColor
991       && myAmbientActivity  == theOther.myAmbientActivity
992       && myDiffuseActivity  == theOther.myDiffuseActivity
993       && mySpecularActivity == theOther.mySpecularActivity
994       && myEmissiveActivity == theOther.myEmissiveActivity;
995 }
996
997 // =======================================================================
998 // function : IsDifferent
999 // purpose  :
1000 // =======================================================================
1001 Standard_Boolean Graphic3d_MaterialAspect::IsDifferent (const Graphic3d_MaterialAspect& theOther) const
1002 {
1003   return !IsEqual (theOther);
1004 }
1005
1006 typedef struct _Material
1007 {
1008   const char*              name;
1009   Graphic3d_TypeOfMaterial type;
1010 } Material;
1011
1012 namespace
1013 {
1014   static const Material theMaterials[] =
1015   {
1016     {"Brass",            Graphic3d_MATERIAL_PHYSIC},
1017     {"Bronze",           Graphic3d_MATERIAL_PHYSIC},
1018     {"Copper",           Graphic3d_MATERIAL_PHYSIC},
1019     {"Gold",             Graphic3d_MATERIAL_PHYSIC},
1020     {"Pewter",           Graphic3d_MATERIAL_PHYSIC},
1021     {"Plastered",        Graphic3d_MATERIAL_ASPECT},
1022     {"Plastified",       Graphic3d_MATERIAL_ASPECT},
1023     {"Silver",           Graphic3d_MATERIAL_PHYSIC},
1024     {"Steel",            Graphic3d_MATERIAL_PHYSIC},
1025     {"Stone",            Graphic3d_MATERIAL_PHYSIC},
1026     {"Shiny_plastified", Graphic3d_MATERIAL_ASPECT},
1027     {"Satined",          Graphic3d_MATERIAL_ASPECT},
1028     {"Metalized",        Graphic3d_MATERIAL_ASPECT},
1029     {"Ionized",          Graphic3d_MATERIAL_ASPECT},
1030     {"Chrome",           Graphic3d_MATERIAL_PHYSIC},
1031     {"Aluminium",        Graphic3d_MATERIAL_PHYSIC},
1032     {"Obsidian",         Graphic3d_MATERIAL_PHYSIC},
1033     {"Neon",             Graphic3d_MATERIAL_PHYSIC},
1034     {"Jade",             Graphic3d_MATERIAL_PHYSIC},
1035     {"Charcoal",         Graphic3d_MATERIAL_PHYSIC},
1036     {"Water",            Graphic3d_MATERIAL_PHYSIC},
1037     {"Glass",            Graphic3d_MATERIAL_PHYSIC},
1038     {"Diamond",          Graphic3d_MATERIAL_PHYSIC}
1039
1040   };
1041 }
1042
1043 // =======================================================================
1044 // function : NumberOfMaterials
1045 // purpose  :
1046 // =======================================================================
1047 Standard_Integer Graphic3d_MaterialAspect::NumberOfMaterials()
1048 {
1049   Standard_STATIC_ASSERT(sizeof(theMaterials)/sizeof(Material) == Graphic3d_NOM_DEFAULT);
1050   return Graphic3d_NOM_DEFAULT;
1051 }
1052
1053 // =======================================================================
1054 // function : MaterialName
1055 // purpose  :
1056 // =======================================================================
1057 Standard_CString Graphic3d_MaterialAspect::MaterialName (const Standard_Integer theRank)
1058 {
1059   if (theRank < 1 || theRank > NumberOfMaterials())
1060   {
1061     Standard_OutOfRange::Raise ("BAD index of material");
1062   }
1063   return theMaterials[theRank - 1].name;
1064 }
1065
1066 // =======================================================================
1067 // function : MaterialFromName
1068 // purpose  :
1069 // =======================================================================
1070 Graphic3d_NameOfMaterial Graphic3d_MaterialAspect::MaterialFromName (const Standard_CString theName)
1071 {
1072   TCollection_AsciiString aName (theName);
1073   aName.LowerCase();
1074   aName.Capitalize();
1075   const Standard_Integer aNbMaterials = Graphic3d_MaterialAspect::NumberOfMaterials();
1076   for (Standard_Integer aMatIter = 1; aMatIter <= aNbMaterials; ++aMatIter)
1077   {
1078     if (aName == Graphic3d_MaterialAspect::MaterialName (aMatIter))
1079     {
1080       return Graphic3d_NameOfMaterial(aMatIter - 1);
1081     }
1082   }
1083
1084   // parse aliases
1085   if (aName == "Plastic")            // Plastified
1086   {
1087     return Graphic3d_NOM_PLASTIC;
1088   }
1089   else if (aName == "Shiny_plastic") // Shiny_plastified
1090   {
1091     return Graphic3d_NOM_SHINY_PLASTIC;
1092   }
1093   else if (aName == "Plaster")       // Plastered
1094   {
1095     return Graphic3d_NOM_PLASTER;
1096   }
1097   else if (aName == "Satin")         // Satined
1098   {
1099     return Graphic3d_NOM_SATIN;
1100   }
1101   else if (aName == "Neon_gnc")      // Ionized
1102   {
1103     return Graphic3d_NOM_NEON_GNC;
1104   }
1105   else if (aName == "Neon_phc") // Neon
1106   {
1107     return Graphic3d_NOM_NEON_PHC;
1108   }
1109   return Graphic3d_NOM_DEFAULT;
1110 }
1111
1112 // =======================================================================
1113 // function : MaterialType
1114 // purpose  :
1115 // =======================================================================
1116 Graphic3d_TypeOfMaterial Graphic3d_MaterialAspect::MaterialType (const Standard_Integer theRank)
1117 {
1118   if (theRank < 1 || theRank > NumberOfMaterials())
1119   {
1120     Standard_OutOfRange::Raise ("BAD index of material");
1121   }
1122   return theMaterials[theRank - 1].type;
1123 }
1124
1125 // =======================================================================
1126 // function : MaterialName
1127 // purpose  :
1128 // =======================================================================
1129 Standard_CString Graphic3d_MaterialAspect::MaterialName() const
1130 {
1131   return myStringName.ToCString();
1132 }
1133
1134 // =======================================================================
1135 // function : SetMaterialName
1136 // purpose  :
1137 // =======================================================================
1138 void Graphic3d_MaterialAspect::SetMaterialName (const Standard_CString theNewName)
1139 {
1140   // if a component of a "standard" material change, the
1141   // result is no more standard (a blue gold is not a gold)
1142   myMaterialName = Graphic3d_NOM_UserDefined;
1143   myStringName   = theNewName;
1144 }