Panda3D
oSphereLens.cxx
1 // Filename: oSphereLens.cxx
2 // Created by: drose (25Feb11)
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 "oSphereLens.h"
16 #include "deg_2_rad.h"
17 
18 TypeHandle OSphereLens::_type_handle;
19 
20 // This is the focal-length constant for fisheye lenses. See
21 // fisheyeLens.cxx.
22 static const PN_stdfloat ospherical_k = 60.0f;
23 // focal_length = film_size * ospherical_k / fov;
24 
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: OSphereLens::make_copy
28 // Access: Public, Virtual
29 // Description: Allocates a new Lens just like this one.
30 ////////////////////////////////////////////////////////////////////
31 PT(Lens) OSphereLens::
32 make_copy() const {
33  return new OSphereLens(*this);
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: OSphereLens::do_extrude
38 // Access: Protected, Virtual
39 // Description: Given a 2-d point in the range (-1,1) in both
40 // dimensions, where (0,0) is the center of the
41 // lens and (-1,-1) is the lower-left corner,
42 // compute the corresponding vector in space that maps
43 // to this point, if such a vector can be determined.
44 // The vector is returned by indicating the points on
45 // the near plane and far plane that both map to the
46 // indicated 2-d point.
47 //
48 // The z coordinate of the 2-d point is ignored.
49 //
50 // Returns true if the vector is defined, or false
51 // otherwise.
52 ////////////////////////////////////////////////////////////////////
53 bool OSphereLens::
54 do_extrude(const Lens::CData *lens_cdata,
55  const LPoint3 &point2d, LPoint3 &near_point, LPoint3 &far_point) const {
56  // Undo the shifting from film offsets, etc. This puts the point
57  // into the range [-film_size/2, film_size/2] in x and y.
58  LPoint3 f = point2d * do_get_film_mat_inv(lens_cdata);
59 
60  PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
61  PN_stdfloat angle = f[0] * cylindrical_k / focal_length;
62  PN_stdfloat sinAngle, cosAngle;
63  csincos(deg_2_rad(angle), &sinAngle, &cosAngle);
64 
65  // Define a unit vector that represents the vector corresponding to
66  // this point.
67  LPoint3 v(sinAngle, cosAngle, 0.0f);
68 
69  near_point = (v * do_get_near(lens_cdata));
70  far_point = (v * do_get_far(lens_cdata));
71  near_point[2] = f[1];
72  far_point[2] = f[1];
73 
74  // And we'll need to account for the lens's rotations, etc. at the
75  // end of the day.
76  const LMatrix4 &lens_mat = do_get_lens_mat(lens_cdata);
77  const LMatrix4 &proj_inv_mat = do_get_projection_mat_inv(lens_cdata);
78 
79  near_point = near_point * proj_inv_mat * lens_mat;
80  far_point = far_point * proj_inv_mat * lens_mat;
81  return true;
82 }
83 
84 ////////////////////////////////////////////////////////////////////
85 // Function: OSphereLens::do_project
86 // Access: Protected, Virtual
87 // Description: Given a 3-d point in space, determine the 2-d point
88 // this maps to, in the range (-1,1) in both dimensions,
89 // where (0,0) is the center of the lens and
90 // (-1,-1) is the lower-left corner.
91 //
92 // Some lens types also set the z coordinate of the 2-d
93 // point to a value in the range (-1, 1), where -1
94 // represents a point on the near plane, and 1
95 // represents a point on the far plane.
96 //
97 // Returns true if the 3-d point is in front of the lens
98 // and within the viewing frustum (in which case point2d
99 // is filled in), or false otherwise.
100 ////////////////////////////////////////////////////////////////////
101 bool OSphereLens::
102 do_project(const Lens::CData *lens_cdata, const LPoint3 &point3d, LPoint3 &point2d) const {
103  // First, account for any rotations, etc. on the lens.
104  LPoint3 p = point3d * do_get_lens_mat_inv(lens_cdata) * do_get_projection_mat(lens_cdata);
105 
106  // To compute the x position on the frame, we only need to consider
107  // the angle of the vector about the Z axis. Project the vector
108  // into the XY plane to do this.
109  LVector2 xy(p[0], p[1]);
110 
111  PN_stdfloat dist = xy.length();
112  if (dist == 0.0f) {
113  point2d.set(0.0f, 0.0f, 0.0f);
114  return false;
115  }
116 
117  PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
118  // Compute the depth as a linear distance in the range 0 .. 1.
119  PN_stdfloat z = (dist - do_get_near(lens_cdata)) / (do_get_far(lens_cdata) - do_get_near(lens_cdata));
120 
121  point2d.set
122  (
123  // The x position is the angle about the Z axis.
124  rad_2_deg(catan2(xy[0], xy[1])) * focal_length / ospherical_k,
125  // The y position is the Z height.
126  p[2],
127  // Z is the distance scaled into the range -1 .. 1.
128  2.0 * z - 1.0
129  );
130 
131  // Now we have to transform the point according to the film
132  // adjustments.
133  point2d = point2d * do_get_film_mat(lens_cdata);
134 
135  return
136  point2d[0] >= -1.0f && point2d[0] <= 1.0f &&
137  point2d[1] >= -1.0f && point2d[1] <= 1.0f;
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: OSphereLens::fov_to_film
142 // Access: Protected, Virtual
143 // Description: Given a field of view in degrees and a focal length,
144 // compute the correspdonding width (or height) on the
145 // film. If horiz is true, this is in the horizontal
146 // direction; otherwise, it is in the vertical direction
147 // (some lenses behave differently in each direction).
148 ////////////////////////////////////////////////////////////////////
149 PN_stdfloat OSphereLens::
150 fov_to_film(PN_stdfloat fov, PN_stdfloat focal_length, bool) const {
151  return focal_length * fov / ospherical_k;
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: OSphereLens::fov_to_focal_length
156 // Access: Protected, Virtual
157 // Description: Given a field of view in degrees and a width (or
158 // height) on the film, compute the focal length of the
159 // lens. If horiz is true, this is in the horizontal
160 // direction; otherwise, it is in the vertical direction
161 // (some lenses behave differently in each direction).
162 ////////////////////////////////////////////////////////////////////
163 PN_stdfloat OSphereLens::
164 fov_to_focal_length(PN_stdfloat fov, PN_stdfloat film_size, bool) const {
165  return film_size * ospherical_k / fov;
166 }
167 
168 ////////////////////////////////////////////////////////////////////
169 // Function: OSphereLens::film_to_fov
170 // Access: Protected, Virtual
171 // Description: Given a width (or height) on the film and a focal
172 // length, compute the field of view in degrees. If
173 // horiz is true, this is in the horizontal direction;
174 // otherwise, it is in the vertical direction (some
175 // lenses behave differently in each direction).
176 ////////////////////////////////////////////////////////////////////
177 PN_stdfloat OSphereLens::
178 film_to_fov(PN_stdfloat film_size, PN_stdfloat focal_length, bool) const {
179  return film_size * ospherical_k / focal_length;
180 }
A base class for any number of different kinds of lenses, linear and otherwise.
Definition: lens.h:45
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
A OSphereLens is a special nonlinear lens that doesn&#39;t correspond to any real physical lenses...
Definition: oSphereLens.h:34
This is a two-component vector offset.
Definition: lvector2.h:91
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85