GteCamera.cpp
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 #include <GTEnginePCH.h>
9 #include <Graphics/GteCamera.h>
10 using namespace gte;
11 
12 Camera::Camera(bool isPerspective, bool isDepthRangeZeroOne)
13  :
14  ViewVolume(isPerspective, isDepthRangeZeroOne),
15  mPreViewMatrix(Matrix4x4<float>::Identity()),
16  mPostProjectionMatrix(Matrix4x4<float>::Identity()),
17  mPreViewIsIdentity(true),
18  mPostProjectionIsIdentity(true)
19 {
20 }
21 
23  Vector4<float> const& p11, Vector4<float> const& p01, float nearExtrude, float farExtrude)
24 {
25  LogAssert(nearExtrude > 0.0f, "Invalid nearExtrude.");
26  LogAssert(farExtrude > nearExtrude, "Invalid farExtrude.");
27 
28  // Compute the near face of the view volume.
29  Vector4<float> origin{ 0.0f, 0.0f, 0.0f, 1.0f };
30  Vector4<float> q000 = origin + nearExtrude*(p00 - origin);
31  Vector4<float> q100 = origin + nearExtrude*(p10 - origin);
32  Vector4<float> q110 = origin + nearExtrude*(p11 - origin);
33  Vector4<float> q010 = origin + nearExtrude*(p01 - origin);
34 
35  // Compute the far face of the view volume.
36  Vector4<float> q001 = origin + farExtrude*(p00 - origin);
37  Vector4<float> q101 = origin + farExtrude*(p10 - origin);
38  Vector4<float> q111 = origin + farExtrude*(p11 - origin);
39  Vector4<float> q011 = origin + farExtrude*(p01 - origin);
40 
41  // Compute the representation of q111.
42  Vector4<float> u0 = q100 - q000;
43  Vector4<float> u1 = q010 - q000;
44  Vector4<float> u2 = q001 - q000;
45 #if defined(GTE_USE_MAT_VEC)
47  M.SetCol(0, u0);
48  M.SetCol(1, u1);
49  M.SetCol(2, u2);
50  M.SetCol(3, q000);
51  Matrix4x4<float> invM = Inverse(M);
52  Vector4<float> a = invM * q111;
53 #else
55  M.SetRow(0, u0);
56  M.SetRow(1, u1);
57  M.SetRow(2, u2);
58  M.SetRow(3, q000);
59  Matrix4x4<float> invM = Inverse(M);
60  Vector4<float> a = q111 * invM;
61 #endif
62 
63  // Compute the coefficients in the fractional linear transformation.
64  // y[i] = n[i]*x[i]/(d[0]*x[0] + d[1]*x[1] + d[2]*x[2] + d[3])
65  float n0 = 2.0f * a[0];
66  float n1 = 2.0f * a[1];
67  float n2 = 2.0f * a[2];
68  float d0 = +a[0] - a[1] - a[2] + 1.0f;
69  float d1 = -a[0] + a[1] - a[2] + 1.0f;
70  float d2 = -a[0] - a[1] + a[2] + 1.0f;
71  float d3 = +a[0] + a[1] + a[2] - 1.0f;
72 
73  // Compute the perspective projection from the canonical cuboid to the
74  // canonical cube [-1,1]^2 x [0,1].
75  float n2divn0 = n2 / n0;
76  float n2divn1 = n2 / n1;
77  Matrix4x4<float> project;
78 #if defined(GTE_USE_MAT_VEC)
79  project(0,0) = n2divn0 * (2.0f * d3 + d0);
80  project(0,1) = n2divn1 * d1;
81  project(0,2) = d2;
82  project(0,3) = -n2;
83  project(1,0) = n2divn0 * d0;
84  project(1,1) = n2divn1 * (2.0f * d3 + d1);
85  project(1,2) = d2;
86  project(1,3) = -n2;
87  project(2,0) = 0.0f;
88  project(2,1) = 0.0f;
89  project(2,2) = d3;
90  project(2,3) = 0.0f;
91  project(3,0) = -n2divn0 * d0;
92  project(3,1) = -n2divn1 * d1;
93  project(3,2) = -d2;
94  project(3,3) = n2;
95 
96  // The full projection requires mapping the extruded-quadrilateral view
97  // volume to the canonical cuboid, which is then followed by the
98  // perspective projection to the canonical cube.
99  SetProjectionMatrix(project * invM);
100 #else
101  project(0,0) = n2divn0 * (2.0f * d3 + d0);
102  project(1,0) = n2divn1 * d1;
103  project(2,0) = d2;
104  project(3,0) = -n2;
105  project(0,1) = n2divn0 * d0;
106  project(1,1) = n2divn1 * (2.0f * d3 + d1);
107  project(2,1) = d2;
108  project(3,1) = -n2;
109  project(0,2) = 0.0f;
110  project(1,2) = 0.0f;
111  project(2,2) = d3;
112  project(3,2) = 0.0f;
113  project(0,3) = -n2divn0 * d0;
114  project(1,3) = -n2divn1 * d1;
115  project(2,3) = -d2;
116  project(3,3) = n2;
117 
118  // The full projection requires mapping the extruded-quadrilateral view
119  // volume to the canonical cuboid, which is then followed by the
120  // perspective projection to the canonical cube.
121  SetProjectionMatrix(invM*project);
122 #endif
123 }
124 
125 void Camera::SetPreViewMatrix(Matrix4x4<float> const& preViewMatrix)
126 {
127  mPreViewMatrix = preViewMatrix;
129  UpdatePVMatrix();
130 }
131 
133 {
134  mPostProjectionMatrix = postProjMatrix;
136  UpdatePVMatrix();
137 }
138 
139 bool Camera::GetPickLine(int viewX, int viewY, int viewW, int viewH, int x, int y,
140  Vector4<float>& origin, Vector4<float>& direction) const
141 {
142  if (viewX <= x && x <= viewX + viewW && viewY <= y && y <= viewY + viewH)
143  {
144  // Get the [0,1]^2-normalized coordinates of (x,y).
145  float r = ((float)(x - viewX)) / (float)viewW;
146  float u = ((float)(y - viewY)) / (float)viewH;
147 
148  // Get the relative coordinates in [rmin,rmax]x[umin,umax].
149  float rBlend = (1.0f - r) * GetRMin() + r * GetRMax();
150  float uBlend = (1.0f - u) * GetUMin() + u * GetUMax();
151 
152  if (IsPerspective())
153  {
154  origin = GetPosition();
155  direction = GetDMin() * GetDVector() + rBlend * GetRVector() + uBlend * GetUVector();
156  Normalize(direction);
157  }
158  else
159  {
160  origin = GetPosition() + rBlend * GetRVector() + uBlend * GetUVector();
161  direction = GetDVector();
162  }
163  return true;
164  }
165  else
166  {
167  // (x,y) is outside the viewport.
168  return false;
169  }
170 }
171 
173 {
175 
177 
178 #if defined(GTE_USE_MAT_VEC)
180  {
181  pvMatrix = mPostProjectionMatrix * pvMatrix;
182  }
183  if (!mPreViewIsIdentity)
184  {
185  pvMatrix = pvMatrix * mPreViewMatrix;
186  }
187 #else
189  {
190  pvMatrix = pvMatrix * mPostProjectionMatrix;
191  }
192  if (!mPreViewIsIdentity)
193  {
194  pvMatrix = mPreViewMatrix * pvMatrix;
195  }
196 #endif
197 }
GLfixed GLfixed u2
Definition: glext.h:4927
virtual void UpdatePVMatrix()
void SetPreViewMatrix(Matrix4x4< float > const &preViewMatrix)
Definition: GteCamera.cpp:125
Vector4< float > const & GetRVector() const
Camera(bool isPerspective, bool isDepthRangeZeroOne)
Definition: GteCamera.cpp:12
GLfixed u1
Definition: glext.h:4927
float GetDMin() const
#define LogAssert(condition, message)
Definition: GteLogger.h:86
void SetProjectionMatrix(Matrix4x4< float > const &projMatrix)
void SetParallaxProjectionMatrix(Vector4< float > const &p00, Vector4< float > const &p10, Vector4< float > const &p11, Vector4< float > const &p01, float nearExtrude, float farExtrude)
Definition: GteCamera.cpp:22
void SetPostProjectionMatrix(Matrix4x4< float > const &postProjMatrix)
Definition: GteCamera.cpp:132
GLint GLenum GLint x
Definition: glcorearb.h:404
float GetRMax() const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1217
bool IsPerspective() const
float GetRMin() const
Vector4< float > const & GetPosition() const
Matrix4x4< float > mPostProjectionMatrix
Definition: GteCamera.h:66
Matrix4x4< float > mProjectionViewMatrix
void SetCol(int c, Vector< NumRows, Real > const &vec)
Definition: GteMatrix.h:362
Real Normalize(GVector< Real > &v, bool robust=false)
Definition: GteGVector.h:454
bool mPreViewIsIdentity
Definition: GteCamera.h:69
virtual void UpdatePVMatrix()
Definition: GteCamera.cpp:172
Vector4< float > const & GetUVector() const
float GetUMax() const
Vector4< float > const & GetDVector() const
GLboolean r
Definition: glcorearb.h:1217
void SetRow(int r, Vector< NumCols, Real > const &vec)
Definition: GteMatrix.h:352
bool GetPickLine(int viewX, int viewY, int viewW, int viewH, int x, int y, Vector4< float > &origin, Vector4< float > &direction) const
Definition: GteCamera.cpp:139
float GetUMin() const
GLfloat f
Definition: glcorearb.h:1921
Quaternion< Real > Inverse(Quaternion< Real > const &d)
static Matrix Identity()
Definition: GteMatrix.h:490
Matrix4x4< float > mPreViewMatrix
Definition: GteCamera.h:63
GLint y
Definition: glcorearb.h:98
bool mPostProjectionIsIdentity
Definition: GteCamera.h:73


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59