1 /*************************************************************************/
2 /*  mobile_vr_interface.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 MOBILE_VR_INTERFACE_H
32 #define MOBILE_VR_INTERFACE_H
33 
34 #include "servers/arvr/arvr_interface.h"
35 #include "servers/arvr/arvr_positional_tracker.h"
36 
37 /**
38 	@author Bastiaan Olij <mux213@gmail.com>
39 
40 	The mobile interface is a native VR interface that can be used on Android and iOS phones.
41 	It contains a basic implementation supporting 3DOF tracking if a gyroscope and accelerometer are
42 	present and sets up the proper projection matrices based on the values provided.
43 
44 	We're planning to eventually do separate interfaces towards mobile SDKs that have far more capabilities and
45 	do not rely on the user providing most of these settings (though enhancing this with auto detection features
46 	based on the device we're running on would be cool). I'm mostly adding this as an example or base plate for
47 	more advanced interfaces.
48 */
49 
50 class MobileVRInterface : public ARVRInterface {
51 	GDCLASS(MobileVRInterface, ARVRInterface);
52 
53 private:
54 	bool initialized;
55 	Basis orientation;
56 	float eye_height;
57 	uint64_t last_ticks;
58 
59 	real_t intraocular_dist;
60 	real_t display_width;
61 	real_t display_to_lens;
62 	real_t oversample;
63 
64 	//@TODO not yet used, these are needed in our distortion shader...
65 	real_t k1;
66 	real_t k2;
67 
68 	/*
69 		logic for processing our sensor data, this was originally in our positional tracker logic but I think
70 		that doesn't make sense in hindsight. It only makes marginally more sense to park it here for now,
71 		this probably deserves an object of its own
72 	*/
73 	Vector3 scale_magneto(const Vector3 &p_magnetometer);
74 	Basis combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto);
75 
76 	int mag_count;
77 	bool has_gyro;
78 	bool sensor_first;
79 	Vector3 last_accerometer_data;
80 	Vector3 last_magnetometer_data;
81 	Vector3 mag_current_min;
82 	Vector3 mag_current_max;
83 	Vector3 mag_next_min;
84 	Vector3 mag_next_max;
85 
86 	///@TODO a few support functions for trackers, most are math related and should likely be moved elsewhere
floor_decimals(float p_value,float p_decimals)87 	float floor_decimals(float p_value, float p_decimals) {
88 		float power_of_10 = pow(10.0f, p_decimals);
89 		return floor(p_value * power_of_10) / power_of_10;
90 	};
91 
floor_decimals(const Vector3 & p_vector,float p_decimals)92 	Vector3 floor_decimals(const Vector3 &p_vector, float p_decimals) {
93 		return Vector3(floor_decimals(p_vector.x, p_decimals), floor_decimals(p_vector.y, p_decimals), floor_decimals(p_vector.z, p_decimals));
94 	};
95 
low_pass(const Vector3 & p_vector,const Vector3 & p_last_vector,float p_factor)96 	Vector3 low_pass(const Vector3 &p_vector, const Vector3 &p_last_vector, float p_factor) {
97 		return p_vector + (p_factor * (p_last_vector - p_vector));
98 	};
99 
scrub(const Vector3 & p_vector,const Vector3 & p_last_vector,float p_decimals,float p_factor)100 	Vector3 scrub(const Vector3 &p_vector, const Vector3 &p_last_vector, float p_decimals, float p_factor) {
101 		return low_pass(floor_decimals(p_vector, p_decimals), p_last_vector, p_factor);
102 	};
103 
104 	void set_position_from_sensors();
105 
106 protected:
107 	static void _bind_methods();
108 
109 public:
110 	void set_eye_height(const real_t p_eye_height);
111 	real_t get_eye_height() const;
112 
113 	void set_iod(const real_t p_iod);
114 	real_t get_iod() const;
115 
116 	void set_display_width(const real_t p_display_width);
117 	real_t get_display_width() const;
118 
119 	void set_display_to_lens(const real_t p_display_to_lens);
120 	real_t get_display_to_lens() const;
121 
122 	void set_oversample(const real_t p_oversample);
123 	real_t get_oversample() const;
124 
125 	void set_k1(const real_t p_k1);
126 	real_t get_k1() const;
127 
128 	void set_k2(const real_t p_k2);
129 	real_t get_k2() const;
130 
131 	virtual StringName get_name() const;
132 	virtual int get_capabilities() const;
133 
134 	virtual bool is_initialized() const;
135 	virtual bool initialize();
136 	virtual void uninitialize();
137 
138 	virtual Size2 get_render_targetsize();
139 	virtual bool is_stereo();
140 	virtual Transform get_transform_for_eye(ARVRInterface::Eyes p_eye, const Transform &p_cam_transform);
141 	virtual CameraMatrix get_projection_for_eye(ARVRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far);
142 	virtual void commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect);
143 
144 	virtual void process();
145 	virtual void notification(int p_what);
146 
147 	MobileVRInterface();
148 	~MobileVRInterface();
149 };
150 
151 #endif // !MOBILE_VR_INTERFACE_H
152