1 /*************************************************************************/
2 /*  camera.h                                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef CAMERA_H
32 #define CAMERA_H
33 
34 #include "scene/3d/spatial.h"
35 #include "scene/3d/spatial_velocity_tracker.h"
36 #include "scene/main/viewport.h"
37 #include "scene/resources/environment.h"
38 
39 class Camera : public Spatial {
40 
41 	GDCLASS(Camera, Spatial);
42 
43 public:
44 	enum Projection {
45 
46 		PROJECTION_PERSPECTIVE,
47 		PROJECTION_ORTHOGONAL,
48 		PROJECTION_FRUSTUM
49 	};
50 
51 	enum KeepAspect {
52 		KEEP_WIDTH,
53 		KEEP_HEIGHT
54 	};
55 
56 	enum DopplerTracking {
57 		DOPPLER_TRACKING_DISABLED,
58 		DOPPLER_TRACKING_IDLE_STEP,
59 		DOPPLER_TRACKING_PHYSICS_STEP
60 	};
61 
62 private:
63 	bool force_change;
64 	bool current;
65 	Viewport *viewport;
66 
67 	Projection mode;
68 
69 	float fov;
70 	float size;
71 	Vector2 frustum_offset;
72 	float near, far;
73 	float v_offset;
74 	float h_offset;
75 	KeepAspect keep_aspect;
76 
77 	RID camera;
78 	RID scenario_id;
79 
80 	//String camera_group;
81 
82 	uint32_t layers;
83 
84 	Ref<Environment> environment;
85 
86 	virtual bool _can_gizmo_scale() const;
87 
88 	//void _camera_make_current(Node *p_camera);
89 	friend class Viewport;
90 	void _update_audio_listener_state();
91 
92 	DopplerTracking doppler_tracking;
93 	Ref<SpatialVelocityTracker> velocity_tracker;
94 
95 protected:
96 	void _update_camera();
97 	virtual void _request_camera_update();
98 	void _update_camera_mode();
99 
100 	void _notification(int p_what);
101 	virtual void _validate_property(PropertyInfo &p_property) const;
102 
103 	static void _bind_methods();
104 
105 public:
106 	enum {
107 
108 		NOTIFICATION_BECAME_CURRENT = 50,
109 		NOTIFICATION_LOST_CURRENT = 51
110 	};
111 
112 	void set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far);
113 	void set_orthogonal(float p_size, float p_z_near, float p_z_far);
114 	void set_frustum(float p_size, Vector2 p_offset, float p_z_near, float p_z_far);
115 	void set_projection(Camera::Projection p_mode);
116 
117 	void make_current();
118 	void clear_current(bool p_enable_next = true);
119 	void set_current(bool p_current);
120 	bool is_current() const;
121 
122 	RID get_camera() const;
123 
124 	float get_fov() const;
125 	float get_size() const;
126 	float get_zfar() const;
127 	float get_znear() const;
128 	Vector2 get_frustum_offset() const;
129 
130 	Projection get_projection() const;
131 
132 	void set_fov(float p_fov);
133 	void set_size(float p_size);
134 	void set_zfar(float p_zfar);
135 	void set_znear(float p_znear);
136 	void set_frustum_offset(Vector2 p_offset);
137 
138 	virtual Transform get_camera_transform() const;
139 
140 	virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
141 	virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
142 	virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
143 	virtual Point2 unproject_position(const Vector3 &p_pos) const;
144 	bool is_position_behind(const Vector3 &p_pos) const;
145 	virtual Vector3 project_position(const Point2 &p_point, float p_z_depth) const;
146 
147 	Vector<Vector3> get_near_plane_points() const;
148 
149 	void set_cull_mask(uint32_t p_layers);
150 	uint32_t get_cull_mask() const;
151 
152 	void set_cull_mask_bit(int p_layer, bool p_enable);
153 	bool get_cull_mask_bit(int p_layer) const;
154 
155 	virtual Vector<Plane> get_frustum() const;
156 
157 	void set_environment(const Ref<Environment> &p_environment);
158 	Ref<Environment> get_environment() const;
159 
160 	void set_keep_aspect_mode(KeepAspect p_aspect);
161 	KeepAspect get_keep_aspect_mode() const;
162 
163 	void set_v_offset(float p_offset);
164 	float get_v_offset() const;
165 
166 	void set_h_offset(float p_offset);
167 	float get_h_offset() const;
168 
169 	void set_doppler_tracking(DopplerTracking p_tracking);
170 	DopplerTracking get_doppler_tracking() const;
171 
172 	Vector3 get_doppler_tracked_velocity() const;
173 
174 	Camera();
175 	~Camera();
176 };
177 
178 VARIANT_ENUM_CAST(Camera::Projection);
179 VARIANT_ENUM_CAST(Camera::KeepAspect);
180 VARIANT_ENUM_CAST(Camera::DopplerTracking);
181 
182 class ClippedCamera : public Camera {
183 
184 	GDCLASS(ClippedCamera, Camera);
185 
186 public:
187 	enum ProcessMode {
188 		CLIP_PROCESS_PHYSICS,
189 		CLIP_PROCESS_IDLE,
190 	};
191 
192 private:
193 	ProcessMode process_mode;
194 	RID pyramid_shape;
195 	float margin;
196 	float clip_offset;
197 	uint32_t collision_mask;
198 	bool clip_to_areas;
199 	bool clip_to_bodies;
200 
201 	Set<RID> exclude;
202 
203 	Vector<Vector3> points;
204 
205 protected:
206 	void _notification(int p_what);
207 	static void _bind_methods();
208 	virtual Transform get_camera_transform() const;
209 
210 public:
211 	void set_clip_to_areas(bool p_clip);
212 	bool is_clip_to_areas_enabled() const;
213 
214 	void set_clip_to_bodies(bool p_clip);
215 	bool is_clip_to_bodies_enabled() const;
216 
217 	void set_margin(float p_margin);
218 	float get_margin() const;
219 
220 	void set_process_mode(ProcessMode p_mode);
221 	ProcessMode get_process_mode() const;
222 
223 	void set_collision_mask(uint32_t p_mask);
224 	uint32_t get_collision_mask() const;
225 
226 	void set_collision_mask_bit(int p_bit, bool p_value);
227 	bool get_collision_mask_bit(int p_bit) const;
228 
229 	void add_exception_rid(const RID &p_rid);
230 	void add_exception(const Object *p_object);
231 	void remove_exception_rid(const RID &p_rid);
232 	void remove_exception(const Object *p_object);
233 	void clear_exceptions();
234 
235 	float get_clip_offset() const;
236 
237 	ClippedCamera();
238 	~ClippedCamera();
239 };
240 
241 VARIANT_ENUM_CAST(ClippedCamera::ProcessMode);
242 #endif
243