]> git.tdb.fi Git - libs/vr.git/blob - source/oculusriftcamera.cpp
Add positional tracking support on the Oculus Rift DK2
[libs/vr.git] / source / oculusriftcamera.cpp
1 #include "oculusriftcamera.h"
2 #include "oculusriftdevice.h"
3 #include "oculusriftdevice_private.h"
4
5 namespace Msp {
6 namespace VR {
7
8 OculusRiftCamera::OculusRiftCamera(const OculusRiftDevice &d, const GL::Camera &c):
9         device(d),
10         base_camera(c)
11 {
12         const OculusRiftDevice::Private &dev_priv = device.get_private();
13         unsigned supported = ovrTrackingCap_Orientation|ovrTrackingCap_MagYawCorrection|ovrTrackingCap_Position;
14         ovrHmd_ConfigureTracking(dev_priv.ovr_hmd, supported, 0);
15 }
16
17 void OculusRiftCamera::reset_tracking()
18 {
19         ovrHmd_RecenterPose(device.get_private().ovr_hmd);
20 }
21
22 void OculusRiftCamera::update()
23 {
24         ovrTrackingState state = ovrHmd_GetTrackingState(device.get_private().ovr_hmd, ovr_GetTimeInSeconds());
25         OVR::Posef head_pose = state.HeadPose.ThePose;
26         OVR::Matrix4f tracking_matrix(head_pose.Rotation);
27         OVR::Vector3f trans = head_pose.Translation;
28         const float *m = &tracking_matrix.M[0][0];
29
30         const GL::Vector3 &base_look = base_camera.get_look_direction();
31         GL::Vector3 base_right = normalize(cross(base_look, base_camera.get_up_direction()));
32         GL::Vector3 base_up = normalize(cross(base_right, base_look));
33
34         set_position(base_camera.get_position()+trans.x*base_right+trans.y*base_up-trans.z*base_look);
35         set_up_direction(base_right*m[1]+base_up*m[5]-base_look*m[9]);
36         set_look_direction(-base_right*m[2]-base_up*m[6]+base_look*m[10]);
37         set_depth_clip(base_camera.get_near_clip(), base_camera.get_far_clip());
38         set_field_of_view(base_camera.get_field_of_view());
39         set_aspect(base_camera.get_aspect());
40 }
41
42 } // namespace VR
43 } // namespace Msp