Give combiners more control over rendering
[libs/vr.git] / source / sidebysidecombiner.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include <msp/gl/texture2d.h>
3 #include "sidebysidecombiner.h"
4
5 namespace {
6
7 const char vs_source[] =
8         "uniform float offset;\n"
9         "varying vec2 texcoord;\n"
10         "void main()\n"
11         "{\n"
12         "       gl_Position = vec4(gl_Vertex.x*0.5+offset, gl_Vertex.yzw);\n"
13         "       texcoord = gl_Vertex.xy*0.5+0.5;\n"
14         "}\n";
15
16 const char fs_source[] =
17         "uniform sampler2D texture;\n"
18         "varying vec2 texcoord;\n"
19         "void main()\n"
20         "{\n"
21         "       gl_FragColor = texture2D(texture, texcoord);\n"
22         "}\n";
23
24 }
25
26 namespace Msp {
27 namespace VR {
28
29 SideBySideCombiner::SideBySideCombiner(GL::View &v, bool c):
30         view(v),
31         mesh(GL::VERTEX2),
32         shprog(vs_source, fs_source)
33 {
34         target_width = view.get_width()/2;
35         target_height = view.get_height()/2;
36         render_aspect = static_cast<float>(target_width)/target_height;
37
38         left_shdata.uniform("texture", 0);
39         right_shdata.uniform("texture", 0);
40
41         set_cross_eyed(c);
42
43         GL::MeshBuilder bld(mesh);
44         bld.begin(GL::TRIANGLE_STRIP);
45         bld.vertex(-1, 1);
46         bld.vertex(-1, -1);
47         bld.vertex(1, 1);
48         bld.vertex(1, -1);
49         bld.end();
50 }
51
52 void SideBySideCombiner::set_cross_eyed(bool c)
53 {
54         cross_eyed = c;
55         float m = (cross_eyed ? -0.5f : 0.5f);
56         left_shdata.uniform("offset", -m);
57         right_shdata.uniform("offset", m);
58 }
59
60 void SideBySideCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const
61 {
62         GL::Bind bind_shprog(shprog);
63
64         GL::Bind bind_tex(left);
65         left_shdata.apply();
66         mesh.draw();
67
68         right.bind();
69         right_shdata.apply();
70         mesh.draw();
71
72         view.get_context().swap_buffers();
73 }
74
75 } // namespace VR
76 } // namespace Msp