perf analysis: customize float precision in reports
[nit.git] / lib / gamnit / depth / cardboard.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Update the orientation of `world_camera` at each frame using the head position given by `android::cardboard`
16 #
17 # This module is Android specific.
18 module cardboard
19
20 import ::android::cardboard
21
22 import depth
23 intrude import cameras
24
25 redef class EulerCamera
26 # Do not use `yaw` and `pitch`, the value will instead originate from the Cardboard API
27 redef var rotation_matrix = new Matrix.identity(4)
28
29 # Get the angle value from the `rotation_matrix`
30 redef fun pitch
31 do
32 var a = rotation_matrix[0, 1]
33 var b = rotation_matrix[1, 1]
34 return -atan2(a, b)
35 end
36
37 # Get the angle value from the `rotation_matrix`
38 redef fun yaw
39 do
40 var a = rotation_matrix[2, 0]
41 var b = rotation_matrix[2, 2]
42 return -atan2(a, b)
43 end
44 end
45
46 redef class App
47
48 # Cardboard's head tacker instance
49 private var head_tracker: nullable NativeHeadTracker = null
50
51 # Rotation matrix read from `head_tracker`, reusing the same structure as a buffer
52 private var java_rotation_matrix = new JavaFloatArray(16) is lazy
53
54 # Initialize and set `head_tracker`
55 fun initialize_head_tracker
56 do
57 # Initialize the Cardboard head orientation tracker service
58 var head_tracker = new NativeHeadTracker(app.native_activity)
59 head_tracker.neck_model_enabled = true
60 head_tracker.start_tracking
61 self.head_tracker = head_tracker
62
63 # Set a wide field of view
64 world_camera.field_of_view_y = 1.0
65 end
66
67 # Read the rotation matrix from Cardboard and update `world_camera`
68 private fun update_from_head_tracker
69 do
70 var head_tracker = head_tracker
71 if head_tracker == null then return
72
73 head_tracker.last_head_view(java_rotation_matrix, 0)
74
75 # Copy values from the Java array to our matrix
76 for y in [0..4[ do
77 for x in [0..4[ do
78 world_camera.rotation_matrix[y, x] = java_rotation_matrix[y*4+x]
79 end
80 end
81 end
82
83 redef fun on_create
84 do
85 super
86 initialize_head_tracker
87 end
88
89 redef fun update(dt)
90 do
91 super
92 update_from_head_tracker
93 end
94
95 redef fun pause
96 do
97 super
98 var tracker = head_tracker
99 if tracker != null then tracker.stop_tracking
100 end
101
102 redef fun resume
103 do
104 super
105 var tracker = head_tracker
106 if tracker != null then tracker.start_tracking
107 end
108 end