Give combiners more control over rendering
[libs/vr.git] / source / ovr / oculusriftdevice.cpp
1 #include <msp/vr/stereoview.h>
2 #include "oculusriftdevice.h"
3 #include "oculusriftdevice_private.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace VR {
9
10 unsigned OculusRiftDevice::n_instances = 0;
11
12 OculusRiftDevice::OculusRiftDevice():
13         priv(new Private),
14         frame_index(0)
15 {
16         if(!n_instances)
17                 ovr_Initialize();
18         ++n_instances;
19
20         priv->ovr_hmd = ovrHmd_Create(0);
21         if(!priv->ovr_hmd)
22         {
23                 delete priv;
24                 throw runtime_error("rift hmd not found");
25         }
26 }
27
28 OculusRiftDevice::~OculusRiftDevice()
29 {
30         ovrHmd_Destroy(priv->ovr_hmd);
31         delete priv;
32
33         --n_instances;
34         if(!n_instances)
35                 ovr_Shutdown();
36 }
37
38 void OculusRiftDevice::configure_view(StereoView &view) const
39 {
40         ovrEyeRenderDesc left_desc = ovrHmd_GetRenderDesc(priv->ovr_hmd, ovrEye_Left, priv->ovr_hmd->DefaultEyeFov[ovrEye_Left]);
41         ovrEyeRenderDesc right_desc = ovrHmd_GetRenderDesc(priv->ovr_hmd, ovrEye_Right, priv->ovr_hmd->DefaultEyeFov[ovrEye_Left]);
42         view.set_eye_spacing(left_desc.HmdToEyeViewOffset.x-right_desc.HmdToEyeViewOffset.x);
43 }
44
45 OculusRiftCamera *OculusRiftDevice::create_camera(const GL::Camera &bc)
46 {
47         return new OculusRiftCamera(*this, bc);
48 }
49
50 OculusRiftCombiner *OculusRiftDevice::create_combiner(GL::View &view)
51 {
52         return new OculusRiftCombiner(*this, view);
53 }
54
55 void OculusRiftDevice::begin_frame()
56 {
57         priv->frame_timing = ovrHmd_BeginFrameTiming(priv->ovr_hmd, ++frame_index);
58         timing_active = true;
59 }
60
61 void OculusRiftDevice::end_frame()
62 {
63         glFinish();
64         ovrHmd_EndFrameTiming(priv->ovr_hmd);
65         timing_active = false;
66 }
67
68 double OculusRiftDevice::get_tracking_time() const
69 {
70         if(!timing_active)
71                 throw logic_error("timing not active");
72         return priv->frame_timing.ScanoutMidpointSeconds;
73 }
74
75 double OculusRiftDevice::get_timewarp_time() const
76 {
77         if(!timing_active)
78                 throw logic_error("timing not active");
79         return priv->frame_timing.TimewarpPointSeconds;
80 }
81
82 double OculusRiftDevice::get_current_time() const
83 {
84         return ovr_GetTimeInSeconds();
85 }
86
87 } // namespace VR
88 } // namespace Msp