00001 // Filename: colorScaleAttrib.cxx 00002 // Created by: drose (14Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "colorScaleAttrib.h" 00020 #include "graphicsStateGuardianBase.h" 00021 #include "dcast.h" 00022 #include "bamReader.h" 00023 #include "bamWriter.h" 00024 #include "datagram.h" 00025 #include "datagramIterator.h" 00026 00027 TypeHandle ColorScaleAttrib::_type_handle; 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: ColorScaleAttrib::make 00031 // Access: Published, Static 00032 // Description: Constructs a new ColorScaleAttrib object that indicates 00033 // geometry should be scaled by the indicated factor. 00034 //////////////////////////////////////////////////////////////////// 00035 CPT(RenderAttrib) ColorScaleAttrib:: 00036 make(const LVecBase4f &scale) { 00037 ColorScaleAttrib *attrib = new ColorScaleAttrib(scale); 00038 return return_new(attrib); 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: ColorScaleAttrib::issue 00043 // Access: Public, Virtual 00044 // Description: Calls the appropriate method on the indicated GSG 00045 // to issue the graphics commands appropriate to the 00046 // given attribute. This is normally called 00047 // (indirectly) only from 00048 // GraphicsStateGuardian::set_state() or modify_state(). 00049 //////////////////////////////////////////////////////////////////// 00050 void ColorScaleAttrib:: 00051 issue(GraphicsStateGuardianBase *gsg) const { 00052 gsg->issue_color_scale(this); 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function: ColorScaleAttrib::output 00057 // Access: Public, Virtual 00058 // Description: 00059 //////////////////////////////////////////////////////////////////// 00060 void ColorScaleAttrib:: 00061 output(ostream &out) const { 00062 out << get_type() << ":(" << get_scale() << ")"; 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: ColorScaleAttrib::compare_to_impl 00067 // Access: Protected, Virtual 00068 // Description: Intended to be overridden by derived ColorScaleAttrib 00069 // types to return a unique number indicating whether 00070 // this ColorScaleAttrib is equivalent to the other one. 00071 // 00072 // This should return 0 if the two ColorScaleAttrib objects 00073 // are equivalent, a number less than zero if this one 00074 // should be sorted before the other one, and a number 00075 // greater than zero otherwise. 00076 // 00077 // This will only be called with two ColorScaleAttrib 00078 // objects whose get_type() functions return the same. 00079 //////////////////////////////////////////////////////////////////// 00080 int ColorScaleAttrib:: 00081 compare_to_impl(const RenderAttrib *other) const { 00082 const ColorScaleAttrib *ta; 00083 DCAST_INTO_R(ta, other, 0); 00084 return _scale.compare_to(ta->_scale); 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: ColorScaleAttrib::compose_impl 00089 // Access: Protected, Virtual 00090 // Description: Intended to be overridden by derived RenderAttrib 00091 // types to specify how two consecutive RenderAttrib 00092 // objects of the same type interact. 00093 // 00094 // This should return the result of applying the other 00095 // RenderAttrib to a node in the scene graph below this 00096 // RenderAttrib, which was already applied. In most 00097 // cases, the result is the same as the other 00098 // RenderAttrib (that is, a subsequent RenderAttrib 00099 // completely replaces the preceding one). On the other 00100 // hand, some kinds of RenderAttrib (for instance, 00101 // ColorTransformAttrib) might combine in meaningful 00102 // ways. 00103 //////////////////////////////////////////////////////////////////// 00104 CPT(RenderAttrib) ColorScaleAttrib:: 00105 compose_impl(const RenderAttrib *other) const { 00106 const ColorScaleAttrib *ta; 00107 DCAST_INTO_R(ta, other, 0); 00108 LVecBase4f new_scale(ta->_scale[0] * _scale[0], 00109 ta->_scale[1] * _scale[1], 00110 ta->_scale[2] * _scale[2], 00111 ta->_scale[3] * _scale[3]); 00112 00113 ColorScaleAttrib *attrib = new ColorScaleAttrib(new_scale); 00114 return return_new(attrib); 00115 } 00116 00117 //////////////////////////////////////////////////////////////////// 00118 // Function: ColorScaleAttrib::invert_compose_impl 00119 // Access: Protected, Virtual 00120 // Description: Intended to be overridden by derived RenderAttrib 00121 // types to specify how two consecutive RenderAttrib 00122 // objects of the same type interact. 00123 // 00124 // See invert_compose() and compose_impl(). 00125 //////////////////////////////////////////////////////////////////// 00126 CPT(RenderAttrib) ColorScaleAttrib:: 00127 invert_compose_impl(const RenderAttrib *other) const { 00128 const ColorScaleAttrib *ta; 00129 DCAST_INTO_R(ta, other, 0); 00130 LVecBase4f new_scale(ta->_scale[0] / _scale[0], 00131 ta->_scale[1] / _scale[1], 00132 ta->_scale[2] / _scale[2], 00133 ta->_scale[3] / _scale[3]); 00134 00135 ColorScaleAttrib *attrib = new ColorScaleAttrib(new_scale); 00136 return return_new(attrib); 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: ColorScaleAttrib::make_default_impl 00141 // Access: Protected, Virtual 00142 // Description: Intended to be overridden by derived ColorScaleAttrib 00143 // types to specify what the default property for a 00144 // ColorScaleAttrib of this type should be. 00145 // 00146 // This should return a newly-allocated ColorScaleAttrib of 00147 // the same type that corresponds to whatever the 00148 // standard default for this kind of ColorScaleAttrib is. 00149 //////////////////////////////////////////////////////////////////// 00150 RenderAttrib *ColorScaleAttrib:: 00151 make_default_impl() const { 00152 return new ColorScaleAttrib(LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f)); 00153 } 00154 00155 //////////////////////////////////////////////////////////////////// 00156 // Function: ColorScaleAttrib::register_with_read_factory 00157 // Access: Public, Static 00158 // Description: Tells the BamReader how to create objects of type 00159 // ColorScaleAttrib. 00160 //////////////////////////////////////////////////////////////////// 00161 void ColorScaleAttrib:: 00162 register_with_read_factory() { 00163 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00164 } 00165 00166 //////////////////////////////////////////////////////////////////// 00167 // Function: ColorScaleAttrib::write_datagram 00168 // Access: Public, Virtual 00169 // Description: Writes the contents of this object to the datagram 00170 // for shipping out to a Bam file. 00171 //////////////////////////////////////////////////////////////////// 00172 void ColorScaleAttrib:: 00173 write_datagram(BamWriter *manager, Datagram &dg) { 00174 RenderAttrib::write_datagram(manager, dg); 00175 00176 _scale.write_datagram(dg); 00177 } 00178 00179 //////////////////////////////////////////////////////////////////// 00180 // Function: ColorScaleAttrib::make_from_bam 00181 // Access: Protected, Static 00182 // Description: This function is called by the BamReader's factory 00183 // when a new object of type ColorScaleAttrib is encountered 00184 // in the Bam file. It should create the ColorScaleAttrib 00185 // and extract its information from the file. 00186 //////////////////////////////////////////////////////////////////// 00187 TypedWritable *ColorScaleAttrib:: 00188 make_from_bam(const FactoryParams ¶ms) { 00189 ColorScaleAttrib *attrib = new ColorScaleAttrib(LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f)); 00190 DatagramIterator scan; 00191 BamReader *manager; 00192 00193 parse_params(params, scan, manager); 00194 attrib->fillin(scan, manager); 00195 00196 return attrib; 00197 } 00198 00199 //////////////////////////////////////////////////////////////////// 00200 // Function: ColorScaleAttrib::fillin 00201 // Access: Protected 00202 // Description: This internal function is called by make_from_bam to 00203 // read in all of the relevant data from the BamFile for 00204 // the new ColorScaleAttrib. 00205 //////////////////////////////////////////////////////////////////// 00206 void ColorScaleAttrib:: 00207 fillin(DatagramIterator &scan, BamReader *manager) { 00208 RenderAttrib::fillin(scan, manager); 00209 00210 _scale.read_datagram(scan); 00211 }