Panda3D

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                                                   Colorf::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 Colorf &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::register_with_read_factory
00136 //       Access: Public, Static
00137 //  Description: Tells the BamReader how to create objects of type
00138 //               ColorBlendAttrib.
00139 ////////////////////////////////////////////////////////////////////
00140 void ColorBlendAttrib::
00141 register_with_read_factory() {
00142   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00143 }
00144 
00145 ////////////////////////////////////////////////////////////////////
00146 //     Function: ColorBlendAttrib::write_datagram
00147 //       Access: Public, Virtual
00148 //  Description: Writes the contents of this object to the datagram
00149 //               for shipping out to a Bam file.
00150 ////////////////////////////////////////////////////////////////////
00151 void ColorBlendAttrib::
00152 write_datagram(BamWriter *manager, Datagram &dg) {
00153   RenderAttrib::write_datagram(manager, dg);
00154 
00155   dg.add_uint8(_mode);
00156   dg.add_uint8(_a);
00157   dg.add_uint8(_b);
00158   _color.write_datagram(dg);
00159 }
00160 
00161 ////////////////////////////////////////////////////////////////////
00162 //     Function: ColorBlendAttrib::make_from_bam
00163 //       Access: Protected, Static
00164 //  Description: This function is called by the BamReader's factory
00165 //               when a new object of type ColorBlendAttrib is encountered
00166 //               in the Bam file.  It should create the ColorBlendAttrib
00167 //               and extract its information from the file.
00168 ////////////////////////////////////////////////////////////////////
00169 TypedWritable *ColorBlendAttrib::
00170 make_from_bam(const FactoryParams &params) {
00171   ColorBlendAttrib *attrib = new ColorBlendAttrib;
00172   DatagramIterator scan;
00173   BamReader *manager;
00174 
00175   parse_params(params, scan, manager);
00176   attrib->fillin(scan, manager);
00177 
00178   return attrib;
00179 }
00180 
00181 ////////////////////////////////////////////////////////////////////
00182 //     Function: ColorBlendAttrib::fillin
00183 //       Access: Protected
00184 //  Description: This internal function is called by make_from_bam to
00185 //               read in all of the relevant data from the BamFile for
00186 //               the new ColorBlendAttrib.
00187 ////////////////////////////////////////////////////////////////////
00188 void ColorBlendAttrib::
00189 fillin(DatagramIterator &scan, BamReader *manager) {
00190   RenderAttrib::fillin(scan, manager);
00191 
00192   _mode = (Mode)scan.get_uint8();
00193   _a = (Operand)scan.get_uint8();
00194   _b = (Operand)scan.get_uint8();
00195   _color.read_datagram(scan);
00196 
00197   _involves_constant_color = involves_constant_color(_a) || involves_constant_color(_b);
00198   _involves_color_scale = involves_color_scale(_a) || involves_color_scale(_b);
00199 }
00200 
00201 ////////////////////////////////////////////////////////////////////
00202 //     Function: ostream << ColorBlendAttrib::Mode
00203 //  Description: 
00204 ////////////////////////////////////////////////////////////////////
00205 ostream &
00206 operator << (ostream &out, ColorBlendAttrib::Mode mode) {
00207   switch (mode) {
00208   case ColorBlendAttrib::M_none:
00209     return out << "none";
00210 
00211   case ColorBlendAttrib::M_add:
00212     return out << "add";
00213 
00214   case ColorBlendAttrib::M_subtract:
00215     return out << "subtract";
00216 
00217   case ColorBlendAttrib::M_inv_subtract:
00218     return out << "inv_subtract";
00219 
00220   case ColorBlendAttrib::M_min:
00221     return out << "min";
00222 
00223   case ColorBlendAttrib::M_max:
00224     return out << "max";
00225   }
00226 
00227   return out << "**invalid ColorBlendAttrib::Mode(" << (int)mode << ")**";
00228 }
00229 
00230 ////////////////////////////////////////////////////////////////////
00231 //     Function: ostream << ColorBlendAttrib::Operand
00232 //  Description: 
00233 ////////////////////////////////////////////////////////////////////
00234 ostream &
00235 operator << (ostream &out, ColorBlendAttrib::Operand operand) {
00236   switch (operand) {
00237   case ColorBlendAttrib::O_zero:
00238     return out << "zero";
00239 
00240   case ColorBlendAttrib::O_one:
00241     return out << "one";
00242 
00243   case ColorBlendAttrib::O_incoming_color:
00244     return out << "incomfing_color";
00245 
00246   case ColorBlendAttrib::O_one_minus_incoming_color:
00247     return out << "one_minus_incoming_color";
00248 
00249   case ColorBlendAttrib::O_fbuffer_color:
00250     return out << "fbuffer_color";
00251 
00252   case ColorBlendAttrib::O_one_minus_fbuffer_color:
00253     return out << "one_minus_fbuffer_color";
00254 
00255   case ColorBlendAttrib::O_incoming_alpha:
00256     return out << "incoming_alpha";
00257 
00258   case ColorBlendAttrib::O_one_minus_incoming_alpha:
00259     return out << "one_minus_incoming_alpha";
00260 
00261   case ColorBlendAttrib::O_fbuffer_alpha:
00262     return out << "fbuffer_alpha";
00263 
00264   case ColorBlendAttrib::O_one_minus_fbuffer_alpha:
00265     return out << "one_minus_fbuffer_alpha";
00266 
00267   case ColorBlendAttrib::O_constant_color:
00268     return out << "constant_color";
00269 
00270   case ColorBlendAttrib::O_one_minus_constant_color:
00271     return out << "one_minus_constant_color";
00272 
00273   case ColorBlendAttrib::O_constant_alpha:
00274     return out << "constant_alpha";
00275 
00276   case ColorBlendAttrib::O_one_minus_constant_alpha:
00277     return out << "one_minus_constant_alpha";
00278 
00279   case ColorBlendAttrib::O_incoming_color_saturate:
00280     return out << "incoming_color_saturate";
00281 
00282   case ColorBlendAttrib::O_color_scale:
00283     return out << "color_scale";
00284 
00285   case ColorBlendAttrib::O_one_minus_color_scale:
00286     return out << "one_minus_color_scale";
00287 
00288   case ColorBlendAttrib::O_alpha_scale:
00289     return out << "alpha_scale";
00290 
00291   case ColorBlendAttrib::O_one_minus_alpha_scale:
00292     return out << "one_minus_alpha_scale";
00293   }
00294 
00295   return out << "**invalid ColorBlendAttrib::Operand(" << (int)operand << ")**";
00296 }
 All Classes Functions Variables Enumerations