Camera.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <boost/foreach.hpp>
  2. #include "Camera.h"
  3. //==============================================================================
  4. // Constructors & desrtuctor =
  5. //==============================================================================
  6. Camera::~Camera()
  7. {}
  8. //==============================================================================
  9. // lookAtPoint =
  10. //==============================================================================
  11. void Camera::lookAtPoint(const Vec3& point)
  12. {
  13. const Vec3& j = Vec3(0.0, 1.0, 0.0);
  14. Vec3 vdir = (point - getLocalTransform().getOrigin()).getNormalized();
  15. Vec3 vup = j - vdir * j.dot(vdir);
  16. Vec3 vside = vdir.cross(vup);
  17. getLocalTransform().getRotation().setColumns(vside, vup, -vdir);
  18. }
  19. //==============================================================================
  20. // updateWSpaceFrustumPlanes =
  21. //==============================================================================
  22. void Camera::updateWSpaceFrustumPlanes()
  23. {
  24. for(uint i = 0; i < FP_NUM; i++)
  25. {
  26. wspaceFrustumPlanes[i] =
  27. lspaceFrustumPlanes[i].getTransformed(getWorldTransform());
  28. }
  29. }
  30. //==============================================================================
  31. // insideFrustum =
  32. //==============================================================================
  33. bool Camera::insideFrustum(const CollisionShape& bvol) const
  34. {
  35. BOOST_FOREACH(const Plane& plane, wspaceFrustumPlanes)
  36. {
  37. if(bvol.testPlane(plane) < 0.0)
  38. {
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44. //==============================================================================
  45. // insideFrustum =
  46. //==============================================================================
  47. bool Camera::insideFrustum(const Camera& cam) const
  48. {
  49. const uint MAX_EXTREME_POINTS_NUM = 10;
  50. Vec3 points[MAX_EXTREME_POINTS_NUM];
  51. uint pointsNum;
  52. cam.getExtremePoints(points, pointsNum);
  53. ASSERT(pointsNum < MAX_EXTREME_POINTS_NUM);
  54. // the collision code
  55. for(uint i = 0; i < 6; i++) // for the 6 planes
  56. {
  57. uint failed = 0;
  58. for(uint j = 0; j < pointsNum; j++) // for the n points
  59. {
  60. if(wspaceFrustumPlanes[i].test(points[j]) < 0.0)
  61. {
  62. ++failed;
  63. }
  64. }
  65. if(failed == pointsNum)
  66. {
  67. // if all points are behind the plane then the cam is not in
  68. // frustum
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. //==============================================================================
  75. // updateViewMatrix =
  76. //==============================================================================
  77. void Camera::updateViewMatrix()
  78. {
  79. /*
  80. * The point at which the camera looks:
  81. * Vec3 viewpoint = translationLspace + z_axis;
  82. * as we know the up vector, we can easily use gluLookAt:
  83. * gluLookAt(translationLspace.x, translationLspace.x,
  84. * translationLspace.z, z_axis.x, z_axis.y, z_axis.z, y_axis.x,
  85. * y_axis.y, y_axis.z);
  86. */
  87. // The view matrix is: Mview = camera.world_transform.Inverted().
  88. // But instead of inverting we do the following:
  89. Mat3 camInvertedRot = getWorldTransform().getRotation().getTransposed();
  90. camInvertedRot *= 1.0 / getWorldTransform().getScale();
  91. Vec3 camInvertedTsl = -(camInvertedRot * getWorldTransform().getOrigin());
  92. viewMat = Mat4(camInvertedTsl, camInvertedRot);
  93. }
  94. //==============================================================================
  95. // moveUpdate =
  96. //==============================================================================
  97. void Camera::moveUpdate()
  98. {
  99. updateViewMatrix();
  100. updateWSpaceFrustumPlanes();
  101. }