Camera.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "Matrix4x4.h"
  27. #include "Vector3.h"
  28. namespace crown
  29. {
  30. struct ProjectionType
  31. {
  32. enum Enum
  33. {
  34. ORTHOGRAPHIC,
  35. PERSPECTIVE
  36. };
  37. };
  38. typedef Id UnitId;
  39. class Quaternion;
  40. class Unit;
  41. /// Represents the point of view into the game world.
  42. struct Camera
  43. {
  44. void create(int32_t node, const Vector3& pos, const Quaternion& rot);
  45. Vector3 local_position() const;
  46. Quaternion local_rotation() const;
  47. Matrix4x4 local_pose() const;
  48. Vector3 world_position() const;
  49. Quaternion world_rotation() const;
  50. Matrix4x4 world_pose() const;
  51. void set_local_position(Unit* unit, const Vector3& pos);
  52. void set_local_rotation(Unit* unit, const Quaternion& rot);
  53. void set_local_pose(Unit* unit, const Matrix4x4& pose);
  54. void set_projection_type(ProjectionType::Enum type);
  55. ProjectionType::Enum projection_type() const;
  56. float fov() const;
  57. void set_fov(float fov);
  58. float aspect() const;
  59. void set_aspect(float aspect);
  60. float near_clip_distance() const;
  61. void set_near_clip_distance(float near);
  62. float far_clip_distance() const;
  63. void set_far_clip_distance(float far);
  64. void set_orthographic_metrics(float left, float right, float bottom, float top);
  65. void set_viewport_metrics(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
  66. Vector3 screen_to_world(const Vector3& pos);
  67. Vector3 world_to_screen(const Vector3& pos);
  68. public:
  69. void update_projection_matrix();
  70. void update_frustum();
  71. public:
  72. int32_t m_node;
  73. Matrix4x4 m_local_pose;
  74. Matrix4x4 m_world_pose;
  75. ProjectionType::Enum m_projection_type;
  76. Matrix4x4 m_projection;
  77. Frustum m_frustum;
  78. float m_FOV;
  79. float m_aspect;
  80. float m_near;
  81. float m_far;
  82. // Orthographic projection only
  83. float m_left;
  84. float m_right;
  85. float m_bottom;
  86. float m_top;
  87. uint16_t m_view_x;
  88. uint16_t m_view_y;
  89. uint16_t m_view_width;
  90. uint16_t m_view_height;
  91. };
  92. } // namespace crown