]> git.tdb.fi Git - libs/vr.git/blob - source/sidebysidecombiner.cpp
c5aef041ecf3f123501d9fc062d5c497f1abe1de
[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(bool c):
30         mesh(GL::VERTEX2),
31         shprog(vs_source, fs_source)
32 {
33         width_factor = 0.5f;
34         aspect_factor = 0.5f;
35
36         left_shdata.uniform("texture", 0);
37         right_shdata.uniform("texture", 0);
38
39         set_cross_eyed(c);
40
41         GL::MeshBuilder bld(mesh);
42         bld.begin(GL::TRIANGLE_STRIP);
43         bld.vertex(-1, 1);
44         bld.vertex(-1, -1);
45         bld.vertex(1, 1);
46         bld.vertex(1, -1);
47         bld.end();
48 }
49
50 void SideBySideCombiner::set_cross_eyed(bool c)
51 {
52         cross_eyed = c;
53         float m = (cross_eyed ? -0.5f : 0.5f);
54         left_shdata.uniform("offset", -m);
55         right_shdata.uniform("offset", m);
56 }
57
58 void SideBySideCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const
59 {
60         GL::Bind bind_shprog(shprog);
61
62         GL::Bind bind_tex(left);
63         left_shdata.apply();
64         mesh.draw();
65
66         right.bind();
67         right_shdata.apply();
68         mesh.draw();
69 }
70
71 } // namespace VR
72 } // namespace Msp