]> git.tdb.fi Git - libs/vr.git/blob - source/stereocombiner.cpp
Add functions to use absolute tracking
[libs/vr.git] / source / stereocombiner.cpp
1 #include <algorithm>
2 #include <msp/gl/meshbuilder.h>
3 #include "stereocombiner.h"
4
5 using namespace std;
6
7 namespace {
8
9 const char mirror_vs_source[] =
10         "uniform vec2 scale;\n"
11         "varying vec2 texcoord;\n"
12         "void main()\n"
13         "{\n"
14         "       gl_Position = vec4(gl_Vertex.xy, 0.0, 1.0);\n"
15         "       texcoord = gl_Vertex.xy*scale*0.5+0.5;\n"
16         "}";
17
18 const char mirror_fs_source[] =
19         "uniform sampler2D texture;\n"
20         "varying vec2 texcoord;\n"
21         "void main()\n"
22         "{\n"
23         "       gl_FragColor = texture2D(texture, texcoord);\n"
24         "}";
25
26 }
27
28
29 namespace Msp {
30 namespace VR {
31
32 StereoCombiner::StereoCombiner():
33         target_width(0),
34         target_height(0),
35         render_aspect(1.0f),
36         frustum_skew(0.0f),
37         mirror(0)
38 { }
39
40 void StereoCombiner::configure_eye_frustums(const Frustum &left_frustum, const Frustum &right_frustum)
41 {
42         float vertical = max(max(left_frustum.top, -left_frustum.bottom), max(right_frustum.top, -right_frustum.bottom));
43         fov = Geometry::atan<float>(vertical)*2.0f;
44
45         float inner = max(left_frustum.right, -right_frustum.left);
46         float outer = max(-left_frustum.left, right_frustum.right);
47         frustum_skew = (inner-outer)/(inner+outer);
48
49         render_aspect = (inner+outer)/(vertical*2);
50 }
51
52 void StereoCombiner::set_mirroring(bool m)
53 {
54         if(m && !mirror)
55                 mirror = new MirrorView;
56         else if(!m && mirror)
57         {
58                 delete mirror;
59                 mirror = 0;
60         }
61 }
62
63 void StereoCombiner::render_mirror(const GL::Texture2D &tex) const
64 {
65         if(!mirror)
66                 return;
67
68         GL::Bind bind_tex(tex);
69         GL::Bind bind_shprog(mirror->shader);
70         mirror->shdata.apply();
71         mirror->mesh.draw();
72 }
73
74
75 StereoCombiner::Frustum::Frustum():
76         left(-1),
77         right(1),
78         bottom(-1),
79         top(1)
80 { }
81
82 StereoCombiner::Frustum::Frustum(float l, float r, float b, float t):
83         left(l),
84         right(r),
85         bottom(b),
86         top(t)
87 { }
88
89
90 StereoCombiner::MirrorView::MirrorView():
91         mesh(GL::VERTEX2),
92         shader(mirror_vs_source, mirror_fs_source)
93 {
94         GL::MeshBuilder bld(mesh);
95         bld.begin(GL::TRIANGLE_STRIP);
96         bld.vertex(-1, 1);
97         bld.vertex(-1, -1);
98         bld.vertex(1, 1);
99         bld.vertex(1, -1);
100         bld.end();
101
102         shdata.uniform("scale", 0.5f, 0.5f);
103 }
104
105 } // namespace VR
106 } // namespace Msp