From: Mikko Rasa Date: Sun, 15 Sep 2013 12:42:33 +0000 (+0300) Subject: Integrate with LibOVR X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b7f83509d995b5037e27eb7c94811d43c4d51e1b;p=libs%2Fvr.git Integrate with LibOVR --- diff --git a/Build b/Build index b27a592..d5185dc 100644 --- a/Build +++ b/Build @@ -4,6 +4,12 @@ package "mspvr" version "0.1"; require "mspgl"; + require "libovr"; + + build_info + { + warning_level 1; + }; library "mspvr" { diff --git a/source/oculusriftcamera.cpp b/source/oculusriftcamera.cpp new file mode 100644 index 0000000..9b85d12 --- /dev/null +++ b/source/oculusriftcamera.cpp @@ -0,0 +1,40 @@ +#include "oculusriftcamera.h" +#include "oculusriftcamera_private.h" +#include "oculusriftdevice.h" +#include "oculusriftdevice_private.h" + +namespace Msp { +namespace VR { + +OculusRiftCamera::OculusRiftCamera(const OculusRiftDevice &d, const GL::Camera &c): + device(d), + base_camera(c), + priv(new Private) +{ + const OculusRiftDevice::Private &dev_priv = device.get_private(); + priv->ovr_sensor = dev_priv.ovr_hmd->GetSensor(); + priv->fusion.AttachToSensor(priv->ovr_sensor); +} + +OculusRiftCamera::~OculusRiftCamera() +{ + delete priv; +} + +void OculusRiftCamera::update() +{ + OVR::Matrix4f sensor_matrix = priv->fusion.GetOrientation(); + const float *m = &sensor_matrix.M[0][0]; + + const GL::Vector3 &base_look = base_camera.get_look_direction(); + GL::Vector3 base_right = normalize(cross(base_look, base_camera.get_up_direction())); + GL::Vector3 base_up = normalize(cross(base_right, base_look)); + + set_position(base_camera.get_position()); + set_up_direction(base_right*m[1]+base_up*m[5]-base_look*m[9]); + set_look_direction(-base_right*m[2]-base_up*m[6]+base_look*m[10]); + set_depth_clip(base_camera.get_near_clip(), base_camera.get_far_clip()); +} + +} // namespace VR +} // namespace Msp diff --git a/source/oculusriftcamera.h b/source/oculusriftcamera.h new file mode 100644 index 0000000..bff9967 --- /dev/null +++ b/source/oculusriftcamera.h @@ -0,0 +1,30 @@ +#ifndef MSP_VR_OCULUSRIFTCAMERA_H_ +#define MSP_VR_OCULUSRIFTCAMERA_H_ + +#include "headtrackingcamera.h" + +namespace Msp { +namespace VR { + +class OculusRiftDevice; + +class OculusRiftCamera: public HeadTrackingCamera +{ +private: + struct Private; + + const OculusRiftDevice &device; + const GL::Camera &base_camera; + Private *priv; + +public: + OculusRiftCamera(const OculusRiftDevice &, const GL::Camera &); + virtual ~OculusRiftCamera(); + + virtual void update(); +}; + +} // namespace VR +} // namespace Msp + +#endif diff --git a/source/oculusriftcamera_private.h b/source/oculusriftcamera_private.h new file mode 100644 index 0000000..6359930 --- /dev/null +++ b/source/oculusriftcamera_private.h @@ -0,0 +1,19 @@ +#ifndef MSP_VR_OCULUSRIFTCAMERA_PRIVATE_H_ +#define MSP_VR_OCULUSRIFTCAMERA_PRIVATE_H_ + +#include +#include "oculusriftcamera.h" + +namespace Msp { +namespace VR { + +struct OculusRiftCamera::Private +{ + OVR::Ptr ovr_sensor; + OVR::SensorFusion fusion; +}; + +} // namespace VR +} // namespace Msp + +#endif diff --git a/source/oculusriftdevice.cpp b/source/oculusriftdevice.cpp new file mode 100644 index 0000000..f0cccc7 --- /dev/null +++ b/source/oculusriftdevice.cpp @@ -0,0 +1,51 @@ +#include "oculusriftdevice.h" +#include "oculusriftdevice_private.h" + +using namespace std; + +namespace Msp { +namespace VR { + +OculusRiftDevice::OculusRiftDevice(): + priv(new Private) +{ + static OVR::System system(OVR::Log::ConfigureDefaultLog(OVR::LogMask_Debug)); + + priv->ovr_manager = OVR::DeviceManager::Create(); + priv->ovr_hmd = priv->ovr_manager->EnumerateDevices().CreateDevice(); + if(!priv->ovr_hmd) + { + delete priv; + throw runtime_error("rift hmd not found"); + } +} + +OculusRiftDevice::~OculusRiftDevice() +{ + delete priv; +} + +OculusRiftCamera *OculusRiftDevice::create_camera(const GL::Camera &bc) const +{ + return new OculusRiftCamera(*this, bc); +} + +OculusRiftCombiner *OculusRiftDevice::create_combiner() const +{ + OculusRiftCombiner *combiner = new OculusRiftCombiner; + + OVR::HMDInfo info; + if(!priv->ovr_hmd->GetDeviceInfo(&info)) + { + combiner->set_view_distance(info.EyeToScreenDistance/info.HScreenSize); + combiner->set_lens_separation(info.LensSeparationDistance/info.HScreenSize); + combiner->set_distortion(info.DistortionK[0], info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]); + combiner->set_red_aberration(info.ChromaAbCorrection[0], info.ChromaAbCorrection[1]); + combiner->set_blue_aberration(info.ChromaAbCorrection[2], info.ChromaAbCorrection[3]); + } + + return combiner; +} + +} // namespace VR +} // namespace Msp diff --git a/source/oculusriftdevice.h b/source/oculusriftdevice.h new file mode 100644 index 0000000..9d9e2ec --- /dev/null +++ b/source/oculusriftdevice.h @@ -0,0 +1,32 @@ +#ifndef MSP_VR_OCULUSRIFTDEVICE_H_ +#define MSP_VR_OCULUSRIFTDEVICE_H_ + +#include "displaydevice.h" +#include "oculusriftcamera.h" +#include "oculusriftcombiner.h" + +namespace Msp { +namespace VR { + +class OculusRiftDevice: public DisplayDevice +{ +public: + struct Private; + +private: + Private *priv; + +public: + OculusRiftDevice(); + virtual ~OculusRiftDevice(); + + const Private &get_private() const { return *priv; } + + virtual OculusRiftCamera *create_camera(const GL::Camera &) const; + virtual OculusRiftCombiner *create_combiner() const; +}; + +} // namespace VR +} // namespace Msp + +#endif diff --git a/source/oculusriftdevice_private.h b/source/oculusriftdevice_private.h new file mode 100644 index 0000000..c2033ee --- /dev/null +++ b/source/oculusriftdevice_private.h @@ -0,0 +1,19 @@ +#ifndef MSP_VR_OCULUSRIFTDEVICE_PRIVATE_H_ +#define MSP_VR_OCULUSRIFTDEVICE_PRIVATE_H_ + +#include +#include "oculusriftdevice.h" + +namespace Msp { +namespace VR { + +struct OculusRiftDevice::Private +{ + OVR::Ptr ovr_manager; + OVR::Ptr ovr_hmd; +}; + +} // namespace VR +} // namespace Msp + +#endif