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