]> git.tdb.fi Git - libs/gl.git/blob - source/stereoview.cpp
Field of view is an angle too
[libs/gl.git] / source / stereoview.cpp
1 #include "renderer.h"
2 #include "stereocombiner.h"
3 #include "stereoview.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 StereoView::StereoView(unsigned w, unsigned h, const Camera &c, const 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();
26         unsigned h = height/combiner->get_height_divisor();
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         left.setup_frame(base_camera, offset_axis*-eye_spacing, params);
53         right.setup_frame(base_camera, offset_axis*eye_spacing, params);
54
55         renderable.setup_frame();
56 }
57
58 void StereoView::finish_frame() const
59 {
60         renderable.finish_frame();
61 }
62
63 void StereoView::render(const Tag &tag) const
64 {
65         setup_frame();
66         left.render(renderable, tag);
67         right.render(renderable, tag);
68         combiner->render(left.target->color, right.target->color);
69         finish_frame();
70 }
71
72 void StereoView::render(Renderer &renderer, const Tag &tag) const
73 {
74         renderer.escape();
75         return render(tag);
76 }
77
78
79 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
80 {
81         color.set_min_filter(LINEAR);
82         color.set_wrap(CLAMP_TO_EDGE);
83         color.storage(RGB, width, height);
84         fbo.attach(COLOR_ATTACHMENT0, color);
85
86         depth.storage(DEPTH_COMPONENT, width, height);
87         fbo.attach(DEPTH_ATTACHMENT, depth);
88 }
89
90
91 StereoView::Eye::Eye():
92         target(0)
93 { }
94
95 void StereoView::Eye::create_target(unsigned w, unsigned h)
96 {
97         delete target;
98         target = new RenderTarget(w, h);
99 }
100
101 void StereoView::Eye::setup_frame(const Camera &base_camera, const Vector3 &offset, const EyeParams &params) const
102 {
103         camera.set_position(base_camera.get_position()+offset);
104         camera.set_up_direction(base_camera.get_up_direction());
105         camera.set_look_direction(base_camera.get_look_direction());
106
107         camera.set_field_of_view(params.fov);
108         camera.set_aspect(params.aspect);
109         camera.set_depth_clip(params.near_clip, params.far_clip);
110 }
111
112 void StereoView::Eye::render(const Renderable &renderable, const Tag &tag) const
113 {
114         Bind bind_fbo(target->fbo);
115         Renderer renderer(&camera);
116         renderable.render(renderer, tag);
117 }
118
119 } // namespace GL
120 } // namespace Msp