Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/distort/pSphereLens.cxx

Go to the documentation of this file.
00001 // Filename: pSphereLens.cxx
00002 // Created by:  drose (12Dec01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "pSphereLens.h"
00020 #include "deg_2_rad.h"
00021 
00022 TypeHandle PSphereLens::_type_handle;
00023 
00024 // This is the focal-length constant for fisheye lenses.  See
00025 // fisheyeLens.cxx.
00026 static const float spherical_k = 60.0f;
00027 // focal_length = film_size * spherical_k / fov;
00028 
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: PSphereLens::make_copy
00032 //       Access: Public, Virtual
00033 //  Description: Allocates a new Lens just like this one.
00034 ////////////////////////////////////////////////////////////////////
00035 PT(Lens) PSphereLens::
00036 make_copy() const {
00037   return new PSphereLens(*this);
00038 }
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: PSphereLens::extrude_impl
00042 //       Access: Protected, Virtual
00043 //  Description: Given a 2-d point in the range (-1,1) in both
00044 //               dimensions, where (0,0) is the center of the
00045 //               lens and (-1,-1) is the lower-left corner,
00046 //               compute the corresponding vector in space that maps
00047 //               to this point, if such a vector can be determined.
00048 //               The vector is returned by indicating the points on
00049 //               the near plane and far plane that both map to the
00050 //               indicated 2-d point.
00051 //
00052 //               The z coordinate of the 2-d point is ignored.
00053 //
00054 //               Returns true if the vector is defined, or false
00055 //               otherwise.
00056 ////////////////////////////////////////////////////////////////////
00057 bool PSphereLens::
00058 extrude_impl(const LPoint3f &point2d, LPoint3f &near_point, LPoint3f &far_point) const {
00059   // Undo the shifting from film offsets, etc.  This puts the point
00060   // into the range [-film_size/2, film_size/2] in x and y.
00061   LPoint3f f = point2d * get_film_mat_inv();
00062 
00063   float focal_length = get_focal_length();
00064 
00065   // Rotate the forward vector through the rotation angles
00066   // corresponding to this point.
00067   LPoint3f v = LPoint3f(0.0f, 1.0f, 0.0f) *
00068     LMatrix3f::rotate_mat(f[1] * spherical_k / focal_length, LVector3f(1.0f, 0.0f, 0.0f)) *
00069     LMatrix3f::rotate_mat(f[0] * spherical_k / focal_length, LVector3f(0.0f, 0.0f, -1.0f));
00070 
00071   // And we'll need to account for the lens's rotations, etc. at the
00072   // end of the day.
00073   const LMatrix4f &lens_mat = get_lens_mat();
00074 
00075   near_point = (v * get_near()) * lens_mat;
00076   far_point = (v * get_far()) * lens_mat;
00077   return true;
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: PSphereLens::project_impl
00082 //       Access: Protected, Virtual
00083 //  Description: Given a 3-d point in space, determine the 2-d point
00084 //               this maps to, in the range (-1,1) in both dimensions,
00085 //               where (0,0) is the center of the lens and
00086 //               (-1,-1) is the lower-left corner.
00087 //
00088 //               Some lens types also set the z coordinate of the 2-d
00089 //               point to a value in the range (-1, 1), where 1
00090 //               represents a point on the near plane, and -1
00091 //               represents a point on the far plane.
00092 //
00093 //               Returns true if the 3-d point is in front of the lens
00094 //               and within the viewing frustum (in which case point2d
00095 //               is filled in), or false otherwise.
00096 ////////////////////////////////////////////////////////////////////
00097 bool PSphereLens::
00098 project_impl(const LPoint3f &point3d, LPoint3f &point2d) const {
00099   // First, account for any rotations, etc. on the lens.
00100   LVector3f v3 = point3d * get_lens_mat_inv();
00101   float dist = v3.length();
00102   if (dist == 0.0f) {
00103     point2d.set(0.0f, 0.0f, 0.0f);
00104     return false;
00105   }
00106 
00107   v3 /= dist;
00108 
00109   float focal_length = get_focal_length();
00110 
00111   // To compute the x position on the frame, we only need to consider
00112   // the angle of the vector about the Z axis.  Project the vector
00113   // into the XY plane to do this.
00114   LVector2f xy(v3[0], v3[1]);
00115 
00116   // Unroll the Z angle, and the y position is the angle about the X
00117   // axis.
00118   xy.normalize();
00119   LVector2d yz(v3[0]*xy[0] + v3[1]*xy[1], v3[2]);
00120 
00121   point2d.set
00122     (
00123      // The x position is the angle about the Z axis.
00124      rad_2_deg(catan2(xy[0], xy[1])) * focal_length / spherical_k,
00125      // The y position is the angle about the X axis.
00126      rad_2_deg(catan2(yz[1], yz[0])) * focal_length / spherical_k,
00127      // Z is the distance scaled into the range (1, -1).
00128      (get_near() - dist) / (get_far() - get_near())
00129      );
00130 
00131   // Now we have to transform the point according to the film
00132   // adjustments.
00133   point2d = point2d * get_film_mat();
00134 
00135   return
00136     point2d[0] >= -1.0f && point2d[0] <= 1.0f && 
00137     point2d[1] >= -1.0f && point2d[1] <= 1.0f;
00138 }
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //     Function: PSphereLens::fov_to_film
00142 //       Access: Protected, Virtual
00143 //  Description: Given a field of view in degrees and a focal length,
00144 //               compute the correspdonding width (or height) on the
00145 //               film.  If horiz is true, this is in the horizontal
00146 //               direction; otherwise, it is in the vertical direction
00147 //               (some lenses behave differently in each direction).
00148 ////////////////////////////////////////////////////////////////////
00149 float PSphereLens::
00150 fov_to_film(float fov, float focal_length, bool) const {
00151   return focal_length * fov / spherical_k;
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 //     Function: PSphereLens::fov_to_focal_length
00156 //       Access: Protected, Virtual
00157 //  Description: Given a field of view in degrees and a width (or
00158 //               height) on the film, compute the focal length of the
00159 //               lens.  If horiz is true, this is in the horizontal
00160 //               direction; otherwise, it is in the vertical direction
00161 //               (some lenses behave differently in each direction).
00162 ////////////////////////////////////////////////////////////////////
00163 float PSphereLens::
00164 fov_to_focal_length(float fov, float film_size, bool) const {
00165   return film_size * spherical_k / fov;
00166 }
00167 
00168 ////////////////////////////////////////////////////////////////////
00169 //     Function: PSphereLens::film_to_fov
00170 //       Access: Protected, Virtual
00171 //  Description: Given a width (or height) on the film and a focal
00172 //               length, compute the field of view in degrees.  If
00173 //               horiz is true, this is in the horizontal direction;
00174 //               otherwise, it is in the vertical direction (some
00175 //               lenses behave differently in each direction).
00176 ////////////////////////////////////////////////////////////////////
00177 float PSphereLens::
00178 film_to_fov(float film_size, float focal_length, bool) const {
00179   return film_size * spherical_k / focal_length;
00180 }

Generated on Fri May 2 00:36:41 2003 for Panda by doxygen1.3