Update camera pose as part of the render call
[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         eye_spacing = s;
48 }
49
50 void StereoView::set_strabismus(const Geometry::Angle<float> &s)
51 {
52         strabismus = s;
53 }
54
55 void StereoView::setup_frame() const
56 {
57         if(head_camera)
58                 head_camera->update();
59
60         GL::Vector3 offset_axis = normalize(cross(base_camera.get_look_direction(), base_camera.get_up_direction()))*0.5f;
61
62         EyeParams params;
63         params.fov = combiner.get_field_of_view();
64         if(params.fov==Geometry::Angle<float>::zero())
65                 params.fov = base_camera.get_field_of_view();
66
67         params.aspect = combiner.get_render_aspect();
68         params.near_clip = base_camera.get_near_clip();
69         params.far_clip = base_camera.get_far_clip();
70
71         float frustum_skew = combiner.get_frustum_skew();
72         float halfw = tan(params.fov/2.0f)*params.aspect;
73         frustum_skew = tan(Geometry::atan<float>(frustum_skew*halfw)+strabismus)/halfw;
74
75         left.setup_frame(base_camera, offset_axis*-eye_spacing, frustum_skew, params);
76         right.setup_frame(base_camera, offset_axis*eye_spacing, -frustum_skew, params);
77 }
78
79 void StereoView::render() const
80 {
81         combiner.prepare();
82         setup_frame();
83         if(content)
84         {
85                 content->setup_frame();
86                 left.render(*content);
87                 right.render(*content);
88                 content->finish_frame();
89         }
90         combiner.render(left.target->color, right.target->color);
91 }
92
93
94 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
95 {
96         color.set_min_filter(GL::LINEAR);
97         color.set_wrap(GL::CLAMP_TO_EDGE);
98         color.storage(GL::RGB, width, height);
99         fbo.attach(GL::COLOR_ATTACHMENT0, color);
100
101         depth.storage(GL::DEPTH_COMPONENT, width, height);
102         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
103 }
104
105
106 StereoView::Eye::Eye():
107         target(0)
108 { }
109
110 void StereoView::Eye::create_target(unsigned w, unsigned h)
111 {
112         delete target;
113         target = new RenderTarget(w, h);
114 }
115
116 void StereoView::Eye::setup_frame(const GL::Camera &base_camera, const GL::Vector3 &offset, float frustum_skew, const EyeParams &params) const
117 {
118         camera.set_position(base_camera.get_position()+offset);
119         camera.set_up_direction(base_camera.get_up_direction());
120         camera.set_look_direction(base_camera.get_look_direction());
121
122         camera.set_field_of_view(params.fov);
123         camera.set_aspect(params.aspect);
124         camera.set_depth_clip(params.near_clip, params.far_clip);
125         camera.set_frustum_axis(frustum_skew, 0);
126 }
127
128 void StereoView::Eye::render(const GL::Renderable &renderable) const
129 {
130         GL::Bind bind_fbo(target->fbo);
131         target->fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
132         GL::Renderer renderer(&camera);
133         renderable.render(renderer);
134 }
135
136 } // namespace VR
137 } // namespace Msp