]> git.tdb.fi Git - libs/vr.git/blob - source/stereoview.cpp
Change the mutability of StereoView members
[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         setup_frame();
63         if(content)
64         {
65                 content->setup_frame();
66                 left.render(*content);
67                 right.render(*content);
68                 content->finish_frame();
69         }
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