Panda3D
|
00001 // Filename: rescaleNormalAttrib.cxx 00002 // Created by: drose (30Dec04) 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 "rescaleNormalAttrib.h" 00016 #include "graphicsStateGuardianBase.h" 00017 #include "string_utils.h" 00018 #include "dcast.h" 00019 #include "bamReader.h" 00020 #include "bamWriter.h" 00021 #include "datagram.h" 00022 #include "datagramIterator.h" 00023 #include "configVariableEnum.h" 00024 00025 TypeHandle RescaleNormalAttrib::_type_handle; 00026 int RescaleNormalAttrib::_attrib_slot; 00027 00028 // This variable is defined here instead of in config_pgraph.cxx, 00029 // because it depends on rescaleNormalAttrib.h having already been 00030 // included. 00031 static ConfigVariableEnum<RescaleNormalAttrib::Mode> rescale_normals 00032 ("rescale-normals", RescaleNormalAttrib::M_auto, 00033 PRC_DESC("Specifies the kind of RescaleNormalAttrib that should be " 00034 "created for the top of the scene graph. This can automatically " 00035 "ensure that your lighting normals are unit-length, which may be " 00036 "particularly necessary in the presence of scales in the scene " 00037 "graph. Turning it off ('none') may produce a small performance " 00038 "benefit.")); 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Function: RescaleNormalAttrib::make 00042 // Access: Published, Static 00043 // Description: Constructs a new RescaleNormalAttrib object that 00044 // specifies whether to rescale normals to compensate 00045 // for transform scales or incorrectly defined normals. 00046 //////////////////////////////////////////////////////////////////// 00047 CPT(RenderAttrib) RescaleNormalAttrib:: 00048 make(RescaleNormalAttrib::Mode mode) { 00049 RescaleNormalAttrib *attrib = new RescaleNormalAttrib(mode); 00050 return return_new(attrib); 00051 } 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function: RescaleNormalAttrib::make_default 00055 // Access: Published, Static 00056 // Description: Constructs a RescaleNoramlAttrib object that's 00057 // suitable for putting at the top of a scene graph. 00058 // This will contain whatever attrib was suggested by 00059 // the user's rescale-normals Config variable. 00060 //////////////////////////////////////////////////////////////////// 00061 CPT(RenderAttrib) RescaleNormalAttrib:: 00062 make_default() { 00063 RescaleNormalAttrib *attrib = new RescaleNormalAttrib(rescale_normals); 00064 return return_new(attrib); 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: RescaleNormalAttrib::output 00069 // Access: Public, Virtual 00070 // Description: 00071 //////////////////////////////////////////////////////////////////// 00072 void RescaleNormalAttrib:: 00073 output(ostream &out) const { 00074 out << get_type() << ":" << get_mode(); 00075 } 00076 00077 //////////////////////////////////////////////////////////////////// 00078 // Function: RescaleNormalAttrib::compare_to_impl 00079 // Access: Protected, Virtual 00080 // Description: Intended to be overridden by derived RescaleNormalAttrib 00081 // types to return a unique number indicating whether 00082 // this RescaleNormalAttrib is equivalent to the other one. 00083 // 00084 // This should return 0 if the two RescaleNormalAttrib objects 00085 // are equivalent, a number less than zero if this one 00086 // should be sorted before the other one, and a number 00087 // greater than zero otherwise. 00088 // 00089 // This will only be called with two RescaleNormalAttrib 00090 // objects whose get_type() functions return the same. 00091 //////////////////////////////////////////////////////////////////// 00092 int RescaleNormalAttrib:: 00093 compare_to_impl(const RenderAttrib *other) const { 00094 const RescaleNormalAttrib *ta; 00095 DCAST_INTO_R(ta, other, 0); 00096 return (int)_mode - (int)ta->_mode; 00097 } 00098 00099 //////////////////////////////////////////////////////////////////// 00100 // Function: RescaleNormalAttrib::get_hash_impl 00101 // Access: Protected, Virtual 00102 // Description: Intended to be overridden by derived RenderAttrib 00103 // types to return a unique hash for these particular 00104 // properties. RenderAttribs that compare the same with 00105 // compare_to_impl(), above, should return the same 00106 // hash; RenderAttribs that compare differently should 00107 // return a different hash. 00108 //////////////////////////////////////////////////////////////////// 00109 size_t RescaleNormalAttrib:: 00110 get_hash_impl() const { 00111 size_t hash = 0; 00112 hash = int_hash::add_hash(hash, (int)_mode); 00113 return hash; 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: RescaleNormalAttrib::register_with_read_factory 00118 // Access: Public, Static 00119 // Description: Tells the BamReader how to create objects of type 00120 // RescaleNormalAttrib. 00121 //////////////////////////////////////////////////////////////////// 00122 void RescaleNormalAttrib:: 00123 register_with_read_factory() { 00124 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00125 } 00126 00127 //////////////////////////////////////////////////////////////////// 00128 // Function: RescaleNormalAttrib::write_datagram 00129 // Access: Public, Virtual 00130 // Description: Writes the contents of this object to the datagram 00131 // for shipping out to a Bam file. 00132 //////////////////////////////////////////////////////////////////// 00133 void RescaleNormalAttrib:: 00134 write_datagram(BamWriter *manager, Datagram &dg) { 00135 RenderAttrib::write_datagram(manager, dg); 00136 00137 dg.add_int8(_mode); 00138 } 00139 00140 //////////////////////////////////////////////////////////////////// 00141 // Function: RescaleNormalAttrib::make_from_bam 00142 // Access: Protected, Static 00143 // Description: This function is called by the BamReader's factory 00144 // when a new object of type RescaleNormalAttrib is encountered 00145 // in the Bam file. It should create the RescaleNormalAttrib 00146 // and extract its information from the file. 00147 //////////////////////////////////////////////////////////////////// 00148 TypedWritable *RescaleNormalAttrib:: 00149 make_from_bam(const FactoryParams ¶ms) { 00150 RescaleNormalAttrib *attrib = new RescaleNormalAttrib(M_none); 00151 DatagramIterator scan; 00152 BamReader *manager; 00153 00154 parse_params(params, scan, manager); 00155 attrib->fillin(scan, manager); 00156 00157 return attrib; 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: RescaleNormalAttrib::fillin 00162 // Access: Protected 00163 // Description: This internal function is called by make_from_bam to 00164 // read in all of the relevant data from the BamFile for 00165 // the new RescaleNormalAttrib. 00166 //////////////////////////////////////////////////////////////////// 00167 void RescaleNormalAttrib:: 00168 fillin(DatagramIterator &scan, BamReader *manager) { 00169 RenderAttrib::fillin(scan, manager); 00170 00171 _mode = (Mode)scan.get_int8(); 00172 } 00173 00174 //////////////////////////////////////////////////////////////////// 00175 // Function: RescaleNormalAttrib::Mode output operator 00176 // Description: 00177 //////////////////////////////////////////////////////////////////// 00178 ostream & 00179 operator << (ostream &out, RescaleNormalAttrib::Mode mode) { 00180 switch (mode) { 00181 case RescaleNormalAttrib::M_none: 00182 return out << "none"; 00183 00184 case RescaleNormalAttrib::M_rescale: 00185 return out << "rescale"; 00186 00187 case RescaleNormalAttrib::M_normalize: 00188 return out << "normalize"; 00189 00190 case RescaleNormalAttrib::M_auto: 00191 return out << "auto"; 00192 } 00193 00194 return out << "(**invalid RescaleNormalAttrib::Mode(" << (int)mode << ")**)"; 00195 } 00196 00197 //////////////////////////////////////////////////////////////////// 00198 // Function: RescaleNormalAttrib::Mode input operator 00199 // Description: 00200 //////////////////////////////////////////////////////////////////// 00201 istream & 00202 operator >> (istream &in, RescaleNormalAttrib::Mode &mode) { 00203 string word; 00204 in >> word; 00205 00206 if (cmp_nocase(word, "none") == 0) { 00207 mode = RescaleNormalAttrib::M_none; 00208 00209 } else if (cmp_nocase(word, "rescale") == 0) { 00210 mode = RescaleNormalAttrib::M_rescale; 00211 00212 } else if (cmp_nocase(word, "normalize") == 0) { 00213 mode = RescaleNormalAttrib::M_normalize; 00214 00215 } else if (cmp_nocase(word, "auto") == 0) { 00216 mode = RescaleNormalAttrib::M_auto; 00217 00218 } else { 00219 pgraph_cat.error() 00220 << "Invalid RescaleNormalAttrib::Mode value: " << word << "\n"; 00221 mode = RescaleNormalAttrib::M_none; 00222 } 00223 00224 return in; 00225 }