Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 869 vs 877 for /trunk/Source Code/Rendering/Camera/Camera.cpp

Diff revisions: vs.
  @@ -11,6 +11,7 @@
11 11 _movementLocked(false),
12 12 _frustumLocked(false),
13 13 _frustumDirty(true),
14 + _reflectionActive(false),
14 15 _mouseSensitivity(1.0f),
15 16 _zoomSpeedFactor(35.0f),
16 17 _moveSpeedFactor(35.0f),
  @@ -50,6 +51,8 @@
50 51 _cameraMoveSpeed = camera._cameraMoveSpeed;
51 52 _cameraTurnSpeed = camera._cameraTurnSpeed;
52 53 _cameraZoomSpeed = camera._cameraZoomSpeed;
54 + _reflectionPlane = camera._reflectionPlane;
55 + _reflectionActive = camera._reflectionActive;
53 56
54 57 lockFrustum(camera._frustumLocked);
55 58 _accumPitchDegrees = camera._accumPitchDegrees;
  @@ -202,20 +205,62 @@
202 205 _viewMatrixDirty = true;
203 206 }
204 207
208 + vec3<F32> ExtractCameraPos2(const mat4<F32>& a_modelView)
209 + {
210 + // Get the 3 basis vector planes at the camera origin and transform them into model space.
211 + //
212 + // NOTE: Planes have to be transformed by the inverse transpose of a matrix
213 + // Nice reference here: http://www.opengl.org/discussion_boards/showthread.php/159564-Clever-way-to-transform-plane-by-matrix
214 + //
215 + // So for a transform to model space we need to do:
216 + // inverse(transpose(inverse(MV)))
217 + // This equals : transpose(MV) - see Lemma 5 in http://mathrefresher.blogspot.com.au/2007/06/transpose-of-matrix.html
218 + //
219 + // As each plane is simply (1,0,0,0), (0,1,0,0), (0,0,1,0) we can pull the data directly from the transpose matrix.
220 + //
221 + mat4<F32> modelViewT(a_modelView.getTranspose());
222 +
223 + // Get plane normals
224 + vec4<F32> n1(modelViewT.getRow(0));
225 + vec4<F32> n2(modelViewT.getRow(1));
226 + vec4<F32> n3(modelViewT.getRow(2));
227 +
228 + // Get plane distances
229 + F32 d1(n1.w);
230 + F32 d2(n2.w);
231 + F32 d3(n3.w);
232 +
233 + // Get the intersection of these 3 planes
234 + // (uisng math from RealTime Collision Detection by Christer Ericson)
235 + vec3<F32> n2n3 = Cross(n2.xyz(), n3.xyz());
236 + float denom = Dot(n1.xyz(), n2n3);
237 +
238 + vec3<F32> top = (n2n3 * d1) + Cross(n1.xyz(), (d3*n2.xyz()) - (d2*n3.xyz()));
239 + return top / -denom;
240 + }
241 + const mat4<F32>& Camera::lookAt(const mat4<F32>& viewMatrix) {
242 + _eye.set(ExtractCameraPos2(viewMatrix));
243 + _orientation.fromMatrix(viewMatrix);
244 + _viewMatrixDirty = true;
245 + _frustumDirty = true;
246 + updateViewMatrix();
247 +
248 + return getViewMatrix();
249 + }
250 +
205 251 const mat4<F32>& Camera::lookAt(const vec3<F32>& eye,
206 - const vec3<F32>& target,
252 + const vec3<F32>& direction,
207 253 const vec3<F32>& up) {
208 254 _eye.set(eye);
209 - _target.set(target);
210 - _viewMatrix.lookAt(_eye, _target, up);
211 - // Extract the pitch angle from the view matrix.
212 - _accumPitchDegrees = Angle::to_DEGREES(std::asinf(getForwardDir().y));
213 - _orientation.fromLookAt(_eye - _target, up);
255 + vec3<F32> target(eye + direction);
214 256
215 - _viewMatrixDirty = false;
257 + _orientation.fromLookAt(direction, up);
258 + _viewMatrixDirty = true;
216 259 _frustumDirty = true;
217 260
218 - return _viewMatrix;
261 + updateViewMatrix();
262 +
263 + return getViewMatrix();
219 264 }
220 265
221 266 /// Tell the rendering API to set up our desired PoV
  @@ -228,13 +273,15 @@
228 273 }
229 274 }
230 275
231 - void Camera::reflect(const Plane<F32>& reflectionPlane) {
232 - mat4<F32> reflectedMatrix;
233 - reflectedMatrix.reflect(reflectionPlane);
234 -
235 - lookAt(reflectedMatrix * getEye(),
236 - getTarget(),
237 - reflectedMatrix * getUpDir());
276 + void Camera::setReflection(const Plane<F32>& reflectionPlane) {
277 + _reflectionPlane = reflectionPlane;
278 + _reflectionActive = true;
279 + _viewMatrixDirty = true;
280 + }
281 +
282 + void Camera::clearReflection() {
283 + _reflectionActive = false;
284 + _viewMatrixDirty = true;
238 285 }
239 286
240 287 bool Camera::updateProjection() {
  @@ -312,12 +359,19 @@
312 359 vec3<F32> yAxis = _orientation.yAxis();
313 360 vec3<F32> zAxis = _orientation.zAxis();
314 361
315 - _target = -zAxis + _eye;
362 + vec3<F32> target = -zAxis + _eye;
316 363
317 364 _viewMatrix.set(rotation);
318 365 _viewMatrix.setRow(3, -xAxis.dot(_eye), -yAxis.dot(_eye), -zAxis.dot(_eye), 1.0f);
319 366 _orientation.getEuler(_euler);
320 367 _euler = Angle::to_DEGREES(_euler);
368 + // Extract the pitch angle from the view matrix.
369 + _accumPitchDegrees = Angle::to_DEGREES(std::asinf(getForwardDir().y));
370 +
371 + if (_reflectionActive) {
372 + _viewMatrix.reflect(_reflectionPlane);
373 + _eye.set(mat4<F32>(_reflectionPlane).transformNonHomogeneous(_eye));
374 + }
321 375
322 376 _viewMatrixDirty = false;
323 377 _frustumDirty = true;
  @@ -334,7 +388,7 @@
334 388 return false;
335 389 }
336 390
337 - _frustum->Extract(_viewMatrix, _projectionMatrix);
391 + _frustum->Extract(getViewMatrix(), _projectionMatrix);
338 392
339 393 _frustumDirty = false;
340 394