Panda3D

rescaleNormalAttrib.cxx

00001 // Filename: rescaleNormalAttrib.cxx
00002 // Created by:  drose (30Dec04)
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 "rescaleNormalAttrib.h"
00016 #include "graphicsStateGuardianBase.h"
00017 #include "string_utils.h"
00018 #include "dcast.h"
00019 #include "bamReader.h"
00020 #include "bamWriter.h"
00021 #include "datagram.h"
00022 #include "datagramIterator.h"
00023 #include "configVariableEnum.h"
00024 
00025 TypeHandle RescaleNormalAttrib::_type_handle;
00026 int RescaleNormalAttrib::_attrib_slot;
00027 
00028 // This variable is defined here instead of in config_pgraph.cxx,
00029 // because it depends on rescaleNormalAttrib.h having already been
00030 // included.
00031 static ConfigVariableEnum<RescaleNormalAttrib::Mode> rescale_normals
00032 ("rescale-normals", RescaleNormalAttrib::M_auto,
00033  PRC_DESC("Specifies the kind of RescaleNormalAttrib that should be "
00034           "created for the top of the scene graph.  This can automatically "
00035           "ensure that your lighting normals are unit-length, which may be "
00036           "particularly necessary in the presence of scales in the scene "
00037           "graph.  Turning it off ('none') may produce a small performance "
00038           "benefit."));
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: RescaleNormalAttrib::make
00042 //       Access: Published, Static
00043 //  Description: Constructs a new RescaleNormalAttrib object that
00044 //               specifies whether to rescale normals to compensate
00045 //               for transform scales or incorrectly defined normals.
00046 ////////////////////////////////////////////////////////////////////
00047 CPT(RenderAttrib) RescaleNormalAttrib::
00048 make(RescaleNormalAttrib::Mode mode) {
00049   RescaleNormalAttrib *attrib = new RescaleNormalAttrib(mode);
00050   return return_new(attrib);
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: RescaleNormalAttrib::make_default
00055 //       Access: Published, Static
00056 //  Description: Constructs a RescaleNoramlAttrib object that's
00057 //               suitable for putting at the top of a scene graph.
00058 //               This will contain whatever attrib was suggested by
00059 //               the user's rescale-normals Config variable.
00060 ////////////////////////////////////////////////////////////////////
00061 CPT(RenderAttrib) RescaleNormalAttrib::
00062 make_default() {
00063   RescaleNormalAttrib *attrib = new RescaleNormalAttrib(rescale_normals);
00064   return return_new(attrib);
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: RescaleNormalAttrib::output
00069 //       Access: Public, Virtual
00070 //  Description: 
00071 ////////////////////////////////////////////////////////////////////
00072 void RescaleNormalAttrib::
00073 output(ostream &out) const {
00074   out << get_type() << ":" << get_mode();
00075 }
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: RescaleNormalAttrib::compare_to_impl
00079 //       Access: Protected, Virtual
00080 //  Description: Intended to be overridden by derived RescaleNormalAttrib
00081 //               types to return a unique number indicating whether
00082 //               this RescaleNormalAttrib is equivalent to the other one.
00083 //
00084 //               This should return 0 if the two RescaleNormalAttrib objects
00085 //               are equivalent, a number less than zero if this one
00086 //               should be sorted before the other one, and a number
00087 //               greater than zero otherwise.
00088 //
00089 //               This will only be called with two RescaleNormalAttrib
00090 //               objects whose get_type() functions return the same.
00091 ////////////////////////////////////////////////////////////////////
00092 int RescaleNormalAttrib::
00093 compare_to_impl(const RenderAttrib *other) const {
00094   const RescaleNormalAttrib *ta;
00095   DCAST_INTO_R(ta, other, 0);
00096   return (int)_mode - (int)ta->_mode;
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: RescaleNormalAttrib::register_with_read_factory
00101 //       Access: Public, Static
00102 //  Description: Tells the BamReader how to create objects of type
00103 //               RescaleNormalAttrib.
00104 ////////////////////////////////////////////////////////////////////
00105 void RescaleNormalAttrib::
00106 register_with_read_factory() {
00107   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: RescaleNormalAttrib::write_datagram
00112 //       Access: Public, Virtual
00113 //  Description: Writes the contents of this object to the datagram
00114 //               for shipping out to a Bam file.
00115 ////////////////////////////////////////////////////////////////////
00116 void RescaleNormalAttrib::
00117 write_datagram(BamWriter *manager, Datagram &dg) {
00118   RenderAttrib::write_datagram(manager, dg);
00119 
00120   dg.add_int8(_mode);
00121 }
00122 
00123 ////////////////////////////////////////////////////////////////////
00124 //     Function: RescaleNormalAttrib::make_from_bam
00125 //       Access: Protected, Static
00126 //  Description: This function is called by the BamReader's factory
00127 //               when a new object of type RescaleNormalAttrib is encountered
00128 //               in the Bam file.  It should create the RescaleNormalAttrib
00129 //               and extract its information from the file.
00130 ////////////////////////////////////////////////////////////////////
00131 TypedWritable *RescaleNormalAttrib::
00132 make_from_bam(const FactoryParams &params) {
00133   RescaleNormalAttrib *attrib = new RescaleNormalAttrib(M_none);
00134   DatagramIterator scan;
00135   BamReader *manager;
00136 
00137   parse_params(params, scan, manager);
00138   attrib->fillin(scan, manager);
00139 
00140   return attrib;
00141 }
00142 
00143 ////////////////////////////////////////////////////////////////////
00144 //     Function: RescaleNormalAttrib::fillin
00145 //       Access: Protected
00146 //  Description: This internal function is called by make_from_bam to
00147 //               read in all of the relevant data from the BamFile for
00148 //               the new RescaleNormalAttrib.
00149 ////////////////////////////////////////////////////////////////////
00150 void RescaleNormalAttrib::
00151 fillin(DatagramIterator &scan, BamReader *manager) {
00152   RenderAttrib::fillin(scan, manager);
00153 
00154   _mode = (Mode)scan.get_int8();
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: RescaleNormalAttrib::Mode output operator
00159 //  Description:
00160 ////////////////////////////////////////////////////////////////////
00161 ostream &
00162 operator << (ostream &out, RescaleNormalAttrib::Mode mode) {
00163   switch (mode) {
00164   case RescaleNormalAttrib::M_none:
00165     return out << "none";
00166 
00167   case RescaleNormalAttrib::M_rescale:
00168     return out << "rescale";
00169 
00170   case RescaleNormalAttrib::M_normalize:
00171     return out << "normalize";
00172 
00173   case RescaleNormalAttrib::M_auto:
00174     return out << "auto";
00175   }
00176 
00177   return out << "(**invalid RescaleNormalAttrib::Mode(" << (int)mode << ")**)";
00178 }
00179 
00180 ////////////////////////////////////////////////////////////////////
00181 //     Function: RescaleNormalAttrib::Mode input operator
00182 //  Description:
00183 ////////////////////////////////////////////////////////////////////
00184 istream &
00185 operator >> (istream &in, RescaleNormalAttrib::Mode &mode) {
00186   string word;
00187   in >> word;
00188 
00189   if (cmp_nocase(word, "none") == 0) {
00190     mode = RescaleNormalAttrib::M_none;
00191 
00192   } else if (cmp_nocase(word, "rescale") == 0) {
00193     mode = RescaleNormalAttrib::M_rescale;
00194 
00195   } else if (cmp_nocase(word, "normalize") == 0) {
00196     mode = RescaleNormalAttrib::M_normalize;
00197 
00198   } else if (cmp_nocase(word, "auto") == 0) {
00199     mode = RescaleNormalAttrib::M_auto;
00200 
00201   } else {
00202     pgraph_cat.error()
00203       << "Invalid RescaleNormalAttrib::Mode value: " << word << "\n";
00204     mode = RescaleNormalAttrib::M_none;
00205   }
00206 
00207   return in;
00208 }
 All Classes Functions Variables Enumerations