Panda3D
camera.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 camera.cxx
10  * @author drose
11  * @date 2002-02-26
12  */
13 
14 #include "pandabase.h"
15 #include "camera.h"
16 #include "lens.h"
17 #include "throw_event.h"
18 
19 using std::string;
20 
21 TypeHandle Camera::_type_handle;
22 
23 /**
24  *
25  */
26 Camera::
27 Camera(const string &name, Lens *lens) :
28  LensNode(name, lens),
29  _active(true),
30  _camera_mask(~PandaNode::get_overall_bit()),
31  _initial_state(RenderState::make_empty()),
32  _lod_scale(1)
33 {
34 }
35 
36 /**
37  *
38  */
39 Camera::
40 Camera(const Camera &copy) :
41  LensNode(copy),
42  _active(copy._active),
43  _scene(copy._scene),
44  _camera_mask(copy._camera_mask),
45  _initial_state(copy._initial_state),
46  _lod_scale(copy._lod_scale),
47  _tag_state_key(copy._tag_state_key),
48  _tag_states(copy._tag_states)
49 {
50 }
51 
52 /**
53  *
54  */
55 Camera::
56 ~Camera() {
57  // We don't have to destroy the display region(s) associated with the
58  // camera; they're responsible for themselves. However, they should have
59  // removed themselves before we destruct, or something went wrong.
60  nassertv(_display_regions.empty());
61 }
62 
63 /**
64  * Returns a newly-allocated Node that is a shallow copy of this one. It will
65  * be a different Node pointer, but its internal data may or may not be shared
66  * with that of the original Node.
67  */
69 make_copy() const {
70  return new Camera(*this);
71 }
72 
73 /**
74  * Returns true if it is generally safe to flatten out this particular kind of
75  * Node by duplicating instances, false otherwise (for instance, a Camera
76  * cannot be safely flattened, because the Camera pointer itself is
77  * meaningful).
78  */
79 bool Camera::
80 safe_to_flatten() const {
81  return false;
82 }
83 
84 /**
85  * Returns true if it is generally safe to transform this particular kind of
86  * Node by calling the xform() method, false otherwise. For instance, it's
87  * usually a bad idea to attempt to xform a Character.
88  */
89 bool Camera::
91  return false;
92 }
93 
94 /**
95  * Associates a particular state transition with the indicated tag value.
96  * When a node is encountered during traversal with the tag key specified by
97  * set_tag_state_key(), if the value of that tag matches tag_state, then the
98  * indicated state is applied to this node--but only when it is rendered by
99  * this camera.
100  *
101  * This can be used to apply special effects to nodes when they are rendered
102  * by certain cameras. It is particularly useful for multipass rendering, in
103  * which specialty cameras might be needed to render the scene with a
104  * particular set of effects.
105  */
106 void Camera::
107 set_tag_state(const string &tag_state, const RenderState *state) {
108  _tag_states[tag_state] = state;
109 }
110 
111 /**
112  * Removes the association established by a previous call to set_tag_state().
113  */
114 void Camera::
115 clear_tag_state(const string &tag_state) {
116  _tag_states.erase(tag_state);
117 }
118 
119 /**
120  * Removes all associations established by previous calls to set_tag_state().
121  */
122 void Camera::
124  _tag_states.clear();
125 }
126 
127 /**
128  * Returns true if set_tag_state() has previously been called with the
129  * indicated tag state, false otherwise.
130  */
131 bool Camera::
132 has_tag_state(const string &tag_state) const {
133  TagStates::const_iterator tsi;
134  tsi = _tag_states.find(tag_state);
135  return (tsi != _tag_states.end());
136 }
137 
138 /**
139  * Returns the state associated with the indicated tag state by a previous
140  * call to set_tag_state(), or the empty state if nothing has been associated.
141  */
142 CPT(RenderState) Camera::
143 get_tag_state(const string &tag_state) const {
144  TagStates::const_iterator tsi;
145  tsi = _tag_states.find(tag_state);
146  if (tsi != _tag_states.end()) {
147  return (*tsi).second;
148  }
149  return RenderState::make_empty();
150 }
151 
152 /**
153  * Associates the indicated AuxSceneData object with the given NodePath,
154  * possibly replacing a previous data defined for the same NodePath, if any.
155  */
156 void Camera::
157 set_aux_scene_data(const NodePath &node_path, AuxSceneData *data) {
158  if (data == nullptr) {
159  clear_aux_scene_data(node_path);
160  } else {
161  _aux_data[node_path] = data;
162  }
163 }
164 
165 /**
166  * Removes the AuxSceneData associated with the indicated NodePath. Returns
167  * true if it is removed successfully, false if it was already gone.
168  */
169 bool Camera::
170 clear_aux_scene_data(const NodePath &node_path) {
171  AuxData::iterator ai;
172  ai = _aux_data.find(node_path);
173  if (ai != _aux_data.end()) {
174  _aux_data.erase(ai);
175  return true;
176  }
177 
178  return false;
179 }
180 
181 /**
182  * Returns the AuxSceneData associated with the indicated NodePath, or NULL if
183  * nothing is associated.
184  */
186 get_aux_scene_data(const NodePath &node_path) const {
187  AuxData::const_iterator ai;
188  ai = _aux_data.find(node_path);
189  if (ai != _aux_data.end()) {
190  return (*ai).second;
191  }
192 
193  return nullptr;
194 }
195 
196 /**
197  * Outputs all of the NodePaths and AuxSceneDatas in use.
198  */
199 void Camera::
200 list_aux_scene_data(std::ostream &out) const {
201  out << _aux_data.size() << " data objects held:\n";
202  AuxData::const_iterator ai;
203  for (ai = _aux_data.begin(); ai != _aux_data.end(); ++ai) {
204  out << (*ai).first << " " << *(*ai).second << "\n";
205  }
206 }
207 
208 /**
209  * Walks through the list of currently-assigned AuxSceneData objects and
210  * releases any that are past their expiration times. Returns the number of
211  * elements released.
212  */
213 int Camera::
214 cleanup_aux_scene_data(Thread *current_thread) {
215  int num_deleted = 0;
216 
217  double now = ClockObject::get_global_clock()->get_frame_time(current_thread);
218 
219  AuxData::iterator ai;
220  ai = _aux_data.begin();
221  while (ai != _aux_data.end()) {
222  AuxData::iterator anext = ai;
223  ++anext;
224 
225  if (now > (*ai).second->get_expiration_time()) {
226  _aux_data.erase(ai);
227  num_deleted++;
228  }
229 
230  ai = anext;
231  }
232 
233  return num_deleted;
234 }
235 
236 /**
237  * Adds the indicated DisplayRegion to the set of DisplayRegions shared by the
238  * camera. This is only intended to be called from the DisplayRegion.
239  */
240 void Camera::
241 add_display_region(DisplayRegion *display_region) {
242  _display_regions.push_back(display_region);
243 }
244 
245 /**
246  * Removes the indicated DisplayRegion from the set of DisplayRegions shared
247  * by the camera. This is only intended to be called from the DisplayRegion.
248  */
249 void Camera::
250 remove_display_region(DisplayRegion *display_region) {
251  DisplayRegions::iterator dri =
252  find(_display_regions.begin(), _display_regions.end(), display_region);
253  if (dri != _display_regions.end()) {
254  _display_regions.erase(dri);
255  }
256 }
257 
258 /**
259  * Tells the BamReader how to create objects of type Camera.
260  */
261 void Camera::
263  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
264 }
265 
266 /**
267  * Writes the contents of this object to the datagram for shipping out to a
268  * Bam file.
269  */
270 void Camera::
272  LensNode::write_datagram(manager, dg);
273 
274  dg.add_bool(_active);
275  dg.add_uint32(_camera_mask.get_word());
276 
277  if (manager->get_file_minor_ver() >= 41) {
278  manager->write_pointer(dg, _initial_state);
279  dg.add_stdfloat(_lod_scale);
280  }
281 }
282 
283 /**
284  * Receives an array of pointers, one for each time manager->read_pointer()
285  * was called in fillin(). Returns the number of pointers processed.
286  */
287 int Camera::
289  int pi = LensNode::complete_pointers(p_list, manager);
290 
291  if (manager->get_file_minor_ver() >= 41) {
292  _initial_state = DCAST(RenderState, p_list[pi++]);
293  }
294  return pi;
295 }
296 
297 /**
298  * This function is called by the BamReader's factory when a new object of
299  * type Camera is encountered in the Bam file. It should create the Camera
300  * and extract its information from the file.
301  */
302 TypedWritable *Camera::
303 make_from_bam(const FactoryParams &params) {
304  Camera *node = new Camera("");
305  DatagramIterator scan;
306  BamReader *manager;
307 
308  parse_params(params, scan, manager);
309  node->fillin(scan, manager);
310 
311  return node;
312 }
313 
314 /**
315  * This internal function is called by make_from_bam to read in all of the
316  * relevant data from the BamFile for the new Camera.
317  */
318 void Camera::
319 fillin(DatagramIterator &scan, BamReader *manager) {
320  LensNode::fillin(scan, manager);
321 
322  _active = scan.get_bool();
323  _camera_mask.set_word(scan.get_uint32());
324 
325  if (manager->get_file_minor_ver() >= 41) {
326  manager->read_pointer(scan);
327  _lod_scale = scan.get_stdfloat();
328  }
329 }
This is a base class for a generic data structure that can be attached per- instance to the camera,...
Definition: auxSceneData.h:31
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
Definition: clockObject.I:215
void clear_tag_states()
Removes all associations established by previous calls to set_tag_state().
Definition: camera.cxx:123
int cleanup_aux_scene_data(Thread *current_thread=Thread::get_current_thread())
Walks through the list of currently-assigned AuxSceneData objects and releases any that are past thei...
Definition: camera.cxx:214
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
bool get_bool()
Extracts a boolean value.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:41
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
CPT(RenderState) Camera
Returns the state associated with the indicated tag state by a previous call to set_tag_state(),...
Definition: camera.cxx:142
static void register_with_read_factory()
Tells the BamReader how to create objects of type Camera.
Definition: camera.cxx:262
CPT(RenderState) get_tag_state(const std has_tag_state
Returns true if set_tag_state() has previously been called with the indicated tag state,...
Definition: camera.h:93
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A node that contains a Lens.
Definition: lensNode.h:29
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.
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
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: camera.cxx:69
void set_word(WordType value)
Sets the entire BitMask to the value indicated by the given word.
Definition: bitMask.I:255
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
CPT(RenderState) get_tag_state(const std set_tag_state
Associates a particular state transition with the indicated tag value.
Definition: camera.h:93
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: camera.cxx:288
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the datagram.
Definition: datagram.I:94
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition: datagram.I:133
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: camera.cxx:271
WordType get_word() const
Returns the entire BitMask as a single word.
Definition: bitMask.I:246
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
get_frame_time
Returns the time in seconds as of the last time tick() was called (typically, this will be as of the ...
Definition: clockObject.h:91
virtual bool safe_to_flatten() const
Returns true if it is generally safe to flatten out this particular kind of Node by duplicating insta...
Definition: camera.cxx:80
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
virtual bool safe_to_transform() const
Returns true if it is generally safe to transform this particular kind of Node by calling the xform()...
Definition: camera.cxx:90
uint32_t get_uint32()
Extracts an unsigned 32-bit integer.
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
get_aux_scene_data
Returns the AuxSceneData associated with the indicated NodePath, or NULL if nothing is associated.
Definition: camera.h:103
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
clear_aux_scene_data
Removes the AuxSceneData associated with the indicated NodePath.
Definition: camera.h:103
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
A thread; that is, a lightweight process.
Definition: thread.h:46
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
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 rectangular subregion within a window for rendering into.
Definition: displayRegion.h:57
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
A node that can be positioned around in the scene graph to represent a point of view for rendering a ...
Definition: camera.h:35
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
CPT(RenderState) get_tag_state(const std clear_tag_state
Removes the association established by a previous call to set_tag_state().
Definition: camera.h:93
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
void list_aux_scene_data(std::ostream &out) const
Outputs all of the NodePaths and AuxSceneDatas in use.
Definition: camera.cxx:200