Use matrices for eye offsets instead of a simple spacing
[libs/vr.git] / source / stereoview.cpp
1 #include <msp/gl/renderer.h>
2 #include "headtrackingcamera.h"
3 #include "stereocombiner.h"
4 #include "stereoview.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace VR {
10
11 StereoView::StereoView(const StereoCombiner &c, const GL::Camera &a):
12         combiner(c),
13         base_camera(a),
14         head_camera(0)
15 {
16         init();
17 }
18
19 StereoView::StereoView(const StereoCombiner &c, HeadTrackingCamera &a):
20         combiner(c),
21         base_camera(a),
22         head_camera(&a)
23 {
24         init();
25 }
26
27 void StereoView::init()
28 {
29         content = 0;
30
31         unsigned w = combiner.get_target_width();
32         unsigned h = combiner.get_target_height();
33         left.create_target(w, h);
34         right.create_target(w, h);
35
36         set_eye_spacing(0.064);
37         set_strabismus(Geometry::Angle<float>::zero());
38 }
39
40 void StereoView::set_content(const GL::Renderable *r)
41 {
42         content = r;
43 }
44
45 void StereoView::set_eye_spacing(float s)
46 {
47         GL::Vector3 offset(s/2, 0, 0);
48         set_eye_matrices(GL::Matrix::translation(-offset), GL::Matrix::translation(offset));
49 }
50
51 void StereoView::set_eye_matrices(const GL::Matrix &left_matrix, const GL::Matrix &right_matrix)
52 {
53         left.offset_matrix = left_matrix;
54         right.offset_matrix = right_matrix;
55 }
56
57 void StereoView::set_strabismus(const Geometry::Angle<float> &s)
58 {
59         strabismus = s;
60 }
61
62 void StereoView::setup_frame() const
63 {
64         if(head_camera)
65                 head_camera->update();
66
67         EyeParams params;
68         params.fov = combiner.get_field_of_view();
69         if(params.fov==Geometry::Angle<float>::zero())
70                 params.fov = base_camera.get_field_of_view();
71
72         params.aspect = combiner.get_render_aspect();
73         params.near_clip = base_camera.get_near_clip();
74         params.far_clip = base_camera.get_far_clip();
75
76         float frustum_skew = combiner.get_frustum_skew();
77         float halfw = tan(params.fov/2.0f)*params.aspect;
78         frustum_skew = tan(Geometry::atan<float>(frustum_skew*halfw)+strabismus)/halfw;
79
80         left.setup_frame(base_camera, frustum_skew, params);
81         right.setup_frame(base_camera, -frustum_skew, params);
82 }
83
84 void StereoView::render() const
85 {
86         combiner.prepare();
87         setup_frame();
88         if(content)
89         {
90                 content->setup_frame();
91                 left.render(*content);
92                 right.render(*content);
93                 content->finish_frame();
94         }
95         combiner.render(left.target->color, right.target->color);
96 }
97
98
99 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
100 {
101         color.set_min_filter(GL::LINEAR);
102         color.set_wrap(GL::CLAMP_TO_EDGE);
103         color.storage(GL::RGB, width, height);
104         fbo.attach(GL::COLOR_ATTACHMENT0, color);
105
106         depth.storage(GL::DEPTH_COMPONENT, width, height);
107         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
108 }
109
110
111 StereoView::Eye::Eye():
112         target(0)
113 { }
114
115 void StereoView::Eye::create_target(unsigned w, unsigned h)
116 {
117         delete target;
118         target = new RenderTarget(w, h);
119 }
120
121 void StereoView::Eye::setup_frame(const GL::Camera &base_camera, float frustum_skew, const EyeParams &params) const
122 {
123         GL::Matrix matrix = base_camera.get_object_matrix()*offset_matrix;
124         camera.set_position(matrix*GL::Vector3());
125         camera.set_up_direction((matrix*GL::Vector4(0, 1, 0, 0)).slice<3>(0));
126         camera.set_look_direction((matrix*GL::Vector4(0, 0, -1, 0)).slice<3>(0));
127
128         camera.set_field_of_view(params.fov);
129         camera.set_aspect(params.aspect);
130         camera.set_depth_clip(params.near_clip, params.far_clip);
131         camera.set_frustum_axis(frustum_skew, 0);
132 }
133
134 void StereoView::Eye::render(const GL::Renderable &renderable) const
135 {
136         GL::Bind bind_fbo(target->fbo);
137         target->fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
138         GL::Renderer renderer(&camera);
139         renderable.render(renderer);
140 }
141
142 } // namespace VR
143 } // namespace Msp