Panda3D
|
00001 // Filename: eggMaterial.cxx 00002 // Created by: drose (29Jan99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "eggMaterial.h" 00016 00017 #include "indent.h" 00018 00019 TypeHandle EggMaterial::_type_handle; 00020 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: EggMaterial::Constructor 00024 // Access: Public 00025 // Description: 00026 //////////////////////////////////////////////////////////////////// 00027 EggMaterial:: 00028 EggMaterial(const string &mref_name) 00029 : EggNode(mref_name) 00030 { 00031 _flags = 0; 00032 } 00033 00034 //////////////////////////////////////////////////////////////////// 00035 // Function: EggMaterial::Copy Constructor 00036 // Access: Public 00037 // Description: 00038 //////////////////////////////////////////////////////////////////// 00039 EggMaterial:: 00040 EggMaterial(const EggMaterial ©) 00041 : EggNode(copy), 00042 _diff(copy._diff), 00043 _amb(copy._amb), 00044 _emit(copy._emit), 00045 _spec(copy._spec), 00046 _shininess(copy._shininess), 00047 _local(copy._local), 00048 _flags(copy._flags) 00049 { 00050 } 00051 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function: EggMaterial::write 00055 // Access: Public, Virtual 00056 // Description: Writes the material definition to the indicated output 00057 // stream in Egg format. 00058 //////////////////////////////////////////////////////////////////// 00059 void EggMaterial:: 00060 write(ostream &out, int indent_level) const { 00061 write_header(out, indent_level, "<Material>"); 00062 00063 if (has_diff()) { 00064 indent(out, indent_level + 2) 00065 << "<Scalar> diffr { " << get_diff()[0] << " }\n"; 00066 indent(out, indent_level + 2) 00067 << "<Scalar> diffg { " << get_diff()[1] << " }\n"; 00068 indent(out, indent_level + 2) 00069 << "<Scalar> diffb { " << get_diff()[2] << " }\n"; 00070 if (get_diff()[3] != 1.0) { 00071 indent(out, indent_level + 2) 00072 << "<Scalar> diffa { " << get_diff()[3] << " }\n"; 00073 } 00074 } 00075 00076 if (has_amb()) { 00077 indent(out, indent_level + 2) 00078 << "<Scalar> ambr { " << get_amb()[0] << " }\n"; 00079 indent(out, indent_level + 2) 00080 << "<Scalar> ambg { " << get_amb()[1] << " }\n"; 00081 indent(out, indent_level + 2) 00082 << "<Scalar> ambb { " << get_amb()[2] << " }\n"; 00083 if (get_amb()[3] != 1.0) { 00084 indent(out, indent_level + 2) 00085 << "<Scalar> amba { " << get_amb()[3] << " }\n"; 00086 } 00087 } 00088 00089 if (has_emit()) { 00090 indent(out, indent_level + 2) 00091 << "<Scalar> emitr { " << get_emit()[0] << " }\n"; 00092 indent(out, indent_level + 2) 00093 << "<Scalar> emitg { " << get_emit()[1] << " }\n"; 00094 indent(out, indent_level + 2) 00095 << "<Scalar> emitb { " << get_emit()[2] << " }\n"; 00096 if (get_emit()[3] != 1.0) { 00097 indent(out, indent_level + 2) 00098 << "<Scalar> emita { " << get_emit()[3] << " }\n"; 00099 } 00100 } 00101 00102 if (has_spec()) { 00103 indent(out, indent_level + 2) 00104 << "<Scalar> specr { " << get_spec()[0] << " }\n"; 00105 indent(out, indent_level + 2) 00106 << "<Scalar> specg { " << get_spec()[1] << " }\n"; 00107 indent(out, indent_level + 2) 00108 << "<Scalar> specb { " << get_spec()[2] << " }\n"; 00109 if (get_spec()[3] != 1.0) { 00110 indent(out, indent_level + 2) 00111 << "<Scalar> speca { " << get_spec()[3] << " }\n"; 00112 } 00113 } 00114 00115 if (has_shininess()) { 00116 indent(out, indent_level + 2) 00117 << "<Scalar> shininess { " << get_shininess() << " }\n"; 00118 } 00119 00120 if (has_local()) { 00121 indent(out, indent_level + 2) 00122 << "<Scalar> local { " << get_local() << " }\n"; 00123 } 00124 00125 indent(out, indent_level) << "}\n"; 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: EggMaterial::is_equivalent_to 00130 // Access: Public 00131 // Description: Returns true if the two materials are equivalent in 00132 // all relevant properties (according to eq), false 00133 // otherwise. 00134 // 00135 // The Equivalence parameter, eq, should be set to the 00136 // bitwise OR of the following properties, according to 00137 // what you consider relevant: 00138 // 00139 // EggMaterial::E_attributes: 00140 // All material attributes (diff, spec, 00141 // etc.) except MRef name. 00142 // 00143 // EggMaterial::E_mref_name: 00144 // The MRef name. 00145 //////////////////////////////////////////////////////////////////// 00146 bool EggMaterial:: 00147 is_equivalent_to(const EggMaterial &other, int eq) const { 00148 if (eq & E_attributes) { 00149 if (_flags != other._flags || 00150 (has_diff() && get_diff() != other.get_diff()) || 00151 (has_amb() && get_amb() != other.get_amb()) || 00152 (has_emit() && get_emit() != other.get_emit()) || 00153 (has_spec() && get_spec() != other.get_spec()) || 00154 (has_shininess() && get_shininess() != other.get_shininess()) || 00155 (has_local() && get_local() != other.get_local())) { 00156 return false; 00157 } 00158 } 00159 00160 if (eq & E_mref_name) { 00161 if (get_name() != other.get_name()) { 00162 return false; 00163 } 00164 } 00165 00166 return true; 00167 } 00168 00169 //////////////////////////////////////////////////////////////////// 00170 // Function: EggMaterial::sorts_less_than 00171 // Access: Public 00172 // Description: An ordering operator to compare two materials for 00173 // sorting order. This imposes an arbitrary ordering 00174 // useful to identify unique materials, according to the 00175 // indicated Equivalence factor. See 00176 // is_equivalent_to(). 00177 //////////////////////////////////////////////////////////////////// 00178 bool EggMaterial:: 00179 sorts_less_than(const EggMaterial &other, int eq) const { 00180 if (eq & E_attributes) { 00181 if (_flags != other._flags) { 00182 return _flags < (int)other._flags; 00183 } 00184 if (has_diff() && get_diff() != other.get_diff()) { 00185 return get_diff().compare_to(other.get_diff()) < 0; 00186 } 00187 if (has_amb() && get_amb() != other.get_amb()) { 00188 return get_amb().compare_to(other.get_amb()) < 0; 00189 } 00190 if (has_emit() && get_emit() != other.get_emit()) { 00191 return get_emit().compare_to(other.get_emit()) < 0; 00192 } 00193 if (has_spec() && get_spec() != other.get_spec()) { 00194 return get_spec().compare_to(other.get_spec()) < 0; 00195 } 00196 if (has_shininess() && get_shininess() != other.get_shininess()) { 00197 return get_shininess() < other.get_shininess(); 00198 } 00199 if (has_local() && get_local() != other.get_local()) { 00200 return get_local() < other.get_local(); 00201 } 00202 } 00203 00204 if (eq & E_mref_name) { 00205 if (get_name() != other.get_name()) { 00206 return get_name() < other.get_name(); 00207 } 00208 } 00209 00210 return false; 00211 }