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