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