33 #ifndef __VMML__FRUSTUM__HPP__ 34 #define __VMML__FRUSTUM__HPP__ 37 #include <vmmlib/matrix.hpp> 41 #define _USE_MATH_DEFINES 44 #include <corecrt_math_defines.h> 48 #define M_PI 3.1415926535897932 63 Frustum(T left, T right, T bottom, T top, T nearPlane, T farPlane);
66 Frustum(T field_of_view_y, T aspect_ratio, T nearPlane_, T farPlane);
81 T tolerance = std::numeric_limits<T>::epsilon())
const;
102 T& left() {
return _array[0]; }
103 T left()
const {
return _array[0]; }
104 T& right() {
return _array[1]; }
105 T right()
const {
return _array[1]; }
106 T& bottom() {
return _array[2]; }
107 T bottom()
const {
return _array[2]; }
108 T& top() {
return _array[3]; }
109 T top()
const {
return _array[3]; }
110 T& nearPlane() {
return _array[4]; }
111 T nearPlane()
const {
return _array[4]; }
112 T& farPlane() {
return _array[5]; }
113 T farPlane()
const {
return _array[5]; }
122 friend std::ostream& operator<<(std::ostream& os,
const Frustum& f)
124 const std::ios::fmtflags flags = os.flags();
125 const int prec = os.precision();
127 os.setf(std::ios::right, std::ios::adjustfield);
129 os <<
"[" << std::setw(10) << f.left() <<
" " << std::setw(10)
130 << f.right() <<
" " << std::setw(10) << f.bottom() <<
" " 131 << std::setw(10) << f.top() <<
" " << std::setw(10) << f.nearPlane()
132 <<
" " << std::setw(10) << f.farPlane() <<
"]";
147 template <
typename T>
158 template <
typename T>
160 const T _top,
const T _near,
const T _far)
170 template <
typename T>
174 _array[2] = std::tan(0.5 * fov_y * M_PI / 180.0) * nearPlane_;
175 _array[3] = -_array[2];
177 _array[0] = bottom() * aspect_ratio;
178 _array[1] = top() * aspect_ratio;
179 _array[4] = nearPlane_;
180 _array[5] = farPlane_;
183 template <
typename T>
186 _array[4] = projection(2, 3) / (projection(2, 2) - 1.0);
187 _array[5] = projection(2, 3) / (projection(2, 2) + 1.0);
188 _array[2] = nearPlane() * (projection(1, 2) - 1.0) / projection(1, 1);
189 _array[3] = nearPlane() * (projection(1, 2) + 1.0) / projection(1, 1);
190 _array[0] = nearPlane() * (projection(0, 2) - 1.0) / projection(0, 0);
191 _array[1] = nearPlane() * (projection(0, 2) + 1.0) / projection(0, 0);
194 template <
typename T>
197 return ::memcmp(_array, other._array,
sizeof(_array)) == 0;
200 template <
typename T>
203 return ::memcmp(_array, other._array,
sizeof(_array)) != 0;
206 template <
typename T>
209 return std::abs(_array[0] - other._array[0]) <= tolerance &&
210 std::abs(_array[1] - other._array[1]) <= tolerance &&
211 std::abs(_array[2] - other._array[2]) <= tolerance &&
212 std::abs(_array[3] - other._array[3]) <= tolerance &&
213 std::abs(_array[4] - other._array[4]) <= tolerance &&
214 std::abs(_array[5] - other._array[5]) <= tolerance;
217 template <
typename T>
222 M(0, 0) = 2.0 * nearPlane() / (right() - left());
224 M(0, 2) = (right() + left()) / (right() - left());
228 M(1, 1) = 2.0 * nearPlane() / (top() - bottom());
229 M(1, 2) = (top() + bottom()) / (top() - bottom());
235 M(2, 2) = -(farPlane() + nearPlane()) / (farPlane() - nearPlane());
236 M(2, 3) = -2.0 * farPlane() * nearPlane() / (farPlane() - nearPlane());
246 template <
typename T>
251 M(0, 0) = 2.0 / (right() - left());
254 M(0, 3) = -(right() + left()) / (right() - left());
257 M(1, 1) = 2.0 / (top() - bottom());
259 M(1, 3) = -(top() + bottom()) / (top() - bottom());
263 M(2, 2) = -2.0 / (farPlane() - nearPlane());
264 M(2, 3) = -(farPlane() + nearPlane()) / (farPlane() - nearPlane());
274 template <
typename T>
277 left() = left() + jitter_.x();
278 right() = right() + jitter_.x();
279 bottom() = bottom() + jitter_.y();
280 top() = top() + jitter_.y();
283 template <
typename T>
286 if (new_near == nearPlane())
289 const T ratio = new_near / nearPlane();
294 nearPlane() = new_near;
297 template <
typename T>
300 return std::abs(right() - left());
303 template <
typename T>
306 return std::abs(top() - bottom());
Represents a frustum, following OpenGL conventions.
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
Matrix< 4, 4, T > computePerspectiveMatrix() const
Matrix< 4, 4, T > computeOrthoMatrix() const
void adjustNearPlane(const T nearPlane)
Move the frustum near plane.
~Frustum()
Destruct this frustum.
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".
Matrix with R rows and C columns of element type T.