08949e653599509021f24eb29b2f9b37f41a6826
[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_divisor()*combiner->get_oversize();
26         unsigned h = height/combiner->get_height_divisor()*combiner->get_oversize();
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();
46         if(!combiner->is_aspect_kept())
47                 params.aspect = params.aspect*combiner->get_height_divisor()/combiner->get_width_divisor();
48
49         params.near_clip = base_camera.get_near_clip();
50         params.far_clip = base_camera.get_far_clip();
51
52         float frustum_skew = combiner->get_frustum_skew();
53
54         left.setup_frame(base_camera, offset_axis*-eye_spacing, frustum_skew, params);
55         right.setup_frame(base_camera, offset_axis*eye_spacing, -frustum_skew, params);
56
57         renderable.setup_frame();
58 }
59
60 void StereoView::finish_frame() const
61 {
62         renderable.finish_frame();
63 }
64
65 void StereoView::render(const GL::Tag &tag) const
66 {
67         setup_frame();
68         left.render(renderable, tag);
69         right.render(renderable, tag);
70         combiner->render(left.target->color, right.target->color);
71         finish_frame();
72 }
73
74 void StereoView::render(GL::Renderer &renderer, const GL::Tag &tag) const
75 {
76         renderer.escape();
77         return render(tag);
78 }
79
80
81 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
82 {
83         color.set_min_filter(GL::LINEAR);
84         color.set_wrap(GL::CLAMP_TO_EDGE);
85         color.storage(GL::RGB, width, height);
86         fbo.attach(GL::COLOR_ATTACHMENT0, color);
87
88         depth.storage(GL::DEPTH_COMPONENT, width, height);
89         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
90 }
91
92
93 StereoView::Eye::Eye():
94         target(0)
95 { }
96
97 void StereoView::Eye::create_target(unsigned w, unsigned h)
98 {
99         delete target;
100         target = new RenderTarget(w, h);
101 }
102
103 void StereoView::Eye::setup_frame(const GL::Camera &base_camera, const GL::Vector3 &offset, float frustum_skew, const EyeParams &params) const
104 {
105         camera.set_position(base_camera.get_position()+offset);
106         camera.set_up_direction(base_camera.get_up_direction());
107         camera.set_look_direction(base_camera.get_look_direction());
108
109         camera.set_field_of_view(params.fov);
110         camera.set_aspect(params.aspect);
111         camera.set_depth_clip(params.near_clip, params.far_clip);
112         camera.set_frustum_axis(frustum_skew, 0);
113 }
114
115 void StereoView::Eye::render(const GL::Renderable &renderable, const GL::Tag &tag) const
116 {
117         GL::Bind bind_fbo(target->fbo);
118         target->fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
119         GL::Renderer renderer(&camera);
120         renderable.render(renderer, tag);
121 }
122
123 } // namespace VR
124 } // namespace Msp