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