]> git.tdb.fi Git - libs/vr.git/blob - source/openvr/openvrcombiner.cpp
20b485eac1a9cf3401bae393dbb422b3cdad2f82
[libs/vr.git] / source / openvr / openvrcombiner.cpp
1 #include <openvr.h>
2 #include "openvrcombiner.h"
3 #include "openvrsystem.h"
4
5 namespace Msp {
6 namespace VR {
7
8 struct OpenVRCombiner::Private
9 {
10         static Frustum get_projection(vr::EVREye);
11 };
12
13
14 OpenVRCombiner::OpenVRCombiner(OpenVRSystem &d, GL::View &v):
15         device(d),
16         view(v)
17 {
18         vr::IVRSystem *vr_sys = vr::VRSystem();
19         uint32_t w, h;
20         vr_sys->GetRecommendedRenderTargetSize(&w, &h);
21         target_width = w;
22         target_height = h;
23
24         Frustum left_frustum = Private::get_projection(vr::Eye_Left);
25         Frustum right_frustum = Private::get_projection(vr::Eye_Right);
26         configure_eye_frustums(left_frustum, right_frustum);
27
28         view.get_context().set_swap_interval(0);
29         set_mirroring(true);
30 }
31
32 void OpenVRCombiner::prepare() const
33 {
34         device.update_pose_matrices();
35 }
36
37 void OpenVRCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const
38 {
39         vr::Texture_t tex;
40         tex.eType = vr::API_OpenGL;
41         tex.eColorSpace = vr::ColorSpace_Gamma;
42
43         vr::IVRCompositor *compositor = vr::VRCompositor();
44         tex.handle = reinterpret_cast<void *>(left.get_id());
45         compositor->Submit(vr::Eye_Left, &tex);
46         tex.handle = reinterpret_cast<void *>(right.get_id());
47         compositor->Submit(vr::Eye_Right, &tex);
48
49         if(mirror)
50         {
51                 mirror->shdata.uniform("scale", view.get_aspect()/render_aspect/2, 0.5f);
52                 render_mirror(left);
53                 view.get_context().swap_buffers();
54         }
55 }
56
57
58 StereoCombiner::Frustum OpenVRCombiner::Private::get_projection(vr::EVREye eye)
59 {
60         Frustum frustum;
61         /* The parameter order is documented as left, right, top, bottom; however
62         the third parameter is actually negative and fourth one positive. */
63         vr::VRSystem()->GetProjectionRaw(eye, &frustum.left, &frustum.right, &frustum.bottom, &frustum.top);
64         return frustum;
65 }
66
67 } // namespace VR
68 } // namespace Msp