00001 // Filename: scissorAttrib.cxx 00002 // Created by: drose (29Jul08) 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 "scissorAttrib.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 ScissorAttrib::_type_handle; 00024 int ScissorAttrib::_attrib_slot; 00025 CPT(RenderAttrib) ScissorAttrib::_off; 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: ScissorAttrib::Constructor 00029 // Access: Private 00030 // Description: Use ScissorAttrib::make() to construct a new 00031 // ScissorAttrib object. 00032 //////////////////////////////////////////////////////////////////// 00033 ScissorAttrib:: 00034 ScissorAttrib(const LVecBase4 &frame) : 00035 _frame(frame) 00036 { 00037 // Impose sensible bounds. 00038 _frame[0] = max(min(_frame[0], (PN_stdfloat)1.0), (PN_stdfloat)0.0); 00039 _frame[1] = max(min(_frame[1], (PN_stdfloat)1.0), _frame[0]); 00040 _frame[2] = max(min(_frame[2], (PN_stdfloat)1.0), (PN_stdfloat)0.0); 00041 _frame[3] = max(min(_frame[3], (PN_stdfloat)1.0), _frame[2]); 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: ScissorAttrib::make_off 00046 // Access: Published, Static 00047 // Description: Constructs a new ScissorAttrib object that removes 00048 // the scissor region and fills the DisplayRegion. 00049 //////////////////////////////////////////////////////////////////// 00050 CPT(RenderAttrib) ScissorAttrib:: 00051 make_off() { 00052 if (_off != 0) { 00053 return _off; 00054 } 00055 ScissorAttrib *attrib = new ScissorAttrib(LVecBase4(0.0f, 1.0f, 0.0f, 1.0f)); 00056 _off = return_new(attrib); 00057 return _off; 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: ScissorAttrib::make 00062 // Access: Published, Static 00063 // Description: Constructs a ScissorAttrib that restricts rendering 00064 // to the indicated frame within the current 00065 // DisplayRegion. (0,0) is the lower-left corner of the 00066 // DisplayRegion, and (1,1) is the upper-right corner. 00067 //////////////////////////////////////////////////////////////////// 00068 CPT(RenderAttrib) ScissorAttrib:: 00069 make(const LVecBase4 &frame) { 00070 ScissorAttrib *attrib = new ScissorAttrib(frame); 00071 return return_new(attrib); 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function: ScissorAttrib::make_default 00076 // Access: Published, Static 00077 // Description: Returns a RenderAttrib that corresponds to whatever 00078 // the standard default properties for render attributes 00079 // of this type ought to be. 00080 //////////////////////////////////////////////////////////////////// 00081 CPT(RenderAttrib) ScissorAttrib:: 00082 make_default() { 00083 return return_new(new ScissorAttrib(LVecBase4(0.0f, 1.0f, 0.0f, 1.0f))); 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: ScissorAttrib::output 00088 // Access: Public, Virtual 00089 // Description: 00090 //////////////////////////////////////////////////////////////////// 00091 void ScissorAttrib:: 00092 output(ostream &out) const { 00093 out << get_type() << ":[" << _frame << "]"; 00094 } 00095 00096 //////////////////////////////////////////////////////////////////// 00097 // Function: ScissorAttrib::compare_to_impl 00098 // Access: Protected, Virtual 00099 // Description: Intended to be overridden by derived ScissorAttrib 00100 // types to return a unique number indicating whether 00101 // this ScissorAttrib is equivalent to the other one. 00102 // 00103 // This should return 0 if the two ScissorAttrib objects 00104 // are equivalent, a number less than zero if this one 00105 // should be sorted before the other one, and a number 00106 // greater than zero otherwise. 00107 // 00108 // This will only be called with two ScissorAttrib 00109 // objects whose get_type() functions return the same. 00110 //////////////////////////////////////////////////////////////////// 00111 int ScissorAttrib:: 00112 compare_to_impl(const RenderAttrib *other) const { 00113 const ScissorAttrib *ta; 00114 DCAST_INTO_R(ta, other, 0); 00115 return _frame.compare_to(ta->_frame); 00116 } 00117 00118 //////////////////////////////////////////////////////////////////// 00119 // Function: ScissorAttrib::get_hash_impl 00120 // Access: Protected, Virtual 00121 // Description: Intended to be overridden by derived RenderAttrib 00122 // types to return a unique hash for these particular 00123 // properties. RenderAttribs that compare the same with 00124 // compare_to_impl(), above, should return the same 00125 // hash; RenderAttribs that compare differently should 00126 // return a different hash. 00127 //////////////////////////////////////////////////////////////////// 00128 size_t ScissorAttrib:: 00129 get_hash_impl() const { 00130 size_t hash = 0; 00131 hash = _frame.add_hash(hash); 00132 return hash; 00133 } 00134 00135 //////////////////////////////////////////////////////////////////// 00136 // Function: ScissorAttrib::compose_impl 00137 // Access: Protected, Virtual 00138 // Description: Intended to be overridden by derived RenderAttrib 00139 // types to specify how two consecutive RenderAttrib 00140 // objects of the same type interact. 00141 // 00142 // This should return the result of applying the other 00143 // RenderAttrib to a node in the scene graph below this 00144 // RenderAttrib, which was already applied. In most 00145 // cases, the result is the same as the other 00146 // RenderAttrib (that is, a subsequent RenderAttrib 00147 // completely replaces the preceding one). On the other 00148 // hand, some kinds of RenderAttrib (for instance, 00149 // ColorTransformAttrib) might combine in meaningful 00150 // ways. 00151 //////////////////////////////////////////////////////////////////// 00152 CPT(RenderAttrib) ScissorAttrib:: 00153 compose_impl(const RenderAttrib *other) const { 00154 const ScissorAttrib *ta; 00155 DCAST_INTO_R(ta, other, 0); 00156 00157 LVecBase4 new_frame(max(ta->_frame[0], _frame[0]), 00158 min(ta->_frame[1], _frame[1]), 00159 max(ta->_frame[2], _frame[2]), 00160 min(ta->_frame[3], _frame[3])); 00161 00162 ScissorAttrib *attrib = new ScissorAttrib(new_frame); 00163 return return_new(attrib); 00164 } 00165 00166 //////////////////////////////////////////////////////////////////// 00167 // Function: ScissorAttrib::register_with_read_factory 00168 // Access: Public, Static 00169 // Description: Tells the BamReader how to create objects of type 00170 // ScissorAttrib. 00171 //////////////////////////////////////////////////////////////////// 00172 void ScissorAttrib:: 00173 register_with_read_factory() { 00174 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00175 } 00176 00177 //////////////////////////////////////////////////////////////////// 00178 // Function: ScissorAttrib::write_datagram 00179 // Access: Public, Virtual 00180 // Description: Writes the contents of this object to the datagram 00181 // for shipping out to a Bam file. 00182 //////////////////////////////////////////////////////////////////// 00183 void ScissorAttrib:: 00184 write_datagram(BamWriter *manager, Datagram &dg) { 00185 RenderAttrib::write_datagram(manager, dg); 00186 00187 _frame.write_datagram(dg); 00188 } 00189 00190 //////////////////////////////////////////////////////////////////// 00191 // Function: ScissorAttrib::make_from_bam 00192 // Access: Protected, Static 00193 // Description: This function is called by the BamReader's factory 00194 // when a new object of type ScissorAttrib is encountered 00195 // in the Bam file. It should create the ScissorAttrib 00196 // and extract its information from the file. 00197 //////////////////////////////////////////////////////////////////// 00198 TypedWritable *ScissorAttrib:: 00199 make_from_bam(const FactoryParams ¶ms) { 00200 ScissorAttrib *attrib = new ScissorAttrib(LVecBase4(0.0f, 1.0f, 0.0f, 1.0f)); 00201 DatagramIterator scan; 00202 BamReader *manager; 00203 00204 parse_params(params, scan, manager); 00205 attrib->fillin(scan, manager); 00206 00207 return attrib; 00208 } 00209 00210 //////////////////////////////////////////////////////////////////// 00211 // Function: ScissorAttrib::fillin 00212 // Access: Protected 00213 // Description: This internal function is called by make_from_bam to 00214 // read in all of the relevant data from the BamFile for 00215 // the new ScissorAttrib. 00216 //////////////////////////////////////////////////////////////////// 00217 void ScissorAttrib:: 00218 fillin(DatagramIterator &scan, BamReader *manager) { 00219 RenderAttrib::fillin(scan, manager); 00220 00221 _frame.read_datagram(scan); 00222 }