Panda3D
Loading...
Searching...
No Matches
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
23TypeHandle LensNode::_type_handle;
24
25/**
26 *
27 */
28LensNode::
29LensNode(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 */
41LensNode::
42LensNode(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 */
53xform(const LMatrix4 &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 */
64make_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 */
75set_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) {
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 */
99set_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). The point is assumed to
123 * be relative to the LensNode itself.
124 */
126is_in_view(int index, const LPoint3 &pos) {
127 Lens *lens = get_lens(index);
128 nassertr(lens != nullptr, false);
129 PT(BoundingVolume) bv = lens->make_bounds();
130 if (bv == nullptr) {
131 return false;
132 }
134 int ret = gbv->contains(pos);
135 return (ret != 0);
136}
137
138/**
139 * Enables the drawing of the lens's frustum to aid in visualization. This
140 * actually creates a GeomNode which is parented to the LensNode.
141 */
143show_frustum() {
144 if (_shown_frustum != nullptr) {
145 hide_frustum();
146 }
147 PT(GeomNode) geom_node = new GeomNode("frustum");
148 _shown_frustum = geom_node;
149 add_child(_shown_frustum);
150
151 for (Lenses::const_iterator li = _lenses.begin();
152 li != _lenses.end();
153 ++li) {
154 if ((*li)._is_active && (*li)._lens != nullptr) {
155 geom_node->add_geom((*li)._lens->make_geometry());
156 }
157 }
158}
159
160/**
161 * Disables the drawing of the lens's frustum to aid in visualization.
162 */
164hide_frustum() {
165 if (_shown_frustum != nullptr) {
166 remove_child(_shown_frustum);
167 _shown_frustum = nullptr;
168 }
169}
170
171/**
172 *
173 */
174void LensNode::
175output(std::ostream &out) const {
176 PandaNode::output(out);
177
178 out << " (";
179 for (Lenses::const_iterator li = _lenses.begin();
180 li != _lenses.end();
181 ++li) {
182 if ((*li)._is_active && (*li)._lens != nullptr) {
183 out << " ";
184 (*li)._lens->output(out);
185 }
186 }
187 out << " )";
188}
189
190/**
191 *
192 */
193void LensNode::
194write(std::ostream &out, int indent_level) const {
195 PandaNode::write(out, indent_level);
196
197 for (Lenses::const_iterator li = _lenses.begin();
198 li != _lenses.end();
199 ++li) {
200 if ((*li)._is_active && (*li)._lens != nullptr) {
201 (*li)._lens->write(out, indent_level + 2);
202 }
203 }
204}
205
206/**
207 * Tells the BamReader how to create objects of type LensNode.
208 */
211 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
212}
213
214/**
215 * Writes the contents of this object to the datagram for shipping out to a
216 * Bam file.
217 */
219write_datagram(BamWriter *manager, Datagram &dg) {
220 PandaNode::write_datagram(manager, dg);
221
222 if (manager->get_file_minor_ver() < 41) {
223 // Prior to bam 6.41, we stored only one lens.
224 manager->write_pointer(dg, get_lens(0));
225 } else {
226 dg.add_uint16(_lenses.size());
227
228 Lenses::const_iterator li;
229 for (li = _lenses.begin(); li != _lenses.end(); ++li) {
230 manager->write_pointer(dg, (*li)._lens);
231 dg.add_bool((*li)._is_active);
232 }
233 }
234}
235
236/**
237 * Receives an array of pointers, one for each time manager->read_pointer()
238 * was called in fillin(). Returns the number of pointers processed.
239 */
241complete_pointers(TypedWritable **p_list, BamReader *manager) {
242 int pi = PandaNode::complete_pointers(p_list, manager);
243
244 Lenses::iterator li;
245 for (li = _lenses.begin(); li != _lenses.end(); ++li) {
246 (*li)._lens = DCAST(Lens, p_list[pi++]);
247 }
248
249 if (_shown_frustum != nullptr) {
250 show_frustum();
251 }
252
253 return pi;
254}
255
256/**
257 * This function is called by the BamReader's factory when a new object of
258 * type LensNode is encountered in the Bam file. It should create the
259 * LensNode and extract its information from the file.
260 */
261TypedWritable *LensNode::
262make_from_bam(const FactoryParams &params) {
263 LensNode *node = new LensNode("");
264 DatagramIterator scan;
265 BamReader *manager;
266
267 parse_params(params, scan, manager);
268 node->fillin(scan, manager);
269
270 return node;
271}
272
273/**
274 * This internal function is called by make_from_bam to read in all of the
275 * relevant data from the BamFile for the new LensNode.
276 */
277void LensNode::
278fillin(DatagramIterator &scan, BamReader *manager) {
279 PandaNode::fillin(scan, manager);
280
281 if (manager->get_file_minor_ver() < 41) {
282 // Prior to bam 6.41, we stored only one lens.
283 _lenses.resize(1);
284 manager->read_pointer(scan);
285
286 } else {
287 _lenses.resize(scan.get_uint16());
288 Lenses::iterator li;
289 for (li = _lenses.begin(); li != _lenses.end(); ++li) {
290 manager->read_pointer(scan);
291 (*li)._is_active = scan.get_bool();
292 }
293 }
294}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition bamReader.I:83
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition bamWriter.I:59
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A class to retrieve the individual data elements previously stored in a Datagram.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
bool get_bool()
Extracts a boolean value.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition datagram.I:85
An instance of this class is passed to the Factory when requesting it to do its business and construc...
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
A node that holds Geom objects, renderable pieces of geometry.
Definition geomNode.h:34
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
int contains(const GeometricBoundingVolume *vol) const
Returns the appropriate set of IntersectionFlags to indicate the amount of intersection with the indi...
A node that contains a Lens.
Definition lensNode.h:29
void hide_frustum()
Disables the drawing of the lens's frustum to aid in visualization.
Definition lensNode.cxx:164
void set_lens(Lens *lens)
Sets up the LensNode using this particular Lens pointer.
Definition lensNode.I:37
void show_frustum()
Enables the drawing of the lens's frustum to aid in visualization.
Definition lensNode.cxx:143
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:241
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type LensNode.
Definition lensNode.cxx:210
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
bool set_lens_active(int index, bool active)
Sets the active flag for the nth lens.
Definition lensNode.cxx:99
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:219
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
A base class for any number of different kinds of lenses, linear and otherwise.
Definition lens.h:41
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
void remove_child(int child_index, Thread *current_thread=Thread::get_current_thread())
Removes the nth child from the node.
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this PandaNode by the indicated matrix, if it means anything to do so.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A perspective-type lens: a normal camera.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
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().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.