Make offset_axis a local variable of StereoView::setup_frame
[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.064);
19         set_strabismus(Geometry::Angle<float>::zero());
20 }
21
22 void StereoView::set_combiner(const StereoCombiner &c)
23 {
24         combiner = &c;
25
26         unsigned w = width*combiner->get_width_factor();
27         unsigned h = height*combiner->get_height_factor();
28         left.create_target(w, h);
29         right.create_target(w, h);
30 }
31
32 void StereoView::set_eye_spacing(float s)
33 {
34         eye_spacing = s;
35 }
36
37 void StereoView::set_strabismus(const Geometry::Angle<float> &s)
38 {
39         strabismus = s;
40 }
41
42 void StereoView::setup_frame() const
43 {
44         GL::Vector3 offset_axis = normalize(cross(base_camera.get_look_direction(), base_camera.get_up_direction()))*0.5f;
45
46         EyeParams params;
47         params.fov = combiner->get_field_of_view();
48         if(params.fov==Geometry::Angle<float>::zero())
49                 params.fov = base_camera.get_field_of_view();
50
51         params.aspect = base_camera.get_aspect()*combiner->get_aspect_factor();
52         params.near_clip = base_camera.get_near_clip();
53         params.far_clip = base_camera.get_far_clip();
54
55         float frustum_skew = combiner->get_frustum_skew();
56         float halfw = tan(params.fov/2.0f)*params.aspect;
57         frustum_skew = tan(Geometry::atan<float>(frustum_skew*halfw)+strabismus)/halfw;
58
59         left.setup_frame(base_camera, offset_axis*-eye_spacing, frustum_skew, params);
60         right.setup_frame(base_camera, offset_axis*eye_spacing, -frustum_skew, params);
61 }
62
63 void StereoView::render() const
64 {
65         setup_frame();
66         renderable.setup_frame();
67         left.render(renderable);
68         right.render(renderable);
69         renderable.finish_frame();
70         combiner->render(left.target->color, right.target->color);
71 }
72
73
74 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
75 {
76         color.set_min_filter(GL::LINEAR);
77         color.set_wrap(GL::CLAMP_TO_EDGE);
78         color.storage(GL::RGB, width, height);
79         fbo.attach(GL::COLOR_ATTACHMENT0, color);
80
81         depth.storage(GL::DEPTH_COMPONENT, width, height);
82         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
83 }
84
85
86 StereoView::Eye::Eye():
87         target(0)
88 { }
89
90 void StereoView::Eye::create_target(unsigned w, unsigned h)
91 {
92         delete target;
93         target = new RenderTarget(w, h);
94 }
95
96 void StereoView::Eye::setup_frame(const GL::Camera &base_camera, const GL::Vector3 &offset, float frustum_skew, const EyeParams &params) const
97 {
98         camera.set_position(base_camera.get_position()+offset);
99         camera.set_up_direction(base_camera.get_up_direction());
100         camera.set_look_direction(base_camera.get_look_direction());
101
102         camera.set_field_of_view(params.fov);
103         camera.set_aspect(params.aspect);
104         camera.set_depth_clip(params.near_clip, params.far_clip);
105         camera.set_frustum_axis(frustum_skew, 0);
106 }
107
108 void StereoView::Eye::render(const GL::Renderable &renderable) const
109 {
110         GL::Bind bind_fbo(target->fbo);
111         target->fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
112         GL::Renderer renderer(&camera);
113         renderable.render(renderer);
114 }
115
116 } // namespace VR
117 } // namespace Msp