X-Git-Url: http://git.tdb.fi/?p=libs%2Fvr.git;a=blobdiff_plain;f=source%2Flibovr%2Flibovrsystem.cpp;fp=source%2Flibovr%2Flibovrsystem.cpp;h=c8183b08eb64d939d10e4c1e060da0ac46008fed;hp=0000000000000000000000000000000000000000;hb=31c28161d36749d040cbab0099b352f53cad232d;hpb=b6de7d24475dec8f5d6b8148a69cf8b561bc0761 diff --git a/source/libovr/libovrsystem.cpp b/source/libovr/libovrsystem.cpp new file mode 100644 index 0000000..c8183b0 --- /dev/null +++ b/source/libovr/libovrsystem.cpp @@ -0,0 +1,111 @@ +#include +#include +#include "libovrsystem.h" +#include "libovrsystem_private.h" + +using namespace std; + +namespace Msp { +namespace VR { + +unsigned LibOVRSystem::n_instances = 0; + +LibOVRSystem::LibOVRSystem(): + priv(new Private), + frame_index(0) +{ + if(!n_instances) + ovr_Initialize(); + ++n_instances; + + priv->ovr_hmd = ovrHmd_Create(0); + if(!priv->ovr_hmd) + { + delete priv; + // XXX decrement n_instances + throw runtime_error("rift hmd not found"); + } +} + +LibOVRSystem::~LibOVRSystem() +{ + ovrHmd_Destroy(priv->ovr_hmd); + delete priv; + + --n_instances; + if(!n_instances) + ovr_Shutdown(); +} + +void LibOVRSystem::configure_window(Graphics::Window &window) const +{ + Graphics::WindowOptions win_opts = window.get_options(); + win_opts.width = priv->ovr_hmd->Resolution.w; + win_opts.height = priv->ovr_hmd->Resolution.h; + + const list &monitors = window.get_display().get_monitors(); + string hmd_name = priv->ovr_hmd->ProductName; + for(list::const_iterator i=monitors.begin(); i!=monitors.end(); ++i) + if(hmd_name.find(i->name)!=string::npos) + { + win_opts.fullscreen = true; + win_opts.fullscreen_monitor = &*i; + win_opts.fullscreen_exclusive = false; + } + + window.reconfigure(win_opts); +} + +void LibOVRSystem::configure_view(StereoView &view) const +{ + ovrEyeRenderDesc left_desc = ovrHmd_GetRenderDesc(priv->ovr_hmd, ovrEye_Left, priv->ovr_hmd->DefaultEyeFov[ovrEye_Left]); + ovrEyeRenderDesc right_desc = ovrHmd_GetRenderDesc(priv->ovr_hmd, ovrEye_Right, priv->ovr_hmd->DefaultEyeFov[ovrEye_Left]); + const ovrVector3f &l = left_desc.HmdToEyeViewOffset; + const ovrVector3f &r = right_desc.HmdToEyeViewOffset; + view.set_eye_matrices(GL::Matrix::translation(GL::Vector3(l.x, l.y, l.z)), GL::Matrix::translation(GL::Vector3(r.x, r.y, r.z))); +} + +LibOVRCamera *LibOVRSystem::create_camera(const GL::Camera &bc) +{ + return new LibOVRCamera(*this, bc); +} + +LibOVRCombiner *LibOVRSystem::create_combiner(GL::View &view) +{ + return new LibOVRCombiner(*this, view); +} + +void LibOVRSystem::begin_frame() +{ + priv->frame_timing = ovrHmd_BeginFrameTiming(priv->ovr_hmd, ++frame_index); + timing_active = true; +} + +void LibOVRSystem::end_frame() +{ + glFinish(); + ovrHmd_EndFrameTiming(priv->ovr_hmd); + timing_active = false; +} + +double LibOVRSystem::get_tracking_time() const +{ + if(!timing_active) + throw logic_error("timing not active"); + return priv->frame_timing.ScanoutMidpointSeconds; +} + +double LibOVRSystem::get_timewarp_time() const +{ + if(!timing_active) + throw logic_error("timing not active"); + return priv->frame_timing.TimewarpPointSeconds; +} + +double LibOVRSystem::get_current_time() const +{ + return ovr_GetTimeInSeconds(); +} + +} // namespace VR +} // namespace Msp