33 #ifndef __VMML__FRUSTUM__HPP__
34 #define __VMML__FRUSTUM__HPP__
36 #include <vmmlib/vmmlib_config.hpp>
37 #include <vmmlib/matrix.hpp>
52 frustum(
const T left,
const T right,
const T bottom,
const T top,
53 const T near_plane,
const T far_plane );
58 template<
typename U >
frustum(
const U* values );
63 template<
typename U >
66 void set(
const T _left,
const T _right,
const T _bottom,
67 const T _top,
const T _near,
const T _far );
70 void set_perspective( T field_of_view_y, T aspect_ratio, T near_plane_,
82 void adjust_near(
const T near_plane );
85 inline const T& left()
const;
88 inline const T& right()
const;
91 inline const T& bottom()
const;
94 inline const T& top()
const;
96 inline T& near_plane();
97 inline const T& near_plane()
const;
99 inline T& far_plane();
100 inline const T& far_plane()
const;
102 inline T get_width()
const;
103 inline T get_height()
const;
105 friend std::ostream& operator << ( std::ostream& os,
const frustum& f )
107 const std::ios::fmtflags flags = os.flags();
108 const int prec = os.precision();
110 os.setf( std::ios::right, std::ios::adjustfield );
112 os <<
"[" << std::setw(10) << f.left() <<
" "
113 << std::setw(10) << f.right() <<
" "
114 << std::setw(10) << f.bottom() <<
" "
115 << std::setw(10) << f.top() <<
" "
116 << std::setw(10) << f.near_plane() <<
" "
117 << std::setw(10) << f.far_plane() <<
"]";
118 os.precision( prec );
123 static const frustum DEFAULT;
126 #ifndef VMMLIB_NO_TYPEDEFS
127 typedef frustum< float > Frustumf;
128 typedef frustum< double > Frustumd;
138 template<
typename T >
139 const frustum< T > frustum< T >::DEFAULT( static_cast< T >( -1.0 ),
140 static_cast< T >( 1.0 ),
141 static_cast< T >( -1.0 ),
142 static_cast< T >( 1.0 ),
143 static_cast< T >( 0.1 ),
144 static_cast< T >( 100.0 ) );
147 template <
typename T >
148 frustum<T>::frustum(
const T _left,
const T _right,
const T _bottom,
149 const T _top,
const T _near,
const T _far )
151 set( _left, _right, _bottom, _top, _near, _far );
155 template <
typename T >
156 template<
typename U >
157 frustum< T >::frustum(
const frustum< U >& source_ )
164 template <
typename T >
165 template<
typename U >
166 frustum< T >::frustum(
const U* values )
169 "frustum: Nullpointer argument as source for initialisation!" );
170 left() =
static_cast< T
> ( values[0] );
171 right() =
static_cast< T
> ( values[1] );
172 bottom() =
static_cast< T
> ( values[2] );
173 top() =
static_cast< T
> ( values[3] );
174 near_plane() =
static_cast< T
> ( values[4] );
175 far_plane() =
static_cast< T
> ( values[5] );
180 template <
typename T >
181 frustum< T >::~frustum()
186 template<
typename T >
187 frustum< T >& frustum< T >::operator=(
const frustum& source_ )
189 memcpy( array, source_.array, 6 *
sizeof( T ) );
193 template<
typename T >
template<
typename U >
194 void frustum< T >::operator = (
const frustum< U >& source_ )
196 for(
size_t index = 0; index < 6; ++index )
198 array[ index ] =
static_cast< T
>( source_.array[ index ] );
204 template <
typename T >
206 frustum< T >::set(
const T _left,
const T _right,
const T _bottom,
207 const T _top,
const T _near,
const T _far )
213 near_plane() = _near;
220 template <
typename T >
222 frustum<T>::adjust_near(
const T new_near )
224 if( new_near == near_plane() )
227 const T ratio = new_near / near_plane();
232 near_plane() = new_near;
238 template <
typename T >
240 frustum<T>::set_perspective( T fov_y, T aspect_ratio, T near_plane_,
243 near_plane() = near_plane_;
244 far_plane() = far_plane_;
246 top() = tan( 0.5 * fov_y * M_PI / 180.0 ) * 0.5;
249 left() = bottom() * aspect_ratio;
250 right() = top() * aspect_ratio;
255 template <
typename T >
257 frustum<T>::compute_matrix()
const
259 matrix< 4, 4, T > matrix_;
260 compute_matrix( matrix_ );
266 template <
typename T >
268 frustum<T>::compute_matrix( matrix< 4, 4, T >& M )
const
270 M( 0,0 ) = 2.0 * near_plane() / ( right() - left() );
272 M( 0,2 ) = ( right() + left() ) / ( right() - left() );
276 M( 1,1 ) = 2.0 * near_plane() / ( top() - bottom() );
277 M( 1,2 ) = ( top() + bottom() ) / ( top() - bottom() );
283 M( 2,2 ) = -( far_plane() + near_plane() ) / ( far_plane() - near_plane() );
284 M( 2,3 ) = -2.0 * far_plane() * near_plane() / ( far_plane() - near_plane() );
294 template <
typename T >
296 frustum< T >::compute_ortho_matrix()
const
298 matrix< 4, 4, T > matrix_;
299 compute_ortho_matrix( matrix_ );
305 template <
typename T >
307 frustum< T >::compute_ortho_matrix( matrix< 4, 4, T >& M )
const
309 M( 0,0 ) = 2.0 / ( right() - left() );
312 M( 0,3 ) = -( right() + left() ) / ( right() - left() );
315 M( 1,1 ) = 2.0 / ( top() - bottom() );
317 M( 1,3 ) = -( top() + bottom() ) / ( top() - bottom() );
321 M( 2,2 ) = -2.0 / ( far_plane() - near_plane() );
322 M( 2,3 ) = -( far_plane() + near_plane() ) / ( far_plane() - near_plane() );
330 template <
typename T >
331 void frustum< T >::apply_jitter(
const vector< 2, T >& jitter_ )
333 left() = left() + jitter_.x();
334 right() = right() + jitter_.x();
335 bottom() = bottom() + jitter_.y();
336 top() = top() + jitter_.y();
339 template<
typename T >
340 inline T& frustum< T >::left()
345 template<
typename T >
346 inline const T& frustum< T >::left()
const
351 template<
typename T >
352 inline T& frustum< T >::right()
357 template<
typename T >
358 inline const T& frustum< T >::right()
const
363 template<
typename T >
364 inline T& frustum< T >::bottom()
369 template<
typename T >
370 inline const T& frustum< T >::bottom()
const
375 template<
typename T >
376 inline T& frustum< T >::top()
381 template<
typename T >
382 inline const T& frustum< T >::top()
const
387 template<
typename T >
388 inline T& frustum< T >::near_plane()
393 template<
typename T >
394 inline const T& frustum< T >::near_plane()
const
399 template<
typename T >
400 inline T& frustum< T >::far_plane()
405 template<
typename T >
406 inline const T& frustum< T >::far_plane()
const
411 template<
typename T >
inline T frustum< T >::get_width()
const
413 return fabs( right() - left( ));
416 template<
typename T >
inline T frustum< T >::get_height()
const
418 return fabs( top() - bottom( ));
T array[6]
left, right, bottom, top, near, far storage