Camera.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <boost/foreach.hpp>
  2. #include "anki/scene/Camera.h"
  3. namespace anki {
  4. //==============================================================================
  5. Camera::~Camera()
  6. {}
  7. //==============================================================================
  8. // lookAtPoint =
  9. //==============================================================================
  10. void Camera::lookAtPoint(const Vec3& point)
  11. {
  12. const Vec3& j = Vec3(0.0, 1.0, 0.0);
  13. Vec3 vdir = (point - getLocalTransform().getOrigin()).getNormalized();
  14. Vec3 vup = j - vdir * j.dot(vdir);
  15. Vec3 vside = vdir.cross(vup);
  16. getLocalTransform().getRotation().setColumns(vside, vup, -vdir);
  17. }
  18. //==============================================================================
  19. // updateWSpaceFrustumPlanes =
  20. //==============================================================================
  21. void Camera::updateWSpaceFrustumPlanes()
  22. {
  23. for(uint i = 0; i < FP_NUM; i++)
  24. {
  25. wspaceFrustumPlanes[i] =
  26. lspaceFrustumPlanes[i].getTransformed(getWorldTransform());
  27. }
  28. }
  29. //==============================================================================
  30. // insideFrustum =
  31. //==============================================================================
  32. bool Camera::insideFrustum(const CollisionShape& bvol) const
  33. {
  34. BOOST_FOREACH(const Plane& plane, wspaceFrustumPlanes)
  35. {
  36. if(bvol.testPlane(plane) < 0.0)
  37. {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. //==============================================================================
  44. // updateViewMatrix =
  45. //==============================================================================
  46. void Camera::updateViewMatrix()
  47. {
  48. /*
  49. * The point at which the camera looks:
  50. * Vec3 viewpoint = translationLspace + z_axis;
  51. * as we know the up vector, we can easily use gluLookAt:
  52. * gluLookAt(translationLspace.x, translationLspace.x,
  53. * translationLspace.z, z_axis.x, z_axis.y, z_axis.z, y_axis.x,
  54. * y_axis.y, y_axis.z);
  55. */
  56. // The view matrix is: Mview = camera.world_transform.Inverted().
  57. // But instead of inverting we do the following:
  58. Mat3 camInvertedRot = getWorldTransform().getRotation().getTransposed();
  59. camInvertedRot *= 1.0 / getWorldTransform().getScale();
  60. Vec3 camInvertedTsl = -(camInvertedRot * getWorldTransform().getOrigin());
  61. viewMat = Mat4(camInvertedTsl, camInvertedRot);
  62. }
  63. //==============================================================================
  64. // moveUpdate =
  65. //==============================================================================
  66. void Camera::moveUpdate()
  67. {
  68. updateViewMatrix();
  69. updateWSpaceFrustumPlanes();
  70. }
  71. } // end namespace