0028832: MMgt_TShared can be replaced by Standard_Transient
[occt.git] / src / Vrml / Vrml_Material.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Standard_Type.hxx>
16 #include <Vrml_Material.hxx>
17
18 IMPLEMENT_STANDARD_RTTIEXT(Vrml_Material,Standard_Transient)
19
20 Vrml_Material::Vrml_Material(const Handle(Quantity_HArray1OfColor)& aAmbientColor, 
21                               const Handle(Quantity_HArray1OfColor)& aDiffuseColor, 
22                               const Handle(Quantity_HArray1OfColor)& aSpecularColor, 
23                               const Handle(Quantity_HArray1OfColor)& aEmissiveColor, 
24                               const Handle(TColStd_HArray1OfReal)& aShininess, 
25                               const Handle(TColStd_HArray1OfReal)& aTransparency)
26 {
27  myAmbientColor = aAmbientColor;
28  myDiffuseColor = aDiffuseColor;
29  mySpecularColor = aSpecularColor;
30  myEmissiveColor = aEmissiveColor;
31
32  Standard_Integer i;
33  for ( i = aShininess->Lower(); i <= aShininess->Upper(); i++ )
34    {
35      if (aShininess->Value(i) < 0. || aShininess->Value(i) > 1.)
36        {
37          throw Standard_Failure("The value of aShininess is out of range (0 - 1)");
38        }
39    }
40  myShininess     = aShininess;
41
42  for ( i = aTransparency->Lower(); i <= aTransparency->Upper(); i++ )
43    {
44      if (aTransparency->Value(i) < 0. || aTransparency->Value(i) > 1.)
45        {
46          throw Standard_Failure("The value of aTransparency is out of range (0 - 1)");
47        }
48    }
49  myTransparency  = aTransparency;
50 }
51
52  Vrml_Material::Vrml_Material()
53 {
54  Quantity_Color tmpcolor;
55 // myAmbientColor  = new Quantity_HArray1OfColor(1,1);
56     tmpcolor.SetValues(0.2, 0.2, 0.2, Quantity_TOC_RGB);
57  myAmbientColor  = new Quantity_HArray1OfColor(1,1,tmpcolor);
58 //    myAmbientColor->SetValue(1, tmpcolor);
59 // myDiffuseColor  = new Quantity_HArray1OfColor(1,1);
60     tmpcolor.SetValues(0.8, 0.8, 0.8, Quantity_TOC_RGB);
61  myDiffuseColor  = new Quantity_HArray1OfColor(1,1,tmpcolor);
62 //    myDiffuseColor->SetValue(1, tmpcolor);
63  mySpecularColor = new Quantity_HArray1OfColor(1,1);
64     tmpcolor.SetValues(0, 0, 0, Quantity_TOC_RGB);
65     mySpecularColor->SetValue(1, tmpcolor);
66  myEmissiveColor = new Quantity_HArray1OfColor(1,1);
67     tmpcolor.SetValues(0, 0, 0, Quantity_TOC_RGB);
68     myEmissiveColor->SetValue(1, tmpcolor);
69
70  myShininess     = new TColStd_HArray1OfReal (1,1,0.2);
71  myTransparency  = new TColStd_HArray1OfReal (1,1,0);
72 }
73
74 void Vrml_Material::SetAmbientColor(const Handle(Quantity_HArray1OfColor)& aAmbientColor)
75 {
76  myAmbientColor = aAmbientColor;
77 }
78
79 Handle(Quantity_HArray1OfColor) Vrml_Material::AmbientColor() const 
80 {
81  return myAmbientColor;
82 }
83
84 void Vrml_Material::SetDiffuseColor(const Handle(Quantity_HArray1OfColor)& aDiffuseColor)
85 {
86  myDiffuseColor = aDiffuseColor;
87 }
88
89 Handle(Quantity_HArray1OfColor) Vrml_Material::DiffuseColor() const 
90 {
91  return myDiffuseColor;
92 }
93
94 void Vrml_Material::SetSpecularColor(const Handle(Quantity_HArray1OfColor)& aSpecularColor)
95 {
96  mySpecularColor = aSpecularColor;
97 }
98
99 Handle(Quantity_HArray1OfColor) Vrml_Material::SpecularColor() const 
100 {
101  return mySpecularColor;
102 }
103
104 void Vrml_Material::SetEmissiveColor(const Handle(Quantity_HArray1OfColor)& aEmissiveColor)
105 {
106  myEmissiveColor = aEmissiveColor;
107 }
108
109 Handle(Quantity_HArray1OfColor) Vrml_Material::EmissiveColor() const 
110 {
111  return myEmissiveColor;
112 }
113
114 void Vrml_Material::SetShininess(const Handle(TColStd_HArray1OfReal)& aShininess)
115 {
116  Standard_Integer i;
117  for ( i = aShininess->Lower(); i <= aShininess->Upper(); i++ )
118    {
119      if (aShininess->Value(i) < 0. || aShininess->Value(i) > 1.)
120        {
121          throw Standard_Failure("The value of aShininess is out of range (0 - 1)");
122        }
123    }
124  myShininess = aShininess;
125 }
126
127 Handle(TColStd_HArray1OfReal) Vrml_Material::Shininess() const 
128 {
129  return myShininess;
130 }
131
132 void Vrml_Material::SetTransparency(const Handle(TColStd_HArray1OfReal)& aTransparency)
133 {
134  Standard_Integer i;
135  for ( i = aTransparency->Lower(); i <= aTransparency->Upper(); i++ )
136    {
137      if (aTransparency->Value(i) < 0. || aTransparency->Value(i) > 1.)
138        {
139          throw Standard_Failure("The value of aTransparency is out of range (0 - 1)");
140        }
141    }
142  myTransparency = aTransparency;
143 }
144
145 Handle(TColStd_HArray1OfReal) Vrml_Material::Transparency() const 
146 {
147  return myTransparency;
148 }
149
150 Standard_OStream& Vrml_Material::Print(Standard_OStream& anOStream) const 
151 {
152  Standard_Integer i;
153  anOStream  << "Material {\n";
154
155  if ( myAmbientColor->Length() != 1 ||
156      Abs(myAmbientColor->Value(myAmbientColor->Lower()).Red() - 0.2)    > 0.0001 ||
157      Abs(myAmbientColor->Value(myAmbientColor->Lower()).Green() - 0.2)  > 0.0001 || 
158      Abs(myAmbientColor->Value(myAmbientColor->Lower()).Blue() - 0.2)   > 0.0001 )
159   {
160   anOStream  << "    ambientColor [\n\t";
161   for ( i = myAmbientColor->Lower(); i <= myAmbientColor->Upper(); i++ )
162     {
163      anOStream << myAmbientColor->Value(i).Red() << ' ' << myAmbientColor->Value(i).Green() << ' ' << myAmbientColor->Value(i).Blue();
164      if ( i < myAmbientColor->Length() )
165         anOStream  << ",\n\t"; // ,,,,,,,,,,
166     }
167    anOStream  << " ]\n";
168   }
169
170  if ( myDiffuseColor->Length() != 1 || 
171      Abs(myDiffuseColor->Value(myDiffuseColor->Lower()).Red() - 0.8)   > 0.0001  || 
172      Abs(myDiffuseColor->Value(myDiffuseColor->Lower()).Green() - 0.8) > 0.0001  || 
173      Abs(myDiffuseColor->Value(myDiffuseColor->Lower()).Blue() - 0.8)  > 0.0001 )
174   {
175   anOStream  << "    diffuseColor [\n\t";
176   for ( i = myDiffuseColor->Lower(); i <= myDiffuseColor->Upper(); i++ )
177     {
178      anOStream << myDiffuseColor->Value(i).Red() << ' ' << myDiffuseColor->Value(i).Green() << ' ' << myDiffuseColor->Value(i).Blue();
179      if ( i < myDiffuseColor->Length() )
180         anOStream  << ",\n\t";
181     }
182    anOStream  << " ]\n";
183   }
184
185  if ( mySpecularColor->Length() != 1 || 
186      Abs(mySpecularColor->Value(mySpecularColor->Lower()).Red() - 0)   > 0.0001 || 
187      Abs(mySpecularColor->Value(mySpecularColor->Lower()).Green() - 0) > 0.0001 || 
188      Abs(mySpecularColor->Value(mySpecularColor->Lower()).Blue() - 0)  > 0.0001 )
189   {
190    anOStream  << "    specularColor [\n\t";
191    for ( i = mySpecularColor->Lower(); i <= mySpecularColor->Upper(); i++ )
192      {
193      anOStream << mySpecularColor->Value(i).Red() << ' ' << mySpecularColor->Value(i).Green() << ' ' << mySpecularColor->Value(i).Blue();
194      if ( i < mySpecularColor->Length() )
195         anOStream  << ",\n\t";
196      }
197    anOStream  << " ]\n";
198   }
199
200  if ( myEmissiveColor->Length() != 1 || 
201      Abs(myEmissiveColor->Value(myEmissiveColor->Lower()).Red() - 0)   > 0.0001 || 
202      Abs(myEmissiveColor->Value(myEmissiveColor->Lower()).Green() - 0) > 0.0001 || 
203      Abs(myEmissiveColor->Value(myEmissiveColor->Lower()).Blue() - 0)  > 0.0001 )
204   {
205   anOStream  << "    emissiveColor [\n\t";
206   for ( i = myEmissiveColor->Lower(); i <= myEmissiveColor->Upper(); i++ )
207     {
208      anOStream << myEmissiveColor->Value(i).Red() << ' ' << myEmissiveColor->Value(i).Green() << ' ' << myEmissiveColor->Value(i).Blue();
209      if ( i < myEmissiveColor->Length() )
210         anOStream  << ",\n\t";
211     }
212    anOStream  << " ]\n";
213   }
214
215  if ( myShininess->Length() != 1 ||  Abs(myShininess->Value(myShininess->Lower()) - 0.2) > 0.0001 )
216    {
217     anOStream  << "    shininess\t\t[ ";
218     for ( i = myShininess->Lower(); i <= myShininess->Upper(); i++ )
219       {
220        anOStream << myShininess->Value(i);
221        if ( i < myShininess->Length() )
222           anOStream  << ", ";
223       }
224     anOStream  << " ]\n";
225   }
226
227  if ( myTransparency->Length() != 1 ||  Abs(myTransparency->Value(myTransparency->Lower()) - 0) > 0.0001 )
228   {
229    anOStream  << "    transparency\t[ ";
230    for ( i = myTransparency->Lower(); i <= myTransparency->Upper(); i++ )
231      {
232       anOStream << myTransparency->Value(i);
233       if ( i < myTransparency->Length() )
234          anOStream  << ", ";
235      }
236     anOStream  << " ]\n";
237   } 
238   anOStream  << "}\n";
239   
240  return anOStream;
241 }
242