Panda3D
|
00001 // Filename: colorAttrib.cxx 00002 // Created by: drose (22Feb02) 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 "colorAttrib.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 00023 TypeHandle ColorAttrib::_type_handle; 00024 int ColorAttrib::_attrib_slot; 00025 CPT(RenderAttrib) ColorAttrib::_off; 00026 CPT(RenderAttrib) ColorAttrib::_vertex; 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: ColorAttrib::make_vertex 00030 // Access: Published, Static 00031 // Description: Constructs a new ColorAttrib object that indicates 00032 // geometry should be rendered according to its own 00033 // vertex color. 00034 //////////////////////////////////////////////////////////////////// 00035 CPT(RenderAttrib) ColorAttrib:: 00036 make_vertex() { 00037 if (_vertex != 0) { 00038 return _vertex; 00039 } 00040 ColorAttrib *attrib = new ColorAttrib(T_vertex, LColor::zero()); 00041 _vertex = return_new(attrib); 00042 return _vertex; 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: ColorAttrib::make_flat 00047 // Access: Published, Static 00048 // Description: Constructs a new ColorAttrib object that indicates 00049 // geometry should be rendered in the indicated color. 00050 //////////////////////////////////////////////////////////////////// 00051 CPT(RenderAttrib) ColorAttrib:: 00052 make_flat(const LColor &color) { 00053 ColorAttrib *attrib = new ColorAttrib(T_flat, color); 00054 return return_new(attrib); 00055 } 00056 00057 //////////////////////////////////////////////////////////////////// 00058 // Function: ColorAttrib::make_off 00059 // Access: Published, Static 00060 // Description: Constructs a new ColorAttrib object that indicates 00061 // geometry should be rendered in white. 00062 //////////////////////////////////////////////////////////////////// 00063 CPT(RenderAttrib) ColorAttrib:: 00064 make_off() { 00065 if (_off != 0) { 00066 return _off; 00067 } 00068 ColorAttrib *attrib = new ColorAttrib(T_off, LColor(1.0f, 1.0f, 1.0f, 1.0f)); 00069 _off = return_new(attrib); 00070 return _off; 00071 } 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Function: ColorAttrib::make_default 00075 // Access: Published, Static 00076 // Description: Returns a RenderAttrib that corresponds to whatever 00077 // the standard default properties for render attributes 00078 // of this type ought to be. 00079 //////////////////////////////////////////////////////////////////// 00080 CPT(RenderAttrib) ColorAttrib:: 00081 make_default() { 00082 return make_off(); 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: ColorAttrib::output 00087 // Access: Public, Virtual 00088 // Description: 00089 //////////////////////////////////////////////////////////////////// 00090 void ColorAttrib:: 00091 output(ostream &out) const { 00092 out << get_type() << ":"; 00093 switch (get_color_type()) { 00094 case T_vertex: 00095 out << "vertex"; 00096 break; 00097 00098 case T_flat: 00099 out << "(" << get_color() << ")"; 00100 break; 00101 00102 case T_off: 00103 out << "off"; 00104 break; 00105 } 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: ColorAttrib::compare_to_impl 00110 // Access: Protected, Virtual 00111 // Description: Intended to be overridden by derived ColorAttrib 00112 // types to return a unique number indicating whether 00113 // this ColorAttrib is equivalent to the other one. 00114 // 00115 // This should return 0 if the two ColorAttrib objects 00116 // are equivalent, a number less than zero if this one 00117 // should be sorted before the other one, and a number 00118 // greater than zero otherwise. 00119 // 00120 // This will only be called with two ColorAttrib 00121 // objects whose get_type() functions return the same. 00122 //////////////////////////////////////////////////////////////////// 00123 int ColorAttrib:: 00124 compare_to_impl(const RenderAttrib *other) const { 00125 const ColorAttrib *ta; 00126 DCAST_INTO_R(ta, other, 0); 00127 if (_type != ta->_type) { 00128 return (int)_type - (int)ta->_type; 00129 } 00130 if (_type == T_flat) { 00131 return _color.compare_to(ta->_color); 00132 } 00133 return 0; 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function: ColorAttrib::get_hash_impl 00138 // Access: Protected, Virtual 00139 // Description: Intended to be overridden by derived RenderAttrib 00140 // types to return a unique hash for these particular 00141 // properties. RenderAttribs that compare the same with 00142 // compare_to_impl(), above, should return the same 00143 // hash; RenderAttribs that compare differently should 00144 // return a different hash. 00145 //////////////////////////////////////////////////////////////////// 00146 size_t ColorAttrib:: 00147 get_hash_impl() const { 00148 size_t hash = 0; 00149 hash = int_hash::add_hash(hash, (int)_type); 00150 if (_type == T_flat) { 00151 hash = _color.add_hash(hash); 00152 } 00153 return hash; 00154 } 00155 00156 //////////////////////////////////////////////////////////////////// 00157 // Function: ColorAttrib::get_auto_shader_attrib_impl 00158 // Access: Protected, Virtual 00159 // Description: 00160 //////////////////////////////////////////////////////////////////// 00161 CPT(RenderAttrib) ColorAttrib:: 00162 get_auto_shader_attrib_impl(const RenderState *state) const { 00163 // For a ColorAttrib, the only relevant information is the type: is 00164 // it flat-shaded or vertex-shaded? The actual color value is read 00165 // by the shader from the graphics state. 00166 00167 ColorAttrib *attrib = new ColorAttrib(_type, LColor(1.0f, 1.0f, 1.0f, 1.0f)); 00168 return return_new(attrib); 00169 } 00170 00171 //////////////////////////////////////////////////////////////////// 00172 // Function: ColorAttrib::quantize_color 00173 // Access: Private 00174 // Description: Quantizes the color color to the nearest multiple of 00175 // 1000, just to prevent runaway accumulation of 00176 // only slightly-different ColorAttribs. 00177 //////////////////////////////////////////////////////////////////// 00178 void ColorAttrib:: 00179 quantize_color() { 00180 switch (_type) { 00181 case T_flat: 00182 _color[0] = cfloor(_color[0] * 1000.0f + 0.5f) * 0.001f; 00183 _color[1] = cfloor(_color[1] * 1000.0f + 0.5f) * 0.001f; 00184 _color[2] = cfloor(_color[2] * 1000.0f + 0.5f) * 0.001f; 00185 _color[3] = cfloor(_color[3] * 1000.0f + 0.5f) * 0.001f; 00186 break; 00187 00188 case T_off: 00189 _color.set(1.0f, 1.0f, 1.0f, 1.0f); 00190 break; 00191 00192 case T_vertex: 00193 _color.set(0.0f, 0.0f, 0.0f, 0.0f); 00194 break; 00195 } 00196 } 00197 00198 //////////////////////////////////////////////////////////////////// 00199 // Function: ColorAttrib::register_with_read_factory 00200 // Access: Public, Static 00201 // Description: Tells the BamReader how to create objects of type 00202 // ColorAttrib. 00203 //////////////////////////////////////////////////////////////////// 00204 void ColorAttrib:: 00205 register_with_read_factory() { 00206 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00207 } 00208 00209 //////////////////////////////////////////////////////////////////// 00210 // Function: ColorAttrib::write_datagram 00211 // Access: Public, Virtual 00212 // Description: Writes the contents of this object to the datagram 00213 // for shipping out to a Bam file. 00214 //////////////////////////////////////////////////////////////////// 00215 void ColorAttrib:: 00216 write_datagram(BamWriter *manager, Datagram &dg) { 00217 RenderAttrib::write_datagram(manager, dg); 00218 00219 dg.add_int8(_type); 00220 _color.write_datagram(dg); 00221 } 00222 00223 //////////////////////////////////////////////////////////////////// 00224 // Function: ColorAttrib::make_from_bam 00225 // Access: Protected, Static 00226 // Description: This function is called by the BamReader's factory 00227 // when a new object of type ColorAttrib is encountered 00228 // in the Bam file. It should create the ColorAttrib 00229 // and extract its information from the file. 00230 //////////////////////////////////////////////////////////////////// 00231 TypedWritable *ColorAttrib:: 00232 make_from_bam(const FactoryParams ¶ms) { 00233 ColorAttrib *attrib = new ColorAttrib(T_off, LColor::zero()); 00234 DatagramIterator scan; 00235 BamReader *manager; 00236 00237 parse_params(params, scan, manager); 00238 attrib->fillin(scan, manager); 00239 00240 return attrib; 00241 } 00242 00243 //////////////////////////////////////////////////////////////////// 00244 // Function: ColorAttrib::fillin 00245 // Access: Protected 00246 // Description: This internal function is called by make_from_bam to 00247 // read in all of the relevant data from the BamFile for 00248 // the new ColorAttrib. 00249 //////////////////////////////////////////////////////////////////// 00250 void ColorAttrib:: 00251 fillin(DatagramIterator &scan, BamReader *manager) { 00252 RenderAttrib::fillin(scan, manager); 00253 00254 _type = (Type)scan.get_int8(); 00255 _color.read_datagram(scan); 00256 quantize_color(); 00257 }