Open Source Tomb Raider Engine
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.

Camera.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*!
  2. * \file src/Camera.cpp
  3. * \brief Camera class
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <glm/gtc/matrix_transform.hpp>
  9. #include "global.h"
  10. #include "Camera.h"
  11. glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
  12. float Camera::thetaX = 0.0f;
  13. float Camera::thetaY = 0.0f;
  14. float Camera::rotationDeltaX = 0.75f;
  15. float Camera::rotationDeltaY = 0.75f;
  16. void Camera::reset() {
  17. pos = glm::vec3(0.0f, 0.0f, 0.0f);
  18. thetaX = 0.0f;
  19. thetaY = 0.0f;
  20. }
  21. void Camera::handleAction(ActionEvents action, bool isFinished) {
  22. if (isFinished)
  23. return;
  24. const static float step = 256.0f;
  25. glm::vec3 dir(
  26. glm::cos(thetaY) * glm::sin(thetaX),
  27. glm::sin(thetaY),
  28. glm::cos(thetaY) * glm::cos(thetaX)
  29. );
  30. glm::vec3 right(
  31. glm::sin(thetaX - glm::pi<float>() / 2.0f),
  32. 0.0f,
  33. glm::cos(thetaX - glm::pi<float>() / 2.0f)
  34. );
  35. glm::vec3 up = glm::cross(right, dir);
  36. if (action == forwardAction) {
  37. pos += dir * step;
  38. } else if (action == backwardAction) {
  39. pos -= dir * step;
  40. } else if (action == leftAction) {
  41. pos += right * step;
  42. } else if (action == rightAction) {
  43. pos -= right * step;
  44. } else if (action == jumpAction) {
  45. pos += up * step;
  46. } else if (action == crouchAction) {
  47. pos -= up * step;
  48. } else if (action == useAction) {
  49. } else if (action == holsterAction) {
  50. } else if (action == walkAction) {
  51. }
  52. }
  53. void Camera::handleMouseMotion(int x, int y) {
  54. while (x > 0) {
  55. thetaX += rotationDeltaX;
  56. x--;
  57. }
  58. while (x < 0) {
  59. thetaX -= rotationDeltaX;
  60. x++;
  61. }
  62. while (y > 0) {
  63. if (thetaY > -(glm::pi<float>() / 2.0f)) {
  64. thetaY -= rotationDeltaY;
  65. }
  66. y--;
  67. }
  68. while (y < 0) {
  69. if (thetaY < (glm::pi<float>() / 2.0f)) {
  70. thetaY += rotationDeltaY;
  71. }
  72. y++;
  73. }
  74. while (thetaX > (glm::pi<float>() * 2.0f))
  75. thetaX -= glm::pi<float>() * 2.0f;
  76. while (thetaX < -(glm::pi<float>() * 2.0f))
  77. thetaX += glm::pi<float>() * 2.0f;
  78. }
  79. glm::mat4 Camera::getViewMatrix() {
  80. glm::vec3 dir(
  81. glm::cos(thetaY) * glm::sin(thetaX),
  82. glm::sin(thetaY),
  83. glm::cos(thetaY) * glm::cos(thetaX)
  84. );
  85. glm::vec3 right(
  86. glm::sin(thetaX - glm::pi<float>() / 2.0f),
  87. 0.0f,
  88. glm::cos(thetaX - glm::pi<float>() / 2.0f)
  89. );
  90. glm::vec3 up = glm::cross(right, dir);
  91. return glm::lookAt(pos, pos + dir, up);
  92. }