Make the StereoCombiner information interface more flexible
[libs/vr.git] / source / stereoview.cpp
1 #include <msp/gl/renderer.h>
2 #include "stereocombiner.h"
3 #include "stereoview.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace VR {
9
10 StereoView::StereoView(unsigned w, unsigned h, const GL::Camera &c, const GL::Renderable &r, const StereoCombiner &m):
11         width(w),
12         height(h),
13         base_camera(c),
14         renderable(r),
15         combiner(0)
16 {
17         set_combiner(m);
18         set_eye_spacing(0.07);
19 }
20
21 void StereoView::set_combiner(const StereoCombiner &c)
22 {
23         combiner = &c;
24
25         unsigned w = width*combiner->get_width_factor();
26         unsigned h = height*combiner->get_height_factor();
27         left.create_target(w, h);
28         right.create_target(w, h);
29 }
30
31 void StereoView::set_eye_spacing(float s)
32 {
33         eye_spacing = s;
34 }
35
36 void StereoView::setup_frame() const
37 {
38         offset_axis = normalize(cross(base_camera.get_look_direction(), base_camera.get_up_direction()))*0.5f;
39
40         EyeParams params;
41         params.fov = combiner->get_field_of_view();
42         if(params.fov==Geometry::Angle<float>::zero())
43                 params.fov = base_camera.get_field_of_view();
44
45         params.aspect = base_camera.get_aspect()*combiner->get_aspect_factor();
46         params.near_clip = base_camera.get_near_clip();
47         params.far_clip = base_camera.get_far_clip();
48
49         float frustum_skew = combiner->get_frustum_skew();
50
51         left.setup_frame(base_camera, offset_axis*-eye_spacing, frustum_skew, params);
52         right.setup_frame(base_camera, offset_axis*eye_spacing, -frustum_skew, params);
53
54         renderable.setup_frame();
55 }
56
57 void StereoView::finish_frame() const
58 {
59         renderable.finish_frame();
60 }
61
62 void StereoView::render(const GL::Tag &tag) const
63 {
64         setup_frame();
65         left.render(renderable, tag);
66         right.render(renderable, tag);
67         combiner->render(left.target->color, right.target->color);
68         finish_frame();
69 }
70
71 void StereoView::render(GL::Renderer &renderer, const GL::Tag &tag) const
72 {
73         renderer.escape();
74         return render(tag);
75 }
76
77
78 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
79 {
80         color.set_min_filter(GL::LINEAR);
81         color.set_wrap(GL::CLAMP_TO_EDGE);
82         color.storage(GL::RGB, width, height);
83         fbo.attach(GL::COLOR_ATTACHMENT0, color);
84
85         depth.storage(GL::DEPTH_COMPONENT, width, height);
86         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
87 }
88
89
90 StereoView::Eye::Eye():
91         target(0)
92 { }
93
94 void StereoView::Eye::create_target(unsigned w, unsigned h)
95 {
96         delete target;
97         target = new RenderTarget(w, h);
98 }
99
100 void StereoView::Eye::setup_frame(const GL::Camera &base_camera, const GL::Vector3 &offset, float frustum_skew, const EyeParams &params) const
101 {
102         camera.set_position(base_camera.get_position()+offset);
103         camera.set_up_direction(base_camera.get_up_direction());
104         camera.set_look_direction(base_camera.get_look_direction());
105
106         camera.set_field_of_view(params.fov);
107         camera.set_aspect(params.aspect);
108         camera.set_depth_clip(params.near_clip, params.far_clip);
109         camera.set_frustum_axis(frustum_skew, 0);
110 }
111
112 void StereoView::Eye::render(const GL::Renderable &renderable, const GL::Tag &tag) const
113 {
114         GL::Bind bind_fbo(target->fbo);
115         target->fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
116         GL::Renderer renderer(&camera);
117         renderable.render(renderer, tag);
118 }
119
120 } // namespace VR
121 } // namespace Msp