1 | #include "common.h"
|
---|
2 | #include "Camera.h"
|
---|
3 | #include "glInterface.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | namespace CHCDemoEngine
|
---|
7 | {
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | // this is the start vector.
|
---|
12 | // warning: our coordinate system has the negative z axis pointing up
|
---|
13 | // which is different from the system used in opengl!!
|
---|
14 | static Vector3 startVector = Vector3(0, 1, 0);
|
---|
15 |
|
---|
16 |
|
---|
17 | Camera::Camera()
|
---|
18 | {
|
---|
19 | mWidth = 100;
|
---|
20 | mHeight = 100;
|
---|
21 | mFovy = 60.0f * M_PI / 180.0f;
|
---|
22 | mIsOrtho = false;
|
---|
23 |
|
---|
24 | SetPosition(Vector3(0, 0, 0));
|
---|
25 |
|
---|
26 | mPitch = mYaw = 0;
|
---|
27 | Precompute(startVector);
|
---|
28 |
|
---|
29 | CalculateFromPitchAndYaw();
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | Camera::Camera(int width, int height, float fieldOfView)
|
---|
34 | {
|
---|
35 | mWidth = width;
|
---|
36 | mHeight = height;
|
---|
37 |
|
---|
38 | mFovy = fieldOfView * M_PI / 180.0f;
|
---|
39 |
|
---|
40 | mIsOrtho = false;
|
---|
41 |
|
---|
42 | SetPosition(Vector3(0, 0, 0));
|
---|
43 |
|
---|
44 | mPitch = mYaw = 0;
|
---|
45 | Precompute(startVector);
|
---|
46 |
|
---|
47 | CalculateFromPitchAndYaw();
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | void Camera::Precompute(const Vector3 &direction)
|
---|
52 | {
|
---|
53 | Vector3 up = Vector3(0, 0, 1);
|
---|
54 | Vector3 right = Normalize(CrossProd(up, direction));
|
---|
55 | up = Normalize(CrossProd(direction, right));
|
---|
56 |
|
---|
57 | mBaseOrientation = Matrix4x4(right, up, direction);
|
---|
58 | mViewOrientation = mBaseOrientation;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | void Camera::SetPosition(const Vector3 &pos)
|
---|
63 | {
|
---|
64 | mPosition = pos;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | void Camera::SetNear(float nearDist)
|
---|
69 | {
|
---|
70 | mNear = nearDist;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void Camera::SetFar(float farDist)
|
---|
75 | {
|
---|
76 | mFar = farDist;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | void Camera::GetProjectionMatrix(Matrix4x4 &mat)
|
---|
81 | {
|
---|
82 | glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void Camera::GetModelViewMatrix(Matrix4x4 &mat)
|
---|
87 | {
|
---|
88 | glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | void Camera::CalcFrustum(Frustum &frustum)
|
---|
93 | {
|
---|
94 | // we grab the plane equations of the six clipplanes of the viewfrustum
|
---|
95 | Matrix4x4 matViewing, matProjectionView;
|
---|
96 |
|
---|
97 | GetModelViewMatrix(matViewing);
|
---|
98 | GetProjectionMatrix(matProjectionView);
|
---|
99 |
|
---|
100 | matProjectionView = matViewing * matProjectionView;
|
---|
101 |
|
---|
102 | float planes[6][4];
|
---|
103 |
|
---|
104 |
|
---|
105 | //////////
|
---|
106 | //-- extract the plane equations
|
---|
107 |
|
---|
108 | for (int i = 0; i < 4; ++ i)
|
---|
109 | {
|
---|
110 | planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
|
---|
111 | planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
|
---|
112 | planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
|
---|
113 | planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
|
---|
114 | planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
|
---|
115 | planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | ////////////
|
---|
120 | //-- normalize the coefficients
|
---|
121 |
|
---|
122 | for (int i = 0; i < 6; ++ i)
|
---|
123 | {
|
---|
124 | // the clipping planes look outward the frustum,
|
---|
125 | // so distances > 0 mean that a point is outside
|
---|
126 | float fInvLength = -1.0f /
|
---|
127 | sqrt(planes[i][0] * planes[i][0] +
|
---|
128 | planes[i][1] * planes[i][1] +
|
---|
129 | planes[i][2] * planes[i][2]);
|
---|
130 |
|
---|
131 | planes[i][0] *= fInvLength;
|
---|
132 | planes[i][1] *= fInvLength;
|
---|
133 | planes[i][2] *= fInvLength;
|
---|
134 | planes[i][3] *= fInvLength;
|
---|
135 |
|
---|
136 | frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]);
|
---|
137 | frustum.mClipPlanes[i].mD = planes[i][3];
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | void Camera::Frustum::CalcNPVertexIndices(int *indices)
|
---|
143 | {
|
---|
144 | for (int i = 0; i < 6; ++ i)
|
---|
145 | {
|
---|
146 | // n-vertex
|
---|
147 | indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
|
---|
148 | // p-vertex
|
---|
149 | indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | void Camera::SetupCameraView()
|
---|
155 | {
|
---|
156 | Matrix4x4 tview = mViewOrientation;
|
---|
157 |
|
---|
158 | Vector3 pos = -mPosition;
|
---|
159 | pos = tview * pos;
|
---|
160 |
|
---|
161 | Matrix4x4 viewOrientation = mViewOrientation;
|
---|
162 |
|
---|
163 | viewOrientation.x[3][0] = pos.x;
|
---|
164 | viewOrientation.x[3][1] = pos.y;
|
---|
165 | viewOrientation.x[3][2] = pos.z;
|
---|
166 |
|
---|
167 | glLoadMatrixf((float *)viewOrientation.x);
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
|
---|
172 | Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
|
---|
173 | {
|
---|
174 | float z_near = mNear;
|
---|
175 | float z_far = mFar;
|
---|
176 |
|
---|
177 | float fov = 120 * M_PI / 180.0f;//mFovy;
|
---|
178 |
|
---|
179 | const float w_near = 2.0f * tan(fov * 0.5f) * z_near;
|
---|
180 | const float h_near = w_near / GetAspect();
|
---|
181 |
|
---|
182 | const float w_far = 2.0f * tan(fov * 0.5f) * z_far;
|
---|
183 | const float h_far = w_far / GetAspect();
|
---|
184 |
|
---|
185 | const Vector3 view = GetDirection();
|
---|
186 | const Vector3 fc = mPosition + view * z_far;
|
---|
187 |
|
---|
188 | const Vector3 up = -GetUpVector();
|
---|
189 | const Vector3 right = -GetRightVector();
|
---|
190 |
|
---|
191 | Vector3 t1, t2;
|
---|
192 |
|
---|
193 | t1 = h_far * 0.5f * up;
|
---|
194 | t2 = w_far * 0.5f * right;
|
---|
195 |
|
---|
196 | ftl = fc + t1 - t2;
|
---|
197 | ftr = fc + t1 + t2;
|
---|
198 | fbl = fc - t1 - t2;
|
---|
199 | fbr = fc - t1 + t2;
|
---|
200 |
|
---|
201 | const Vector3 nc = mPosition + view * z_near;
|
---|
202 |
|
---|
203 | t1 = h_near * 0.5f * up;
|
---|
204 | t2 = w_near * 0.5f * right;
|
---|
205 |
|
---|
206 | ntl = nc + t1 - t2;
|
---|
207 | ntr = nc + t1 + t2;
|
---|
208 | nbl = nc - t1 - t2;
|
---|
209 | nbr = nc - t1 + t2;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | void Camera::SetOrtho(bool ortho)
|
---|
214 | {
|
---|
215 | mIsOrtho = true;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | void Camera::Yaw(float angle)
|
---|
220 | {
|
---|
221 | mYaw += angle;
|
---|
222 | CalculateFromPitchAndYaw();
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | void Camera::Pitch(float angle)
|
---|
227 | {
|
---|
228 | mPitch += angle;
|
---|
229 | CalculateFromPitchAndYaw();
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | void Camera::SetDirection(const Vector3 &dir)
|
---|
234 | {
|
---|
235 | Vector3 ndir = -Normalize(dir);
|
---|
236 |
|
---|
237 | //mPitch = atan2(dir.x, dir.z);
|
---|
238 | //mYaw = atan2(dir.y, sqrt((dir.x * dir.x) + (dir.z * dir.z)));
|
---|
239 |
|
---|
240 | //mPitch = atan2(dir.x, dir.y);
|
---|
241 | mPitch = -atan2(ndir.x, ndir.y);
|
---|
242 | mYaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y)));
|
---|
243 |
|
---|
244 | CalculateFromPitchAndYaw();
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | void Camera::CalculateFromPitchAndYaw()
|
---|
249 | {
|
---|
250 | mViewOrientation = mBaseOrientation;
|
---|
251 |
|
---|
252 | Matrix4x4 roty = RotationYMatrix(mPitch);
|
---|
253 | Matrix4x4 rotx = RotationXMatrix(mYaw);
|
---|
254 |
|
---|
255 | mViewOrientation *= roty;
|
---|
256 | mViewOrientation *= rotx;
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 | Vector3 Camera::GetDirection() const
|
---|
261 | {
|
---|
262 | return -Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | Vector3 Camera::GetUpVector() const
|
---|
267 | {
|
---|
268 | return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | Vector3 Camera::GetRightVector() const
|
---|
273 | {
|
---|
274 | return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);
|
---|
275 |
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | }
|
---|
280 |
|
---|