Panda3D

shadeModelAttrib.cxx

00001 // Filename: shadeModelAttrib.cxx
00002 // Created by:  drose (14Mar05)
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 "shadeModelAttrib.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 ShadeModelAttrib::_type_handle;
00024 int ShadeModelAttrib::_attrib_slot;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: ShadeModelAttrib::make
00028 //       Access: Published, Static
00029 //  Description: Constructs a new ShadeModelAttrib object that specifies
00030 //               whether to draw polygons with flat shading or with
00031 //               per-vertex (smooth) shading.
00032 ////////////////////////////////////////////////////////////////////
00033 CPT(RenderAttrib) ShadeModelAttrib::
00034 make(ShadeModelAttrib::Mode mode) {
00035   ShadeModelAttrib *attrib = new ShadeModelAttrib(mode);
00036   return return_new(attrib);
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: ShadeModelAttrib::make_default
00041 //       Access: Published, Static
00042 //  Description: Returns a RenderAttrib that corresponds to whatever
00043 //               the standard default properties for render attributes
00044 //               of this type ought to be.
00045 ////////////////////////////////////////////////////////////////////
00046 CPT(RenderAttrib) ShadeModelAttrib::
00047 make_default() {
00048   return return_new(new ShadeModelAttrib(M_smooth));
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: ShadeModelAttrib::output
00053 //       Access: Public, Virtual
00054 //  Description: 
00055 ////////////////////////////////////////////////////////////////////
00056 void ShadeModelAttrib::
00057 output(ostream &out) const {
00058   out << get_type() << ":";
00059   switch (get_mode()) {
00060   case M_flat:
00061     out << "flat";
00062     break;
00063 
00064   case M_smooth:
00065     out << "smooth";
00066     break;
00067   }
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: ShadeModelAttrib::compare_to_impl
00072 //       Access: Protected, Virtual
00073 //  Description: Intended to be overridden by derived ShadeModelAttrib
00074 //               types to return a unique number indicating whether
00075 //               this ShadeModelAttrib is equivalent to the other one.
00076 //
00077 //               This should return 0 if the two ShadeModelAttrib objects
00078 //               are equivalent, a number less than zero if this one
00079 //               should be sorted before the other one, and a number
00080 //               greater than zero otherwise.
00081 //
00082 //               This will only be called with two ShadeModelAttrib
00083 //               objects whose get_type() functions return the same.
00084 ////////////////////////////////////////////////////////////////////
00085 int ShadeModelAttrib::
00086 compare_to_impl(const RenderAttrib *other) const {
00087   const ShadeModelAttrib *ta;
00088   DCAST_INTO_R(ta, other, 0);
00089   return (int)_mode - (int)ta->_mode;
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: ShadeModelAttrib::get_hash_impl
00094 //       Access: Protected, Virtual
00095 //  Description: Intended to be overridden by derived RenderAttrib
00096 //               types to return a unique hash for these particular
00097 //               properties.  RenderAttribs that compare the same with
00098 //               compare_to_impl(), above, should return the same
00099 //               hash; RenderAttribs that compare differently should
00100 //               return a different hash.
00101 ////////////////////////////////////////////////////////////////////
00102 size_t ShadeModelAttrib::
00103 get_hash_impl() const {
00104   size_t hash = 0;
00105   hash = int_hash::add_hash(hash, (int)_mode);
00106   return hash;
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: ShadeModelAttrib::compose_impl
00111 //       Access: Protected, Virtual
00112 //  Description: Intended to be overridden by derived RenderAttrib
00113 //               types to specify how two consecutive RenderAttrib
00114 //               objects of the same type interact.
00115 //
00116 //               This should return the result of applying the other
00117 //               RenderAttrib to a node in the scene graph below this
00118 //               RenderAttrib, which was already applied.  In most
00119 //               cases, the result is the same as the other
00120 //               RenderAttrib (that is, a subsequent RenderAttrib
00121 //               completely replaces the preceding one).  On the other
00122 //               hand, some kinds of RenderAttrib (for instance,
00123 //               ColorTransformAttrib) might combine in meaningful
00124 //               ways.
00125 ////////////////////////////////////////////////////////////////////
00126 CPT(RenderAttrib) ShadeModelAttrib::
00127 compose_impl(const RenderAttrib *other) const {
00128   const ShadeModelAttrib *ta;
00129   DCAST_INTO_R(ta, other, 0);
00130 
00131   Mode mode = ta->get_mode();
00132   return make(mode);
00133 }
00134 
00135 ////////////////////////////////////////////////////////////////////
00136 //     Function: ShadeModelAttrib::register_with_read_factory
00137 //       Access: Public, Static
00138 //  Description: Tells the BamReader how to create objects of type
00139 //               ShadeModelAttrib.
00140 ////////////////////////////////////////////////////////////////////
00141 void ShadeModelAttrib::
00142 register_with_read_factory() {
00143   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: ShadeModelAttrib::write_datagram
00148 //       Access: Public, Virtual
00149 //  Description: Writes the contents of this object to the datagram
00150 //               for shipping out to a Bam file.
00151 ////////////////////////////////////////////////////////////////////
00152 void ShadeModelAttrib::
00153 write_datagram(BamWriter *manager, Datagram &dg) {
00154   RenderAttrib::write_datagram(manager, dg);
00155 
00156   dg.add_int8(_mode);
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: ShadeModelAttrib::make_from_bam
00161 //       Access: Protected, Static
00162 //  Description: This function is called by the BamReader's factory
00163 //               when a new object of type ShadeModelAttrib is encountered
00164 //               in the Bam file.  It should create the ShadeModelAttrib
00165 //               and extract its information from the file.
00166 ////////////////////////////////////////////////////////////////////
00167 TypedWritable *ShadeModelAttrib::
00168 make_from_bam(const FactoryParams &params) {
00169   ShadeModelAttrib *attrib = new ShadeModelAttrib(M_smooth);
00170   DatagramIterator scan;
00171   BamReader *manager;
00172 
00173   parse_params(params, scan, manager);
00174   attrib->fillin(scan, manager);
00175 
00176   return attrib;
00177 }
00178 
00179 ////////////////////////////////////////////////////////////////////
00180 //     Function: ShadeModelAttrib::fillin
00181 //       Access: Protected
00182 //  Description: This internal function is called by make_from_bam to
00183 //               read in all of the relevant data from the BamFile for
00184 //               the new ShadeModelAttrib.
00185 ////////////////////////////////////////////////////////////////////
00186 void ShadeModelAttrib::
00187 fillin(DatagramIterator &scan, BamReader *manager) {
00188   RenderAttrib::fillin(scan, manager);
00189 
00190   _mode = (Mode)scan.get_int8();
00191 }
 All Classes Functions Variables Enumerations