1 #include <msp/gl/renderer.h>
2 #include "stereocombiner.h"
3 #include "stereoview.h"
10 StereoView::StereoView(const StereoCombiner &c, const GL::Camera &a):
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);
20 set_eye_spacing(0.064);
21 set_strabismus(Geometry::Angle<float>::zero());
24 void StereoView::set_content(const GL::Renderable *r)
29 void StereoView::set_eye_spacing(float s)
34 void StereoView::set_strabismus(const Geometry::Angle<float> &s)
39 void StereoView::setup_frame() const
41 GL::Vector3 offset_axis = normalize(cross(base_camera.get_look_direction(), base_camera.get_up_direction()))*0.5f;
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();
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();
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;
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);
60 void StereoView::render() const
65 content->setup_frame();
66 left.render(*content);
67 right.render(*content);
68 content->finish_frame();
70 combiner.render(left.target->color, right.target->color);
74 StereoView::RenderTarget::RenderTarget(unsigned width, unsigned height)
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);
81 depth.storage(GL::DEPTH_COMPONENT, width, height);
82 fbo.attach(GL::DEPTH_ATTACHMENT, depth);
86 StereoView::Eye::Eye():
90 void StereoView::Eye::create_target(unsigned w, unsigned h)
93 target = new RenderTarget(w, h);
96 void StereoView::Eye::setup_frame(const GL::Camera &base_camera, const GL::Vector3 &offset, float frustum_skew, const EyeParams ¶ms) const
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());
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);
108 void StereoView::Eye::render(const GL::Renderable &renderable) const
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);