projection.h

Go to the documentation of this file.
00001 /* -*- C++ -*- ------------------------------------------------------------
00002  
00003 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
00004 
00005 The Configurable Math Library (CML) is distributed under the terms of the
00006 Boost Software License, v1.0 (see cml/LICENSE for details).
00007 
00008  *-----------------------------------------------------------------------*/
00013 #ifndef projection_h
00014 #define projection_h
00015 
00016 #include <cml/mathlib/matrix_concat.h>
00017 #include <cml/mathlib/vector_transform.h>
00018 
00019 /* Functions for projection and 'unprojection' of points in 3D. */
00020 
00021 namespace cml {
00022 
00023 namespace detail {
00024 
00025 template < typename E > void
00026 divide_by_w(vector< E,fixed<4> >& v) {
00027     v *= E(1) / v[3];
00028 }
00029 
00030 } // namespace detail
00031 
00032 /* Project a point to screen space using the given model, view, projection,
00033  * and viewport matrices. The z value of the returned point is a depth value
00034  * in the range specified by the viewport matrix.
00035  */
00036 
00037 template <class MatT_1, class MatT_2, class MatT_3, class MatT_4, class VecT>
00038 vector< typename VecT::value_type, fixed<3> > project_point(
00039     const MatT_1& model,
00040     const MatT_2& view,
00041     const MatT_3& projection,
00042     const MatT_4& viewport,
00043     const VecT& p)
00044 {
00045     return project_point(
00046         detail::matrix_concat_transforms_4x4(model,view),
00047         projection,
00048         viewport,
00049         p
00050     );
00051 }
00052 
00053 /* Project a point to screen space using the given modelview, projection, and
00054  * viewport matrices. The z value of the returned point is a depth value in
00055  * the range specified by the viewport matrix.
00056  */
00057 
00058 template < class MatT_1, class MatT_2, class MatT_3, class VecT >
00059 vector< typename VecT::value_type, fixed<3> > project_point(
00060     const MatT_1& modelview,
00061     const MatT_2& projection,
00062     const MatT_3& viewport,
00063     const VecT& p)
00064 {
00065     typedef vector< typename VecT::value_type, fixed<3> > vector3_type;
00066     typedef vector< typename VecT::value_type, fixed<4> > vector4_type;
00067     typedef typename vector3_type::value_type value_type;
00068 
00069     detail::CheckVec3(p);
00070 
00071     vector4_type result = transform_vector_4D(
00072         detail::matrix_concat_transforms_4x4(
00073             modelview,
00074             detail::matrix_concat_transforms_4x4(
00075                 projection,
00076                 viewport
00077             )
00078         ),
00079         vector4_type(p[0],p[1],p[2],value_type(1))
00080     );
00081     detail::divide_by_w(result);
00082     return vector3_type(result[0],result[1],result[2]);
00083 }
00084 
00085 /* 'Unproject' a point from screen space using the given model, view,
00086  * projection, and viewport matrices. The z value of the input point is a
00087  * depth value in the range specified by the viewport matrix.
00088  */
00089 
00090 template <class MatT_1, class MatT_2, class MatT_3, class MatT_4, class VecT>
00091 vector< typename VecT::value_type, fixed<3> > unproject_point(
00092     const MatT_1& model,
00093     const MatT_2& view,
00094     const MatT_3& projection,
00095     const MatT_4& viewport,
00096     const VecT& p)
00097 {
00098     return unproject_point(
00099         detail::matrix_concat_transforms_4x4(model,view),
00100         projection,
00101         viewport,
00102         p
00103     );
00104 }
00105 
00106 /* 'Unproject' a point from screen space using the given modelview,
00107  * projection, and viewport matrices. The z value of the input point is a
00108  * depth value in the range specified by the viewport matrix.
00109  */
00110  
00111 template < class MatT_1, class MatT_2, class MatT_3, class VecT >
00112 vector< typename VecT::value_type, fixed<3> > unproject_point(
00113     const MatT_1& modelview,
00114     const MatT_2& projection,
00115     const MatT_3& viewport,
00116     const VecT& p)
00117 {
00118     typedef vector< typename VecT::value_type, fixed<3> > vector3_type;
00119     typedef vector< typename VecT::value_type, fixed<4> > vector4_type;
00120     typedef typename vector3_type::value_type value_type;
00121 
00122     detail::CheckVec3(p);
00123 
00124     vector4_type result = transform_vector_4D(
00125         inverse(
00126             detail::matrix_concat_transforms_4x4(
00127                 modelview,
00128                 detail::matrix_concat_transforms_4x4(
00129                     projection,
00130                     viewport
00131                 )
00132             )
00133         ),
00134         vector4_type(p[0],p[1],p[2],value_type(1))
00135     );
00136     detail::divide_by_w(result);
00137     return vector3_type(result[0],result[1],result[2]);
00138 }
00139 
00140 } // namespace cml
00141 
00142 #endif

Generated on Sat Jul 18 19:35:22 2009 for CML 1.0 by  doxygen 1.5.9