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