camera_matrix.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*************************************************************************/
  2. /* camera_matrix.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef CAMERA_MATRIX_H
  31. #define CAMERA_MATRIX_H
  32. #include "core/math/rect2.h"
  33. #include "core/math/transform.h"
  34. struct CameraMatrix {
  35. enum Planes {
  36. PLANE_NEAR,
  37. PLANE_FAR,
  38. PLANE_LEFT,
  39. PLANE_TOP,
  40. PLANE_RIGHT,
  41. PLANE_BOTTOM
  42. };
  43. real_t matrix[4][4];
  44. void set_identity();
  45. void set_zero();
  46. void set_light_bias();
  47. void set_light_atlas_rect(const Rect2 &p_rect);
  48. void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov = false);
  49. void set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov, int p_eye, real_t p_intraocular_dist, real_t p_convergence_dist);
  50. void set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_dist, real_t p_display_width, real_t p_display_to_lens, real_t p_oversample, real_t p_z_near, real_t p_z_far);
  51. void set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar);
  52. void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false);
  53. void set_frustum(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_near, real_t p_far);
  54. void set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, real_t p_near, real_t p_far, bool p_flip_fov = false);
  55. static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
  56. return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0);
  57. }
  58. real_t get_z_far() const;
  59. real_t get_z_near() const;
  60. real_t get_aspect() const;
  61. real_t get_fov() const;
  62. bool is_orthogonal() const;
  63. Vector<Plane> get_projection_planes(const Transform &p_transform) const;
  64. bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const;
  65. Vector2 get_viewport_half_extents() const;
  66. void invert();
  67. CameraMatrix inverse() const;
  68. CameraMatrix operator*(const CameraMatrix &p_matrix) const;
  69. Plane xform4(const Plane &p_vec4) const;
  70. _FORCE_INLINE_ Vector3 xform(const Vector3 &p_vec3) const;
  71. operator String() const;
  72. void scale_translate_to_fit(const AABB &p_aabb);
  73. void make_scale(const Vector3 &p_scale);
  74. int get_pixels_per_meter(int p_for_pixel_width) const;
  75. operator Transform() const;
  76. CameraMatrix();
  77. CameraMatrix(const Transform &p_transform);
  78. ~CameraMatrix();
  79. };
  80. Vector3 CameraMatrix::xform(const Vector3 &p_vec3) const {
  81. Vector3 ret;
  82. ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0];
  83. ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1];
  84. ret.z = matrix[0][2] * p_vec3.x + matrix[1][2] * p_vec3.y + matrix[2][2] * p_vec3.z + matrix[3][2];
  85. real_t w = matrix[0][3] * p_vec3.x + matrix[1][3] * p_vec3.y + matrix[2][3] * p_vec3.z + matrix[3][3];
  86. return ret / w;
  87. }
  88. #endif