X-Git-Url: http://git.tdb.fi/?p=libs%2Fvr.git;a=blobdiff_plain;f=source%2Fstereoview.cpp;h=73be19c4527a420aacad992148a56ac25573c2cf;hp=d9f5822a4913204e3f9b8bb7997ba46f65e97b74;hb=23a6e642715fab2f275a2015ea079137cb31036d;hpb=8c61fd4fc7044c4ec0fd008173947b5b4bc00c6f diff --git a/source/stereoview.cpp b/source/stereoview.cpp index d9f5822..73be19c 100644 --- a/source/stereoview.cpp +++ b/source/stereoview.cpp @@ -1,4 +1,5 @@ #include +#include "headtrackingcamera.h" #include "stereocombiner.h" #include "stereoview.h" @@ -10,8 +11,23 @@ namespace VR { StereoView::StereoView(const StereoCombiner &c, const GL::Camera &a): combiner(c), base_camera(a), - content(0) + head_camera(0) { + init(); +} + +StereoView::StereoView(const StereoCombiner &c, HeadTrackingCamera &a): + combiner(c), + base_camera(a), + head_camera(&a) +{ + init(); +} + +void StereoView::init() +{ + content = 0; + unsigned w = combiner.get_target_width(); unsigned h = combiner.get_target_height(); left.create_target(w, h); @@ -28,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 &s) @@ -38,7 +61,8 @@ void StereoView::set_strabismus(const Geometry::Angle &s) void StereoView::setup_frame() const { - GL::Vector3 offset_axis = normalize(cross(base_camera.get_look_direction(), base_camera.get_up_direction()))*0.5f; + if(head_camera) + head_camera->update(); EyeParams params; params.fov = combiner.get_field_of_view(); @@ -53,12 +77,13 @@ void StereoView::setup_frame() const float halfw = tan(params.fov/2.0f)*params.aspect; frustum_skew = tan(Geometry::atan(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 { + combiner.prepare(); setup_frame(); if(content) { @@ -93,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 ¶ms) const +void StereoView::Eye::setup_frame(const GL::Camera &base_camera, float frustum_skew, const EyeParams ¶ms) 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);