Camera.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "client/math/Camera.h"
  2. #include "client/Utils.h"
  3. Camera::Camera() : oldLengthAngle(0), lengthAngle(0), oldWidthAngle(0), widthAngle(0) {
  4. }
  5. const Vector& Camera::getFront() const {
  6. return front;
  7. }
  8. const Vector& Camera::getBack() const {
  9. return back;
  10. }
  11. const Vector& Camera::getRight() const {
  12. return right;
  13. }
  14. const Vector& Camera::getLeft() const {
  15. return left;
  16. }
  17. const Vector& Camera::getUp() const {
  18. return up;
  19. }
  20. const Vector& Camera::getDown() const {
  21. return down;
  22. }
  23. const Vector& Camera::getFlatFront() const {
  24. return flatFront;
  25. }
  26. const Vector& Camera::getFlatBack() const {
  27. return flatBack;
  28. }
  29. const Vector& Camera::getFlatRight() const {
  30. return flatRight;
  31. }
  32. const Vector& Camera::getFlatLeft() const {
  33. return flatLeft;
  34. }
  35. const Vector& Camera::getFlatUp() const {
  36. return flatUp;
  37. }
  38. const Vector& Camera::getFlatDown() const {
  39. return flatDown;
  40. }
  41. const Vector& Camera::getPosition() const {
  42. return interPosition;
  43. }
  44. void Camera::storePosition() {
  45. oldPosition = position;
  46. oldLengthAngle = lengthAngle;
  47. oldWidthAngle = widthAngle;
  48. }
  49. void Camera::setPosition(const Vector& pos, float length, float width) {
  50. position = pos;
  51. lengthAngle = length;
  52. widthAngle = width;
  53. }
  54. void Camera::update(float lag) {
  55. // back
  56. back.setAngles(interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
  57. // front
  58. front.setInverse(back);
  59. // right
  60. right = front;
  61. right.cross(0.0f, 1.0f, 0.0f);
  62. right.normalize();
  63. // left
  64. left.setInverse(right);
  65. // up
  66. up = front;
  67. up.cross(left);
  68. up.normalize();
  69. // down
  70. down.setInverse(up);
  71. // interpolated position
  72. interPosition = oldPosition;
  73. interPosition.addMul(position, lag);
  74. interPosition.addMul(oldPosition, -lag);
  75. // flat back
  76. flatBack = back;
  77. flatBack.setY(0.0f);
  78. flatBack.normalize();
  79. // flat back
  80. flatFront.setInverse(flatBack);
  81. // flat right
  82. flatRight = front;
  83. flatRight.cross(0.0f, 1.0f, 0.0f);
  84. flatRight.normalize();
  85. // flat left
  86. flatLeft.setInverse(flatRight);
  87. // flat up
  88. flatUp.set(0.0f, 1.0f, 0.0f);
  89. // flat down
  90. flatDown.setInverse(flatUp);
  91. }