1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <Standard_Type.hxx>
16 #include <Vrml_Material.hxx>
18 IMPLEMENT_STANDARD_RTTIEXT(Vrml_Material,Standard_Transient)
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)
27 myAmbientColor = aAmbientColor;
28 myDiffuseColor = aDiffuseColor;
29 mySpecularColor = aSpecularColor;
30 myEmissiveColor = aEmissiveColor;
33 for ( i = aShininess->Lower(); i <= aShininess->Upper(); i++ )
35 if (aShininess->Value(i) < 0. || aShininess->Value(i) > 1.)
37 throw Standard_Failure("The value of aShininess is out of range (0 - 1)");
40 myShininess = aShininess;
42 for ( i = aTransparency->Lower(); i <= aTransparency->Upper(); i++ )
44 if (aTransparency->Value(i) < 0. || aTransparency->Value(i) > 1.)
46 throw Standard_Failure("The value of aTransparency is out of range (0 - 1)");
49 myTransparency = aTransparency;
52 Vrml_Material::Vrml_Material()
54 myAmbientColor = new Quantity_HArray1OfColor (1, 1, Quantity_Color (0.2, 0.2, 0.2, Quantity_TOC_sRGB));
55 myDiffuseColor = new Quantity_HArray1OfColor (1, 1, Quantity_Color (0.8, 0.8, 0.8, Quantity_TOC_sRGB));
56 mySpecularColor = new Quantity_HArray1OfColor (1, 1, Quantity_NOC_BLACK);
57 myEmissiveColor = new Quantity_HArray1OfColor (1, 1, Quantity_NOC_BLACK);
59 myShininess = new TColStd_HArray1OfReal (1,1,0.2);
60 myTransparency = new TColStd_HArray1OfReal (1,1,0);
63 void Vrml_Material::SetAmbientColor(const Handle(Quantity_HArray1OfColor)& aAmbientColor)
65 myAmbientColor = aAmbientColor;
68 Handle(Quantity_HArray1OfColor) Vrml_Material::AmbientColor() const
70 return myAmbientColor;
73 void Vrml_Material::SetDiffuseColor(const Handle(Quantity_HArray1OfColor)& aDiffuseColor)
75 myDiffuseColor = aDiffuseColor;
78 Handle(Quantity_HArray1OfColor) Vrml_Material::DiffuseColor() const
80 return myDiffuseColor;
83 void Vrml_Material::SetSpecularColor(const Handle(Quantity_HArray1OfColor)& aSpecularColor)
85 mySpecularColor = aSpecularColor;
88 Handle(Quantity_HArray1OfColor) Vrml_Material::SpecularColor() const
90 return mySpecularColor;
93 void Vrml_Material::SetEmissiveColor(const Handle(Quantity_HArray1OfColor)& aEmissiveColor)
95 myEmissiveColor = aEmissiveColor;
98 Handle(Quantity_HArray1OfColor) Vrml_Material::EmissiveColor() const
100 return myEmissiveColor;
103 void Vrml_Material::SetShininess(const Handle(TColStd_HArray1OfReal)& aShininess)
106 for ( i = aShininess->Lower(); i <= aShininess->Upper(); i++ )
108 if (aShininess->Value(i) < 0. || aShininess->Value(i) > 1.)
110 throw Standard_Failure("The value of aShininess is out of range (0 - 1)");
113 myShininess = aShininess;
116 Handle(TColStd_HArray1OfReal) Vrml_Material::Shininess() const
121 void Vrml_Material::SetTransparency(const Handle(TColStd_HArray1OfReal)& aTransparency)
124 for ( i = aTransparency->Lower(); i <= aTransparency->Upper(); i++ )
126 if (aTransparency->Value(i) < 0. || aTransparency->Value(i) > 1.)
128 throw Standard_Failure("The value of aTransparency is out of range (0 - 1)");
131 myTransparency = aTransparency;
134 Handle(TColStd_HArray1OfReal) Vrml_Material::Transparency() const
136 return myTransparency;
139 Standard_OStream& Vrml_Material::Print(Standard_OStream& anOStream) const
141 NCollection_Vec3<Standard_Real> aColor_sRGB;
143 anOStream << "Material {\n";
145 if ( myAmbientColor->Length() != 1 ||
146 Abs(myAmbientColor->Value(myAmbientColor->Lower()).Red() - 0.2) > 0.0001 ||
147 Abs(myAmbientColor->Value(myAmbientColor->Lower()).Green() - 0.2) > 0.0001 ||
148 Abs(myAmbientColor->Value(myAmbientColor->Lower()).Blue() - 0.2) > 0.0001 )
150 anOStream << " ambientColor [\n\t";
151 for ( i = myAmbientColor->Lower(); i <= myAmbientColor->Upper(); i++ )
153 myAmbientColor->Value(i).Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
154 anOStream << aColor_sRGB.r() << ' ' << aColor_sRGB.g() << ' ' << aColor_sRGB.b();
155 if ( i < myAmbientColor->Length() )
156 anOStream << ",\n\t"; // ,,,,,,,,,,
161 if ( myDiffuseColor->Length() != 1 ||
162 Abs(myDiffuseColor->Value(myDiffuseColor->Lower()).Red() - 0.8) > 0.0001 ||
163 Abs(myDiffuseColor->Value(myDiffuseColor->Lower()).Green() - 0.8) > 0.0001 ||
164 Abs(myDiffuseColor->Value(myDiffuseColor->Lower()).Blue() - 0.8) > 0.0001 )
166 anOStream << " diffuseColor [\n\t";
167 for ( i = myDiffuseColor->Lower(); i <= myDiffuseColor->Upper(); i++ )
169 myDiffuseColor->Value(i).Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
170 anOStream << aColor_sRGB.r() << ' ' << aColor_sRGB.g() << ' ' << aColor_sRGB.b();
171 if ( i < myDiffuseColor->Length() )
172 anOStream << ",\n\t";
177 if ( mySpecularColor->Length() != 1 ||
178 Abs(mySpecularColor->Value(mySpecularColor->Lower()).Red() - 0) > 0.0001 ||
179 Abs(mySpecularColor->Value(mySpecularColor->Lower()).Green() - 0) > 0.0001 ||
180 Abs(mySpecularColor->Value(mySpecularColor->Lower()).Blue() - 0) > 0.0001 )
182 anOStream << " specularColor [\n\t";
183 for ( i = mySpecularColor->Lower(); i <= mySpecularColor->Upper(); i++ )
185 mySpecularColor->Value(i).Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
186 anOStream << aColor_sRGB.r() << ' ' << aColor_sRGB.g() << ' ' << aColor_sRGB.b();
187 if ( i < mySpecularColor->Length() )
188 anOStream << ",\n\t";
193 if ( myEmissiveColor->Length() != 1 ||
194 Abs(myEmissiveColor->Value(myEmissiveColor->Lower()).Red() - 0) > 0.0001 ||
195 Abs(myEmissiveColor->Value(myEmissiveColor->Lower()).Green() - 0) > 0.0001 ||
196 Abs(myEmissiveColor->Value(myEmissiveColor->Lower()).Blue() - 0) > 0.0001 )
198 anOStream << " emissiveColor [\n\t";
199 for ( i = myEmissiveColor->Lower(); i <= myEmissiveColor->Upper(); i++ )
201 myEmissiveColor->Value(i).Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
202 anOStream << aColor_sRGB.r() << ' ' << aColor_sRGB.g() << ' ' << aColor_sRGB.b();
203 if ( i < myEmissiveColor->Length() )
204 anOStream << ",\n\t";
209 if ( myShininess->Length() != 1 || Abs(myShininess->Value(myShininess->Lower()) - 0.2) > 0.0001 )
211 anOStream << " shininess\t\t[ ";
212 for ( i = myShininess->Lower(); i <= myShininess->Upper(); i++ )
214 anOStream << myShininess->Value(i);
215 if ( i < myShininess->Length() )
221 if ( myTransparency->Length() != 1 || Abs(myTransparency->Value(myTransparency->Lower()) - 0) > 0.0001 )
223 anOStream << " transparency\t[ ";
224 for ( i = myTransparency->Lower(); i <= myTransparency->Upper(); i++ )
226 anOStream << myTransparency->Value(i);
227 if ( i < myTransparency->Length() )