Panda3D
 All Classes Functions Variables Enumerations
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   const LMatrix4 &proj_inv_mat = do_get_projection_mat_inv(lens_cdata);
00073 
00074   near_point = (v * do_get_near(lens_cdata)) * proj_inv_mat * lens_mat;
00075   far_point = (v * do_get_far(lens_cdata)) * proj_inv_mat * lens_mat;
00076   return true;
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: CylindricalLens::do_extrude_vec
00081 //       Access: Protected, Virtual
00082 //  Description: Given a 2-d point in the range (-1,1) in both
00083 //               dimensions, where (0,0) is the center of the
00084 //               lens and (-1,-1) is the lower-left corner,
00085 //               compute the vector that corresponds to the view
00086 //               direction.  This will be parallel to the normal on
00087 //               the surface (the far plane) corresponding to the lens
00088 //               shape at this point.
00089 //
00090 //               See the comment block on Lens::extrude_vec_impl() for
00091 //               a more in-depth comment on the meaning of this
00092 //               vector.
00093 //
00094 //               The z coordinate of the 2-d point is ignored.
00095 //
00096 //               Returns true if the vector is defined, or false
00097 //               otherwise.
00098 ////////////////////////////////////////////////////////////////////
00099 bool CylindricalLens::
00100 do_extrude_vec(const Lens::CData *lens_cdata, const LPoint3 &point2d, LVector3 &vec) const {
00101   // Undo the shifting from film offsets, etc.  This puts the point
00102   // into the range [-film_size/2, film_size/2] in x and y.
00103   LPoint3 f = point2d * do_get_film_mat_inv(lens_cdata);
00104 
00105   PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
00106   PN_stdfloat angle = f[0] * cylindrical_k / focal_length;
00107   PN_stdfloat sinAngle, cosAngle;
00108   csincos(deg_2_rad(angle), &sinAngle, &cosAngle);
00109 
00110   vec = LVector3(sinAngle, cosAngle, 0.0f) * do_get_projection_mat_inv(lens_cdata) * do_get_lens_mat(lens_cdata);
00111 
00112   return true;
00113 }
00114 
00115 ////////////////////////////////////////////////////////////////////
00116 //     Function: CylindricalLens::do_project
00117 //       Access: Protected, Virtual
00118 //  Description: Given a 3-d point in space, determine the 2-d point
00119 //               this maps to, in the range (-1,1) in both dimensions,
00120 //               where (0,0) is the center of the lens and
00121 //               (-1,-1) is the lower-left corner.
00122 //
00123 //               Some lens types also set the z coordinate of the 2-d
00124 //               point to a value in the range (-1, 1), where 1
00125 //               represents a point on the near plane, and -1
00126 //               represents a point on the far plane.
00127 //
00128 //               Returns true if the 3-d point is in front of the lens
00129 //               and within the viewing frustum (in which case point2d
00130 //               is filled in), or false otherwise.
00131 ////////////////////////////////////////////////////////////////////
00132 bool CylindricalLens::
00133 do_project(const Lens::CData *lens_cdata, const LPoint3 &point3d, LPoint3 &point2d) const {
00134   // First, account for any rotations, etc. on the lens.
00135   LPoint3 p = point3d * do_get_lens_mat_inv(lens_cdata) * do_get_projection_mat(lens_cdata);
00136 
00137   // To compute the x position on the frame, we only need to consider
00138   // the angle of the vector about the Z axis.  Project the vector
00139   // into the XY plane to do this.
00140   LVector2 xy(p[0], p[1]);
00141 
00142   // The perspective distance is the length of this vector in the XY
00143   // plane.
00144   PN_stdfloat pdist = xy.length();
00145   if (pdist == 0.0f) {
00146     point2d.set(0.0f, 0.0f, 0.0f);
00147     return false;
00148   }
00149 
00150   PN_stdfloat focal_length = do_get_focal_length(lens_cdata);
00151 
00152   point2d.set
00153     (
00154      // The x position is the angle about the Z axis.
00155      rad_2_deg(catan2(xy[0], xy[1])) * focal_length / cylindrical_k,
00156      // The y position is the Z height divided by the perspective
00157      // distance.
00158      p[2] * focal_length / pdist,
00159      // Z is the perspective distance scaled into the range (1, -1).
00160      (do_get_near(lens_cdata) - pdist) / (do_get_far(lens_cdata) - do_get_near(lens_cdata))
00161      );
00162 
00163   // Now we have to transform the point according to the film
00164   // adjustments.
00165   point2d = point2d * do_get_film_mat(lens_cdata);
00166 
00167   return
00168     point2d[0] >= -1.0f && point2d[0] <= 1.0f && 
00169     point2d[1] >= -1.0f && point2d[1] <= 1.0f;
00170 }
00171 
00172 ////////////////////////////////////////////////////////////////////
00173 //     Function: CylindricalLens::fov_to_film
00174 //       Access: Protected, Virtual
00175 //  Description: Given a field of view in degrees and a focal length,
00176 //               compute the correspdonding width (or height) on the
00177 //               film.  If horiz is true, this is in the horizontal
00178 //               direction; otherwise, it is in the vertical direction
00179 //               (some lenses behave differently in each direction).
00180 ////////////////////////////////////////////////////////////////////
00181 PN_stdfloat CylindricalLens::
00182 fov_to_film(PN_stdfloat fov, PN_stdfloat focal_length, bool horiz) const {
00183   if (horiz) {
00184     return focal_length * fov / cylindrical_k;
00185   } else {
00186     return (ctan(deg_2_rad(fov * 0.5f)) * focal_length) * 2.0f;
00187   }
00188 }
00189 
00190 ////////////////////////////////////////////////////////////////////
00191 //     Function: CylindricalLens::fov_to_focal_length
00192 //       Access: Protected, Virtual
00193 //  Description: Given a field of view in degrees and a width (or
00194 //               height) on the film, compute the focal length of the
00195 //               lens.  If horiz is true, this is in the horizontal
00196 //               direction; otherwise, it is in the vertical direction
00197 //               (some lenses behave differently in each direction).
00198 ////////////////////////////////////////////////////////////////////
00199 PN_stdfloat CylindricalLens::
00200 fov_to_focal_length(PN_stdfloat fov, PN_stdfloat film_size, bool horiz) const {
00201   if (horiz) {
00202     return film_size * cylindrical_k / fov;
00203   } else {
00204     return film_size * 0.5f / ctan(deg_2_rad(fov * 0.5f));
00205   }
00206 }
00207 
00208 ////////////////////////////////////////////////////////////////////
00209 //     Function: CylindricalLens::film_to_fov
00210 //       Access: Protected, Virtual
00211 //  Description: Given a width (or height) on the film and a focal
00212 //               length, compute the field of view in degrees.  If
00213 //               horiz is true, this is in the horizontal direction;
00214 //               otherwise, it is in the vertical direction (some
00215 //               lenses behave differently in each direction).
00216 ////////////////////////////////////////////////////////////////////
00217 PN_stdfloat CylindricalLens::
00218 film_to_fov(PN_stdfloat film_size, PN_stdfloat focal_length, bool horiz) const {
00219   if (horiz) {
00220     return film_size * cylindrical_k / focal_length;
00221   } else {
00222     return rad_2_deg(catan(film_size * 0.5f / focal_length)) * 2.0f;
00223   }
00224 }
 All Classes Functions Variables Enumerations