Panda3D
|
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 LVecBase4f &frame) : 00035 _frame(frame) 00036 { 00037 // Impose sensible bounds. 00038 _frame[0] = max(min(_frame[0], 1.0f), 0.0f); 00039 _frame[1] = max(min(_frame[1], 1.0f), _frame[0]); 00040 _frame[2] = max(min(_frame[2], 1.0f), 0.0f); 00041 _frame[3] = max(min(_frame[3], 1.0f), _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(LVecBase4f(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 LVecBase4f &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(LVecBase4f(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::compose_impl 00120 // Access: Protected, Virtual 00121 // Description: Intended to be overridden by derived RenderAttrib 00122 // types to specify how two consecutive RenderAttrib 00123 // objects of the same type interact. 00124 // 00125 // This should return the result of applying the other 00126 // RenderAttrib to a node in the scene graph below this 00127 // RenderAttrib, which was already applied. In most 00128 // cases, the result is the same as the other 00129 // RenderAttrib (that is, a subsequent RenderAttrib 00130 // completely replaces the preceding one). On the other 00131 // hand, some kinds of RenderAttrib (for instance, 00132 // ColorTransformAttrib) might combine in meaningful 00133 // ways. 00134 //////////////////////////////////////////////////////////////////// 00135 CPT(RenderAttrib) ScissorAttrib:: 00136 compose_impl(const RenderAttrib *other) const { 00137 const ScissorAttrib *ta; 00138 DCAST_INTO_R(ta, other, 0); 00139 00140 LVecBase4f new_frame(max(ta->_frame[0], _frame[0]), 00141 min(ta->_frame[1], _frame[1]), 00142 max(ta->_frame[2], _frame[2]), 00143 min(ta->_frame[3], _frame[3])); 00144 00145 ScissorAttrib *attrib = new ScissorAttrib(new_frame); 00146 return return_new(attrib); 00147 } 00148 00149 //////////////////////////////////////////////////////////////////// 00150 // Function: ScissorAttrib::register_with_read_factory 00151 // Access: Public, Static 00152 // Description: Tells the BamReader how to create objects of type 00153 // ScissorAttrib. 00154 //////////////////////////////////////////////////////////////////// 00155 void ScissorAttrib:: 00156 register_with_read_factory() { 00157 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: ScissorAttrib::write_datagram 00162 // Access: Public, Virtual 00163 // Description: Writes the contents of this object to the datagram 00164 // for shipping out to a Bam file. 00165 //////////////////////////////////////////////////////////////////// 00166 void ScissorAttrib:: 00167 write_datagram(BamWriter *manager, Datagram &dg) { 00168 RenderAttrib::write_datagram(manager, dg); 00169 00170 _frame.write_datagram(dg); 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: ScissorAttrib::make_from_bam 00175 // Access: Protected, Static 00176 // Description: This function is called by the BamReader's factory 00177 // when a new object of type ScissorAttrib is encountered 00178 // in the Bam file. It should create the ScissorAttrib 00179 // and extract its information from the file. 00180 //////////////////////////////////////////////////////////////////// 00181 TypedWritable *ScissorAttrib:: 00182 make_from_bam(const FactoryParams ¶ms) { 00183 ScissorAttrib *attrib = new ScissorAttrib(LVecBase4f(0.0f, 1.0f, 0.0f, 1.0f)); 00184 DatagramIterator scan; 00185 BamReader *manager; 00186 00187 parse_params(params, scan, manager); 00188 attrib->fillin(scan, manager); 00189 00190 return attrib; 00191 } 00192 00193 //////////////////////////////////////////////////////////////////// 00194 // Function: ScissorAttrib::fillin 00195 // Access: Protected 00196 // Description: This internal function is called by make_from_bam to 00197 // read in all of the relevant data from the BamFile for 00198 // the new ScissorAttrib. 00199 //////////////////////////////////////////////////////////////////// 00200 void ScissorAttrib:: 00201 fillin(DatagramIterator &scan, BamReader *manager) { 00202 RenderAttrib::fillin(scan, manager); 00203 00204 _frame.read_datagram(scan); 00205 }