9b85d12e8f474e32d5bb4372d271e0263e785fa0
[libs/vr.git] / source / oculusriftcamera.cpp
1 #include "oculusriftcamera.h"
2 #include "oculusriftcamera_private.h"
3 #include "oculusriftdevice.h"
4 #include "oculusriftdevice_private.h"
5
6 namespace Msp {
7 namespace VR {
8
9 OculusRiftCamera::OculusRiftCamera(const OculusRiftDevice &d, const GL::Camera &c):
10         device(d),
11         base_camera(c),
12         priv(new Private)
13 {
14         const OculusRiftDevice::Private &dev_priv = device.get_private();
15         priv->ovr_sensor = dev_priv.ovr_hmd->GetSensor();
16         priv->fusion.AttachToSensor(priv->ovr_sensor);
17 }
18
19 OculusRiftCamera::~OculusRiftCamera()
20 {
21         delete priv;
22 }
23
24 void OculusRiftCamera::update()
25 {
26         OVR::Matrix4f sensor_matrix = priv->fusion.GetOrientation();
27         const float *m = &sensor_matrix.M[0][0];
28
29         const GL::Vector3 &base_look = base_camera.get_look_direction();
30         GL::Vector3 base_right = normalize(cross(base_look, base_camera.get_up_direction()));
31         GL::Vector3 base_up = normalize(cross(base_right, base_look));
32
33         set_position(base_camera.get_position());
34         set_up_direction(base_right*m[1]+base_up*m[5]-base_look*m[9]);
35         set_look_direction(-base_right*m[2]-base_up*m[6]+base_look*m[10]);
36         set_depth_clip(base_camera.get_near_clip(), base_camera.get_far_clip());
37 }
38
39 } // namespace VR
40 } // namespace Msp