CameraMatrix.hpp 3.3 KB

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