💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.3 KiB

7 years ago
  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "camera-controller.hpp"
  20. CameraController::CameraController():
  21. camera(nullptr)
  22. {}
  23. SurfaceCameraController::SurfaceCameraController():
  24. elevationRotation(1, 0, 0, 0),
  25. azimuthRotation(1, 0, 0, 0),
  26. rotation(1, 0, 0, 0),
  27. targetElevationRotation(1, 0, 0, 0),
  28. targetAzimuthRotation(1, 0, 0, 0),
  29. targetRotation(1, 0, 0, 0)
  30. {}
  31. SurfaceCameraController::~SurfaceCameraController()
  32. {}
  33. void SurfaceCameraController::update(float dt)
  34. {
  35. float interpolationFactor = 0.25f / (1.0 / 60.0f) * dt;
  36. // Calculate rotation and target rotation quaternions
  37. //rotation = azimuthRotation * elevationRotation;
  38. targetRotation = targetAzimuthRotation * targetElevationRotation;
  39. // Calculate target translation
  40. targetTranslation = targetFocalPoint + targetRotation * Vector3(0.0f, 0.0f, targetFocalDistance);
  41. // Interpolate rotation
  42. //rotation = glm::mix(rotation, targetRotation, interpolationFactor);
  43. // Interpolate angles
  44. setElevation(glm::mix(elevation, targetElevation, interpolationFactor));
  45. setAzimuth(glm::mix(azimuth, targetAzimuth, interpolationFactor));
  46. // Calculate rotation
  47. rotation = azimuthRotation * elevationRotation;
  48. // Interpolate focal point and focal distance
  49. focalPoint = glm::mix(focalPoint, targetFocalPoint, interpolationFactor);
  50. focalDistance = glm::mix(focalDistance, targetFocalDistance, interpolationFactor);
  51. // Caluclate translation
  52. translation = focalPoint + rotation * Vector3(0.0f, 0.0f, focalDistance);
  53. /*
  54. // Recalculate azimuth
  55. azimuthRotation = rotation;
  56. azimuthRotation.x = 0.0f;
  57. azimuthRotation.z = 0.0f;
  58. azimuthRotation = glm::normalize(azimuthRotation);
  59. azimuth = 2.0f * std::acos(azimuthRotation.w);
  60. // Recalculate elevation
  61. elevationRotation = rotation;
  62. elevationRotation.y = 0.0f;
  63. elevationRotation.z = 0.0f;
  64. elevationRotation = glm::normalize(elevationRotation);
  65. elevation = 2.0f * std::acos(elevationRotation.w);
  66. */
  67. // Update camera
  68. if (camera != nullptr)
  69. {
  70. camera->lookAt(translation, focalPoint, Vector3(0.0f, 1.0f, 0.0f));
  71. }
  72. }
  73. void SurfaceCameraController::move(Vector2 direction)
  74. {
  75. targetFocalPoint += azimuthRotation * Vector3(direction.x, 0.0f, direction.y);
  76. }
  77. void SurfaceCameraController::rotate(float angle)
  78. {
  79. setTargetAzimuth(targetAzimuth + angle);
  80. }
  81. void SurfaceCameraController::zoom(float distance)
  82. {
  83. setTargetFocalDistance(targetFocalDistance - distance);
  84. }
  85. void SurfaceCameraController::setFocalPoint(const Vector3& point)
  86. {
  87. focalPoint = point;
  88. }
  89. void SurfaceCameraController::setFocalDistance(float distance)
  90. {
  91. focalDistance = distance;
  92. }
  93. void SurfaceCameraController::setElevation(float angle)
  94. {
  95. elevation = angle;
  96. elevationRotation = glm::angleAxis(elevation, Vector3(-1.0f, 0.0f, 0.0f));
  97. }
  98. void SurfaceCameraController::setAzimuth(float angle)
  99. {
  100. azimuth = angle;
  101. azimuthRotation = glm::angleAxis(azimuth, Vector3(0.0f, 1.0f, 0.0f));
  102. }
  103. void SurfaceCameraController::setTargetFocalPoint(const Vector3& point)
  104. {
  105. targetFocalPoint = point;
  106. }
  107. void SurfaceCameraController::setTargetFocalDistance(float distance)
  108. {
  109. targetFocalDistance = distance;
  110. }
  111. void SurfaceCameraController::setTargetElevation(float angle)
  112. {
  113. targetElevation = angle;
  114. targetElevationRotation = glm::angleAxis(targetElevation, Vector3(-1.0f, 0.0f, 0.0f));
  115. }
  116. void SurfaceCameraController::setTargetAzimuth(float angle)
  117. {
  118. targetAzimuth = angle;
  119. targetAzimuthRotation = glm::angleAxis(targetAzimuth, Vector3(0.0f, 1.0f, 0.0f));
  120. }
  121. TunnelCameraController::TunnelCameraController()
  122. {}
  123. TunnelCameraController::~TunnelCameraController()
  124. {}
  125. void TunnelCameraController::update(float dt)
  126. {
  127. }