]> git.tdb.fi Git - libs/gl.git/blobdiff - source/sidebysidecombiner.cpp
Add classes for stereographic rendering
[libs/gl.git] / source / sidebysidecombiner.cpp
diff --git a/source/sidebysidecombiner.cpp b/source/sidebysidecombiner.cpp
new file mode 100644 (file)
index 0000000..b9d1d89
--- /dev/null
@@ -0,0 +1,71 @@
+#include "meshbuilder.h"
+#include "sidebysidecombiner.h"
+#include "texture2d.h"
+
+namespace {
+
+const char vs_source[] =
+       "uniform float offset;\n"
+       "varying vec2 texcoord;\n"
+       "void main()\n"
+       "{\n"
+       "       gl_Position = vec4(gl_Vertex.x*0.5+offset, gl_Vertex.yzw);\n"
+       "       texcoord = gl_Vertex.xy*0.5+0.5;\n"
+       "}\n";
+
+const char fs_source[] =
+       "uniform sampler2D texture;\n"
+       "varying vec2 texcoord;\n"
+       "void main()\n"
+       "{\n"
+       "       gl_FragColor = texture2D(texture, texcoord);\n"
+       "}\n";
+
+}
+
+namespace Msp {
+namespace GL {
+
+SideBySideCombiner::SideBySideCombiner(bool c):
+       mesh(VERTEX2),
+       shprog(vs_source, fs_source)
+{
+       width_div = 2;
+
+       left_shdata.uniform("texture", 0);
+       right_shdata.uniform("texture", 0);
+
+       set_cross_eyed(c);
+
+       MeshBuilder bld(mesh);
+       bld.begin(TRIANGLE_STRIP);
+       bld.vertex(-1, 1);
+       bld.vertex(-1, -1);
+       bld.vertex(1, 1);
+       bld.vertex(1, -1);
+       bld.end();
+}
+
+void SideBySideCombiner::set_cross_eyed(bool c)
+{
+       cross_eyed = c;
+       float m = (cross_eyed ? -0.5f : 0.5f);
+       left_shdata.uniform("offset", -m);
+       right_shdata.uniform("offset", m);
+}
+
+void SideBySideCombiner::render(const Texture2D &left, const Texture2D &right) const
+{
+       Bind bind_shprog(shprog);
+
+       Bind bind_tex(left);
+       left_shdata.apply();
+       mesh.draw();
+
+       right.bind();
+       right_shdata.apply();
+       mesh.draw();
+}
+
+} // namespace GL
+} // namespace Msp