]> git.tdb.fi Git - libs/vr.git/commitdiff
Use matrices for eye offsets instead of a simple spacing
authorMikko Rasa <tdb@tdb.fi>
Sun, 18 Sep 2016 13:56:53 +0000 (16:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 18 Sep 2016 13:56:53 +0000 (16:56 +0300)
source/ovr/oculusriftdevice.cpp
source/stereoview.cpp
source/stereoview.h

index efc0cfaaf4d0baba5a43d64834c4b885ef70afc7..38fd20e68179d657cf05a7292818b2e038384c61 100644 (file)
@@ -59,7 +59,9 @@ void OculusRiftDevice::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]);
-       view.set_eye_spacing(left_desc.HmdToEyeViewOffset.x-right_desc.HmdToEyeViewOffset.x);
+       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)));
 }
 
 OculusRiftCamera *OculusRiftDevice::create_camera(const GL::Camera &bc)
index 5945016da1ac8d1e20d01c0a4e25f0ceb0300954..73be19c4527a420aacad992148a56ac25573c2cf 100644 (file)
@@ -44,7 +44,14 @@ void StereoView::set_content(const GL::Renderable *r)
 
 void StereoView::set_eye_spacing(float s)
 {
-       eye_spacing = s;
+       GL::Vector3 offset(s/2, 0, 0);
+       set_eye_matrices(GL::Matrix::translation(-offset), GL::Matrix::translation(offset));
+}
+
+void StereoView::set_eye_matrices(const GL::Matrix &left_matrix, const GL::Matrix &right_matrix)
+{
+       left.offset_matrix = left_matrix;
+       right.offset_matrix = right_matrix;
 }
 
 void StereoView::set_strabismus(const Geometry::Angle<float> &s)
@@ -57,8 +64,6 @@ void StereoView::setup_frame() const
        if(head_camera)
                head_camera->update();
 
-       GL::Vector3 offset_axis = normalize(cross(base_camera.get_look_direction(), base_camera.get_up_direction()))*0.5f;
-
        EyeParams params;
        params.fov = combiner.get_field_of_view();
        if(params.fov==Geometry::Angle<float>::zero())
@@ -72,8 +77,8 @@ void StereoView::setup_frame() const
        float halfw = tan(params.fov/2.0f)*params.aspect;
        frustum_skew = tan(Geometry::atan<float>(frustum_skew*halfw)+strabismus)/halfw;
 
-       left.setup_frame(base_camera, offset_axis*-eye_spacing, frustum_skew, params);
-       right.setup_frame(base_camera, offset_axis*eye_spacing, -frustum_skew, params);
+       left.setup_frame(base_camera, frustum_skew, params);
+       right.setup_frame(base_camera, -frustum_skew, params);
 }
 
 void StereoView::render() const
@@ -113,11 +118,12 @@ void StereoView::Eye::create_target(unsigned w, unsigned h)
        target = new RenderTarget(w, h);
 }
 
-void StereoView::Eye::setup_frame(const GL::Camera &base_camera, const GL::Vector3 &offset, float frustum_skew, const EyeParams &params) const
+void StereoView::Eye::setup_frame(const GL::Camera &base_camera, float frustum_skew, const EyeParams &params) const
 {
-       camera.set_position(base_camera.get_position()+offset);
-       camera.set_up_direction(base_camera.get_up_direction());
-       camera.set_look_direction(base_camera.get_look_direction());
+       GL::Matrix matrix = base_camera.get_object_matrix()*offset_matrix;
+       camera.set_position(matrix*GL::Vector3());
+       camera.set_up_direction((matrix*GL::Vector4(0, 1, 0, 0)).slice<3>(0));
+       camera.set_look_direction((matrix*GL::Vector4(0, 0, -1, 0)).slice<3>(0));
 
        camera.set_field_of_view(params.fov);
        camera.set_aspect(params.aspect);
index ff34706cc4b450d1daff8d7844b5147c282ffc5d..bc9073ed5808ebf8e9802bdbd92f21d673fd584f 100644 (file)
@@ -37,12 +37,13 @@ private:
        struct Eye
        {
                mutable GL::Camera camera;
+               GL::Matrix offset_matrix;
                RenderTarget *target;
 
                Eye();
 
                void create_target(unsigned, unsigned);
-               void setup_frame(const GL::Camera &, const GL::Vector3 &, float, const EyeParams &) const;
+               void setup_frame(const GL::Camera &, float, const EyeParams &) const;
                void render(const GL::Renderable &) const;
        };
 
@@ -52,7 +53,6 @@ private:
        const GL::Renderable *content;
        Eye left;
        Eye right;
-       float eye_spacing;
        Geometry::Angle<float> strabismus;
 
 public:
@@ -64,6 +64,7 @@ private:
 public:
        void set_content(const GL::Renderable *);
        void set_eye_spacing(float);
+       void set_eye_matrices(const GL::Matrix &, const GL::Matrix &);
        void set_strabismus(const Geometry::Angle<float> &);
 
 private: