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