Panda3D

antialiasAttrib.cxx

00001 // Filename: antialiasAttrib.cxx
00002 // Created by:  drose (26Jan05)
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 "antialiasAttrib.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 AntialiasAttrib::_type_handle;
00024 int AntialiasAttrib::_attrib_slot;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: AntialiasAttrib::make
00028 //       Access: Published, Static
00029 //  Description: Constructs a new AntialiasAttrib object.
00030 //
00031 //               The mode should be either M_none, M_auto, or a union
00032 //               of any or all of M_point, M_line, M_polygon, and
00033 //               M_multisample.  Also, in addition to the above
00034 //               choices, it may include either of M_better of
00035 //               M_faster to specify a performance/quality tradeoff
00036 //               hint.
00037 //
00038 //               If M_none is specified, no antialiasing is performed.  
00039 //
00040 //               If M_multisample is specified, it means to use the
00041 //               special framebuffer multisample bits for
00042 //               antialiasing, if it is available.  If so, the
00043 //               M_point, M_line, and M_polygon modes are ignored.
00044 //               This advanced antialiasing mode is only available on
00045 //               certain graphics hardware.  If it is not available,
00046 //               the M_multisample bit is ignored (and the other modes
00047 //               may be used instead, if specified).
00048 //
00049 //               M_point, M_line, and/or M_polygon specify
00050 //               per-primitive smoothing.  When enabled, M_point and
00051 //               M_line may force transparency on.  M_polygon requires
00052 //               a frame buffer that includes an alpha channel, and it
00053 //               works best if the primitives are sorted
00054 //               front-to-back.
00055 //
00056 //               If M_auto is specified, M_multisample is selected if
00057 //               it is available, otherwise M_polygon is selected,
00058 //               unless drawing lines or points, in which case M_line
00059 //               or M_point is selected (these two generally produce
00060 //               better results than M_multisample)
00061 ////////////////////////////////////////////////////////////////////
00062 CPT(RenderAttrib) AntialiasAttrib::
00063 make(unsigned short mode) {
00064   AntialiasAttrib *attrib = new AntialiasAttrib(mode);
00065   return return_new(attrib);
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: AntialiasAttrib::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) AntialiasAttrib::
00076 make_default() {
00077   return return_new(new AntialiasAttrib(M_none));
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: AntialiasAttrib::output
00082 //       Access: Public, Virtual
00083 //  Description: 
00084 ////////////////////////////////////////////////////////////////////
00085 void AntialiasAttrib::
00086 output(ostream &out) const {
00087   out << get_type() << ":";
00088   
00089   int type = get_mode_type();
00090   char sep = ' ';
00091 
00092   if (type == M_none) {
00093     out << " none";
00094 
00095   } else if (type == M_auto) {
00096     out << " auto";
00097 
00098   } else {
00099     if ((_mode & M_point) != 0) {
00100       out << sep << "point";
00101       sep = '|';
00102     }
00103     if ((_mode & M_line) != 0) {
00104       out << sep << "line";
00105       sep = '|';
00106     }
00107     if ((_mode & M_polygon) != 0) {
00108       out << sep << "polygon";
00109       sep = '|';
00110     }
00111     if ((_mode & M_auto) != 0) {
00112       out << sep << "best";
00113       sep = '|';
00114     }
00115   }
00116 
00117   if ((_mode & M_faster) != 0) {
00118     out << sep << "faster";
00119     sep = '|';
00120   }
00121   if ((_mode & M_better) != 0) {
00122     out << sep << "better";
00123     sep = '|';
00124   }
00125 }
00126 
00127 ////////////////////////////////////////////////////////////////////
00128 //     Function: AntialiasAttrib::compare_to_impl
00129 //       Access: Protected, Virtual
00130 //  Description: Intended to be overridden by derived AntialiasAttrib
00131 //               types to return a unique number indicating whether
00132 //               this AntialiasAttrib is equivalent to the other one.
00133 //
00134 //               This should return 0 if the two AntialiasAttrib objects
00135 //               are equivalent, a number less than zero if this one
00136 //               should be sorted before the other one, and a number
00137 //               greater than zero otherwise.
00138 //
00139 //               This will only be called with two AntialiasAttrib
00140 //               objects whose get_type() functions return the same.
00141 ////////////////////////////////////////////////////////////////////
00142 int AntialiasAttrib::
00143 compare_to_impl(const RenderAttrib *other) const {
00144   const AntialiasAttrib *ta;
00145   DCAST_INTO_R(ta, other, 0);
00146   if (_mode != ta->_mode) {
00147     return (int)_mode - (int)ta->_mode;
00148   }
00149   return 0;
00150 }
00151 
00152 ////////////////////////////////////////////////////////////////////
00153 //     Function: AntialiasAttrib::compose_impl
00154 //       Access: Protected, Virtual
00155 //  Description: Intended to be overridden by derived RenderAttrib
00156 //               types to specify how two consecutive RenderAttrib
00157 //               objects of the same type interact.
00158 //
00159 //               This should return the result of applying the other
00160 //               RenderAttrib to a node in the scene graph below this
00161 //               RenderAttrib, which was already applied.  In most
00162 //               cases, the result is the same as the other
00163 //               RenderAttrib (that is, a subsequent RenderAttrib
00164 //               completely replaces the preceding one).  On the other
00165 //               hand, some kinds of RenderAttrib (for instance,
00166 //               ColorTransformAttrib) might combine in meaningful
00167 //               ways.
00168 ////////////////////////////////////////////////////////////////////
00169 CPT(RenderAttrib) AntialiasAttrib::
00170 compose_impl(const RenderAttrib *other) const {
00171   const AntialiasAttrib *ta;
00172   DCAST_INTO_R(ta, other, 0);
00173 
00174   unsigned short mode_type;
00175   unsigned short mode_quality;
00176 
00177   if (ta->get_mode_type() == M_none || ta->get_mode_type() == M_auto ||
00178       get_mode_type() == M_auto) {
00179     // These two special types don't combine: if one of these modes is
00180     // involved, the lower attrib wins.
00181     mode_type = ta->get_mode_type();
00182 
00183   } else {
00184     // Otherwise, the both modes reflect an explicit setting.  In that
00185     // case, these modes combine in the sensible way, as a union of
00186     // bits.
00187     mode_type = get_mode_type() | ta->get_mode_type();
00188   }
00189 
00190   if (ta->get_mode_quality() != 0) {
00191     // If any quality is specified on the lower attrib, it wins.
00192     mode_quality = ta->get_mode_quality();
00193   } else {
00194     // Otherwise, the upper quality wins.
00195     mode_quality = get_mode_quality();
00196   }
00197 
00198   return make(mode_type | mode_quality);
00199 }
00200 
00201 ////////////////////////////////////////////////////////////////////
00202 //     Function: AntialiasAttrib::register_with_read_factory
00203 //       Access: Public, Static
00204 //  Description: Tells the BamReader how to create objects of type
00205 //               AntialiasAttrib.
00206 ////////////////////////////////////////////////////////////////////
00207 void AntialiasAttrib::
00208 register_with_read_factory() {
00209   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: AntialiasAttrib::write_datagram
00214 //       Access: Public, Virtual
00215 //  Description: Writes the contents of this object to the datagram
00216 //               for shipping out to a Bam file.
00217 ////////////////////////////////////////////////////////////////////
00218 void AntialiasAttrib::
00219 write_datagram(BamWriter *manager, Datagram &dg) {
00220   RenderAttrib::write_datagram(manager, dg);
00221 
00222   dg.add_uint16(_mode);
00223 }
00224 
00225 ////////////////////////////////////////////////////////////////////
00226 //     Function: AntialiasAttrib::make_from_bam
00227 //       Access: Protected, Static
00228 //  Description: This function is called by the BamReader's factory
00229 //               when a new object of type AntialiasAttrib is encountered
00230 //               in the Bam file.  It should create the AntialiasAttrib
00231 //               and extract its information from the file.
00232 ////////////////////////////////////////////////////////////////////
00233 TypedWritable *AntialiasAttrib::
00234 make_from_bam(const FactoryParams &params) {
00235   AntialiasAttrib *attrib = new AntialiasAttrib(M_none);
00236   DatagramIterator scan;
00237   BamReader *manager;
00238 
00239   parse_params(params, scan, manager);
00240   attrib->fillin(scan, manager);
00241 
00242   return attrib;
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////
00246 //     Function: AntialiasAttrib::fillin
00247 //       Access: Protected
00248 //  Description: This internal function is called by make_from_bam to
00249 //               read in all of the relevant data from the BamFile for
00250 //               the new AntialiasAttrib.
00251 ////////////////////////////////////////////////////////////////////
00252 void AntialiasAttrib::
00253 fillin(DatagramIterator &scan, BamReader *manager) {
00254   RenderAttrib::fillin(scan, manager);
00255 
00256   _mode = scan.get_uint16();
00257 }
 All Classes Functions Variables Enumerations