Panda3D
scissorAttrib.cxx
1 // Filename: scissorAttrib.cxx
2 // Created by: drose (29Jul08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "scissorAttrib.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "dcast.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 
23 TypeHandle ScissorAttrib::_type_handle;
24 int ScissorAttrib::_attrib_slot;
25 CPT(RenderAttrib) ScissorAttrib::_off_attrib;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: ScissorAttrib::Constructor
29 // Access: Private
30 // Description: Use ScissorAttrib::make() to construct a new
31 // ScissorAttrib object.
32 ////////////////////////////////////////////////////////////////////
33 ScissorAttrib::
34 ScissorAttrib(const LVecBase4 &frame) :
35  _frame(frame),
36  _off(false)
37 {
38  // Impose sensible bounds.
39  _frame[0] = max(min(_frame[0], (PN_stdfloat)1.0), (PN_stdfloat)0.0);
40  _frame[1] = max(min(_frame[1], (PN_stdfloat)1.0), _frame[0]);
41  _frame[2] = max(min(_frame[2], (PN_stdfloat)1.0), (PN_stdfloat)0.0);
42  _frame[3] = max(min(_frame[3], (PN_stdfloat)1.0), _frame[2]);
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: ScissorAttrib::make_off
47 // Access: Published, Static
48 // Description: Constructs a new ScissorAttrib object that removes
49 // the scissor region and fills the DisplayRegion.
50 ////////////////////////////////////////////////////////////////////
51 CPT(RenderAttrib) ScissorAttrib::
52 make_off() {
53  if (_off_attrib != NULL) {
54  return _off_attrib;
55  }
56  ScissorAttrib *attrib = new ScissorAttrib(LVecBase4(0.0f, 1.0f, 0.0f, 1.0f));
57  attrib->_off = true;
58  _off_attrib = return_new(attrib);
59  return _off_attrib;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: ScissorAttrib::make
64 // Access: Published, Static
65 // Description: Constructs a ScissorAttrib that restricts rendering
66 // to the indicated frame within the current
67 // DisplayRegion. (0,0) is the lower-left corner of the
68 // DisplayRegion, and (1,1) is the upper-right corner.
69 ////////////////////////////////////////////////////////////////////
70 CPT(RenderAttrib) ScissorAttrib::
71 make(const LVecBase4 &frame) {
72  ScissorAttrib *attrib = new ScissorAttrib(frame);
73  return return_new(attrib);
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: ScissorAttrib::make_default
78 // Access: Published, Static
79 // Description: Returns a RenderAttrib that corresponds to whatever
80 // the standard default properties for render attributes
81 // of this type ought to be.
82 ////////////////////////////////////////////////////////////////////
83 CPT(RenderAttrib) ScissorAttrib::
84 make_default() {
85  return make_off();
86  //return return_new(new ScissorAttrib(LVecBase4(0.0f, 1.0f, 0.0f, 1.0f)));
87 }
88 
89 ////////////////////////////////////////////////////////////////////
90 // Function: ScissorAttrib::output
91 // Access: Public, Virtual
92 // Description:
93 ////////////////////////////////////////////////////////////////////
94 void ScissorAttrib::
95 output(ostream &out) const {
96  out << get_type() << ":[" << _frame << "]";
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: ScissorAttrib::compare_to_impl
101 // Access: Protected, Virtual
102 // Description: Intended to be overridden by derived ScissorAttrib
103 // types to return a unique number indicating whether
104 // this ScissorAttrib is equivalent to the other one.
105 //
106 // This should return 0 if the two ScissorAttrib objects
107 // are equivalent, a number less than zero if this one
108 // should be sorted before the other one, and a number
109 // greater than zero otherwise.
110 //
111 // This will only be called with two ScissorAttrib
112 // objects whose get_type() functions return the same.
113 ////////////////////////////////////////////////////////////////////
114 int ScissorAttrib::
115 compare_to_impl(const RenderAttrib *other) const {
116  const ScissorAttrib *ta;
117  DCAST_INTO_R(ta, other, 0);
118 
119  if (!_off && !ta->_off) {
120  return 0;
121  }
122 
123  if (_off && !ta->_off) {
124  return -1;
125  }
126 
127  if (!_off && ta->_off) {
128  return 1;
129  }
130 
131  return _frame.compare_to(ta->_frame);
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: ScissorAttrib::get_hash_impl
136 // Access: Protected, Virtual
137 // Description: Intended to be overridden by derived RenderAttrib
138 // types to return a unique hash for these particular
139 // properties. RenderAttribs that compare the same with
140 // compare_to_impl(), above, should return the same
141 // hash; RenderAttribs that compare differently should
142 // return a different hash.
143 ////////////////////////////////////////////////////////////////////
144 size_t ScissorAttrib::
145 get_hash_impl() const {
146  size_t hash = 0;
147  if (!_off) {
148  hash = _frame.add_hash(hash);
149  }
150  return hash;
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: ScissorAttrib::compose_impl
155 // Access: Protected, Virtual
156 // Description: Intended to be overridden by derived RenderAttrib
157 // types to specify how two consecutive RenderAttrib
158 // objects of the same type interact.
159 //
160 // This should return the result of applying the other
161 // RenderAttrib to a node in the scene graph below this
162 // RenderAttrib, which was already applied. In most
163 // cases, the result is the same as the other
164 // RenderAttrib (that is, a subsequent RenderAttrib
165 // completely replaces the preceding one). On the other
166 // hand, some kinds of RenderAttrib (for instance,
167 // ColorTransformAttrib) might combine in meaningful
168 // ways.
169 ////////////////////////////////////////////////////////////////////
170 CPT(RenderAttrib) ScissorAttrib::
171 compose_impl(const RenderAttrib *other) const {
172  if (_off) {
173  return other;
174  }
175 
176  const ScissorAttrib *ta;
177  DCAST_INTO_R(ta, other, 0);
178 
179  if (ta->_off) {
180  return this;
181  }
182 
183  LVecBase4 new_frame(max(ta->_frame[0], _frame[0]),
184  min(ta->_frame[1], _frame[1]),
185  max(ta->_frame[2], _frame[2]),
186  min(ta->_frame[3], _frame[3]));
187 
188  ScissorAttrib *attrib = new ScissorAttrib(new_frame);
189  return return_new(attrib);
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function: ScissorAttrib::register_with_read_factory
194 // Access: Public, Static
195 // Description: Tells the BamReader how to create objects of type
196 // ScissorAttrib.
197 ////////////////////////////////////////////////////////////////////
198 void ScissorAttrib::
199 register_with_read_factory() {
200  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
201 }
202 
203 ////////////////////////////////////////////////////////////////////
204 // Function: ScissorAttrib::write_datagram
205 // Access: Public, Virtual
206 // Description: Writes the contents of this object to the datagram
207 // for shipping out to a Bam file.
208 ////////////////////////////////////////////////////////////////////
209 void ScissorAttrib::
211  RenderAttrib::write_datagram(manager, dg);
212 
213  _frame.write_datagram(dg);
214  dg.add_bool(_off);
215 }
216 
217 ////////////////////////////////////////////////////////////////////
218 // Function: ScissorAttrib::make_from_bam
219 // Access: Protected, Static
220 // Description: This function is called by the BamReader's factory
221 // when a new object of type ScissorAttrib is encountered
222 // in the Bam file. It should create the ScissorAttrib
223 // and extract its information from the file.
224 ////////////////////////////////////////////////////////////////////
225 TypedWritable *ScissorAttrib::
226 make_from_bam(const FactoryParams &params) {
227  ScissorAttrib *attrib = new ScissorAttrib(LVecBase4(0.0f, 1.0f, 0.0f, 1.0f));
228  DatagramIterator scan;
229  BamReader *manager;
230 
231  parse_params(params, scan, manager);
232  attrib->fillin(scan, manager);
233 
234  return attrib;
235 }
236 
237 ////////////////////////////////////////////////////////////////////
238 // Function: ScissorAttrib::fillin
239 // Access: Protected
240 // Description: This internal function is called by make_from_bam to
241 // read in all of the relevant data from the BamFile for
242 // the new ScissorAttrib.
243 ////////////////////////////////////////////////////////////////////
244 void ScissorAttrib::
245 fillin(DatagramIterator &scan, BamReader *manager) {
246  RenderAttrib::fillin(scan, manager);
247 
248  _frame.read_datagram(scan);
249  _off = false;
250 
251  if (manager->get_file_minor_ver() >= 34) {
252  _off = scan.get_bool();
253  }
254 }
bool get_bool()
Extracts a boolean value.
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
size_t add_hash(size_t hash) const
Adds the vector into the running hash.
Definition: lvecBase4.h:1026
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:105
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:118
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
int compare_to(const LVecBase4f &other) const
This flavor of compare_to uses a default threshold value based on the numeric type.
Definition: lvecBase4.h:988
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void read_datagram(DatagramIterator &source)
Reads the vector from the Datagram using get_stdfloat().
Definition: lvecBase4.h:1458
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
void write_datagram(Datagram &destination) const
Writes the vector to the Datagram using add_stdfloat().
Definition: lvecBase4.h:1438
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
This restricts rendering to within a rectangular region of the scene, without otherwise affecting the...
Definition: scissorAttrib.h:41
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43