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