camera.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "math_types.h"
  26. #include "world_types.h"
  27. namespace crown
  28. {
  29. struct ProjectionType
  30. {
  31. enum Enum
  32. {
  33. ORTHOGRAPHIC,
  34. PERSPECTIVE
  35. };
  36. };
  37. struct Quaternion;
  38. struct Unit;
  39. struct SceneGraph;
  40. /// Represents the point of view into the game world.
  41. ///
  42. /// @ingroup World
  43. struct Camera
  44. {
  45. Camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far);
  46. /// Returns the local position of the camera.
  47. Vector3 local_position() const;
  48. /// Returns the local rotation of the camera.
  49. Quaternion local_rotation() const;
  50. /// Returns the local pose of the camera.
  51. Matrix4x4 local_pose() const;
  52. /// Returns the world position of the camera.
  53. Vector3 world_position() const;
  54. /// Returns the world rotation of the camera.
  55. Quaternion world_rotation() const;
  56. /// Returns the world pose of the camera.
  57. Matrix4x4 world_pose() const;
  58. /// Sets the local position of the camera.
  59. void set_local_position(Unit* unit, const Vector3& pos);
  60. /// Sets the local rotation of the camera.
  61. void set_local_rotation(Unit* unit, const Quaternion& rot);
  62. /// Sets the local pose of the camera.
  63. void set_local_pose(Unit* unit, const Matrix4x4& pose);
  64. /// Sets the projection type of the camera.
  65. void set_projection_type(ProjectionType::Enum type);
  66. /// Returns the projection type of the camera.
  67. ProjectionType::Enum projection_type() const;
  68. /// Returns the projection matrix of the camera.
  69. const Matrix4x4& projection_matrix() const;
  70. /// Returns the field-of-view of the camera in degrees.
  71. float fov() const;
  72. /// Sets the field-of-view of the camera in degrees.
  73. void set_fov(float fov);
  74. /// Returns the aspect ratio of the camera. (Perspective projection only.)
  75. float aspect() const;
  76. /// Sets the aspect ratio of the camera. (Perspective projection only.)
  77. void set_aspect(float aspect);
  78. /// Returns the near clip distance of the camera.
  79. float near_clip_distance() const;
  80. /// Sets the near clip distance of the camera.
  81. void set_near_clip_distance(float near);
  82. /// Returns the far clip distance of the camera.
  83. float far_clip_distance() const;
  84. /// Sets the far clip distance of the camera.
  85. void set_far_clip_distance(float far);
  86. /// Sets the coordinates for orthographic clipping planes. (Orthographic projection only.)
  87. void set_orthographic_metrics(float left, float right, float bottom, float top);
  88. /// Sets the coordinates for the camera viewport in pixels.
  89. void set_viewport_metrics(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
  90. /// Returns @a pos from screen-space to world-space coordinates.
  91. Vector3 screen_to_world(const Vector3& pos);
  92. /// Returns @a pos from world-space to screen-space coordinates.
  93. Vector3 world_to_screen(const Vector3& pos);
  94. public:
  95. void update_projection_matrix();
  96. void update_frustum();
  97. public:
  98. SceneGraph& m_scene_graph;
  99. int32_t m_node;
  100. ProjectionType::Enum m_projection_type;
  101. Matrix4x4 m_projection;
  102. Frustum m_frustum;
  103. float m_FOV;
  104. float m_aspect;
  105. float m_near;
  106. float m_far;
  107. // Orthographic projection only
  108. float m_left;
  109. float m_right;
  110. float m_bottom;
  111. float m_top;
  112. uint16_t m_view_x;
  113. uint16_t m_view_y;
  114. uint16_t m_view_width;
  115. uint16_t m_view_height;
  116. };
  117. } // namespace crown