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

panda/src/mathutil/frustum_src.I

Go to the documentation of this file.
00001 // Filename: frustum_src.I
00002 // Created by:  mike (09Jan97)
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 ////////////////////////////////////////////////////////////////////
00020 //     Function: Constructor
00021 //       Access:
00022 //  Description:
00023 ////////////////////////////////////////////////////////////////////
00024 INLINE_MATHUTIL FLOATNAME(Frustum)::
00025 FLOATNAME(Frustum)() {
00026   _fnear = FLOATCONST(1.4142);
00027   _ffar = FLOATCONST(10.0);
00028   _l = -1.0f;
00029   _r = 1.0f;
00030   _t = 1.0f;
00031   _b = -1.0f;
00032 }
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //     Function: make_ortho_2D
00036 //       Access:
00037 //  Description: Sets up a two-dimensional orthographic frustum
00038 ////////////////////////////////////////////////////////////////////
00039 INLINE_MATHUTIL void FLOATNAME(Frustum)::make_ortho_2D(void) {
00040   make_ortho(-1.0f, 1.0f);
00041 }
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 //     Function: make_ortho_2D
00045 //       Access:
00046 //  Description: Sets up a two-dimensional orthographic frustum
00047 ////////////////////////////////////////////////////////////////////
00048 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00049 make_ortho_2D(FLOATTYPE l, FLOATTYPE r, FLOATTYPE t, FLOATTYPE b) {
00050   make_ortho(-1.0f, 1.0f, l, r, t, b);
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: make_ortho_2D
00055 //       Access:
00056 //  Description: Behaves like gluOrtho
00057 ////////////////////////////////////////////////////////////////////
00058 INLINE_MATHUTIL void FLOATNAME(Frustum)::make_ortho(FLOATTYPE fnear, FLOATTYPE ffar) {
00059   _fnear = fnear;
00060   _ffar = ffar;
00061   _l = -1.0f;
00062   _r = 1.0f;
00063   _t = 1.0f;
00064   _b = -1.0f;
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: make_ortho_2D
00069 //       Access:
00070 //  Description: Behaves like gluOrtho
00071 ////////////////////////////////////////////////////////////////////
00072 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00073 make_ortho(FLOATTYPE fnear, FLOATTYPE ffar, FLOATTYPE l, FLOATTYPE r,
00074            FLOATTYPE t, FLOATTYPE b) {
00075   _fnear = fnear;
00076   _ffar = ffar;
00077   _l = l;
00078   _r = r;
00079   _t = t;
00080   _b = b;
00081 }
00082 
00083 ////////////////////////////////////////////////////////////////////
00084 //     Function: make_perspective
00085 //       Access:
00086 //  Description: Behaves like gluPerspective (Aspect = width/height,
00087 //               Yfov in degrees)
00088 //       aspect
00089 //   +------------+
00090 //   |            |
00091 // 1 |            | yfov
00092 //   |            |
00093 //   +------------+
00094 //
00095 //     -------+------
00096 //      \     |     /
00097 //       \    |    /
00098 //        \   |   /
00099 //         \  |  /
00100 //          \ | /
00101 //           \|/
00102 //            W yfov
00103 //
00104 ////////////////////////////////////////////////////////////////////
00105 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00106 make_perspective_hfov(FLOATTYPE hfov, FLOATTYPE aspect, FLOATTYPE fnear,
00107                       FLOATTYPE ffar) {
00108   _fnear = fnear;
00109   _ffar = ffar;
00110   _r = tan(deg_2_rad(hfov) * FLOATCONST(0.5)) * _fnear;
00111   _l = -_r;
00112   _t = _r / aspect;
00113   _b = -_t;
00114 }
00115 
00116 
00117 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00118 make_perspective_vfov(FLOATTYPE yfov, FLOATTYPE aspect, FLOATTYPE fnear,
00119                       FLOATTYPE ffar) {
00120   _fnear = fnear;
00121   _ffar = ffar;
00122   _t = tan(deg_2_rad(yfov) * 0.5f) * _fnear;
00123   _b = -_t;
00124   _r = _t * aspect;
00125   _l = -_r;
00126 }
00127 
00128 
00129 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00130 make_perspective(FLOATTYPE xfov, FLOATTYPE yfov, FLOATTYPE fnear,
00131                  FLOATTYPE ffar) {
00132   _fnear = fnear;
00133   _ffar = ffar;
00134   _t = tan(deg_2_rad(yfov) * 0.5f) * _fnear;
00135   _b = -_t;
00136   _r = tan(deg_2_rad(xfov) * 0.5f) * _fnear;
00137   _l = -_r;
00138 }
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //     Function: get_perspective_params
00142 //       Access:
00143 //  Description:
00144 ////////////////////////////////////////////////////////////////////
00145 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00146 get_perspective_params(FLOATTYPE& yfov, FLOATTYPE& aspect,
00147                        FLOATTYPE& fnear, FLOATTYPE& ffar) const {
00148   yfov = rad_2_deg(atan(_t / _fnear)) * 2.0f;
00149   aspect = _r / _t;
00150   fnear = _fnear;
00151   ffar = _ffar;
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 //     Function: get_perspective_params
00156 //       Access:
00157 //  Description:
00158 ////////////////////////////////////////////////////////////////////
00159 INLINE_MATHUTIL void FLOATNAME(Frustum)::
00160 get_perspective_params(FLOATTYPE& xfov, FLOATTYPE& yfov, FLOATTYPE& aspect,
00161                        FLOATTYPE& fnear, FLOATTYPE& ffar) const {
00162   xfov = rad_2_deg(atan(_r / _fnear)) * 2.0f;
00163   get_perspective_params(yfov, aspect, fnear, ffar);
00164 }
00165 
00166 ////////////////////////////////////////////////////////////////////
00167 //     Function: get_perspective_projection_mat
00168 //       Access: Public
00169 //  Description: This computes a transform matrix that performs the
00170 //               perspective transform defined by the frustum,
00171 //               accordinate to the indicated coordinate system.
00172 ////////////////////////////////////////////////////////////////////
00173 INLINE_MATHUTIL FLOATNAME(LMatrix4) FLOATNAME(Frustum)::
00174 get_perspective_projection_mat(CoordinateSystem cs) const {
00175   if (cs == CS_default) {
00176     cs = default_coordinate_system;
00177   }
00178 
00179   FLOATTYPE recip_far_minus_near = 1.0f/(_ffar - _fnear);
00180   FLOATTYPE recip_r_minus_l = 1.0f/(_r - _l);
00181   FLOATTYPE recip_t_minus_b = 1.0f/(_t - _b);
00182   FLOATTYPE two_fnear = 2.0f*_fnear;
00183 
00184   FLOATTYPE d = (_r + _l) * recip_r_minus_l;
00185   FLOATTYPE a = two_fnear * recip_r_minus_l;
00186   FLOATTYPE e = two_fnear * recip_t_minus_b;
00187   FLOATTYPE b = (_t + _b) * recip_t_minus_b;
00188   FLOATTYPE c = (_ffar + _fnear) * recip_far_minus_near;
00189   FLOATTYPE f = -_ffar * two_fnear * recip_far_minus_near;
00190 
00191 /*
00192   FLOATTYPE a = (2.0f * _fnear) / (_r - _l);
00193   FLOATTYPE b = (_t + _b) / (_t - _b);
00194   FLOATTYPE c = (_ffar + _fnear) / (_ffar - _fnear);
00195   FLOATTYPE d = (_r + _l) / (_r - _l);
00196   FLOATTYPE e = (2.0f * _fnear) / (_t - _b);
00197   FLOATTYPE f = (-2.0f * _ffar * _fnear) / (_ffar - _fnear);
00198 */
00199 
00200   switch (cs) {
00201   case CS_zup_right:
00202     return FLOATNAME(LMatrix4)(  a, 0.0f, 0.0f, 0.0f,
00203                                0.0f,  -b,   c, 1.0f,
00204                                  d,   e, 0.0f, 0.0f,
00205                                0.0f, 0.0f,   f, 0.0f);
00206 
00207   case CS_yup_right:
00208     return FLOATNAME(LMatrix4)(  a, 0.0f, 0.0f, 0.0f,
00209                                0.0f,   e, 0.0f, 0.0f,
00210                                  d,   b,  -c,-1.0f,
00211                                0.0f, 0.0f,   f, 0.0f);
00212 
00213   case CS_zup_left:
00214     return FLOATNAME(LMatrix4)::convert_mat(CS_zup_right, CS_zup_left) *
00215       get_perspective_projection_mat(CS_zup_right);
00216 
00217   case CS_yup_left:
00218     return FLOATNAME(LMatrix4)::convert_mat(CS_yup_right, CS_yup_left) *
00219       get_perspective_projection_mat(CS_yup_right);
00220 
00221   default:
00222     mathutil_cat.error()
00223       << "Invalid coordinate system!\n";
00224     return FLOATNAME(LMatrix4)::ident_mat();
00225   }
00226 }
00227 
00228 ////////////////////////////////////////////////////////////////////
00229 //     Function: get_ortho_projection_mat
00230 //       Access: Public
00231 //  Description: This computes a transform matrix that performs the
00232 //               orthographic transform defined by the frustum,
00233 //               accordinate to the indicated coordinate system.
00234 ////////////////////////////////////////////////////////////////////
00235 INLINE_MATHUTIL FLOATNAME(LMatrix4) FLOATNAME(Frustum)::
00236 get_ortho_projection_mat(CoordinateSystem cs) const {
00237   if (cs == CS_default) {
00238     cs = default_coordinate_system;
00239   }
00240 
00241   FLOATTYPE a = 2.0f / (_r - _l);
00242   FLOATTYPE b = 2.0f / (_t - _b);
00243   FLOATTYPE c = 2.0f / (_ffar - _fnear);
00244   FLOATTYPE d = (_r + _l) * a * 0.5f;
00245   FLOATTYPE e = (_t + _b) * b * 0.5f;
00246   FLOATTYPE f = (_ffar + _fnear) * c * 0.5f;
00247 
00248 /*
00249   FLOATTYPE a = 2.0f / (_r - _l);
00250   FLOATTYPE b = 2.0f / (_t - _b);
00251   FLOATTYPE c = 2.0f / (_ffar - _fnear);
00252   FLOATTYPE d = (_r + _l) / (_r + _l)
00253   FLOATTYPE e = (_t + _b) / (_t - _b);
00254   FLOATTYPE f = (_ffar + _fnear) / (_ffar - _fnear);
00255 */
00256   switch (cs) {
00257   case CS_zup_right:
00258     return FLOATNAME(LMatrix4)::convert_mat(CS_yup_right, CS_zup_right) *
00259       get_ortho_projection_mat(CS_yup_right);
00260 
00261   case CS_yup_right:
00262     return FLOATNAME(LMatrix4)(  a, 0.0f, 0.0f, 0.0f,
00263                                0.0f,   b, 0.0f, 0.0f,
00264                                0.0f, 0.0f,  -c, 0.0f,
00265                                 -d,  -e,  -f, 1.0f);
00266 
00267   case CS_zup_left:
00268     return FLOATNAME(LMatrix4)::convert_mat(CS_zup_right, CS_zup_left) *
00269       get_ortho_projection_mat(CS_zup_right);
00270 
00271   case CS_yup_left:
00272     return FLOATNAME(LMatrix4)::convert_mat(CS_yup_right, CS_yup_left) *
00273       get_ortho_projection_mat(CS_yup_right);
00274 
00275   default:
00276     mathutil_cat.error()
00277       << "Invalid coordinate system!\n";
00278     return FLOATNAME(LMatrix4)::ident_mat();
00279   }
00280 }

Generated on Fri May 2 00:40:27 2003 for Panda by doxygen1.3