Panda3D
lens.h
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 lens.h
10  * @author drose
11  * @date 1999-02-18
12  */
13 
14 #ifndef LENS_H
15 #define LENS_H
16 
17 #include "pandabase.h"
18 
20 #include "luse.h"
21 #include "geom.h"
22 #include "updateSeq.h"
23 #include "geomVertexData.h"
24 #include "pointerTo.h"
25 #include "cycleData.h"
26 #include "cycleDataReader.h"
27 #include "cycleDataWriter.h"
28 #include "pipelineCycler.h"
29 
30 class BoundingVolume;
31 
32 /**
33  * A base class for any number of different kinds of lenses, linear and
34  * otherwise. Presently, this includes perspective and orthographic lenses.
35  *
36  * A Lens object is the main part of a Camera node, which defines the
37  * fundamental interface to point-of-view for rendering. Lenses are also used
38  * in other contexts, however; for instance, a Spotlight is also defined using
39  * a lens.
40  */
41 class EXPCL_PANDA_GOBJ Lens : public TypedWritableReferenceCount {
42 public:
43  Lens();
44  Lens(const Lens &copy);
45  void operator = (const Lens &copy);
46 
47 PUBLISHED:
48  enum StereoChannel {
49  SC_mono = 0x00,
50  SC_left = 0x01,
51  SC_right = 0x02,
52  SC_stereo = 0x03, // == SC_left | SC_right
53  };
54 
55  virtual PT(Lens) make_copy() const=0;
56 
57  INLINE bool extrude(const LPoint2 &point2d,
58  LPoint3 &near_point, LPoint3 &far_point) const;
59  INLINE bool extrude(const LPoint3 &point2d,
60  LPoint3 &near_point, LPoint3 &far_point) const;
61  INLINE bool extrude_depth(const LPoint3 &point2d, LPoint3 &point3d) const;
62  INLINE bool extrude_vec(const LPoint2 &point2d, LVector3 &vec3d) const;
63  INLINE bool extrude_vec(const LPoint3 &point2d, LVector3 &vec3d) const;
64  INLINE bool project(const LPoint3 &point3d, LPoint3 &point2d) const;
65  INLINE bool project(const LPoint3 &point3d, LPoint2 &point2d) const;
66 
67  INLINE void set_change_event(const std::string &event);
68  INLINE const std::string &get_change_event() const;
69  MAKE_PROPERTY(change_event, get_change_event, set_change_event);
70 
71  void set_coordinate_system(CoordinateSystem cs);
72  INLINE CoordinateSystem get_coordinate_system() const;
73  MAKE_PROPERTY(coordinate_system, get_coordinate_system,
74  set_coordinate_system);
75 
76  void clear();
77 
78  INLINE void set_film_size(PN_stdfloat width);
79  INLINE void set_film_size(PN_stdfloat width, PN_stdfloat height);
80  INLINE void set_film_size(const LVecBase2 &film_size);
81  INLINE const LVecBase2 &get_film_size() const;
82  MAKE_PROPERTY(film_size, get_film_size, set_film_size);
83 
84  INLINE void set_film_offset(PN_stdfloat x, PN_stdfloat y);
85  INLINE void set_film_offset(const LVecBase2 &film_offset);
86  INLINE const LVector2 &get_film_offset() const;
87  MAKE_PROPERTY(film_offset, get_film_offset, set_film_offset);
88 
89  INLINE void set_focal_length(PN_stdfloat focal_length);
90  INLINE PN_stdfloat get_focal_length() const;
91  MAKE_PROPERTY(focal_length, get_focal_length, set_focal_length);
92 
93  void set_min_fov(PN_stdfloat min_fov);
94  INLINE void set_fov(PN_stdfloat fov);
95  INLINE void set_fov(PN_stdfloat hfov, PN_stdfloat vfov);
96  INLINE void set_fov(const LVecBase2 &fov);
97  INLINE const LVecBase2 &get_fov() const;
98  INLINE PN_stdfloat get_hfov() const;
99  INLINE PN_stdfloat get_vfov() const;
100  PN_stdfloat get_min_fov() const;
101  MAKE_PROPERTY(fov, get_fov, set_fov);
102  MAKE_PROPERTY(min_fov, get_min_fov, set_min_fov);
103 
104  INLINE void set_aspect_ratio(PN_stdfloat aspect_ratio);
105  INLINE PN_stdfloat get_aspect_ratio() const;
106  MAKE_PROPERTY(aspect_ratio, get_aspect_ratio, set_aspect_ratio);
107 
108  INLINE void set_near(PN_stdfloat near_distance);
109  INLINE PN_stdfloat get_near() const;
110  INLINE void set_far(PN_stdfloat far_distance);
111  INLINE PN_stdfloat get_far() const;
112  INLINE void set_near_far(PN_stdfloat near_distance, PN_stdfloat far_distance);
113  MAKE_PROPERTY(near, get_near, set_near);
114  MAKE_PROPERTY(far, get_far, set_far);
115 
116  static PN_stdfloat get_default_near();
117  static PN_stdfloat get_default_far();
118 
119  INLINE void set_view_hpr(PN_stdfloat h, PN_stdfloat p, PN_stdfloat r);
120  void set_view_hpr(const LVecBase3 &view_hpr);
121  const LVecBase3 &get_view_hpr() const;
122  MAKE_PROPERTY(view_hpr, get_view_hpr, set_view_hpr);
123 
124  INLINE void set_view_vector(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z, PN_stdfloat i, PN_stdfloat j, PN_stdfloat k);
125  void set_view_vector(const LVector3 &view_vector, const LVector3 &up_vector);
126  const LVector3 &get_view_vector() const;
127  const LVector3 &get_up_vector() const;
128  LPoint3 get_nodal_point() const;
129  MAKE_PROPERTY(nodal_point, get_nodal_point);
130 
131  INLINE void set_interocular_distance(PN_stdfloat interocular_distance);
132  INLINE PN_stdfloat get_interocular_distance() const;
133  INLINE void set_convergence_distance(PN_stdfloat convergence_distance);
134  INLINE PN_stdfloat get_convergence_distance() const;
135  MAKE_PROPERTY(interocular_distance, get_interocular_distance, set_interocular_distance);
136  MAKE_PROPERTY(convergence_distance, get_convergence_distance, set_convergence_distance);
137 
138  INLINE void set_view_mat(const LMatrix4 &view_mat);
139  INLINE const LMatrix4 &get_view_mat() const;
140  void clear_view_mat();
141  MAKE_PROPERTY(view_mat, get_view_mat, set_view_mat);
142 
143  void set_keystone(const LVecBase2 &keystone);
144  INLINE const LVecBase2 &get_keystone() const;
145  void clear_keystone();
146  MAKE_PROPERTY(keystone, get_keystone, set_keystone);
147 
148  void set_custom_film_mat(const LMatrix4 &custom_film_mat);
149  INLINE const LMatrix4 &get_custom_film_mat() const;
150  void clear_custom_film_mat();
151 
152  // These flags are passed in as the last parameter to control the behavior
153  // of set_frustum_from_corners(). See the documentation for that method for
154  // an explanation of each flag.
155  enum FromCorners {
156  FC_roll = 0x0001,
157  FC_camera_plane = 0x0002,
158  FC_off_axis = 0x0004,
159  FC_aspect_ratio = 0x0008,
160  FC_shear = 0x0010,
161  FC_keystone = 0x0020,
162  };
163  void set_frustum_from_corners(const LVecBase3 &ul, const LVecBase3 &ur,
164  const LVecBase3 &ll, const LVecBase3 &lr,
165  int flags);
166 
167  void recompute_all();
168 
169  virtual bool is_linear() const;
170  virtual bool is_perspective() const;
171  virtual bool is_orthographic() const;
172  virtual PT(Geom) make_geometry();
173 
174  virtual PT(BoundingVolume) make_bounds() const;
175 
176  INLINE const LMatrix4 &get_projection_mat(StereoChannel channel = SC_mono) const;
177  INLINE const LMatrix4 &get_projection_mat_inv(StereoChannel channel = SC_mono) const;
178 
179  INLINE const LMatrix4 &get_film_mat() const;
180  INLINE const LMatrix4 &get_film_mat_inv() const;
181 
182  INLINE const LMatrix4 &get_lens_mat() const;
183  INLINE const LMatrix4 &get_lens_mat_inv() const;
184 
185  virtual void output(std::ostream &out) const;
186  virtual void write(std::ostream &out, int indent_level = 0) const;
187 
188  INLINE UpdateSeq get_last_change() const;
189 
190 protected:
191  class CData;
192 
193  INLINE void do_adjust_user_flags(CData *cdata, int clear_flags, int set_flags);
194  INLINE void do_adjust_comp_flags(CData *cdata, int clear_flags, int set_flags);
195 
196  void do_set_film_size(CData *cdata, PN_stdfloat width);
197  void do_set_film_size(CData *cdata, const LVecBase2 &film_size);
198  const LVecBase2 &do_get_film_size(const CData *cdata) const;
199 
200  INLINE void do_set_film_offset(CData *cdata, const LVecBase2 &film_offset);
201  INLINE const LVector2 &do_get_film_offset(const CData *cdata) const;
202 
203  void do_set_focal_length(CData *cdata, PN_stdfloat focal_length);
204  PN_stdfloat do_get_focal_length(const CData *cdata) const;
205 
206  void do_set_fov(CData *cdata, PN_stdfloat fov);
207  void do_set_fov(CData *cdata, const LVecBase2 &fov);
208  const LVecBase2 &do_get_fov(const CData *cdata) const;
209 
210  void do_set_aspect_ratio(CData *cdata, PN_stdfloat aspect_ratio);
211  PN_stdfloat do_get_aspect_ratio(const CData *cdata) const;
212 
213  INLINE void do_set_near(CData *cdata, PN_stdfloat near_distance);
214  INLINE PN_stdfloat do_get_near(const CData *cdata) const;
215  INLINE void do_set_far(CData *cdata, PN_stdfloat far_distance);
216  INLINE PN_stdfloat do_get_far(const CData *cdata) const;
217  INLINE void do_set_near_far(CData *cdata, PN_stdfloat near_distance, PN_stdfloat far_distance);
218 
219  const LMatrix4 &do_get_projection_mat(const CData *cdata, StereoChannel channel = SC_mono) const;
220  const LMatrix4 &do_get_projection_mat_inv(const CData *cdata, StereoChannel channel = SC_mono) const;
221 
222  const LMatrix4 &do_get_film_mat(const CData *cdata) const;
223  const LMatrix4 &do_get_film_mat_inv(const CData *cdata) const;
224 
225  const LMatrix4 &do_get_lens_mat(const CData *cdata) const;
226  const LMatrix4 &do_get_lens_mat_inv(const CData *cdata) const;
227 
228  void do_set_interocular_distance(CData *cdata, PN_stdfloat interocular_distance);
229  void do_set_convergence_distance(CData *cdata, PN_stdfloat convergence_distance);
230 
231  void do_set_view_mat(CData *cdata, const LMatrix4 &view_mat);
232  const LMatrix4 &do_get_view_mat(const CData *cdata) const;
233 
234  void do_throw_change_event(CData *cdata);
235 
236  virtual bool do_extrude(const CData *cdata, const LPoint3 &point2d,
237  LPoint3 &near_point, LPoint3 &far_point) const;
238  virtual bool do_extrude_depth(const CData *cdata, const LPoint3 &point2d,
239  LPoint3 &point3d) const;
240  bool do_extrude_depth_with_mat(const CData *cdata, const LPoint3 &point2d,
241  LPoint3 &point3d) const;
242  virtual bool do_extrude_vec(const CData *cdata,
243  const LPoint3 &point2d, LVector3 &vec) const;
244  virtual bool do_project(const CData *cdata,
245  const LPoint3 &point3d, LPoint3 &point2d) const;
246 
247  virtual void do_compute_film_size(CData *cdata);
248  virtual void do_compute_focal_length(CData *cdata);
249  virtual void do_compute_fov(CData *cdata);
250  virtual void do_compute_aspect_ratio(CData *cdata);
251  virtual void do_compute_view_hpr(CData *cdata);
252  virtual void do_compute_view_vector(CData *cdata);
253  virtual void do_compute_projection_mat(CData *cdata);
254  virtual void do_compute_film_mat(CData *cdata);
255  virtual void do_compute_lens_mat(CData *cdata);
256 
257  virtual PN_stdfloat fov_to_film(PN_stdfloat fov, PN_stdfloat focal_length, bool horiz) const;
258  virtual PN_stdfloat fov_to_focal_length(PN_stdfloat fov, PN_stdfloat film_size, bool horiz) const;
259  virtual PN_stdfloat film_to_fov(PN_stdfloat film_size, PN_stdfloat focal_length, bool horiz) const;
260 
261 private:
262  void do_resequence_fov_triad(const CData *cdata,
263  char &newest, char &older_a, char &older_b) const;
264  int do_define_geom_data(CData *cdata);
265  static void build_shear_mat(LMatrix4 &shear_mat,
266  const LPoint3 &cul, const LPoint3 &cur,
267  const LPoint3 &cll, const LPoint3 &clr);
268  static PN_stdfloat sqr_dist_to_line(const LPoint3 &point, const LPoint3 &origin,
269  const LVector3 &vec);
270 
271 protected:
272  enum UserFlags {
273  // Parameters the user may have explicitly specified.
274  UF_film_width = 0x0001,
275  UF_film_height = 0x0002,
276  UF_focal_length = 0x0004,
277  UF_hfov = 0x0008,
278  UF_vfov = 0x0010,
279  UF_aspect_ratio = 0x0020,
280  UF_view_hpr = 0x0040,
281  UF_view_vector = 0x0080,
282  UF_interocular_distance = 0x0100,
283  UF_convergence_distance = 0x0200,
284  UF_view_mat = 0x0400,
285  UF_keystone = 0x0800,
286  UF_min_fov = 0x1000,
287  UF_custom_film_mat = 0x2000,
288  };
289 
290  enum CompFlags {
291  // Values that may need to be recomputed.
292  CF_film_mat = 0x0001,
293  CF_film_mat_inv = 0x0002,
294  CF_lens_mat = 0x0004,
295  CF_lens_mat_inv = 0x0008,
296  CF_projection_mat = 0x0010,
297  CF_projection_mat_inv = 0x0020,
298  CF_projection_mat_left_inv = 0x0040,
299  CF_projection_mat_right_inv = 0x0080,
300  CF_mat = 0x00ff, // all of the above.
301 
302  CF_film_size = 0x0100,
303  CF_aspect_ratio = 0x0200,
304  CF_view_hpr = 0x0400,
305  CF_view_vector = 0x0800,
306  CF_focal_length = 0x1000,
307  CF_fov = 0x2000,
308  };
309 
310  // This is the data that must be cycled between pipeline stages.
311  class EXPCL_PANDA_GOBJ CData : public CycleData {
312  public:
313  CData();
314  CData(const CData &copy);
315  ALLOC_DELETED_CHAIN(CData);
316  virtual CycleData *make_copy() const;
317  virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
318  virtual void fillin(DatagramIterator &scan, BamReader *manager);
319  virtual TypeHandle get_parent_type() const {
320  return Lens::get_class_type();
321  }
322 
323  void clear();
324 
325  std::string _change_event;
326  UpdateSeq _last_change;
327  CoordinateSystem _cs;
328 
329  LVecBase2 _film_size;
330  LVector2 _film_offset;
331  PN_stdfloat _focal_length;
332  LVecBase2 _fov;
333  PN_stdfloat _min_fov;
334  PN_stdfloat _aspect_ratio;
335  PN_stdfloat _near_distance, _far_distance;
336 
337  LVecBase3 _view_hpr;
338  LVector3 _view_vector, _up_vector;
339  PN_stdfloat _interocular_distance;
340  PN_stdfloat _convergence_distance;
341  LVecBase2 _keystone;
342  LMatrix4 _custom_film_mat;
343 
344  LMatrix4 _film_mat, _film_mat_inv;
345  LMatrix4 _lens_mat, _lens_mat_inv;
346  LMatrix4 _projection_mat, _projection_mat_inv;
347  LMatrix4 _projection_mat_left, _projection_mat_left_inv;
348  LMatrix4 _projection_mat_right, _projection_mat_right_inv;
349 
350  short _user_flags;
351  short _comp_flags;
352 
353  // The user may only specify two of these three parameters. Specifying
354  // the third parameter wipes out the first one specified. We therefore
355  // need to remember the order in which the user has specified these three
356  // parameters. A bit of a mess.
357  char _focal_length_seq, _fov_seq, _film_size_seq;
358 
359  PT(GeomVertexData) _geom_data;
360 
361  public:
362  static TypeHandle get_class_type() {
363  return _type_handle;
364  }
365  static void init_type() {
366  register_type(_type_handle, "Lens::CData");
367  }
368 
369  private:
370  static TypeHandle _type_handle;
371  };
372 
373  PipelineCycler<CData> _cycler;
374  typedef CycleDataReader<CData> CDReader;
375  typedef CycleDataWriter<CData> CDWriter;
376 
377 public:
378  virtual void write_datagram(BamWriter *manager, Datagram &dg);
379 
380 protected:
381  void fillin(DatagramIterator &scan, BamReader *manager);
382 
383 public:
384  virtual TypeHandle get_type() const {
385  return get_class_type();
386  }
387  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
388  static TypeHandle get_class_type() {
389  return _type_handle;
390  }
391  static void init_type() {
392  TypedWritableReferenceCount::init_type();
393  register_type(_type_handle, "Lens",
394  TypedWritableReferenceCount::get_class_type());
395  CData::init_type();
396  }
397 
398 private:
399  static TypeHandle _type_handle;
400 };
401 
402 EXPCL_PANDA_GOBJ INLINE std::ostream &operator << (std::ostream &out, const Lens &lens);
403 
404 #include "lens.I"
405 
406 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:47
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual TypeHandle get_parent_type() const
Returns the type of the container that owns the CycleData.
Definition: cycleData.cxx:76
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
virtual void write_datagram(BamWriter *, Datagram &) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: cycleData.cxx:32
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:54
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
Definition: cycleData.cxx:58
A class to retrieve the individual data elements previously stored in a Datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.