Panda3D
|
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, float 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::compose_impl 00138 // Access: Protected, Virtual 00139 // Description: Intended to be overridden by derived RenderAttrib 00140 // types to specify how two consecutive RenderAttrib 00141 // objects of the same type interact. 00142 // 00143 // This should return the result of applying the other 00144 // RenderAttrib to a node in the scene graph below this 00145 // RenderAttrib, which was already applied. In most 00146 // cases, the result is the same as the other 00147 // RenderAttrib (that is, a subsequent RenderAttrib 00148 // completely replaces the preceding one). On the other 00149 // hand, some kinds of RenderAttrib (for instance, 00150 // ColorTransformAttrib) might combine in meaningful 00151 // ways. 00152 //////////////////////////////////////////////////////////////////// 00153 CPT(RenderAttrib) RenderModeAttrib:: 00154 compose_impl(const RenderAttrib *other) const { 00155 const RenderModeAttrib *ta; 00156 DCAST_INTO_R(ta, other, 0); 00157 00158 // The special mode M_unchanged means to keep the current mode. 00159 Mode mode = ta->get_mode(); 00160 if (mode == M_unchanged) { 00161 mode = get_mode(); 00162 } 00163 00164 return make(mode, ta->get_thickness(), ta->get_perspective()); 00165 } 00166 00167 //////////////////////////////////////////////////////////////////// 00168 // Function: RenderModeAttrib::register_with_read_factory 00169 // Access: Public, Static 00170 // Description: Tells the BamReader how to create objects of type 00171 // RenderModeAttrib. 00172 //////////////////////////////////////////////////////////////////// 00173 void RenderModeAttrib:: 00174 register_with_read_factory() { 00175 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00176 } 00177 00178 //////////////////////////////////////////////////////////////////// 00179 // Function: RenderModeAttrib::write_datagram 00180 // Access: Public, Virtual 00181 // Description: Writes the contents of this object to the datagram 00182 // for shipping out to a Bam file. 00183 //////////////////////////////////////////////////////////////////// 00184 void RenderModeAttrib:: 00185 write_datagram(BamWriter *manager, Datagram &dg) { 00186 RenderAttrib::write_datagram(manager, dg); 00187 00188 dg.add_int8(_mode); 00189 dg.add_float32(_thickness); 00190 dg.add_bool(_perspective); 00191 } 00192 00193 //////////////////////////////////////////////////////////////////// 00194 // Function: RenderModeAttrib::make_from_bam 00195 // Access: Protected, Static 00196 // Description: This function is called by the BamReader's factory 00197 // when a new object of type RenderModeAttrib is encountered 00198 // in the Bam file. It should create the RenderModeAttrib 00199 // and extract its information from the file. 00200 //////////////////////////////////////////////////////////////////// 00201 TypedWritable *RenderModeAttrib:: 00202 make_from_bam(const FactoryParams ¶ms) { 00203 RenderModeAttrib *attrib = new RenderModeAttrib(M_filled, 1.0f, false); 00204 DatagramIterator scan; 00205 BamReader *manager; 00206 00207 parse_params(params, scan, manager); 00208 attrib->fillin(scan, manager); 00209 00210 return attrib; 00211 } 00212 00213 //////////////////////////////////////////////////////////////////// 00214 // Function: RenderModeAttrib::fillin 00215 // Access: Protected 00216 // Description: This internal function is called by make_from_bam to 00217 // read in all of the relevant data from the BamFile for 00218 // the new RenderModeAttrib. 00219 //////////////////////////////////////////////////////////////////// 00220 void RenderModeAttrib:: 00221 fillin(DatagramIterator &scan, BamReader *manager) { 00222 RenderAttrib::fillin(scan, manager); 00223 00224 _mode = (Mode)scan.get_int8(); 00225 _thickness = scan.get_float32(); 00226 _perspective = scan.get_bool(); 00227 }