GteViewVolume.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
11 
12 // The basis vectors are tested for orthogonality once. This is useful in
13 // debug builds to trap incorrectly specified vectors.
14 #define GTE_VALIDATE_COORDINATE_FRAME_ONCE
15 
16 namespace gte
17 {
18 
20 {
21 public:
22  // Construction and destruction. The depth range for DirectX is [0,1] and
23  // for OpenGL is [-1,1]. For DirectX, set isDepthRangeZeroToOne to true.
24  // For OpenGL, set isDepthRangeZeroToOne to false.
25  virtual ~ViewVolume();
26  ViewVolume(bool isPerspective, bool isDepthRangeZeroToOne);
27 
28  // Coordinate frame support. The default coordinate frame {P;D,U,R}
29  // is in right-handed world coordinates where
30  // position P = (0, 0, 0; 1)
31  // direction D = (0, 0, -1; 0)
32  // up U = (0, 1, 0; 0)
33  // right R = (1, 0, 0; 0)
34  // The basis {D,U,R} is required to be a right-handed orthonormal set.
35  void SetPosition(Vector4<float> const& position);
36 
37  void SetAxes(
38  Vector4<float> const& dVector,
39  Vector4<float> const& uVector,
40  Vector4<float> const& rVector);
41 
42  void SetFrame(
43  Vector4<float> const& position,
44  Vector4<float> const& dVector,
45  Vector4<float> const& uVector,
46  Vector4<float> const& rVector);
47 
48  void GetAxes(
49  Vector4<float>& dVector,
50  Vector4<float>& uVector,
51  Vector4<float>& rVector) const;
52 
53  void GetFrame(
54  Vector4<float>& position,
55  Vector4<float>& dVector,
56  Vector4<float>& uVector,
57  Vector4<float>& rVector) const;
58 
59  inline Vector4<float> const& GetPosition() const;
60  inline Vector4<float> const& GetDVector() const;
61  inline Vector4<float> const& GetUVector() const;
62  inline Vector4<float> const& GetRVector() const;
63 
64  // Access the view matrix of the coordinate frame. If D = (d0,d1,d2),
65  // U = (u0,u1,u2), and R = (r0,r1,r2), then the view matrix is
66  // +- -+
67  // | r0 r1 r2 -Dot(R,P) |
68  // V = | u0 u1 u2 -Dot(U,P) |
69  // | d0 d1 d2 -Dot(D,P) |
70  // | 0 0 0 1 |
71  // +- -+
72  // when the GTE_USE_MAT_VEC convention is used and the view matrix
73  // multiplies vectors on its right, viewMat * vector4. The transpose
74  // of the displayed matrix is used for the GTE_USE_VEC_MAT convention
75  // and the view matrix multiplies vectors on its left, vector4 * viewMat.
76  //
77  // The inverse view matrix for GTE_USE_MAT_VEC convention is
78  // +- -+
79  // | r0 u0 d0 p0 |
80  // V^{-1} = | r1 u1 d1 p1 |
81  // | r2 u2 d2 p1 |
82  // | 0 0 0 1 |
83  // +- -+
84  // A point X = (x0,x1,x2,1) can be represented by X = P + y0*R + y1*U + y2*D,
85  // where y0 = Dot(R,X-P), y1 = Dot(U,X-P), and y2 = Dot(D,X-P). Using the
86  // GTE_USE_MAT_VEC convention, if Y = (y0,y1,y2,1), then Y = V*X, where V is
87  // the view matrix.
88  inline const Matrix4x4<float>& GetViewMatrix() const;
89  inline const Matrix4x4<float>& GetInverseViewMatrix() const;
90 
91  // View frustum support. The view frustum has parameters [rmin,rmax],
92  // [umin,umax], and [dmin,dmax]. The interval [rmin,rmax] is measured in
93  // the right direction R. These are the "left" and "right" frustum values.
94  // The interval [umin,umax] is measured in the up direction U. These are
95  // the "bottom" and "top" values. The interval [dmin,dmax] is measured
96  // in the view direction D. These are the "near" and "far" values. The
97  // frustum values are stored in an array with order based on the VF_*
98  // enumerates. The default perspective view frustum has an up field-of-view
99  // of 90 degrees, an aspect ratio of 1, near value 1, and far value 10000.
100  // The default orthographic view frustum has (r,u,d) in [-1,1]^2 x [0,1].
101  enum
102  {
103  VF_DMIN = 0, // near
104  VF_DMAX = 1, // far
105  VF_UMIN = 2, // bottom
106  VF_UMAX = 3, // top
107  VF_RMIN = 4, // left
108  VF_RMAX = 5, // right
109  VF_QUANTITY = 6
110  };
111 
112  inline bool IsPerspective() const;
113  inline bool IsDepthRangeZeroOne() const;
114 
115  // Set the view frustum. The interval [rmin,rmax] is measured in the
116  // right direction R. These are the "left" and "right" frustum values.
117  // The interval [umin,umax] is measured in the up direction U. These are
118  // the "bottom" and "top" values. The interval [dmin,dmax] is measured
119  // in the view direction D. These are the "near" and "far" values.
120  void SetFrustum(float dMin, float dMax, float uMin, float uMax,
121  float rMin, float rMax);
122 
123  // Set all the view frustum values simultaneously. The input array must
124  // have the order: dmin, dmax, umin, umax, rmin, rmax.
125  void SetFrustum(float const* frustum);
126 
127  // Set a symmetric view frustum (umin = -umax, rmin = -rmax) using a field
128  // of view in the "up" direction and an aspect ratio "width/height". The
129  // field of view in this function must be specified in degrees and be in
130  // the interval (0,180).
131  void SetFrustum(float upFovDegrees, float aspectRatio, float dMin,
132  float dMax);
133 
134  // Get the view frustum.
135  void GetFrustum(float& dMin, float& dMax, float& uMin, float& uMax,
136  float& rMin, float& rMax) const;
137 
138  // Get all the view frustum values simultaneously.
139  inline float const* GetFrustum() const;
140 
141  // Get the parameters for a symmetric view frustum. The return value is
142  // 'true' iff the current frustum is symmetric, in which case the output
143  // parameters are valid.
144  bool GetFrustum(float& upFovDegrees, float& aspectRatio, float& dMin,
145  float& dMax) const;
146 
147  // Get the individual frustum values.
148  inline float GetDMin() const;
149  inline float GetDMax() const;
150  inline float GetUMin() const;
151  inline float GetUMax() const;
152  inline float GetRMin() const;
153  inline float GetRMax() const;
154 
155  // Access the projection matrices of the camera. These matrices map to
156  // depths in the interval [0,1].
157 
158  // The frustum values are N (near), F (far), B (bottom), T (top),
159  // L (left), and R (right). The various matrices are as follows,
160  // shown for GTE_USE_MAT_VEC. The transpose of these matrices are
161  // used for GTE_USE_VEC_MAT.
162  //
163  // perspective, depth [0,1]
164  // +- -+
165  // | 2*N/(R-L) 0 -(R+L)/(R-L) 0 |
166  // | 0 2*N/(T-B) -(T+B)/(T-B) 0 |
167  // | 0 0 F/(F-N) -N*F/(F-N) |
168  // | 0 0 1 0 |
169  // +- -+
170  //
171  // perspective, depth [-1,1]
172  // +- -+
173  // | 2*N/(R-L) 0 -(R+L)/(R-L) 0 |
174  // | 0 2*N/(T-B) -(T+B)/(T-B) 0 |
175  // | 0 0 (F+N)/(F-N) -2*F*N/(F-N) |
176  // | 0 0 1 0
177  // +- -+
178  //
179  // orthographic, depth [0,1]
180  // +- -+
181  // | 2/(R-L) 0 0 -(R+L)/(R-L) |
182  // | 0 2/(T-B) 0 -(T+B)/(T-B) |
183  // | 0 0 1/(F-N) -N/(F-N) 0 |
184  // | 0 0 0 1 |
185  // +- -+
186  //
187  // orthographic, depth [-1,1]
188  // +- -+
189  // | 2/(R-L) 0 0 -(R+L)/(R-L) |
190  // | 0 2/(T-B) 0 -(T+B)/(T-B) |
191  // | 0 0 2/(F-N) -(F+N)/(F-N) |
192  // | 0 0 0 1 |
193  // +- -+
194  //
195  // The projection matrix multiplies vectors on its right, projMat*vector4.
196 
197  // The returned matrix is perspective or orthographic depending on the
198  // value of mIsPerspective.
199  inline Matrix4x4<float> const& GetProjectionMatrix() const;
200 
201  // For advanced effects, you might want to set the projection matrix to
202  // something other than the standard one.
203  inline void SetProjectionMatrix(Matrix4x4<float> const& projMatrix);
204 
205  // The projection-view-world matrix is commonly used in the shader
206  // programs to transform model-space data to clip-space data. To avoid
207  // repeatedly computing the projection-view matrix for each geometric
208  // object, the product is stored and maintained in this class.
209  inline Matrix4x4<float> const& GetProjectionViewMatrix() const;
210 
211 protected:
212  // Compute the view matrix after the frame changes and then update the
213  // projection-view matrix.
214  virtual void OnFrameChange();
215 
216  // Compute the projection matrices after the frustum changes and then
217  // update the projection-view matrix.
218  virtual void OnFrustumChange();
219 
220  // Compute the projection-view matrix. Derived classes can include
221  // pre-view and post-projection matrices in the product.
222  virtual void UpdatePVMatrix();
223 
224  // The coordinate frame.
225  Vector4<float> mPosition, mDVector, mUVector, mRVector;
226 
227  // The view matrix (V) for the coordinate frame. The storage order depends
228  // on whether you define GTE_USE_MAT_VEC or GTE_USE_VEC_MAT. The other
229  // matrix is the inverse view matrix (V^{-1}).
232 
233  // The projection matrixs for the view volume.
235 
236  // The product of the projection and view matrices.
238 
239  // The view frustum, stored in order as dmin (near), dmax (far),
240  // umin (bottom), umax (top), rmin (left), and rmax (right).
241  float mFrustum[VF_QUANTITY];
242 
243  // This member is 'true' for a perspective camera or 'false' for an
244  // orthographic camera.
246 
247  // This member is 'true' for DirectX or 'false' for OpenGL.
249 
250  // The use of this is controlled by conditional compilation (via
251  // GTE_VALIDATE_COORDINATE_FRAME_ONCE). It allows you to trap incorrectly
252  // built frames or frames that are askew due to numerical round-off errors.
254 };
255 
256 
258 {
259  return mPosition;
260 }
261 
263 {
264  return mDVector;
265 }
266 
268 {
269  return mUVector;
270 }
271 
273 {
274  return mRVector;
275 }
276 
278 {
279  return mViewMatrix;
280 }
281 
283 {
284  return mInverseViewMatrix;
285 }
286 
287 inline bool ViewVolume::IsPerspective() const
288 {
289  return mIsPerspective;
290 }
291 
293 {
294  return mIsDepthRangeZeroOne;
295 }
296 
297 inline float const* ViewVolume::GetFrustum() const
298 {
299  return mFrustum;
300 }
301 
302 inline float ViewVolume::GetDMin() const
303 {
304  return mFrustum[VF_DMIN];
305 }
306 
307 inline float ViewVolume::GetDMax() const
308 {
309  return mFrustum[VF_DMAX];
310 }
311 
312 inline float ViewVolume::GetUMin() const
313 {
314  return mFrustum[VF_UMIN];
315 }
316 
317 inline float ViewVolume::GetUMax() const
318 {
319  return mFrustum[VF_UMAX];
320 }
321 
322 inline float ViewVolume::GetRMin() const
323 {
324  return mFrustum[VF_RMIN];
325 }
326 
327 inline float ViewVolume::GetRMax() const
328 {
329  return mFrustum[VF_RMAX];
330 }
331 
333 {
334  return mProjectionMatrix;
335 }
336 
337 inline void ViewVolume::SetProjectionMatrix(Matrix4x4<float> const& projMatrix)
338 {
339  mProjectionMatrix = projMatrix;
340  UpdatePVMatrix();
341 }
342 
344 {
345  return mProjectionViewMatrix;
346 }
347 
348 }
Vector4< float > const & GetRVector() const
float GetDMax() const
float GetDMin() const
void SetProjectionMatrix(Matrix4x4< float > const &projMatrix)
Matrix4x4< float > mInverseViewMatrix
Matrix4x4< float > mProjectionMatrix
const Matrix4x4< float > & GetInverseViewMatrix() const
Matrix4x4< float > const & GetProjectionViewMatrix() const
float GetRMax() const
bool IsPerspective() const
float GetRMin() const
Vector4< float > mUVector
Vector4< float > const & GetPosition() const
const Matrix4x4< float > & GetViewMatrix() const
Matrix4x4< float > const & GetProjectionMatrix() const
Matrix4x4< float > mProjectionViewMatrix
bool mValidateCoordinateFrame
Vector4< float > const & GetUVector() const
float GetUMax() const
Matrix4x4< float > mViewMatrix
Vector4< float > const & GetDVector() const
float GetUMin() const
float const * GetFrustum() const
bool IsDepthRangeZeroOne() const
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:02