Panda3D
|
00001 // Filename: audioVolumeAttrib.cxx 00002 // Created by: darren (15Dec06) 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 "audioVolumeAttrib.h" 00016 #include "graphicsStateGuardianBase.h" 00017 #include "dcast.h" 00018 #include "bamReader.h" 00019 #include "bamWriter.h" 00020 #include "datagram.h" 00021 #include "datagramIterator.h" 00022 #include "config_pgraph.h" 00023 00024 CPT(RenderAttrib) AudioVolumeAttrib::_identity_attrib; 00025 TypeHandle AudioVolumeAttrib::_type_handle; 00026 int AudioVolumeAttrib::_attrib_slot; 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: AudioVolumeAttrib::Constructor 00030 // Access: Protected 00031 // Description: Use AudioVolumeAttrib::make() to construct a new 00032 // AudioVolumeAttrib object. 00033 //////////////////////////////////////////////////////////////////// 00034 AudioVolumeAttrib:: 00035 AudioVolumeAttrib(bool off, float volume) : 00036 _off(off), 00037 _volume(volume) 00038 { 00039 nassertv(_volume >= 0.f); 00040 _has_volume = !IS_NEARLY_EQUAL(_volume, 1.0f); 00041 } 00042 00043 //////////////////////////////////////////////////////////////////// 00044 // Function: AudioVolumeAttrib::make_identity 00045 // Access: Published, Static 00046 // Description: Constructs an identity audio volume attrib. 00047 //////////////////////////////////////////////////////////////////// 00048 CPT(RenderAttrib) AudioVolumeAttrib:: 00049 make_identity() { 00050 // We make identity a special case and store a pointer forever once 00051 // we find it the first time. 00052 if (_identity_attrib == (AudioVolumeAttrib *)NULL) { 00053 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, 1.0f);; 00054 _identity_attrib = return_new(attrib); 00055 } 00056 00057 return _identity_attrib; 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: AudioVolumeAttrib::make 00062 // Access: Published, Static 00063 // Description: Constructs a new AudioVolumeAttrib object that indicates 00064 // audio volume should be scaled by the indicated factor. 00065 //////////////////////////////////////////////////////////////////// 00066 CPT(RenderAttrib) AudioVolumeAttrib:: 00067 make(float volume) { 00068 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, volume); 00069 return return_new(attrib); 00070 } 00071 00072 //////////////////////////////////////////////////////////////////// 00073 // Function: AudioVolumeAttrib::make_off 00074 // Access: Published, Static 00075 // Description: Constructs a new AudioVolumeAttrib object that ignores 00076 // any AudioVolumeAttrib inherited from above. You may 00077 // also specify an additional volume scale to apply to 00078 // geometry below (using set_volume()). 00079 //////////////////////////////////////////////////////////////////// 00080 CPT(RenderAttrib) AudioVolumeAttrib:: 00081 make_off() { 00082 AudioVolumeAttrib *attrib = 00083 new AudioVolumeAttrib(true, 1.0f); 00084 return return_new(attrib); 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: AudioVolumeAttrib::make_default 00089 // Access: Published, Static 00090 // Description: Returns a RenderAttrib that corresponds to whatever 00091 // the standard default properties for render attributes 00092 // of this type ought to be. 00093 //////////////////////////////////////////////////////////////////// 00094 CPT(RenderAttrib) AudioVolumeAttrib:: 00095 make_default() { 00096 return return_new(new AudioVolumeAttrib(false, 1.0f)); 00097 } 00098 00099 //////////////////////////////////////////////////////////////////// 00100 // Function: AudioVolumeAttrib::set_volume 00101 // Access: Published 00102 // Description: Returns a new AudioVolumeAttrib, just like this one, but 00103 // with the volume changed to the indicated value. 00104 //////////////////////////////////////////////////////////////////// 00105 CPT(RenderAttrib) AudioVolumeAttrib:: 00106 set_volume(float volume) const { 00107 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(*this); 00108 assert(volume >= 0.f); 00109 attrib->_volume = volume; 00110 attrib->_has_volume = !IS_NEARLY_EQUAL(volume, 1.0f); 00111 return return_new(attrib); 00112 } 00113 00114 //////////////////////////////////////////////////////////////////// 00115 // Function: AudioVolumeAttrib::output 00116 // Access: Public, Virtual 00117 // Description: 00118 //////////////////////////////////////////////////////////////////// 00119 void AudioVolumeAttrib:: 00120 output(ostream &out) const { 00121 out << get_type() << ":"; 00122 if (is_off()) { 00123 out << "off"; 00124 } 00125 if (has_volume()) { 00126 out << "(" << get_volume() << ")"; 00127 00128 } else if (!is_off()) { 00129 out << "identity"; 00130 } 00131 } 00132 00133 //////////////////////////////////////////////////////////////////// 00134 // Function: AudioVolumeAttrib::compare_to_impl 00135 // Access: Protected, Virtual 00136 // Description: Intended to be overridden by derived AudioVolumeAttrib 00137 // types to return a unique number indicating whether 00138 // this AudioVolumeAttrib is equivalent to the other one. 00139 // 00140 // This should return 0 if the two AudioVolumeAttrib objects 00141 // are equivalent, a number less than zero if this one 00142 // should be sorted before the other one, and a number 00143 // greater than zero otherwise. 00144 // 00145 // This will only be called with two AudioVolumeAttrib 00146 // objects whose get_type() functions return the same. 00147 //////////////////////////////////////////////////////////////////// 00148 int AudioVolumeAttrib:: 00149 compare_to_impl(const RenderAttrib *other) const { 00150 const AudioVolumeAttrib *ta; 00151 DCAST_INTO_R(ta, other, 0); 00152 00153 if (is_off() != ta->is_off()) { 00154 if (pgraph_cat.is_spam()) { 00155 pgraph_cat.spam() 00156 << "Comparing " << (int)is_off() << " to " << (int)ta->is_off() << " result = " 00157 << (int)is_off() - (int)ta->is_off() << "\n"; 00158 } 00159 00160 return (int)is_off() - (int)ta->is_off(); 00161 } 00162 00163 int result = int(_volume * 1000.0f) - int(ta->_volume * 1000.0f); 00164 if (pgraph_cat.is_spam()) { 00165 pgraph_cat.spam() 00166 << "Comparing " << _volume << " to " << ta->_volume << " result = " 00167 << result << "\n"; 00168 } 00169 00170 return result; 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: AudioVolumeAttrib::compose_impl 00175 // Access: Protected, Virtual 00176 // Description: Intended to be overridden by derived RenderAttrib 00177 // types to specify how two consecutive RenderAttrib 00178 // objects of the same type interact. 00179 // 00180 // This should return the result of applying the other 00181 // RenderAttrib to a node in the scene graph below this 00182 // RenderAttrib, which was already applied. In most 00183 // cases, the result is the same as the other 00184 // RenderAttrib (that is, a subsequent RenderAttrib 00185 // completely replaces the preceding one). On the other 00186 // hand, some kinds of RenderAttrib (for instance, 00187 // ColorTransformAttrib) might combine in meaningful 00188 // ways. 00189 //////////////////////////////////////////////////////////////////// 00190 CPT(RenderAttrib) AudioVolumeAttrib:: 00191 compose_impl(const RenderAttrib *other) const { 00192 const AudioVolumeAttrib *ta; 00193 DCAST_INTO_R(ta, other, 0); 00194 00195 if (ta->is_off()) { 00196 return ta; 00197 } 00198 00199 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(is_off(), ta->_volume * _volume); 00200 return return_new(attrib); 00201 } 00202 00203 //////////////////////////////////////////////////////////////////// 00204 // Function: AudioVolumeAttrib::invert_compose_impl 00205 // Access: Protected, Virtual 00206 // Description: Intended to be overridden by derived RenderAttrib 00207 // types to specify how two consecutive RenderAttrib 00208 // objects of the same type interact. 00209 // 00210 // See invert_compose() and compose_impl(). 00211 //////////////////////////////////////////////////////////////////// 00212 CPT(RenderAttrib) AudioVolumeAttrib:: 00213 invert_compose_impl(const RenderAttrib *other) const { 00214 if (is_off()) { 00215 return other; 00216 } 00217 const AudioVolumeAttrib *ta; 00218 DCAST_INTO_R(ta, other, 0); 00219 float new_volume = _volume == 0.0f ? 1.0f : ta->_volume / _volume; 00220 00221 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, new_volume); 00222 return return_new(attrib); 00223 } 00224 00225 //////////////////////////////////////////////////////////////////// 00226 // Function: AudioVolumeAttrib::register_with_read_factory 00227 // Access: Public, Static 00228 // Description: Tells the BamReader how to create objects of type 00229 // AudioVolumeAttrib. 00230 //////////////////////////////////////////////////////////////////// 00231 void AudioVolumeAttrib:: 00232 register_with_read_factory() { 00233 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00234 } 00235 00236 //////////////////////////////////////////////////////////////////// 00237 // Function: AudioVolumeAttrib::write_datagram 00238 // Access: Public, Virtual 00239 // Description: Writes the contents of this object to the datagram 00240 // for shipping out to a Bam file. 00241 //////////////////////////////////////////////////////////////////// 00242 void AudioVolumeAttrib:: 00243 write_datagram(BamWriter *manager, Datagram &dg) { 00244 RenderAttrib::write_datagram(manager, dg); 00245 00246 // We cheat, and modify the bam stream without upping the bam 00247 // version. We can do this since we know that no existing bam files 00248 // have an AudioVolumeAttrib in them. 00249 dg.add_bool(_off); 00250 dg.add_float32(_volume); 00251 } 00252 00253 //////////////////////////////////////////////////////////////////// 00254 // Function: AudioVolumeAttrib::make_from_bam 00255 // Access: Protected, Static 00256 // Description: This function is called by the BamReader's factory 00257 // when a new object of type AudioVolumeAttrib is encountered 00258 // in the Bam file. It should create the AudioVolumeAttrib 00259 // and extract its information from the file. 00260 //////////////////////////////////////////////////////////////////// 00261 TypedWritable *AudioVolumeAttrib:: 00262 make_from_bam(const FactoryParams ¶ms) { 00263 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, 1.0f); 00264 DatagramIterator scan; 00265 BamReader *manager; 00266 00267 parse_params(params, scan, manager); 00268 attrib->fillin(scan, manager); 00269 00270 return attrib; 00271 } 00272 00273 //////////////////////////////////////////////////////////////////// 00274 // Function: AudioVolumeAttrib::fillin 00275 // Access: Protected 00276 // Description: This internal function is called by make_from_bam to 00277 // read in all of the relevant data from the BamFile for 00278 // the new AudioVolumeAttrib. 00279 //////////////////////////////////////////////////////////////////// 00280 void AudioVolumeAttrib:: 00281 fillin(DatagramIterator &scan, BamReader *manager) { 00282 RenderAttrib::fillin(scan, manager); 00283 00284 _off = scan.get_bool(); 00285 _volume = scan.get_float32(); 00286 nassertv(_volume >= 0.f); 00287 _has_volume = !IS_NEARLY_EQUAL(_volume, 1.0f); 00288 }