camera.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include "camera.h"
  24. #include "types.h"
  25. #include "math_utils.h"
  26. #include "quaternion.h"
  27. #include "unit.h"
  28. #include "scene_graph.h"
  29. #include "assert.h"
  30. #include "vector4.h"
  31. #include "frustum.h"
  32. namespace crown
  33. {
  34. Camera::Camera(SceneGraph& sg, int32_t node, ProjectionType::Enum type, float near, float far)
  35. : _scene_graph(sg)
  36. , _node(node)
  37. , _projection_type(type)
  38. , _near(near)
  39. , _far(far)
  40. {
  41. update_projection_matrix();
  42. }
  43. Vector3 Camera::local_position() const
  44. {
  45. return _scene_graph.local_position(_node);
  46. }
  47. Quaternion Camera::local_rotation() const
  48. {
  49. return _scene_graph.local_rotation(_node);
  50. }
  51. Matrix4x4 Camera::local_pose() const
  52. {
  53. return _scene_graph.local_pose(_node);
  54. }
  55. Vector3 Camera::world_position() const
  56. {
  57. return _scene_graph.world_position(_node);
  58. }
  59. Quaternion Camera::world_rotation() const
  60. {
  61. return _scene_graph.world_rotation(_node);
  62. }
  63. Matrix4x4 Camera::world_pose() const
  64. {
  65. return _scene_graph.world_pose(_node);
  66. }
  67. void Camera::set_local_position(Unit* unit, const Vector3& pos)
  68. {
  69. unit->set_local_position(_node, pos);
  70. }
  71. void Camera::set_local_rotation(Unit* unit, const Quaternion& rot)
  72. {
  73. unit->set_local_rotation(_node, rot);
  74. }
  75. void Camera::set_local_pose(Unit* unit, const Matrix4x4& pose)
  76. {
  77. unit->set_local_pose(_node, pose);
  78. }
  79. void Camera::set_projection_type(ProjectionType::Enum type)
  80. {
  81. _projection_type = type;
  82. update_projection_matrix();
  83. }
  84. ProjectionType::Enum Camera::projection_type() const
  85. {
  86. return _projection_type;
  87. }
  88. const Matrix4x4& Camera::projection_matrix() const
  89. {
  90. return _projection;
  91. }
  92. float Camera::fov() const
  93. {
  94. return _FOV;
  95. }
  96. void Camera::set_fov(float fov)
  97. {
  98. _FOV = fov;
  99. update_projection_matrix();
  100. }
  101. float Camera::aspect() const
  102. {
  103. return _aspect;
  104. }
  105. void Camera::set_aspect(float aspect)
  106. {
  107. _aspect = aspect;
  108. update_projection_matrix();
  109. }
  110. float Camera::near_clip_distance() const
  111. {
  112. return _near;
  113. }
  114. void Camera::set_near_clip_distance(float near)
  115. {
  116. _near = near;
  117. update_projection_matrix();
  118. }
  119. float Camera::far_clip_distance() const
  120. {
  121. return _far;
  122. }
  123. void Camera::set_far_clip_distance(float far)
  124. {
  125. _far = far;
  126. update_projection_matrix();
  127. }
  128. void Camera::set_orthographic_metrics(float left, float right, float bottom, float top)
  129. {
  130. _left = left;
  131. _right = right;
  132. _bottom = bottom;
  133. _top = top;
  134. update_projection_matrix();
  135. }
  136. void Camera::set_viewport_metrics(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  137. {
  138. _view_x = x;
  139. _view_y = y;
  140. _view_width = width;
  141. _view_height = height;
  142. }
  143. Vector3 Camera::screen_to_world(const Vector3& pos)
  144. {
  145. using namespace matrix4x4;
  146. Matrix4x4 world_inv = world_pose();
  147. invert(world_inv);
  148. Matrix4x4 mvp = world_inv * _projection;
  149. invert(mvp);
  150. Vector4 ndc( (2 * (pos.x - 0)) / _view_width - 1,
  151. (2 * (_view_height - pos.y)) / _view_height - 1,
  152. (2 * pos.z) - 1, 1);
  153. Vector4 tmp = ndc * mvp;
  154. tmp *= 1.0f / tmp.w;
  155. return Vector3(tmp.x, tmp.y, tmp.z);
  156. }
  157. Vector3 Camera::world_to_screen(const Vector3& pos)
  158. {
  159. using namespace matrix4x4;
  160. Matrix4x4 world_inv = world_pose();
  161. invert(world_inv);
  162. Vector3 ndc = pos * (world_inv * _projection);
  163. return Vector3( (_view_x + _view_width * (ndc.x + 1.0f)) / 2.0f,
  164. (_view_y + _view_height * (ndc.y + 1.0f)) / 2.0f,
  165. (ndc.z + 1.0f) / 2.0f);
  166. }
  167. void Camera::update_projection_matrix()
  168. {
  169. switch (_projection_type)
  170. {
  171. case ProjectionType::ORTHOGRAPHIC:
  172. {
  173. matrix4x4::set_orthographic(_projection, _left, _right, _bottom, _top, _near, _far);
  174. break;
  175. }
  176. case ProjectionType::PERSPECTIVE:
  177. {
  178. matrix4x4::set_perspective(_projection, _FOV, _aspect, _near, _far);
  179. break;
  180. }
  181. default:
  182. {
  183. CE_FATAL("Oops, unknown projection type");
  184. break;
  185. }
  186. }
  187. }
  188. void Camera::update_frustum()
  189. {
  190. // TODO
  191. //m_frustum.from_matrix(_projection * m_view);
  192. }
  193. } // namespace crown