33 #ifndef __VMML__FRUSTUM__HPP__
34 #define __VMML__FRUSTUM__HPP__
36 #include <vmmlib/matrix.hpp>
49 Frustum( T left, T right, T bottom, T top, T nearPlane, T farPlane );
52 Frustum( T field_of_view_y, T aspect_ratio, T nearPlane_, T farPlane );
68 T tolerance = std::numeric_limits< T >::epsilon( ))
const;
89 T& left() {
return _array[0]; }
90 T left()
const {
return _array[0]; }
92 T& right() {
return _array[1]; }
93 T right()
const {
return _array[1]; }
95 T& bottom() {
return _array[2]; }
96 T bottom()
const {
return _array[2]; }
98 T& top() {
return _array[3]; }
99 T top()
const {
return _array[3]; }
101 T& nearPlane() {
return _array[4]; }
102 T nearPlane()
const {
return _array[4]; }
104 T& farPlane() {
return _array[5]; }
105 T farPlane()
const {
return _array[5]; }
114 friend std::ostream& operator << ( std::ostream& os,
const Frustum& f )
116 const std::ios::fmtflags flags = os.flags();
117 const int prec = os.precision();
119 os.setf( std::ios::right, std::ios::adjustfield );
121 os <<
"[" << std::setw(10) << f.left() <<
" "
122 << std::setw(10) << f.right() <<
" "
123 << std::setw(10) << f.bottom() <<
" "
124 << std::setw(10) << f.top() <<
" "
125 << std::setw(10) << f.nearPlane() <<
" "
126 << std::setw(10) << f.farPlane() <<
"]";
127 os.precision( prec );
152 template <
typename T >
154 const T _top,
const T _near,
const T _far )
164 template <
typename T >
168 _array[2] = std::tan( 0.5 * fov_y * M_PI / 180.0 ) * 0.5;
169 _array[3] = -_array[2];
171 _array[0] = bottom() * aspect_ratio;
172 _array[1] = top() * aspect_ratio;
173 _array[4] = nearPlane_;
174 _array[5] = farPlane_;
177 template <
typename T >
180 _array[4] = projection( 2, 3 ) / (projection( 2, 2 ) - 1.0);
181 _array[5] = projection( 2, 3 ) / (projection( 2, 2 ) + 1.0);
182 _array[2] = nearPlane() * ( projection( 1, 2 ) - 1.0 ) / projection( 1, 1 );
183 _array[3] = nearPlane() * ( projection( 1, 2 ) + 1.0 ) / projection( 1, 1 );
184 _array[0] = nearPlane() * ( projection( 0, 2 ) - 1.0 ) / projection( 0, 0 );
185 _array[1] = nearPlane() * ( projection( 0, 2 ) + 1.0 ) / projection( 0, 0 );
188 template <
typename T >
191 return ::memcmp( _array, other._array,
sizeof( _array )) == 0;
194 template <
typename T >
197 return ::memcmp( _array, other._array,
sizeof( _array )) != 0;
200 template <
typename T >
203 return std::abs( _array[0] - other._array[0] ) <= tolerance &&
204 std::abs( _array[1] - other._array[1] ) <= tolerance &&
205 std::abs( _array[2] - other._array[2] ) <= tolerance &&
206 std::abs( _array[3] - other._array[3] ) <= tolerance &&
207 std::abs( _array[4] - other._array[4] ) <= tolerance &&
208 std::abs( _array[5] - other._array[5] ) <= tolerance;
211 template <
typename T >
216 M( 0,0 ) = 2.0 * nearPlane() / ( right() - left() );
218 M( 0,2 ) = ( right() + left() ) / ( right() - left() );
222 M( 1,1 ) = 2.0 * nearPlane() / ( top() - bottom() );
223 M( 1,2 ) = ( top() + bottom() ) / ( top() - bottom() );
229 M( 2,2 ) = -( farPlane() + nearPlane() ) / ( farPlane() - nearPlane( ));
230 M( 2,3 ) = -2.0 * farPlane() * nearPlane() / (farPlane() - nearPlane());
240 template <
typename T >
245 M( 0,0 ) = 2.0 / ( right() - left() );
248 M( 0,3 ) = -( right() + left() ) / ( right() - left() );
251 M( 1,1 ) = 2.0 / ( top() - bottom() );
253 M( 1,3 ) = -( top() + bottom() ) / ( top() - bottom() );
257 M( 2,2 ) = -2.0 / ( farPlane() - nearPlane() );
258 M( 2,3 ) = -( farPlane() + nearPlane() ) / ( farPlane() - nearPlane() );
268 template <
typename T >
271 left() = left() + jitter_.x();
272 right() = right() + jitter_.x();
273 bottom() = bottom() + jitter_.y();
274 top() = top() + jitter_.y();
279 if( new_near == nearPlane() )
282 const T ratio = new_near / nearPlane();
287 nearPlane() = new_near;
292 return std::abs( right() - left( ));
297 return std::abs( top() - bottom( ));
Represents a frustum, following OpenGL conventions.
matrix< 4, 4, T > computePerspectiveMatrix() const
bool operator==(const Frustum< T > &other) const
Frustum()
Construct a default frustum (-1, 1, -1, 1, 0.1, 100).
bool operator!=(const Frustum< T > &other) const
void adjustNearPlane(const T nearPlane)
Move the frustum near plane.
heavily inspired by boost::enable_if http://www.boost.org, file: boost/utility/enable_if.hpp, Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine
~Frustum()
Destruct this frustum.
matrix< 4, 4, T > computeOrthoMatrix() const
bool equals(const Frustum< T > &other, T tolerance=std::numeric_limits< T >::epsilon()) const
void jitter(const vector< 2, T > &jitter_)
Move the frustum near plane by the given offset "sideways".