ccecf15b6ebfb3965729a0c6dffbe70ac507e2c0
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Update the orientation of `world_camera` at each frame using the head position given by `android::cardboard`
17 # This module is Android specific.
20 import ::android
::cardboard
23 intrude import cameras
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)
29 # Get the angle value from the `rotation_matrix`
32 var a
= rotation_matrix
[0, 1]
33 var b
= rotation_matrix
[1, 1]
37 # Get the angle value from the `rotation_matrix`
40 var a
= rotation_matrix
[2, 0]
41 var b
= rotation_matrix
[2, 2]
48 # Cardboard's head tacker instance
49 private var head_tracker
: nullable NativeHeadTracker = null
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
54 # Initialize and set `head_tracker`
55 fun initialize_head_tracker
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
63 # Set a wide field of view
64 world_camera
.field_of_view_y
= 1.0
67 # Read the rotation matrix from Cardboard and update `world_camera`
68 private fun update_from_head_tracker
70 var head_tracker
= head_tracker
71 if head_tracker
== null then return
73 head_tracker
.last_head_view
(java_rotation_matrix
, 0)
75 # Copy values from the Java array to our matrix
78 world_camera
.rotation_matrix
[y
, x
] = java_rotation_matrix
[y
*4+x
]
86 initialize_head_tracker
92 update_from_head_tracker
98 var tracker
= head_tracker
99 if tracker
!= null then tracker
.stop_tracking
105 var tracker
= head_tracker
106 if tracker
!= null then tracker
.start_tracking