Panda3D

cylindricalLens.cxx

00001 // Filename: cylindricalLens.cxx
00002 // Created by:  drose (12Dec01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "cylindricalLens.h"
00016 #include "deg_2_rad.h"
00017 
00018 TypeHandle CylindricalLens::_type_handle;
00019 
00020 // This is the focal-length constant for fisheye lenses.  See
00021 // fisheyeLens.cxx.
00022 static const PN_stdfloat cylindrical_k = 60.0f;
00023 // focal_length = film_size * cylindrical_k / fov;
00024 
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: CylindricalLens::make_copy
00028 //       Access: Public, Virtual
00029 //  Description: Allocates a new Lens just like this one.
00030 ////////////////////////////////////////////////////////////////////
00031 PT(Lens) CylindricalLens::
00032 make_copy() const {
00033   return new CylindricalLens(*this);
00034 }
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: CylindricalLens::do_extrude
00038 //       Access: Protected, Virtual
00039 //  Description: Given a 2-d point in the range (-1,1) in both
00040 //               dimensions, where (0,0) is the center of the
00041 //               lens and (-1,-1) is the lower-left corner,
00042 //               compute the corresponding vector in space that maps
00043 //               to this point, if such a vector can be determined.
00044 //               The vector is returned by indicating the points on
00045 //               the near plane and far plane that both map to the
00046 //               indicated 2-d point.
00047 //
00048 //               The z coordinate of the 2-d point is ignored.
00049 //
00050 //               Returns true if the vector is defined, or false
00051 //               otherwise.
00052 ////////////////////////////////////////////////////////////////////
00053 bool CylindricalLens::
00054 do_extrude(const Lens::CData *lens_cdata, 
00055            const LPoint3 &point2d, LPoint3 &near_point, LPoint3 &far_point) const {
00056   // Undo the shifting from film offsets, etc.  This puts the point
00057   // into the range [-film_size/2, film_size/2] in x and y.
00058   LPoint3 f = point2d * do_get_film_mat_inv(lens_cdata);
00059 
00060   PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
00061   PN_stdfloat angle = f[0] * cylindrical_k / focal_length;
00062   PN_stdfloat sinAngle, cosAngle;
00063   csincos(deg_2_rad(angle), &sinAngle, &cosAngle);
00064 
00065   // Define a unit vector (well, a unit vector in the XY plane, at
00066   // least) that represents the vector corresponding to this point.
00067   LPoint3 v(sinAngle, cosAngle, f[1] / focal_length);
00068 
00069   // And we'll need to account for the lens's rotations, etc. at the
00070   // end of the day.
00071   const LMatrix4 &lens_mat = do_get_lens_mat(lens_cdata);
00072 
00073   near_point = (v * do_get_near(lens_cdata)) * lens_mat;
00074   far_point = (v * do_get_far(lens_cdata)) * lens_mat;
00075   return true;
00076 }
00077 
00078 ////////////////////////////////////////////////////////////////////
00079 //     Function: CylindricalLens::do_extrude_vec
00080 //       Access: Protected, Virtual
00081 //  Description: Given a 2-d point in the range (-1,1) in both
00082 //               dimensions, where (0,0) is the center of the
00083 //               lens and (-1,-1) is the lower-left corner,
00084 //               compute the vector that corresponds to the view
00085 //               direction.  This will be parallel to the normal on
00086 //               the surface (the far plane) corresponding to the lens
00087 //               shape at this point.
00088 //
00089 //               See the comment block on Lens::extrude_vec_impl() for
00090 //               a more in-depth comment on the meaning of this
00091 //               vector.
00092 //
00093 //               The z coordinate of the 2-d point is ignored.
00094 //
00095 //               Returns true if the vector is defined, or false
00096 //               otherwise.
00097 ////////////////////////////////////////////////////////////////////
00098 bool CylindricalLens::
00099 do_extrude_vec(const Lens::CData *lens_cdata, const LPoint3 &point2d, LVector3 &vec) const {
00100   // Undo the shifting from film offsets, etc.  This puts the point
00101   // into the range [-film_size/2, film_size/2] in x and y.
00102   LPoint3 f = point2d * do_get_film_mat_inv(lens_cdata);
00103 
00104   PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
00105   PN_stdfloat angle = f[0] * cylindrical_k / focal_length;
00106   PN_stdfloat sinAngle, cosAngle;
00107   csincos(deg_2_rad(angle), &sinAngle, &cosAngle);
00108 
00109   vec = LVector3(sinAngle, cosAngle, 0.0f) * do_get_lens_mat(lens_cdata);
00110 
00111   return true;
00112 }
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: CylindricalLens::do_project
00116 //       Access: Protected, Virtual
00117 //  Description: Given a 3-d point in space, determine the 2-d point
00118 //               this maps to, in the range (-1,1) in both dimensions,
00119 //               where (0,0) is the center of the lens and
00120 //               (-1,-1) is the lower-left corner.
00121 //
00122 //               Some lens types also set the z coordinate of the 2-d
00123 //               point to a value in the range (-1, 1), where 1
00124 //               represents a point on the near plane, and -1
00125 //               represents a point on the far plane.
00126 //
00127 //               Returns true if the 3-d point is in front of the lens
00128 //               and within the viewing frustum (in which case point2d
00129 //               is filled in), or false otherwise.
00130 ////////////////////////////////////////////////////////////////////
00131 bool CylindricalLens::
00132 do_project(const Lens::CData *lens_cdata, const LPoint3 &point3d, LPoint3 &point2d) const {
00133   // First, account for any rotations, etc. on the lens.
00134   LPoint3 p = point3d * do_get_lens_mat_inv(lens_cdata);
00135 
00136   // To compute the x position on the frame, we only need to consider
00137   // the angle of the vector about the Z axis.  Project the vector
00138   // into the XY plane to do this.
00139   LVector2 xy(p[0], p[1]);
00140 
00141   // The perspective distance is the length of this vector in the XY
00142   // plane.
00143   PN_stdfloat pdist = xy.length();
00144   if (pdist == 0.0f) {
00145     point2d.set(0.0f, 0.0f, 0.0f);
00146     return false;
00147   }
00148 
00149   PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
00150 
00151   point2d.set
00152     (
00153      // The x position is the angle about the Z axis.
00154      rad_2_deg(catan2(xy[0], xy[1])) * focal_length / cylindrical_k,
00155      // The y position is the Z height divided by the perspective
00156      // distance.
00157      p[2] * focal_length / pdist,
00158      // Z is the perspective distance scaled into the range (1, -1).
00159      (do_get_near(lens_cdata) - pdist) / (do_get_far(lens_cdata) - do_get_near(lens_cdata))
00160      );
00161 
00162   // Now we have to transform the point according to the film
00163   // adjustments.
00164   point2d = point2d * do_get_film_mat(lens_cdata);
00165 
00166   return
00167     point2d[0] >= -1.0f && point2d[0] <= 1.0f && 
00168     point2d[1] >= -1.0f && point2d[1] <= 1.0f;
00169 }
00170 
00171 ////////////////////////////////////////////////////////////////////
00172 //     Function: CylindricalLens::fov_to_film
00173 //       Access: Protected, Virtual
00174 //  Description: Given a field of view in degrees and a focal length,
00175 //               compute the correspdonding width (or height) on the
00176 //               film.  If horiz is true, this is in the horizontal
00177 //               direction; otherwise, it is in the vertical direction
00178 //               (some lenses behave differently in each direction).
00179 ////////////////////////////////////////////////////////////////////
00180 PN_stdfloat CylindricalLens::
00181 fov_to_film(PN_stdfloat fov, PN_stdfloat focal_length, bool horiz) const {
00182   if (horiz) {
00183     return focal_length * fov / cylindrical_k;
00184   } else {
00185     return (ctan(deg_2_rad(fov * 0.5f)) * focal_length) * 2.0f;
00186   }
00187 }
00188 
00189 ////////////////////////////////////////////////////////////////////
00190 //     Function: CylindricalLens::fov_to_focal_length
00191 //       Access: Protected, Virtual
00192 //  Description: Given a field of view in degrees and a width (or
00193 //               height) on the film, compute the focal length of the
00194 //               lens.  If horiz is true, this is in the horizontal
00195 //               direction; otherwise, it is in the vertical direction
00196 //               (some lenses behave differently in each direction).
00197 ////////////////////////////////////////////////////////////////////
00198 PN_stdfloat CylindricalLens::
00199 fov_to_focal_length(PN_stdfloat fov, PN_stdfloat film_size, bool horiz) const {
00200   if (horiz) {
00201     return film_size * cylindrical_k / fov;
00202   } else {
00203     return film_size * 0.5f / ctan(deg_2_rad(fov * 0.5f));
00204   }
00205 }
00206 
00207 ////////////////////////////////////////////////////////////////////
00208 //     Function: CylindricalLens::film_to_fov
00209 //       Access: Protected, Virtual
00210 //  Description: Given a width (or height) on the film and a focal
00211 //               length, compute the field of view in degrees.  If
00212 //               horiz is true, this is in the horizontal direction;
00213 //               otherwise, it is in the vertical direction (some
00214 //               lenses behave differently in each direction).
00215 ////////////////////////////////////////////////////////////////////
00216 PN_stdfloat CylindricalLens::
00217 film_to_fov(PN_stdfloat film_size, PN_stdfloat focal_length, bool horiz) const {
00218   if (horiz) {
00219     return film_size * cylindrical_k / focal_length;
00220   } else {
00221     return rad_2_deg(catan(film_size * 0.5f / focal_length)) * 2.0f;
00222   }
00223 }
 All Classes Functions Variables Enumerations