Panda3D
 All Classes Functions Variables Enumerations
renderModeAttrib.cxx
00001 // Filename: renderModeAttrib.cxx
00002 // Created by:  drose (14Mar02)
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 "renderModeAttrib.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 RenderModeAttrib::_type_handle;
00024 int RenderModeAttrib::_attrib_slot;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: RenderModeAttrib::make
00028 //       Access: Published, Static
00029 //  Description: Constructs a new RenderModeAttrib object that specifies
00030 //               whether to draw polygons in the normal, filled mode,
00031 //               or wireframe mode, or in some other yet-to-be-defined
00032 //               mode.
00033 //
00034 //               The thickness parameter specifies the thickness to be
00035 //               used for wireframe lines, as well as for ordinary
00036 //               linestrip lines; it also specifies the diameter of
00037 //               points.  (Thick lines are presently only supported in
00038 //               OpenGL; but thick points are supported on either
00039 //               platform.)
00040 //
00041 //               If perspective is true, the point thickness
00042 //               represented is actually a width in 3-d units, and the
00043 //               points should scale according to perspective.  When
00044 //               it is false, the point thickness is actually a width
00045 //               in pixels, and points are a uniform screen size
00046 //               regardless of distance from the camera.
00047 ////////////////////////////////////////////////////////////////////
00048 CPT(RenderAttrib) RenderModeAttrib::
00049 make(RenderModeAttrib::Mode mode, PN_stdfloat thickness, bool perspective) {
00050   RenderModeAttrib *attrib = new RenderModeAttrib(mode, thickness, perspective);
00051   return return_new(attrib);
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: RenderModeAttrib::make_default
00056 //       Access: Published, Static
00057 //  Description: Returns a RenderAttrib that corresponds to whatever
00058 //               the standard default properties for render attributes
00059 //               of this type ought to be.
00060 ////////////////////////////////////////////////////////////////////
00061 CPT(RenderAttrib) RenderModeAttrib::
00062 make_default() {
00063   return return_new(new RenderModeAttrib(M_filled, 1.0f, false));
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////
00067 //     Function: RenderModeAttrib::output
00068 //       Access: Public, Virtual
00069 //  Description: 
00070 ////////////////////////////////////////////////////////////////////
00071 void RenderModeAttrib::
00072 output(ostream &out) const {
00073   out << get_type() << ":";
00074   switch (get_mode()) {
00075   case M_unchanged:
00076     out << "unchanged";
00077     break;
00078 
00079   case M_filled:
00080     out << "filled";
00081     break;
00082 
00083   case M_wireframe:
00084     out << "wireframe(" << get_thickness() << ")";
00085     break;
00086 
00087   case M_point:
00088     out << "point(" << get_thickness() << ")";
00089     break;
00090 
00091   case M_filled_flat:
00092     out << "filled_flat";
00093     break;
00094   }
00095 
00096   if (get_thickness() != 1.0f) {
00097     out << ", thick " << get_thickness();
00098   }
00099 
00100   if (get_perspective()) {
00101     out << ", perspective";
00102   }
00103 }
00104 
00105 ////////////////////////////////////////////////////////////////////
00106 //     Function: RenderModeAttrib::compare_to_impl
00107 //       Access: Protected, Virtual
00108 //  Description: Intended to be overridden by derived RenderModeAttrib
00109 //               types to return a unique number indicating whether
00110 //               this RenderModeAttrib is equivalent to the other one.
00111 //
00112 //               This should return 0 if the two RenderModeAttrib objects
00113 //               are equivalent, a number less than zero if this one
00114 //               should be sorted before the other one, and a number
00115 //               greater than zero otherwise.
00116 //
00117 //               This will only be called with two RenderModeAttrib
00118 //               objects whose get_type() functions return the same.
00119 ////////////////////////////////////////////////////////////////////
00120 int RenderModeAttrib::
00121 compare_to_impl(const RenderAttrib *other) const {
00122   const RenderModeAttrib *ta;
00123   DCAST_INTO_R(ta, other, 0);
00124   if (_mode != ta->_mode) {
00125     return (int)_mode - (int)ta->_mode;
00126   }
00127   if (_thickness != ta->_thickness) {
00128     return _thickness < ta->_thickness ? -1 : 1;
00129   }
00130   if (_perspective != ta->_perspective) {
00131     return (int)_perspective - (int)ta->_perspective;
00132   }
00133   return 0;
00134 }
00135 
00136 ////////////////////////////////////////////////////////////////////
00137 //     Function: RenderModeAttrib::get_hash_impl
00138 //       Access: Protected, Virtual
00139 //  Description: Intended to be overridden by derived RenderAttrib
00140 //               types to return a unique hash for these particular
00141 //               properties.  RenderAttribs that compare the same with
00142 //               compare_to_impl(), above, should return the same
00143 //               hash; RenderAttribs that compare differently should
00144 //               return a different hash.
00145 ////////////////////////////////////////////////////////////////////
00146 size_t RenderModeAttrib::
00147 get_hash_impl() const {
00148   size_t hash = 0;
00149   hash = int_hash::add_hash(hash, (int)_mode);
00150   hash = float_hash().add_hash(hash, _thickness);
00151   hash = int_hash::add_hash(hash, (int)_perspective);
00152   return hash;
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //     Function: RenderModeAttrib::compose_impl
00157 //       Access: Protected, Virtual
00158 //  Description: Intended to be overridden by derived RenderAttrib
00159 //               types to specify how two consecutive RenderAttrib
00160 //               objects of the same type interact.
00161 //
00162 //               This should return the result of applying the other
00163 //               RenderAttrib to a node in the scene graph below this
00164 //               RenderAttrib, which was already applied.  In most
00165 //               cases, the result is the same as the other
00166 //               RenderAttrib (that is, a subsequent RenderAttrib
00167 //               completely replaces the preceding one).  On the other
00168 //               hand, some kinds of RenderAttrib (for instance,
00169 //               ColorTransformAttrib) might combine in meaningful
00170 //               ways.
00171 ////////////////////////////////////////////////////////////////////
00172 CPT(RenderAttrib) RenderModeAttrib::
00173 compose_impl(const RenderAttrib *other) const {
00174   const RenderModeAttrib *ta;
00175   DCAST_INTO_R(ta, other, 0);
00176 
00177   // The special mode M_unchanged means to keep the current mode.
00178   Mode mode = ta->get_mode();
00179   if (mode == M_unchanged) {
00180     mode = get_mode();
00181   }
00182 
00183   return make(mode, ta->get_thickness(), ta->get_perspective());
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function: RenderModeAttrib::register_with_read_factory
00188 //       Access: Public, Static
00189 //  Description: Tells the BamReader how to create objects of type
00190 //               RenderModeAttrib.
00191 ////////////////////////////////////////////////////////////////////
00192 void RenderModeAttrib::
00193 register_with_read_factory() {
00194   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00195 }
00196 
00197 ////////////////////////////////////////////////////////////////////
00198 //     Function: RenderModeAttrib::write_datagram
00199 //       Access: Public, Virtual
00200 //  Description: Writes the contents of this object to the datagram
00201 //               for shipping out to a Bam file.
00202 ////////////////////////////////////////////////////////////////////
00203 void RenderModeAttrib::
00204 write_datagram(BamWriter *manager, Datagram &dg) {
00205   RenderAttrib::write_datagram(manager, dg);
00206 
00207   dg.add_int8(_mode);
00208   dg.add_stdfloat(_thickness);
00209   dg.add_bool(_perspective);
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: RenderModeAttrib::make_from_bam
00214 //       Access: Protected, Static
00215 //  Description: This function is called by the BamReader's factory
00216 //               when a new object of type RenderModeAttrib is encountered
00217 //               in the Bam file.  It should create the RenderModeAttrib
00218 //               and extract its information from the file.
00219 ////////////////////////////////////////////////////////////////////
00220 TypedWritable *RenderModeAttrib::
00221 make_from_bam(const FactoryParams &params) {
00222   RenderModeAttrib *attrib = new RenderModeAttrib(M_filled, 1.0f, false);
00223   DatagramIterator scan;
00224   BamReader *manager;
00225 
00226   parse_params(params, scan, manager);
00227   attrib->fillin(scan, manager);
00228 
00229   return attrib;
00230 }
00231 
00232 ////////////////////////////////////////////////////////////////////
00233 //     Function: RenderModeAttrib::fillin
00234 //       Access: Protected
00235 //  Description: This internal function is called by make_from_bam to
00236 //               read in all of the relevant data from the BamFile for
00237 //               the new RenderModeAttrib.
00238 ////////////////////////////////////////////////////////////////////
00239 void RenderModeAttrib::
00240 fillin(DatagramIterator &scan, BamReader *manager) {
00241   RenderAttrib::fillin(scan, manager);
00242 
00243   _mode = (Mode)scan.get_int8();
00244   _thickness = scan.get_stdfloat();
00245   _perspective = scan.get_bool();
00246 }
 All Classes Functions Variables Enumerations