Camera.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "Frustum.h"
  26. #include "Mat4.h"
  27. #include "Mat3.h"
  28. #include "Vec3.h"
  29. namespace crown
  30. {
  31. /// Represents the point of view into the game world.
  32. class Camera
  33. {
  34. public:
  35. /// Construct the camera placed at the given world-space @a position
  36. /// with the given @a fov field of view and @a aspect ratio.
  37. Camera(const Vec3& position, float fov, float aspect);
  38. /// Returns the world-space position of the camera
  39. const Vec3& position() const;
  40. /// Sets the world-space @a position of the camera
  41. void set_position(const Vec3& position);
  42. /// Returns the lookat-point of the camera
  43. const Vec3& look_at() const;
  44. /// Sets the lookat-point of the camera
  45. void set_look_at(const Vec3& lookat);
  46. /// Sets the rotation of the camera about the world's @a x axis and @a y axis
  47. void set_rotation(const float x, const float y);
  48. /// Returns the up-vector of the camera
  49. const Vec3& up() const;
  50. /// Returns the field of view of the camera in degrees
  51. float fov() const;
  52. /// Sets the field of view of the camera
  53. void set_fov(float fov);
  54. /// Returns the aspect ratio of the camera
  55. float aspect() const;
  56. /// Sets the aspect ration of the camera
  57. void set_aspect(float aspect);
  58. /// Returns the near clipping distance of the camera
  59. float near_clip_distance() const;
  60. /// Sets the near clipping distance of the camera
  61. void set_near_clip_distance(float near);
  62. /// Returns the far clipping distance of the camera
  63. float far_clip_distance() const;
  64. /// Sets the far clipping distance of the camera
  65. void set_far_clip_distance(float far);
  66. /// Returns the view-frustum of the camera
  67. const Frustum& frustum() const;
  68. /// Returns the renderer-independent projection matrix used by the camera
  69. const Mat4& projection_matrix() const;
  70. /// Returns the renderer-independent view matrix used by the camera
  71. const Mat4& view_matrix() const;
  72. /// Moves the camera towards look direction by @a meters meters
  73. void move_forward(float meters);
  74. /// Moves the camera towards the opposite look direction by @a meters meters
  75. void move_backward(float meters);
  76. /// Moves the camera along the axis perpendicular to the look direction by @a meters meters
  77. void strafe_left(float meters);
  78. /// Moves the camera along the axis perpendicular to the look direction by @a meters meters
  79. void strafe_right(float meters);
  80. protected:
  81. void update_projection_matrix();
  82. void update_view_matrix();
  83. void update_frustum();
  84. Vec3 m_position;
  85. Vec3 m_look_at;
  86. Vec3 m_up;
  87. Vec2 m_rot_factor;
  88. float m_angle_x;
  89. float m_angle_y;
  90. Mat4 m_view;
  91. Mat4 m_projection;
  92. Frustum m_frustum;
  93. float m_FOV;
  94. float m_aspect;
  95. float m_near;
  96. float m_far;
  97. };
  98. } // namespace crown