CameraMatrix.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef CAMERA_MATRIX_H
  2. #define CAMERA_MATRIX_H
  3. #include "Defs.hpp"
  4. #include "Plane.hpp"
  5. #include "Rect2.hpp"
  6. #include "Transform.hpp"
  7. #include "Math.hpp"
  8. #include <vector>
  9. namespace {
  10. using namespace godot;
  11. } // namespace
  12. struct CameraMatrix {
  13. enum Planes {
  14. PLANE_NEAR,
  15. PLANE_FAR,
  16. PLANE_LEFT,
  17. PLANE_TOP,
  18. PLANE_RIGHT,
  19. PLANE_BOTTOM
  20. };
  21. real_t matrix[4][4];
  22. void set_identity();
  23. void set_zero();
  24. void set_light_bias();
  25. void set_light_atlas_rect(const Rect2 &p_rect);
  26. 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);
  27. 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);
  28. 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);
  29. 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);
  30. void set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov = false);
  31. 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);
  32. 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);
  33. static real_t get_fovy(real_t p_fovx, real_t p_aspect) {
  34. return Math::rad2deg(atan(p_aspect * tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0);
  35. }
  36. static inline double deg2rad(double p_y) { return p_y * Math_PI / 180.0; }
  37. static inline float deg2rad(float p_y) { return p_y * Math_PI / 180.0; }
  38. static inline double rad2deg(double p_y) { return p_y * 180.0 / Math_PI; }
  39. static inline float rad2deg(float p_y) { return p_y * 180.0 / Math_PI; }
  40. static inline double absd(double g) {
  41. union {
  42. double d;
  43. uint64_t i;
  44. } u;
  45. u.d = g;
  46. u.i &= (uint64_t)9223372036854775807ll;
  47. return u.d;
  48. }
  49. real_t get_z_far() const;
  50. real_t get_z_near() const;
  51. real_t get_aspect() const;
  52. real_t get_fov() const;
  53. bool is_orthogonal() const;
  54. std::vector<Plane> get_projection_planes(const Transform &p_transform) const;
  55. bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const;
  56. Vector2 get_viewport_half_extents() const;
  57. void invert();
  58. CameraMatrix inverse() const;
  59. CameraMatrix operator*(const CameraMatrix &p_matrix) const;
  60. Plane xform4(const Plane &p_vec4) const;
  61. inline Vector3 xform(const Vector3 &p_vec3) const;
  62. operator String() const;
  63. void scale_translate_to_fit(const AABB &p_aabb);
  64. void make_scale(const Vector3 &p_scale);
  65. int get_pixels_per_meter(int p_for_pixel_width) const;
  66. operator Transform() const;
  67. CameraMatrix();
  68. CameraMatrix(const Transform &p_transform);
  69. ~CameraMatrix();
  70. };
  71. Vector3 CameraMatrix::xform(const Vector3 &p_vec3) const {
  72. Vector3 ret;
  73. ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0];
  74. ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1];
  75. ret.z = matrix[0][2] * p_vec3.x + matrix[1][2] * p_vec3.y + matrix[2][2] * p_vec3.z + matrix[3][2];
  76. real_t w = matrix[0][3] * p_vec3.x + matrix[1][3] * p_vec3.y + matrix[2][3] * p_vec3.z + matrix[3][3];
  77. return ret / w;
  78. }
  79. #endif