CameraMatrix.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef CAMERA_MATRIX_H
  2. #define CAMERA_MATRIX_H
  3. #include "Defs.hpp"
  4. #include "Math.hpp"
  5. #include "Plane.hpp"
  6. #include "Rect2.hpp"
  7. #include "Transform.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 absd(double g) {
  37. union {
  38. double d;
  39. uint64_t i;
  40. } u;
  41. u.d = g;
  42. u.i &= (uint64_t)9223372036854775807ll;
  43. return u.d;
  44. }
  45. real_t get_z_far() const;
  46. real_t get_z_near() const;
  47. real_t get_aspect() const;
  48. real_t get_fov() const;
  49. bool is_orthogonal() const;
  50. std::vector<Plane> get_projection_planes(const Transform &p_transform) const;
  51. bool get_endpoints(const Transform &p_transform, Vector3 *p_8points) const;
  52. Vector2 get_viewport_half_extents() const;
  53. void invert();
  54. CameraMatrix inverse() const;
  55. CameraMatrix operator*(const CameraMatrix &p_matrix) const;
  56. Plane xform4(const Plane &p_vec4) const;
  57. inline Vector3 xform(const Vector3 &p_vec3) const;
  58. operator String() const;
  59. void scale_translate_to_fit(const AABB &p_aabb);
  60. void make_scale(const Vector3 &p_scale);
  61. int get_pixels_per_meter(int p_for_pixel_width) const;
  62. operator Transform() const;
  63. CameraMatrix();
  64. CameraMatrix(const Transform &p_transform);
  65. ~CameraMatrix();
  66. };
  67. Vector3 CameraMatrix::xform(const Vector3 &p_vec3) const {
  68. Vector3 ret;
  69. ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0];
  70. ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1];
  71. ret.z = matrix[0][2] * p_vec3.x + matrix[1][2] * p_vec3.y + matrix[2][2] * p_vec3.z + matrix[3][2];
  72. real_t w = matrix[0][3] * p_vec3.x + matrix[1][3] * p_vec3.y + matrix[2][3] * p_vec3.z + matrix[3][3];
  73. return ret / w;
  74. }
  75. #endif