]> git.tdb.fi Git - libs/vr.git/blobdiff - source/stereocombiner.cpp
Implement display mirroring in StereoCombiner
[libs/vr.git] / source / stereocombiner.cpp
index 75c788997e770fa0f1a7340d03b578e969062d51..ebb888ee6fe59ec6bd39446ebff00008b21380e4 100644 (file)
@@ -1,8 +1,31 @@
 #include <algorithm>
+#include <msp/gl/meshbuilder.h>
 #include "stereocombiner.h"
 
 using namespace std;
 
+namespace {
+
+const char mirror_vs_source[] =
+       "uniform vec2 scale;\n"
+       "varying vec2 texcoord;\n"
+       "void main()\n"
+       "{\n"
+       "       gl_Position = vec4(gl_Vertex.xy, 0.0, 1.0);\n"
+       "       texcoord = gl_Vertex.xy*scale*0.5+0.5;\n"
+       "}";
+
+const char mirror_fs_source[] =
+       "uniform sampler2D texture;\n"
+       "varying vec2 texcoord;\n"
+       "void main()\n"
+       "{\n"
+       "       gl_FragColor = texture2D(texture, texcoord);\n"
+       "}";
+
+}
+
+
 namespace Msp {
 namespace VR {
 
@@ -10,7 +33,8 @@ StereoCombiner::StereoCombiner():
        target_width(0),
        target_height(0),
        render_aspect(1.0f),
-       frustum_skew(0.0f)
+       frustum_skew(0.0f),
+       mirror(0)
 { }
 
 void StereoCombiner::configure_eye_frustums(const Frustum &left_frustum, const Frustum &right_frustum)
@@ -25,6 +49,28 @@ void StereoCombiner::configure_eye_frustums(const Frustum &left_frustum, const F
        render_aspect = (inner+outer)/(vertical*2);
 }
 
+void StereoCombiner::set_mirroring(bool m)
+{
+       if(m && !mirror)
+               mirror = new MirrorView;
+       else if(!m && mirror)
+       {
+               delete mirror;
+               mirror = 0;
+       }
+}
+
+void StereoCombiner::render_mirror(const GL::Texture2D &tex) const
+{
+       if(!mirror)
+               return;
+
+       GL::Bind bind_tex(tex);
+       GL::Bind bind_shprog(mirror->shader);
+       mirror->shdata.apply();
+       mirror->mesh.draw();
+}
+
 
 StereoCombiner::Frustum::Frustum():
        left(-1),
@@ -40,5 +86,21 @@ StereoCombiner::Frustum::Frustum(float l, float r, float b, float t):
        top(t)
 { }
 
+
+StereoCombiner::MirrorView::MirrorView():
+       mesh(GL::VERTEX2),
+       shader(mirror_vs_source, mirror_fs_source)
+{
+       GL::MeshBuilder bld(mesh);
+       bld.begin(GL::TRIANGLE_STRIP);
+       bld.vertex(-1, 1);
+       bld.vertex(-1, -1);
+       bld.vertex(1, 1);
+       bld.vertex(1, -1);
+       bld.end();
+
+       shdata.uniform("scale", 0.5f, 0.5f);
+}
+
 } // namespace VR
 } // namespace Msp