Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Camera.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "Camera.h"
  19. #include <glm/gtc/matrix_transform.hpp>
  20. #include <algorithm>
  21. Camera::Camera()
  22. {
  23. speed_ = fp_t(0.0);
  24. position_ = glm::vec3{88.5, 22.5, 125};
  25. yaw_ = fp_t(0.0);
  26. pitch_ = fp_t(0.26666);
  27. roll_ = fp_t(0.0);
  28. updateRotationQuat();
  29. }
  30. void Camera::move(fp_t dx, fp_t dy)
  31. {
  32. glm::vec3 dp(dx * speed_, 0.0, -dy * speed_);
  33. position_ = position_ + glm::conjugate(rotationQuat_) * dp;
  34. updateViewMatrix();
  35. }
  36. void Camera::look(fp_t dx, fp_t dy)
  37. {
  38. pitch_ = glm::mod(pitch_ - fp_t(2.0) * dy, fp_t(2.0) * pi());
  39. yaw_ = glm::mod(yaw_ + fp_t(2.0) * dx, fp_t(2.0) * pi());
  40. updateRotationQuat();
  41. }
  42. void Camera::step(fp_t dt)
  43. {
  44. UNUSED(dt);
  45. }
  46. void Camera::setSpeed(fp_t newSpeed)
  47. {
  48. speed_ = newSpeed;
  49. }
  50. void Camera::setPosition(const glm::vec3& newPosition)
  51. {
  52. position_ = newPosition;
  53. updateViewMatrix();
  54. }
  55. void Camera::setHeadPosition(const glm::vec3& newHeadPosition)
  56. {
  57. headPosition_ = newHeadPosition;
  58. updateViewMatrix();
  59. }
  60. void Camera::setHeadRotation(const glm::quat& newHeadRotation)
  61. {
  62. headRotation_ = newHeadRotation;
  63. updateRotationQuat();
  64. }
  65. const glm::vec3& Camera::position() const
  66. {
  67. return position_;
  68. }
  69. const glm::quat& Camera::rotationQuat() const
  70. {
  71. return rotationQuat_;
  72. }
  73. const glm::mat4& Camera::viewMatrix() const
  74. {
  75. return viewMatrix_;
  76. }
  77. void Camera::updateRotationQuat()
  78. {
  79. glm::quat initialPitchQuat = glm::angleAxis(-pi() / fp_t(2.0), glm::vec3{1.0, 0.0, 0.0});
  80. glm::quat yawQuat = glm::angleAxis(yaw_, glm::vec3{0.0, 1.0, 0.0});
  81. glm::quat pitchQuat = glm::angleAxis(pitch_, glm::vec3{1.0, 0.0, 0.0});
  82. rotationQuat_ = headRotation_ * pitchQuat * yawQuat * initialPitchQuat;
  83. updateViewMatrix();
  84. }
  85. void Camera::updateViewMatrix()
  86. {
  87. viewMatrix_ = glm::mat4_cast(rotationQuat_) * glm::translate(glm::mat4{}, -(position_ + headPosition_));
  88. }