]> git.tdb.fi Git - libs/vr.git/blob - source/oculusriftcamera.cpp
Add support for time warp on Oculus Rift
[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         unsigned supported = ovrTrackingCap_Orientation|ovrTrackingCap_MagYawCorrection|ovrTrackingCap_Position;
13         ovrHmd_ConfigureTracking(device.get_private().ovr_hmd, supported, 0);
14 }
15
16 void OculusRiftCamera::reset_tracking()
17 {
18         ovrHmd_RecenterPose(device.get_private().ovr_hmd);
19 }
20
21 void OculusRiftCamera::update()
22 {
23         double time;
24         if(device.is_timing_active())
25                 time = device.get_tracking_time();
26         else
27                 time = device.get_current_time();
28
29         ovrTrackingState state = ovrHmd_GetTrackingState(device.get_private().ovr_hmd, time);
30         OVR::Posef head_pose = state.HeadPose.ThePose;
31         OVR::Matrix4f tracking_matrix(head_pose.Rotation);
32         OVR::Vector3f trans = head_pose.Translation;
33         const float *m = &tracking_matrix.M[0][0];
34
35         const GL::Vector3 &base_look = base_camera.get_look_direction();
36         GL::Vector3 base_right = normalize(cross(base_look, base_camera.get_up_direction()));
37         GL::Vector3 base_up = normalize(cross(base_right, base_look));
38
39         set_position(base_camera.get_position()+trans.x*base_right+trans.y*base_up-trans.z*base_look);
40         set_up_direction(base_right*m[1]+base_up*m[5]-base_look*m[9]);
41         set_look_direction(-base_right*m[2]-base_up*m[6]+base_look*m[10]);
42         set_depth_clip(base_camera.get_near_clip(), base_camera.get_far_clip());
43         set_field_of_view(base_camera.get_field_of_view());
44         set_aspect(base_camera.get_aspect());
45 }
46
47 } // namespace VR
48 } // namespace Msp