Panda3D

materialAttrib.cxx

00001 // Filename: materialAttrib.cxx
00002 // Created by:  drose (04Mar02)
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 "materialAttrib.h"
00016 #include "graphicsStateGuardianBase.h"
00017 #include "bamReader.h"
00018 #include "bamWriter.h"
00019 #include "datagram.h"
00020 #include "datagramIterator.h"
00021 
00022 TypeHandle MaterialAttrib::_type_handle;
00023 int MaterialAttrib::_attrib_slot;
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: MaterialAttrib::make
00027 //       Access: Published, Static
00028 //  Description: Constructs a new MaterialAttrib object suitable for
00029 //               rendering the indicated material onto geometry.
00030 ////////////////////////////////////////////////////////////////////
00031 CPT(RenderAttrib) MaterialAttrib::
00032 make(Material *material) {
00033   MaterialAttrib *attrib = new MaterialAttrib;
00034   attrib->_material = material;
00035   material->set_attrib_lock();
00036   return return_new(attrib);
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: MaterialAttrib::make_off
00041 //       Access: Published, Static
00042 //  Description: Constructs a new MaterialAttrib object suitable for
00043 //               rendering unmateriald geometry.
00044 ////////////////////////////////////////////////////////////////////
00045 CPT(RenderAttrib) MaterialAttrib::
00046 make_off() {
00047   MaterialAttrib *attrib = new MaterialAttrib;
00048   return return_new(attrib);
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: MaterialAttrib::make_default
00053 //       Access: Published, Static
00054 //  Description: Returns a RenderAttrib that corresponds to whatever
00055 //               the standard default properties for render attributes
00056 //               of this type ought to be.
00057 ////////////////////////////////////////////////////////////////////
00058 CPT(RenderAttrib) MaterialAttrib::
00059 make_default() {
00060   return return_new(new MaterialAttrib);
00061 }
00062 
00063 ////////////////////////////////////////////////////////////////////
00064 //     Function: MaterialAttrib::output
00065 //       Access: Public, Virtual
00066 //  Description: 
00067 ////////////////////////////////////////////////////////////////////
00068 void MaterialAttrib::
00069 output(ostream &out) const {
00070   out << get_type() << ":";
00071   if (is_off()) {
00072     out << "(off)";
00073   } else {
00074     out << *_material;
00075   }
00076 }
00077 
00078 ////////////////////////////////////////////////////////////////////
00079 //     Function: MaterialAttrib::compare_to_impl
00080 //       Access: Protected, Virtual
00081 //  Description: Intended to be overridden by derived MaterialAttrib
00082 //               types to return a unique number indicating whether
00083 //               this MaterialAttrib is equivalent to the other one.
00084 //
00085 //               This should return 0 if the two MaterialAttrib objects
00086 //               are equivalent, a number less than zero if this one
00087 //               should be sorted before the other one, and a number
00088 //               greater than zero otherwise.
00089 //
00090 //               This will only be called with two MaterialAttrib
00091 //               objects whose get_type() functions return the same.
00092 ////////////////////////////////////////////////////////////////////
00093 int MaterialAttrib::
00094 compare_to_impl(const RenderAttrib *other) const {
00095   const MaterialAttrib *ta;
00096   DCAST_INTO_R(ta, other, 0);
00097 
00098   // Comparing pointers by subtraction is problematic.  Instead of
00099   // doing this, we'll just depend on the built-in != and < operators
00100   // for comparing pointers.
00101   if (_material != ta->_material) {
00102     return _material < ta->_material ? -1 : 1;
00103   }
00104   return 0;
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////
00108 //     Function: MaterialAttrib::register_with_read_factory
00109 //       Access: Public, Static
00110 //  Description: Tells the BamReader how to create objects of type
00111 //               MaterialAttrib.
00112 ////////////////////////////////////////////////////////////////////
00113 void MaterialAttrib::
00114 register_with_read_factory() {
00115   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: MaterialAttrib::write_datagram
00120 //       Access: Public, Virtual
00121 //  Description: Writes the contents of this object to the datagram
00122 //               for shipping out to a Bam file.
00123 ////////////////////////////////////////////////////////////////////
00124 void MaterialAttrib::
00125 write_datagram(BamWriter *manager, Datagram &dg) {
00126   RenderAttrib::write_datagram(manager, dg);
00127 
00128   manager->write_pointer(dg, _material);
00129 }
00130 
00131 ////////////////////////////////////////////////////////////////////
00132 //     Function: MaterialAttrib::complete_pointers
00133 //       Access: Public, Virtual
00134 //  Description: Receives an array of pointers, one for each time
00135 //               manager->read_pointer() was called in fillin().
00136 //               Returns the number of pointers processed.
00137 ////////////////////////////////////////////////////////////////////
00138 int MaterialAttrib::
00139 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00140   int pi = RenderAttrib::complete_pointers(p_list, manager);
00141 
00142   TypedWritable *material = p_list[pi++];
00143   if (material != (TypedWritable *)NULL) {
00144     _material = DCAST(Material, material);
00145   }
00146 
00147   return pi;
00148 }
00149 
00150 ////////////////////////////////////////////////////////////////////
00151 //     Function: MaterialAttrib::make_from_bam
00152 //       Access: Protected, Static
00153 //  Description: This function is called by the BamReader's factory
00154 //               when a new object of type MaterialAttrib is encountered
00155 //               in the Bam file.  It should create the MaterialAttrib
00156 //               and extract its information from the file.
00157 ////////////////////////////////////////////////////////////////////
00158 TypedWritable *MaterialAttrib::
00159 make_from_bam(const FactoryParams &params) {
00160   MaterialAttrib *attrib = new MaterialAttrib;
00161   DatagramIterator scan;
00162   BamReader *manager;
00163 
00164   parse_params(params, scan, manager);
00165   attrib->fillin(scan, manager);
00166   return attrib;
00167 }
00168 
00169 ////////////////////////////////////////////////////////////////////
00170 //     Function: MaterialAttrib::fillin
00171 //       Access: Protected
00172 //  Description: This internal function is called by make_from_bam to
00173 //               read in all of the relevant data from the BamFile for
00174 //               the new MaterialAttrib.
00175 ////////////////////////////////////////////////////////////////////
00176 void MaterialAttrib::
00177 fillin(DatagramIterator &scan, BamReader *manager) {
00178   RenderAttrib::fillin(scan, manager);
00179 
00180   // Read the _material pointer.
00181   manager->read_pointer(scan);
00182 }
 All Classes Functions Variables Enumerations