0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / Vrml / Vrml_Material.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
42cf5bc1 14
15#include <Standard_Type.hxx>
16#include <Vrml_Material.hxx>
7fd59977 17
25e59720 18IMPLEMENT_STANDARD_RTTIEXT(Vrml_Material,Standard_Transient)
92efcf78 19
b311480e 20Vrml_Material::Vrml_Material(const Handle(Quantity_HArray1OfColor)& aAmbientColor,
7fd59977 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 {
9775fa61 37 throw Standard_Failure("The value of aShininess is out of range (0 - 1)");
7fd59977 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 {
9775fa61 46 throw Standard_Failure("The value of aTransparency is out of range (0 - 1)");
7fd59977 47 }
48 }
49 myTransparency = aTransparency;
50}
51
52 Vrml_Material::Vrml_Material()
53{
ba00aab7 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);
7fd59977 58
59 myShininess = new TColStd_HArray1OfReal (1,1,0.2);
60 myTransparency = new TColStd_HArray1OfReal (1,1,0);
61}
62
63void Vrml_Material::SetAmbientColor(const Handle(Quantity_HArray1OfColor)& aAmbientColor)
64{
65 myAmbientColor = aAmbientColor;
66}
67
68Handle(Quantity_HArray1OfColor) Vrml_Material::AmbientColor() const
69{
70 return myAmbientColor;
71}
72
73void Vrml_Material::SetDiffuseColor(const Handle(Quantity_HArray1OfColor)& aDiffuseColor)
74{
75 myDiffuseColor = aDiffuseColor;
76}
77
78Handle(Quantity_HArray1OfColor) Vrml_Material::DiffuseColor() const
79{
80 return myDiffuseColor;
81}
82
83void Vrml_Material::SetSpecularColor(const Handle(Quantity_HArray1OfColor)& aSpecularColor)
84{
85 mySpecularColor = aSpecularColor;
86}
87
88Handle(Quantity_HArray1OfColor) Vrml_Material::SpecularColor() const
89{
90 return mySpecularColor;
91}
92
93void Vrml_Material::SetEmissiveColor(const Handle(Quantity_HArray1OfColor)& aEmissiveColor)
94{
95 myEmissiveColor = aEmissiveColor;
96}
97
98Handle(Quantity_HArray1OfColor) Vrml_Material::EmissiveColor() const
99{
100 return myEmissiveColor;
101}
102
103void Vrml_Material::SetShininess(const Handle(TColStd_HArray1OfReal)& aShininess)
104{
105 Standard_Integer i;
106 for ( i = aShininess->Lower(); i <= aShininess->Upper(); i++ )
107 {
108 if (aShininess->Value(i) < 0. || aShininess->Value(i) > 1.)
109 {
9775fa61 110 throw Standard_Failure("The value of aShininess is out of range (0 - 1)");
7fd59977 111 }
112 }
113 myShininess = aShininess;
114}
115
116Handle(TColStd_HArray1OfReal) Vrml_Material::Shininess() const
117{
118 return myShininess;
119}
120
121void Vrml_Material::SetTransparency(const Handle(TColStd_HArray1OfReal)& aTransparency)
122{
123 Standard_Integer i;
124 for ( i = aTransparency->Lower(); i <= aTransparency->Upper(); i++ )
125 {
126 if (aTransparency->Value(i) < 0. || aTransparency->Value(i) > 1.)
127 {
9775fa61 128 throw Standard_Failure("The value of aTransparency is out of range (0 - 1)");
7fd59977 129 }
130 }
131 myTransparency = aTransparency;
132}
133
134Handle(TColStd_HArray1OfReal) Vrml_Material::Transparency() const
135{
136 return myTransparency;
137}
138
139Standard_OStream& Vrml_Material::Print(Standard_OStream& anOStream) const
140{
ba00aab7 141 NCollection_Vec3<Standard_Real> aColor_sRGB;
7fd59977 142 Standard_Integer i;
586db386 143 anOStream << "Material {\n";
7fd59977 144
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 )
149 {
150 anOStream << " ambientColor [\n\t";
151 for ( i = myAmbientColor->Lower(); i <= myAmbientColor->Upper(); i++ )
152 {
ba00aab7 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();
7fd59977 155 if ( i < myAmbientColor->Length() )
586db386 156 anOStream << ",\n\t"; // ,,,,,,,,,,
7fd59977 157 }
586db386 158 anOStream << " ]\n";
7fd59977 159 }
160
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 )
165 {
166 anOStream << " diffuseColor [\n\t";
167 for ( i = myDiffuseColor->Lower(); i <= myDiffuseColor->Upper(); i++ )
168 {
ba00aab7 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();
7fd59977 171 if ( i < myDiffuseColor->Length() )
586db386 172 anOStream << ",\n\t";
7fd59977 173 }
586db386 174 anOStream << " ]\n";
7fd59977 175 }
176
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 )
181 {
182 anOStream << " specularColor [\n\t";
183 for ( i = mySpecularColor->Lower(); i <= mySpecularColor->Upper(); i++ )
184 {
ba00aab7 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();
7fd59977 187 if ( i < mySpecularColor->Length() )
586db386 188 anOStream << ",\n\t";
7fd59977 189 }
586db386 190 anOStream << " ]\n";
7fd59977 191 }
192
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 )
197 {
198 anOStream << " emissiveColor [\n\t";
199 for ( i = myEmissiveColor->Lower(); i <= myEmissiveColor->Upper(); i++ )
200 {
ba00aab7 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();
7fd59977 203 if ( i < myEmissiveColor->Length() )
586db386 204 anOStream << ",\n\t";
7fd59977 205 }
586db386 206 anOStream << " ]\n";
7fd59977 207 }
208
209 if ( myShininess->Length() != 1 || Abs(myShininess->Value(myShininess->Lower()) - 0.2) > 0.0001 )
210 {
211 anOStream << " shininess\t\t[ ";
212 for ( i = myShininess->Lower(); i <= myShininess->Upper(); i++ )
213 {
214 anOStream << myShininess->Value(i);
215 if ( i < myShininess->Length() )
216 anOStream << ", ";
217 }
586db386 218 anOStream << " ]\n";
7fd59977 219 }
220
221 if ( myTransparency->Length() != 1 || Abs(myTransparency->Value(myTransparency->Lower()) - 0) > 0.0001 )
222 {
223 anOStream << " transparency\t[ ";
224 for ( i = myTransparency->Lower(); i <= myTransparency->Upper(); i++ )
225 {
226 anOStream << myTransparency->Value(i);
227 if ( i < myTransparency->Length() )
228 anOStream << ", ";
229 }
586db386 230 anOStream << " ]\n";
7fd59977 231 }
586db386 232 anOStream << "}\n";
7fd59977 233
234 return anOStream;
235}
236