Panda3D
 All Classes Functions Variables Enumerations
colorBlendAttrib.cxx
00001 // Filename: colorBlendAttrib.cxx
00002 // Created by:  drose (29Mar02)
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 "colorBlendAttrib.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 ColorBlendAttrib::_type_handle;
00024 int ColorBlendAttrib::_attrib_slot;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: ColorBlendAttrib::make_off
00028 //       Access: Published, Static
00029 //  Description: Constructs a new ColorBlendAttrib object that
00030 //               disables special-effect blending, allowing normal
00031 //               transparency to be used instead.
00032 ////////////////////////////////////////////////////////////////////
00033 CPT(RenderAttrib) ColorBlendAttrib::
00034 make_off() {
00035   ColorBlendAttrib *attrib = new ColorBlendAttrib;
00036   return return_new(attrib);
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: ColorBlendAttrib::make
00041 //       Access: Published, Static
00042 //  Description: Constructs a new ColorBlendAttrib object.  This
00043 //               constructor is deprecated; use the one below, which
00044 //               takes three or four parameters, instead.
00045 ////////////////////////////////////////////////////////////////////
00046 CPT(RenderAttrib) ColorBlendAttrib::
00047 make(ColorBlendAttrib::Mode mode) {
00048   ColorBlendAttrib *attrib = new ColorBlendAttrib(mode, O_one, O_one,
00049                                                   LColor::zero());
00050   return return_new(attrib);
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: ColorBlendAttrib::make
00055 //       Access: Published, Static
00056 //  Description: Constructs a new ColorBlendAttrib object that enables
00057 //               special-effect blending.  This supercedes
00058 //               transparency.
00059 ////////////////////////////////////////////////////////////////////
00060 CPT(RenderAttrib) ColorBlendAttrib::
00061 make(ColorBlendAttrib::Mode mode, 
00062      ColorBlendAttrib::Operand a, ColorBlendAttrib::Operand b,
00063      const LColor &color) {
00064   ColorBlendAttrib *attrib = new ColorBlendAttrib(mode, a, b, color);
00065   return return_new(attrib);
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: ColorBlendAttrib::make_default
00070 //       Access: Published, Static
00071 //  Description: Returns a RenderAttrib that corresponds to whatever
00072 //               the standard default properties for render attributes
00073 //               of this type ought to be.
00074 ////////////////////////////////////////////////////////////////////
00075 CPT(RenderAttrib) ColorBlendAttrib::
00076 make_default() {
00077   return return_new(new ColorBlendAttrib);
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: ColorBlendAttrib::output
00082 //       Access: Public, Virtual
00083 //  Description: 
00084 ////////////////////////////////////////////////////////////////////
00085 void ColorBlendAttrib::
00086 output(ostream &out) const {
00087   out << get_type() << ":" << get_mode();
00088 
00089   if (get_mode() != M_none) {
00090     out << "(" << get_operand_a()
00091         << "," << get_operand_b();
00092     if (involves_constant_color()) {
00093       out << "," << get_color();
00094     }
00095     out << ")";
00096   }
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: ColorBlendAttrib::compare_to_impl
00101 //       Access: Protected, Virtual
00102 //  Description: Intended to be overridden by derived ColorBlendAttrib
00103 //               types to return a unique number indicating whether
00104 //               this ColorBlendAttrib is equivalent to the other one.
00105 //
00106 //               This should return 0 if the two ColorBlendAttrib objects
00107 //               are equivalent, a number less than zero if this one
00108 //               should be sorted before the other one, and a number
00109 //               greater than zero otherwise.
00110 //
00111 //               This will only be called with two ColorBlendAttrib
00112 //               objects whose get_type() functions return the same.
00113 ////////////////////////////////////////////////////////////////////
00114 int ColorBlendAttrib::
00115 compare_to_impl(const RenderAttrib *other) const {
00116   const ColorBlendAttrib *ta;
00117   DCAST_INTO_R(ta, other, 0);
00118 
00119   if (_mode != ta->_mode) {
00120     return (int)_mode - (int)ta->_mode;
00121   }
00122 
00123   if (_a != ta->_a) {
00124     return (int)_a - (int)ta->_a;
00125   }
00126 
00127   if (_b != ta->_b) {
00128     return (int)_b - (int)ta->_b;
00129   }
00130 
00131   return _color.compare_to(ta->_color);
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: ColorBlendAttrib::get_hash_impl
00136 //       Access: Protected, Virtual
00137 //  Description: Intended to be overridden by derived RenderAttrib
00138 //               types to return a unique hash for these particular
00139 //               properties.  RenderAttribs that compare the same with
00140 //               compare_to_impl(), above, should return the same
00141 //               hash; RenderAttribs that compare differently should
00142 //               return a different hash.
00143 ////////////////////////////////////////////////////////////////////
00144 size_t ColorBlendAttrib::
00145 get_hash_impl() const {
00146   size_t hash = 0;
00147   hash = int_hash::add_hash(hash, (int)_mode);
00148   hash = int_hash::add_hash(hash, (int)_a);
00149   hash = int_hash::add_hash(hash, (int)_b);
00150   hash = _color.add_hash(hash);
00151 
00152   return hash;
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //     Function: ColorBlendAttrib::get_auto_shader_attrib_impl
00157 //       Access: Protected, Virtual
00158 //  Description: 
00159 ////////////////////////////////////////////////////////////////////
00160 CPT(RenderAttrib) ColorBlendAttrib::
00161 get_auto_shader_attrib_impl(const RenderState *state) const {
00162   return this;
00163 }
00164 
00165 ////////////////////////////////////////////////////////////////////
00166 //     Function: ColorBlendAttrib::register_with_read_factory
00167 //       Access: Public, Static
00168 //  Description: Tells the BamReader how to create objects of type
00169 //               ColorBlendAttrib.
00170 ////////////////////////////////////////////////////////////////////
00171 void ColorBlendAttrib::
00172 register_with_read_factory() {
00173   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00174 }
00175 
00176 ////////////////////////////////////////////////////////////////////
00177 //     Function: ColorBlendAttrib::write_datagram
00178 //       Access: Public, Virtual
00179 //  Description: Writes the contents of this object to the datagram
00180 //               for shipping out to a Bam file.
00181 ////////////////////////////////////////////////////////////////////
00182 void ColorBlendAttrib::
00183 write_datagram(BamWriter *manager, Datagram &dg) {
00184   RenderAttrib::write_datagram(manager, dg);
00185 
00186   dg.add_uint8(_mode);
00187   dg.add_uint8(_a);
00188   dg.add_uint8(_b);
00189   _color.write_datagram(dg);
00190 }
00191 
00192 ////////////////////////////////////////////////////////////////////
00193 //     Function: ColorBlendAttrib::make_from_bam
00194 //       Access: Protected, Static
00195 //  Description: This function is called by the BamReader's factory
00196 //               when a new object of type ColorBlendAttrib is encountered
00197 //               in the Bam file.  It should create the ColorBlendAttrib
00198 //               and extract its information from the file.
00199 ////////////////////////////////////////////////////////////////////
00200 TypedWritable *ColorBlendAttrib::
00201 make_from_bam(const FactoryParams &params) {
00202   ColorBlendAttrib *attrib = new ColorBlendAttrib;
00203   DatagramIterator scan;
00204   BamReader *manager;
00205 
00206   parse_params(params, scan, manager);
00207   attrib->fillin(scan, manager);
00208 
00209   return attrib;
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: ColorBlendAttrib::fillin
00214 //       Access: Protected
00215 //  Description: This internal function is called by make_from_bam to
00216 //               read in all of the relevant data from the BamFile for
00217 //               the new ColorBlendAttrib.
00218 ////////////////////////////////////////////////////////////////////
00219 void ColorBlendAttrib::
00220 fillin(DatagramIterator &scan, BamReader *manager) {
00221   RenderAttrib::fillin(scan, manager);
00222 
00223   _mode = (Mode)scan.get_uint8();
00224   _a = (Operand)scan.get_uint8();
00225   _b = (Operand)scan.get_uint8();
00226   _color.read_datagram(scan);
00227 
00228   _involves_constant_color = involves_constant_color(_a) || involves_constant_color(_b);
00229   _involves_color_scale = involves_color_scale(_a) || involves_color_scale(_b);
00230 }
00231 
00232 ////////////////////////////////////////////////////////////////////
00233 //     Function: ostream << ColorBlendAttrib::Mode
00234 //  Description: 
00235 ////////////////////////////////////////////////////////////////////
00236 ostream &
00237 operator << (ostream &out, ColorBlendAttrib::Mode mode) {
00238   switch (mode) {
00239   case ColorBlendAttrib::M_none:
00240     return out << "none";
00241 
00242   case ColorBlendAttrib::M_add:
00243     return out << "add";
00244 
00245   case ColorBlendAttrib::M_subtract:
00246     return out << "subtract";
00247 
00248   case ColorBlendAttrib::M_inv_subtract:
00249     return out << "inv_subtract";
00250 
00251   case ColorBlendAttrib::M_min:
00252     return out << "min";
00253 
00254   case ColorBlendAttrib::M_max:
00255     return out << "max";
00256   }
00257 
00258   return out << "**invalid ColorBlendAttrib::Mode(" << (int)mode << ")**";
00259 }
00260 
00261 ////////////////////////////////////////////////////////////////////
00262 //     Function: ostream << ColorBlendAttrib::Operand
00263 //  Description: 
00264 ////////////////////////////////////////////////////////////////////
00265 ostream &
00266 operator << (ostream &out, ColorBlendAttrib::Operand operand) {
00267   switch (operand) {
00268   case ColorBlendAttrib::O_zero:
00269     return out << "zero";
00270 
00271   case ColorBlendAttrib::O_one:
00272     return out << "one";
00273 
00274   case ColorBlendAttrib::O_incoming_color:
00275     return out << "incomfing_color";
00276 
00277   case ColorBlendAttrib::O_one_minus_incoming_color:
00278     return out << "one_minus_incoming_color";
00279 
00280   case ColorBlendAttrib::O_fbuffer_color:
00281     return out << "fbuffer_color";
00282 
00283   case ColorBlendAttrib::O_one_minus_fbuffer_color:
00284     return out << "one_minus_fbuffer_color";
00285 
00286   case ColorBlendAttrib::O_incoming_alpha:
00287     return out << "incoming_alpha";
00288 
00289   case ColorBlendAttrib::O_one_minus_incoming_alpha:
00290     return out << "one_minus_incoming_alpha";
00291 
00292   case ColorBlendAttrib::O_fbuffer_alpha:
00293     return out << "fbuffer_alpha";
00294 
00295   case ColorBlendAttrib::O_one_minus_fbuffer_alpha:
00296     return out << "one_minus_fbuffer_alpha";
00297 
00298   case ColorBlendAttrib::O_constant_color:
00299     return out << "constant_color";
00300 
00301   case ColorBlendAttrib::O_one_minus_constant_color:
00302     return out << "one_minus_constant_color";
00303 
00304   case ColorBlendAttrib::O_constant_alpha:
00305     return out << "constant_alpha";
00306 
00307   case ColorBlendAttrib::O_one_minus_constant_alpha:
00308     return out << "one_minus_constant_alpha";
00309 
00310   case ColorBlendAttrib::O_incoming_color_saturate:
00311     return out << "incoming_color_saturate";
00312 
00313   case ColorBlendAttrib::O_color_scale:
00314     return out << "color_scale";
00315 
00316   case ColorBlendAttrib::O_one_minus_color_scale:
00317     return out << "one_minus_color_scale";
00318 
00319   case ColorBlendAttrib::O_alpha_scale:
00320     return out << "alpha_scale";
00321 
00322   case ColorBlendAttrib::O_one_minus_alpha_scale:
00323     return out << "one_minus_alpha_scale";
00324   }
00325 
00326   return out << "**invalid ColorBlendAttrib::Operand(" << (int)operand << ")**";
00327 }
 All Classes Functions Variables Enumerations