1 /*************************************************************************/
2 /*  camera.h                                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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_H
31 #define CAMERA_H
32 
33 #include "scene/3d/spatial.h"
34 #include "scene/main/viewport.h"
35 #include "scene/resources/environment.h"
36 /**
37 	@author Juan Linietsky <reduzio@gmail.com>
38 */
39 class Camera : public Spatial {
40 
41 	OBJ_TYPE(Camera, Spatial);
42 
43 public:
44 	enum Projection {
45 
46 		PROJECTION_PERSPECTIVE,
47 		PROJECTION_ORTHOGONAL
48 	};
49 
50 	enum KeepAspect {
51 		KEEP_WIDTH,
52 		KEEP_HEIGHT
53 	};
54 
55 private:
56 	bool force_change;
57 	bool current;
58 
59 	Projection mode;
60 
61 	float fov;
62 	float size;
63 	float near, far;
64 	float v_offset;
65 	float h_offset;
66 	KeepAspect keep_aspect;
67 
68 	RID camera;
69 	RID scenario_id;
70 
71 	//String camera_group;
72 
73 	uint32_t layers;
74 
75 	Ref<Environment> environment;
76 
77 	virtual bool _can_gizmo_scale() const;
78 	virtual RES _get_gizmo_geometry() const;
79 
80 	//void _camera_make_current(Node *p_camera);
81 	friend class Viewport;
82 	void _update_audio_listener_state();
83 
84 protected:
85 	void _update_camera();
86 	virtual void _request_camera_update();
87 	void _update_camera_mode();
88 
89 	bool _set(const StringName &p_name, const Variant &p_value);
90 	bool _get(const StringName &p_name, Variant &r_ret) const;
91 	void _get_property_list(List<PropertyInfo> *p_list) const;
92 	void _notification(int p_what);
93 
94 	static void _bind_methods();
95 
96 public:
97 	enum {
98 
99 		NOTIFICATION_BECAME_CURRENT = 50,
100 		NOTIFICATION_LOST_CURRENT = 51
101 	};
102 
103 	void set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far);
104 	void set_orthogonal(float p_size, float p_z_near, float p_z_far);
105 
106 	void make_current();
107 	void clear_current();
108 	bool is_current() const;
109 
110 	RID get_camera() const;
111 
112 	float get_fov() const;
113 	float get_size() const;
114 	float get_zfar() const;
115 	float get_znear() const;
116 	Projection get_projection() const;
117 
118 	virtual Transform get_camera_transform() const;
119 
120 	Vector3 project_ray_normal(const Point2 &p_point) const;
121 	Vector3 project_ray_origin(const Point2 &p_point) const;
122 	Vector3 project_local_ray_normal(const Point2 &p_point) const;
123 	Point2 unproject_position(const Vector3 &p_pos) const;
124 	bool is_position_behind(const Vector3 &p_pos) const;
125 	Vector3 project_position(const Point2 &p_point) const;
126 
127 	void set_visible_layers(uint32_t p_layers);
128 	uint32_t get_visible_layers() const;
129 
130 	Vector<Plane> get_frustum() const;
131 
132 	void set_environment(const Ref<Environment> &p_environment);
133 	Ref<Environment> get_environment() const;
134 
135 	void set_keep_aspect_mode(KeepAspect p_aspect);
136 	KeepAspect get_keep_aspect_mode() const;
137 
138 	void set_v_offset(float p_offset);
139 	float get_v_offset() const;
140 
141 	void set_h_offset(float p_offset);
142 	float get_h_offset() const;
143 
144 	Camera();
145 	~Camera();
146 };
147 
148 VARIANT_ENUM_CAST(Camera::Projection);
149 VARIANT_ENUM_CAST(Camera::KeepAspect);
150 
151 #endif
152