camera.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. #define GLM_ENABLE_EXPERIMENTAL
  3. #include <glm/glm.hpp>
  4. #include <glm/gtc/matrix_transform.hpp>
  5. #include <glm/gtc/type_ptr.hpp>
  6. #include <glm/mat3x3.hpp>
  7. #include <glm/gtx/transform.hpp>
  8. int getViewDirectionRotation(glm::vec3 vec);
  9. struct Camera
  10. {
  11. Camera() = default;
  12. Camera(float aspectRatio, float fovRadians)
  13. :aspectRatio(aspectRatio),
  14. fovRadians(fovRadians)
  15. {
  16. }
  17. glm::vec3 up = {0.f,1.f,0.f};
  18. float aspectRatio = 1;
  19. float fovRadians = glm::radians(70.f);
  20. float closePlane = 0.01f;
  21. float farPlane = 800.f;
  22. glm::dvec3 position = {};
  23. glm::vec3 viewDirection = {0,0,-1};
  24. glm::mat4x4 getProjectionMatrix();
  25. glm::dmat4x4 getProjectionMatrixDouble();
  26. glm::mat4x4 getViewMatrix();
  27. glm::mat4x4 getViewMatrixWithPosition();
  28. glm::dmat4x4 getViewMatrixWithPositionDouble();
  29. glm::mat4x4 getViewProjectionWithPositionMatrix();
  30. glm::dmat4x4 getViewProjectionWithPositionMatrixDouble();
  31. int getViewDirectionRotation()
  32. {
  33. return ::getViewDirectionRotation(viewDirection);
  34. }
  35. void rotateCamera(const glm::vec2 delta);
  36. float yaw = 0.f;
  37. float pitch = 0.f;
  38. void moveFPS(glm::vec3 direction);
  39. void rotateFPS(glm::ivec2 mousePos, float speed, bool shouldMove);
  40. void rotateFPSController(glm::vec2 vector, float speed);
  41. glm::ivec2 lastMousePos = {};
  42. glm::mat4x4 lastFrameViewProjMatrix = getProjectionMatrix() * getViewMatrix();
  43. bool operator==(const Camera& other)
  44. {
  45. return
  46. (up == other.up)
  47. && (aspectRatio == other.aspectRatio)
  48. && (fovRadians == other.fovRadians)
  49. && (closePlane == other.closePlane)
  50. && (farPlane == other.farPlane)
  51. && (position == other.position)
  52. && (viewDirection == other.viewDirection)
  53. ;
  54. };
  55. bool operator!=(const Camera& other)
  56. {
  57. return !(*this == other);
  58. };
  59. void decomposePosition(glm::vec3 &floatPart, glm::ivec3 &intPart);
  60. void decomposePosition(glm::vec3 &floatPart, glm::dvec3 in, glm::ivec3 &intPart);
  61. };
  62. void decomposePosition(glm::dvec3 in, glm::vec3 &floatPart, glm::ivec3 &intPart);
  63. void decomposePosition(glm::dvec3 in, glm::vec4 &floatPart, glm::ivec4 &intPart);
  64. glm::ivec3 from3DPointToBlock(glm::dvec3 in);
  65. glm::mat4 lookAtSafe(glm::vec3 const &eye, glm::vec3 const &center, glm::vec3 const &upVec);
  66. glm::dmat4 lookAtSafe(glm::dvec3 const &eye, glm::dvec3 const &center, glm::dvec3 const &upVec);