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