]> git.tdb.fi Git - libs/vr.git/blob - source/stereocombiner.cpp
Add query functions for whether certain features are supported
[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 && !is_mirroring_supported())
55                 throw runtime_error("mirroring not supported");
56
57         if(m && !mirror)
58                 mirror = new MirrorView;
59         else if(!m && mirror)
60         {
61                 delete mirror;
62                 mirror = 0;
63         }
64 }
65
66 void StereoCombiner::render_mirror(const GL::Texture2D &tex) const
67 {
68         if(!mirror)
69                 return;
70
71         GL::Bind bind_tex(tex);
72         GL::Bind bind_shprog(mirror->shader);
73         mirror->shdata.apply();
74         mirror->mesh.draw();
75 }
76
77
78 StereoCombiner::Frustum::Frustum():
79         left(-1),
80         right(1),
81         bottom(-1),
82         top(1)
83 { }
84
85 StereoCombiner::Frustum::Frustum(float l, float r, float b, float t):
86         left(l),
87         right(r),
88         bottom(b),
89         top(t)
90 { }
91
92
93 StereoCombiner::MirrorView::MirrorView():
94         mesh(GL::VERTEX2),
95         shader(mirror_vs_source, mirror_fs_source)
96 {
97         GL::MeshBuilder bld(mesh);
98         bld.begin(GL::TRIANGLE_STRIP);
99         bld.vertex(-1, 1);
100         bld.vertex(-1, -1);
101         bld.vertex(1, 1);
102         bld.vertex(1, -1);
103         bld.end();
104
105         shdata.uniform("scale", 0.5f, 0.5f);
106 }
107
108 } // namespace VR
109 } // namespace Msp