00001 // Filename: colorAttrib.cxx 00002 // Created by: drose (22Feb02) 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 "colorAttrib.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 ColorAttrib::_type_handle; 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: ColorAttrib::make_vertex 00031 // Access: Published, Static 00032 // Description: Constructs a new ColorAttrib object that indicates 00033 // geometry should be rendered according to its own 00034 // vertex color. 00035 //////////////////////////////////////////////////////////////////// 00036 CPT(RenderAttrib) ColorAttrib:: 00037 make_vertex() { 00038 ColorAttrib *attrib = new ColorAttrib(T_vertex); 00039 return return_new(attrib); 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: ColorAttrib::make_flat 00044 // Access: Published, Static 00045 // Description: Constructs a new ColorAttrib object that indicates 00046 // geometry should be rendered in the indicated color. 00047 //////////////////////////////////////////////////////////////////// 00048 CPT(RenderAttrib) ColorAttrib:: 00049 make_flat(const Colorf &color) { 00050 ColorAttrib *attrib = new ColorAttrib(T_flat, color); 00051 return return_new(attrib); 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: ColorAttrib::make_off 00056 // Access: Published, Static 00057 // Description: Constructs a new ColorAttrib object that indicates 00058 // geometry should be rendered without any color 00059 // commands at all. 00060 //////////////////////////////////////////////////////////////////// 00061 CPT(RenderAttrib) ColorAttrib:: 00062 make_off() { 00063 ColorAttrib *attrib = new ColorAttrib(T_off); 00064 return return_new(attrib); 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: ColorAttrib::issue 00069 // Access: Public, Virtual 00070 // Description: Calls the appropriate method on the indicated GSG 00071 // to issue the graphics commands appropriate to the 00072 // given attribute. This is normally called 00073 // (indirectly) only from 00074 // GraphicsStateGuardian::set_state() or modify_state(). 00075 //////////////////////////////////////////////////////////////////// 00076 void ColorAttrib:: 00077 issue(GraphicsStateGuardianBase *gsg) const { 00078 gsg->issue_color(this); 00079 } 00080 00081 //////////////////////////////////////////////////////////////////// 00082 // Function: ColorAttrib::output 00083 // Access: Public, Virtual 00084 // Description: 00085 //////////////////////////////////////////////////////////////////// 00086 void ColorAttrib:: 00087 output(ostream &out) const { 00088 out << get_type() << ":"; 00089 switch (get_color_type()) { 00090 case T_vertex: 00091 out << "vertex"; 00092 break; 00093 00094 case T_flat: 00095 out << "(" << get_color() << ")"; 00096 break; 00097 00098 case T_off: 00099 out << "off"; 00100 break; 00101 } 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: ColorAttrib::compare_to_impl 00106 // Access: Protected, Virtual 00107 // Description: Intended to be overridden by derived ColorAttrib 00108 // types to return a unique number indicating whether 00109 // this ColorAttrib is equivalent to the other one. 00110 // 00111 // This should return 0 if the two ColorAttrib objects 00112 // are equivalent, a number less than zero if this one 00113 // should be sorted before the other one, and a number 00114 // greater than zero otherwise. 00115 // 00116 // This will only be called with two ColorAttrib 00117 // objects whose get_type() functions return the same. 00118 //////////////////////////////////////////////////////////////////// 00119 int ColorAttrib:: 00120 compare_to_impl(const RenderAttrib *other) const { 00121 const ColorAttrib *ta; 00122 DCAST_INTO_R(ta, other, 0); 00123 if (_type != ta->_type) { 00124 return (int)_type - (int)ta->_type; 00125 } 00126 if (_type == T_flat) { 00127 return _color.compare_to(ta->_color); 00128 } 00129 return 0; 00130 } 00131 00132 //////////////////////////////////////////////////////////////////// 00133 // Function: ColorAttrib::make_default_impl 00134 // Access: Protected, Virtual 00135 // Description: Intended to be overridden by derived ColorAttrib 00136 // types to specify what the default property for a 00137 // ColorAttrib of this type should be. 00138 // 00139 // This should return a newly-allocated ColorAttrib of 00140 // the same type that corresponds to whatever the 00141 // standard default for this kind of ColorAttrib is. 00142 //////////////////////////////////////////////////////////////////// 00143 RenderAttrib *ColorAttrib:: 00144 make_default_impl() const { 00145 return new ColorAttrib; 00146 } 00147 00148 //////////////////////////////////////////////////////////////////// 00149 // Function: ColorAttrib::register_with_read_factory 00150 // Access: Public, Static 00151 // Description: Tells the BamReader how to create objects of type 00152 // ColorAttrib. 00153 //////////////////////////////////////////////////////////////////// 00154 void ColorAttrib:: 00155 register_with_read_factory() { 00156 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00157 } 00158 00159 //////////////////////////////////////////////////////////////////// 00160 // Function: ColorAttrib::write_datagram 00161 // Access: Public, Virtual 00162 // Description: Writes the contents of this object to the datagram 00163 // for shipping out to a Bam file. 00164 //////////////////////////////////////////////////////////////////// 00165 void ColorAttrib:: 00166 write_datagram(BamWriter *manager, Datagram &dg) { 00167 RenderAttrib::write_datagram(manager, dg); 00168 00169 dg.add_int8(_type); 00170 _color.write_datagram(dg); 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: ColorAttrib::make_from_bam 00175 // Access: Protected, Static 00176 // Description: This function is called by the BamReader's factory 00177 // when a new object of type ColorAttrib is encountered 00178 // in the Bam file. It should create the ColorAttrib 00179 // and extract its information from the file. 00180 //////////////////////////////////////////////////////////////////// 00181 TypedWritable *ColorAttrib:: 00182 make_from_bam(const FactoryParams ¶ms) { 00183 ColorAttrib *attrib = new ColorAttrib; 00184 DatagramIterator scan; 00185 BamReader *manager; 00186 00187 parse_params(params, scan, manager); 00188 attrib->fillin(scan, manager); 00189 00190 return attrib; 00191 } 00192 00193 //////////////////////////////////////////////////////////////////// 00194 // Function: ColorAttrib::fillin 00195 // Access: Protected 00196 // Description: This internal function is called by make_from_bam to 00197 // read in all of the relevant data from the BamFile for 00198 // the new ColorAttrib. 00199 //////////////////////////////////////////////////////////////////// 00200 void ColorAttrib:: 00201 fillin(DatagramIterator &scan, BamReader *manager) { 00202 RenderAttrib::fillin(scan, manager); 00203 00204 _type = (Type)scan.get_int8(); 00205 _color.read_datagram(scan); 00206 }