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, Colorf::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 Colorf &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, Colorf(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::quantize_color 00138 // Access: Private 00139 // Description: Quantizes the color color to the nearest multiple of 00140 // 1000, just to prevent runaway accumulation of 00141 // only slightly-different ColorAttribs. 00142 //////////////////////////////////////////////////////////////////// 00143 void ColorAttrib:: 00144 quantize_color() { 00145 switch (_type) { 00146 case T_flat: 00147 _color[0] = cfloor(_color[0] * 1000.0f + 0.5f) * 0.001f; 00148 _color[1] = cfloor(_color[1] * 1000.0f + 0.5f) * 0.001f; 00149 _color[2] = cfloor(_color[2] * 1000.0f + 0.5f) * 0.001f; 00150 _color[3] = cfloor(_color[3] * 1000.0f + 0.5f) * 0.001f; 00151 break; 00152 00153 case T_off: 00154 _color.set(1.0f, 1.0f, 1.0f, 1.0f); 00155 break; 00156 00157 case T_vertex: 00158 _color.set(0.0f, 0.0f, 0.0f, 0.0f); 00159 break; 00160 } 00161 } 00162 00163 //////////////////////////////////////////////////////////////////// 00164 // Function: ColorAttrib::register_with_read_factory 00165 // Access: Public, Static 00166 // Description: Tells the BamReader how to create objects of type 00167 // ColorAttrib. 00168 //////////////////////////////////////////////////////////////////// 00169 void ColorAttrib:: 00170 register_with_read_factory() { 00171 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00172 } 00173 00174 //////////////////////////////////////////////////////////////////// 00175 // Function: ColorAttrib::write_datagram 00176 // Access: Public, Virtual 00177 // Description: Writes the contents of this object to the datagram 00178 // for shipping out to a Bam file. 00179 //////////////////////////////////////////////////////////////////// 00180 void ColorAttrib:: 00181 write_datagram(BamWriter *manager, Datagram &dg) { 00182 RenderAttrib::write_datagram(manager, dg); 00183 00184 dg.add_int8(_type); 00185 _color.write_datagram(dg); 00186 } 00187 00188 //////////////////////////////////////////////////////////////////// 00189 // Function: ColorAttrib::make_from_bam 00190 // Access: Protected, Static 00191 // Description: This function is called by the BamReader's factory 00192 // when a new object of type ColorAttrib is encountered 00193 // in the Bam file. It should create the ColorAttrib 00194 // and extract its information from the file. 00195 //////////////////////////////////////////////////////////////////// 00196 TypedWritable *ColorAttrib:: 00197 make_from_bam(const FactoryParams ¶ms) { 00198 ColorAttrib *attrib = new ColorAttrib(T_off, Colorf::zero()); 00199 DatagramIterator scan; 00200 BamReader *manager; 00201 00202 parse_params(params, scan, manager); 00203 attrib->fillin(scan, manager); 00204 00205 return attrib; 00206 } 00207 00208 //////////////////////////////////////////////////////////////////// 00209 // Function: ColorAttrib::fillin 00210 // Access: Protected 00211 // Description: This internal function is called by make_from_bam to 00212 // read in all of the relevant data from the BamFile for 00213 // the new ColorAttrib. 00214 //////////////////////////////////////////////////////////////////// 00215 void ColorAttrib:: 00216 fillin(DatagramIterator &scan, BamReader *manager) { 00217 RenderAttrib::fillin(scan, manager); 00218 00219 _type = (Type)scan.get_int8(); 00220 _color.read_datagram(scan); 00221 quantize_color(); 00222 }