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::compose_impl
00094 //       Access: Protected, Virtual
00095 //  Description: Intended to be overridden by derived RenderAttrib
00096 //               types to specify how two consecutive RenderAttrib
00097 //               objects of the same type interact.
00098 //
00099 //               This should return the result of applying the other
00100 //               RenderAttrib to a node in the scene graph below this
00101 //               RenderAttrib, which was already applied.  In most
00102 //               cases, the result is the same as the other
00103 //               RenderAttrib (that is, a subsequent RenderAttrib
00104 //               completely replaces the preceding one).  On the other
00105 //               hand, some kinds of RenderAttrib (for instance,
00106 //               ColorTransformAttrib) might combine in meaningful
00107 //               ways.
00108 ////////////////////////////////////////////////////////////////////
00109 CPT(RenderAttrib) ShadeModelAttrib::
00110 compose_impl(const RenderAttrib *other) const {
00111   const ShadeModelAttrib *ta;
00112   DCAST_INTO_R(ta, other, 0);
00113 
00114   Mode mode = ta->get_mode();
00115   return make(mode);
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: ShadeModelAttrib::register_with_read_factory
00120 //       Access: Public, Static
00121 //  Description: Tells the BamReader how to create objects of type
00122 //               ShadeModelAttrib.
00123 ////////////////////////////////////////////////////////////////////
00124 void ShadeModelAttrib::
00125 register_with_read_factory() {
00126   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00127 }
00128 
00129 ////////////////////////////////////////////////////////////////////
00130 //     Function: ShadeModelAttrib::write_datagram
00131 //       Access: Public, Virtual
00132 //  Description: Writes the contents of this object to the datagram
00133 //               for shipping out to a Bam file.
00134 ////////////////////////////////////////////////////////////////////
00135 void ShadeModelAttrib::
00136 write_datagram(BamWriter *manager, Datagram &dg) {
00137   RenderAttrib::write_datagram(manager, dg);
00138 
00139   dg.add_int8(_mode);
00140 }
00141 
00142 ////////////////////////////////////////////////////////////////////
00143 //     Function: ShadeModelAttrib::make_from_bam
00144 //       Access: Protected, Static
00145 //  Description: This function is called by the BamReader's factory
00146 //               when a new object of type ShadeModelAttrib is encountered
00147 //               in the Bam file.  It should create the ShadeModelAttrib
00148 //               and extract its information from the file.
00149 ////////////////////////////////////////////////////////////////////
00150 TypedWritable *ShadeModelAttrib::
00151 make_from_bam(const FactoryParams &params) {
00152   ShadeModelAttrib *attrib = new ShadeModelAttrib(M_smooth);
00153   DatagramIterator scan;
00154   BamReader *manager;
00155 
00156   parse_params(params, scan, manager);
00157   attrib->fillin(scan, manager);
00158 
00159   return attrib;
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function: ShadeModelAttrib::fillin
00164 //       Access: Protected
00165 //  Description: This internal function is called by make_from_bam to
00166 //               read in all of the relevant data from the BamFile for
00167 //               the new ShadeModelAttrib.
00168 ////////////////////////////////////////////////////////////////////
00169 void ShadeModelAttrib::
00170 fillin(DatagramIterator &scan, BamReader *manager) {
00171   RenderAttrib::fillin(scan, manager);
00172 
00173   _mode = (Mode)scan.get_int8();
00174 }
 All Classes Functions Variables Enumerations