]>
Dogcows Code - chaz/yoink/blob - src/Moof/cml/mathlib/projection.h
1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
16 #include <cml/mathlib/matrix_concat.h>
17 #include <cml/mathlib/vector_transform.h>
19 /* Functions for projection and 'unprojection' of points in 3D. */
25 template < typename E
> void
26 divide_by_w(vector
< E
,fixed
<4> >& v
) {
32 /* Project a point to screen space using the given model, view, projection,
33 * and viewport matrices. The z value of the returned point is a depth value
34 * in the range specified by the viewport matrix.
37 template <class MatT_1
, class MatT_2
, class MatT_3
, class MatT_4
, class VecT
>
38 vector
< typename
VecT::value_type
, fixed
<3> > project_point(
41 const MatT_3
& projection
,
42 const MatT_4
& viewport
,
46 detail::matrix_concat_transforms_4x4(model
,view
),
53 /* Project a point to screen space using the given modelview, projection, and
54 * viewport matrices. The z value of the returned point is a depth value in
55 * the range specified by the viewport matrix.
58 template < class MatT_1
, class MatT_2
, class MatT_3
, class VecT
>
59 vector
< typename
VecT::value_type
, fixed
<3> > project_point(
60 const MatT_1
& modelview
,
61 const MatT_2
& projection
,
62 const MatT_3
& viewport
,
65 typedef vector
< typename
VecT::value_type
, fixed
<3> > vector3_type
;
66 typedef vector
< typename
VecT::value_type
, fixed
<4> > vector4_type
;
67 typedef typename
vector3_type::value_type value_type
;
71 vector4_type result
= transform_vector_4D(
72 detail::matrix_concat_transforms_4x4(
74 detail::matrix_concat_transforms_4x4(
79 vector4_type(p
[0],p
[1],p
[2],value_type(1))
81 detail::divide_by_w(result
);
82 return vector3_type(result
[0],result
[1],result
[2]);
85 /* 'Unproject' a point from screen space using the given model, view,
86 * projection, and viewport matrices. The z value of the input point is a
87 * depth value in the range specified by the viewport matrix.
90 template <class MatT_1
, class MatT_2
, class MatT_3
, class MatT_4
, class VecT
>
91 vector
< typename
VecT::value_type
, fixed
<3> > unproject_point(
94 const MatT_3
& projection
,
95 const MatT_4
& viewport
,
98 return unproject_point(
99 detail::matrix_concat_transforms_4x4(model
,view
),
106 /* 'Unproject' a point from screen space using the given modelview,
107 * projection, and viewport matrices. The z value of the input point is a
108 * depth value in the range specified by the viewport matrix.
111 template < class MatT_1
, class MatT_2
, class MatT_3
, class VecT
>
112 vector
< typename
VecT::value_type
, fixed
<3> > unproject_point(
113 const MatT_1
& modelview
,
114 const MatT_2
& projection
,
115 const MatT_3
& viewport
,
118 typedef vector
< typename
VecT::value_type
, fixed
<3> > vector3_type
;
119 typedef vector
< typename
VecT::value_type
, fixed
<4> > vector4_type
;
120 typedef typename
vector3_type::value_type value_type
;
122 detail::CheckVec3(p
);
124 vector4_type result
= transform_vector_4D(
126 detail::matrix_concat_transforms_4x4(
128 detail::matrix_concat_transforms_4x4(
134 vector4_type(p
[0],p
[1],p
[2],value_type(1))
136 detail::divide_by_w(result
);
137 return vector3_type(result
[0],result
[1],result
[2]);
This page took 0.042394 seconds and 4 git commands to generate.