Panda3D
lensNode.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 lensNode.cxx
10  * @author drose
11  * @date 2002-02-26
12  */
13 
14 #include "lensNode.h"
16 #include "bamWriter.h"
17 #include "bamReader.h"
18 #include "datagram.h"
19 #include "datagramIterator.h"
20 #include "perspectiveLens.h"
21 #include "geomNode.h"
22 
23 TypeHandle LensNode::_type_handle;
24 
25 /**
26  *
27  */
28 LensNode::
29 LensNode(const std::string &name, Lens *lens) :
30  PandaNode(name)
31 {
32  if (lens == nullptr) {
33  lens = new PerspectiveLens;
34  }
35  set_lens(0, lens);
36 }
37 
38 /**
39  *
40  */
41 LensNode::
42 LensNode(const LensNode &copy) :
43  PandaNode(copy),
44  _lenses(copy._lenses)
45 {
46 }
47 
48 /**
49  * Transforms the contents of this PandaNode by the indicated matrix, if it
50  * means anything to do so. For most kinds of PandaNodes, this does nothing.
51  */
52 void LensNode::
53 xform(const LMatrix4 &mat) {
54  PandaNode::xform(mat);
55  // We need to actually transform the lens here.
56 }
57 
58 /**
59  * Returns a newly-allocated Node that is a shallow copy of this one. It will
60  * be a different Node pointer, but its internal data may or may not be shared
61  * with that of the original Node.
62  */
64 make_copy() const {
65  return new LensNode(*this);
66 }
67 
68 /**
69  * Sets the indicated lens. Although a LensNode normally holds only one lens,
70  * it may optionally include multiple lenses, each with a different index
71  * number. The different lenses may be referenced by index number on the
72  * DisplayRegion. Adding a new lens automatically makes it active.
73  */
74 void LensNode::
75 set_lens(int index, Lens *lens) {
76  nassertv(index >= 0 && index < max_lenses); // Sanity check
77 
78  while (index >= (int)_lenses.size()) {
79  LensSlot slot;
80  slot._is_active = false;
81  _lenses.push_back(slot);
82  }
83 
84  _lenses[index]._lens = lens;
85  _lenses[index]._is_active = true;
86 
87  if (_shown_frustum != nullptr) {
88  show_frustum();
89  }
90 }
91 
92 /**
93  * Sets the active flag for the nth lens. When a lens is inactive, it is not
94  * used for rendering, and any DisplayRegions associated with it are
95  * implicitly inactive as well. Returns true if the flag is changed, false if
96  * it already had this value.
97  */
98 bool LensNode::
99 set_lens_active(int index, bool flag) {
100  nassertr(index >= 0 && index < max_lenses, false);
101 
102  while (index >= (int)_lenses.size()) {
103  LensSlot slot;
104  slot._is_active = false;
105  _lenses.push_back(slot);
106  }
107 
108  if (_lenses[index]._is_active == flag) {
109  return false;
110  }
111 
112  _lenses[index]._is_active = flag;
113 
114  if (_shown_frustum != nullptr) {
115  show_frustum();
116  }
117  return true;
118 }
119 
120 /**
121  * Returns true if the given point is within the bounds of the lens of the
122  * LensNode (i.e. if the camera can see the point).
123  */
124 bool LensNode::
125 is_in_view(int index, const LPoint3 &pos) {
126  Lens *lens = get_lens(index);
127  nassertr(lens != nullptr, false);
128  PT(BoundingVolume) bv = lens->make_bounds();
129  if (bv == nullptr) {
130  return false;
131  }
133  int ret = gbv->contains(pos);
134  return (ret != 0);
135 }
136 
137 /**
138  * Enables the drawing of the lens's frustum to aid in visualization. This
139  * actually creates a GeomNode which is parented to the LensNode.
140  */
141 void LensNode::
143  if (_shown_frustum != nullptr) {
144  hide_frustum();
145  }
146  PT(GeomNode) geom_node = new GeomNode("frustum");
147  _shown_frustum = geom_node;
148  add_child(_shown_frustum);
149 
150  for (Lenses::const_iterator li = _lenses.begin();
151  li != _lenses.end();
152  ++li) {
153  if ((*li)._is_active && (*li)._lens != nullptr) {
154  geom_node->add_geom((*li)._lens->make_geometry());
155  }
156  }
157 }
158 
159 /**
160  * Disables the drawing of the lens's frustum to aid in visualization.
161  */
162 void LensNode::
164  if (_shown_frustum != nullptr) {
165  remove_child(_shown_frustum);
166  _shown_frustum = nullptr;
167  }
168 }
169 
170 /**
171  *
172  */
173 void LensNode::
174 output(std::ostream &out) const {
175  PandaNode::output(out);
176 
177  out << " (";
178  for (Lenses::const_iterator li = _lenses.begin();
179  li != _lenses.end();
180  ++li) {
181  if ((*li)._is_active && (*li)._lens != nullptr) {
182  out << " ";
183  (*li)._lens->output(out);
184  }
185  }
186  out << " )";
187 }
188 
189 /**
190  *
191  */
192 void LensNode::
193 write(std::ostream &out, int indent_level) const {
194  PandaNode::write(out, indent_level);
195 
196  for (Lenses::const_iterator li = _lenses.begin();
197  li != _lenses.end();
198  ++li) {
199  if ((*li)._is_active && (*li)._lens != nullptr) {
200  (*li)._lens->write(out, indent_level + 2);
201  }
202  }
203 }
204 
205 /**
206  * Tells the BamReader how to create objects of type LensNode.
207  */
208 void LensNode::
210  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
211 }
212 
213 /**
214  * Writes the contents of this object to the datagram for shipping out to a
215  * Bam file.
216  */
217 void LensNode::
219  PandaNode::write_datagram(manager, dg);
220 
221  if (manager->get_file_minor_ver() < 41) {
222  // Prior to bam 6.41, we stored only one lens.
223  manager->write_pointer(dg, get_lens(0));
224  } else {
225  dg.add_uint16(_lenses.size());
226 
227  Lenses::const_iterator li;
228  for (li = _lenses.begin(); li != _lenses.end(); ++li) {
229  manager->write_pointer(dg, (*li)._lens);
230  dg.add_bool((*li)._is_active);
231  }
232  }
233 }
234 
235 /**
236  * Receives an array of pointers, one for each time manager->read_pointer()
237  * was called in fillin(). Returns the number of pointers processed.
238  */
239 int LensNode::
241  int pi = PandaNode::complete_pointers(p_list, manager);
242 
243  Lenses::iterator li;
244  for (li = _lenses.begin(); li != _lenses.end(); ++li) {
245  (*li)._lens = DCAST(Lens, p_list[pi++]);
246  }
247 
248  if (_shown_frustum != nullptr) {
249  show_frustum();
250  }
251 
252  return pi;
253 }
254 
255 /**
256  * This function is called by the BamReader's factory when a new object of
257  * type LensNode is encountered in the Bam file. It should create the
258  * LensNode and extract its information from the file.
259  */
260 TypedWritable *LensNode::
261 make_from_bam(const FactoryParams &params) {
262  LensNode *node = new LensNode("");
263  DatagramIterator scan;
264  BamReader *manager;
265 
266  parse_params(params, scan, manager);
267  node->fillin(scan, manager);
268 
269  return node;
270 }
271 
272 /**
273  * This internal function is called by make_from_bam to read in all of the
274  * relevant data from the BamFile for the new LensNode.
275  */
276 void LensNode::
277 fillin(DatagramIterator &scan, BamReader *manager) {
278  PandaNode::fillin(scan, manager);
279 
280  if (manager->get_file_minor_ver() < 41) {
281  // Prior to bam 6.41, we stored only one lens.
282  _lenses.resize(1);
283  manager->read_pointer(scan);
284 
285  } else {
286  _lenses.resize(scan.get_uint16());
287  Lenses::iterator li;
288  for (li = _lenses.begin(); li != _lenses.end(); ++li) {
289  manager->read_pointer(scan);
290  (*li)._is_active = scan.get_bool();
291  }
292  }
293 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
void remove_child(int child_index, Thread *current_thread=Thread::get_current_thread())
Removes the nth child from the node.
Definition: pandaNode.cxx:570
bool get_bool()
Extracts a boolean value.
bool is_in_view(const LPoint3 &pos)
Returns true if the given point is within the bounds of the lens of the LensNode (i....
Definition: lensNode.I:90
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:41
int contains(const GeometricBoundingVolume *vol) const
Returns the appropriate set of IntersectionFlags to indicate the amount of intersection with the indi...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: pandaNode.cxx:3589
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
void set_lens(Lens *lens)
Sets up the LensNode using this particular Lens pointer.
Definition: lensNode.I:37
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A node that contains a Lens.
Definition: lensNode.h:29
void show_frustum()
Enables the drawing of the lens's frustum to aid in visualization.
Definition: lensNode.cxx:142
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
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
Lens * get_lens(int index=0) const
Returns a pointer to the particular Lens associated with this LensNode, or NULL if there is not yet a...
Definition: lensNode.I:47
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so.
Definition: pandaNode.cxx:310
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
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_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:34
static void register_with_read_factory()
Tells the BamReader how to create objects of type LensNode.
Definition: lensNode.cxx:209
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
A perspective-type lens: a normal camera.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
void hide_frustum()
Disables the drawing of the lens's frustum to aid in visualization.
Definition: lensNode.cxx:163
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so.
Definition: lensNode.cxx:53
bool set_lens_active(int index, bool active)
Sets the active flag for the nth lens.
Definition: lensNode.cxx:99
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: lensNode.cxx:64
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: lensNode.cxx:218
A class to retrieve the individual data elements previously stored in a Datagram.
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
Definition: lensNode.cxx:240
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
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317