00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "pandabase.h"
00016 #include "material.h"
00017 #include "indent.h"
00018 #include "datagram.h"
00019 #include "datagramIterator.h"
00020 #include "bamReader.h"
00021 #include "bamWriter.h"
00022
00023 TypeHandle Material::_type_handle;
00024 PT(Material) Material::_default;
00025
00026
00027
00028
00029
00030
00031 void Material::
00032 operator = (const Material ©) {
00033 Namable::operator = (copy);
00034 _ambient = copy._ambient;
00035 _diffuse = copy._diffuse;
00036 _specular = copy._specular;
00037 _emission = copy._emission;
00038 _shininess = copy._shininess;
00039 _flags = copy._flags & (~F_attrib_lock);
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 void Material::
00055 set_ambient(const LColor &color) {
00056 if (enforce_attrib_lock) {
00057 if ((_flags & F_ambient)==0) {
00058 nassertv(!is_attrib_locked());
00059 }
00060 }
00061 _ambient = color;
00062 _flags |= F_ambient;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 void Material::
00080 set_diffuse(const LColor &color) {
00081 if (enforce_attrib_lock) {
00082 if ((_flags & F_diffuse)==0) {
00083 nassertv(!is_attrib_locked());
00084 }
00085 }
00086 _diffuse = color;
00087 _flags |= F_diffuse;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 void Material::
00104 set_specular(const LColor &color) {
00105 if (enforce_attrib_lock) {
00106 if ((_flags & F_specular)==0) {
00107 nassertv(!is_attrib_locked());
00108 }
00109 }
00110 _specular = color;
00111 _flags |= F_specular;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 void Material::
00129 set_emission(const LColor &color) {
00130 if (enforce_attrib_lock) {
00131 if ((_flags & F_emission)==0) {
00132 nassertv(!is_attrib_locked());
00133 }
00134 }
00135 _emission = color;
00136 _flags |= F_emission;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 int Material::
00149 compare_to(const Material &other) const {
00150 if (_flags != other._flags) {
00151 return _flags - other._flags;
00152 }
00153 if (has_ambient() && get_ambient() != other.get_ambient()) {
00154 return get_ambient().compare_to(other.get_ambient());
00155 }
00156 if (has_diffuse() && get_diffuse() != other.get_diffuse()) {
00157 return get_diffuse().compare_to(other.get_diffuse());
00158 }
00159 if (has_specular() && get_specular() != other.get_specular()) {
00160 return get_specular().compare_to(other.get_specular());
00161 }
00162 if (has_emission() && get_emission() != other.get_emission()) {
00163 return get_emission().compare_to(other.get_emission());
00164 }
00165 if (get_shininess() != other.get_shininess()) {
00166 return get_shininess() < other.get_shininess() ? -1 : 1;
00167 }
00168
00169 return strcmp(get_name().c_str(), other.get_name().c_str());
00170 }
00171
00172
00173
00174
00175
00176
00177 void Material::
00178 output(ostream &out) const {
00179 out << "Material " << get_name();
00180 if (has_ambient()) {
00181 out << " a(" << get_ambient() << ")";
00182 }
00183 if (has_diffuse()) {
00184 out << " d(" << get_diffuse() << ")";
00185 }
00186 if (has_specular()) {
00187 out << " s(" << get_specular() << ")";
00188 }
00189 if (has_emission()) {
00190 out << " e(" << get_emission() << ")";
00191 }
00192 out << " s" << get_shininess()
00193 << " l" << get_local()
00194 << " t" << get_twoside();
00195 }
00196
00197
00198
00199
00200
00201
00202 void Material::
00203 write(ostream &out, int indent_level) const {
00204 indent(out, indent_level) << "Material " << get_name() << "\n";
00205 if (has_ambient()) {
00206 indent(out, indent_level + 2) << "ambient = " << get_ambient() << "\n";
00207 }
00208 if (has_diffuse()) {
00209 indent(out, indent_level + 2) << "diffuse = " << get_diffuse() << "\n";
00210 }
00211 if (has_specular()) {
00212 indent(out, indent_level + 2) << "specular = " << get_specular() << "\n";
00213 }
00214 if (has_emission()) {
00215 indent(out, indent_level + 2) << "emission = " << get_emission() << "\n";
00216 }
00217 indent(out, indent_level + 2) << "shininess = " << get_shininess() << "\n";
00218 indent(out, indent_level + 2) << "local = " << get_local() << "\n";
00219 indent(out, indent_level + 2) << "twoside = " << get_twoside() << "\n";
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229 void Material::
00230 register_with_read_factory() {
00231 BamReader::get_factory()->register_factory(get_class_type(), make_Material);
00232 }
00233
00234
00235
00236
00237
00238
00239
00240 void Material::
00241 write_datagram(BamWriter *manager, Datagram &me) {
00242 me.add_string(get_name());
00243 _ambient.write_datagram(me);
00244 _diffuse.write_datagram(me);
00245 _specular.write_datagram(me);
00246 _emission.write_datagram(me);
00247 me.add_stdfloat(_shininess);
00248 me.add_int32(_flags);
00249 }
00250
00251
00252
00253
00254
00255
00256 TypedWritable *Material::
00257 make_Material(const FactoryParams ¶ms) {
00258 Material *me = new Material;
00259 DatagramIterator scan;
00260 BamReader *manager;
00261
00262 parse_params(params, scan, manager);
00263 me->fillin(scan, manager);
00264 return me;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 void Material::
00276 fillin(DatagramIterator &scan, BamReader *manager) {
00277 set_name(scan.get_string());
00278 _ambient.read_datagram(scan);
00279 _diffuse.read_datagram(scan);
00280 _specular.read_datagram(scan);
00281 _emission.read_datagram(scan);
00282 _shininess = scan.get_stdfloat();
00283 _flags = scan.get_int32();
00284 }