2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/texture2d.h>
4 #include "oculusriftcombiner.h"
5 #include "oculusriftdevice.h"
6 #include "oculusriftdevice_private.h"
13 const char vs_source[] =
14 "uniform mat4 timewarp[2];\n"
15 "uniform vec2 uv_scale;\n"
16 "uniform vec2 uv_offset;\n"
17 "varying vec2 texcoord_r;\n"
18 "varying vec2 texcoord_g;\n"
19 "varying vec2 texcoord_b;\n"
20 "varying float vignette;\n"
21 "vec2 apply_timewarp(vec2 coords, float factor)\n"
23 " vec4 coords4 = vec4(coords, 1.0, 1.0);\n"
24 " vec4 warped = mix(timewarp[0]*coords4, timewarp[1]*coords4, factor);\n"
25 " return (warped.xy/warped.z)*uv_scale+uv_offset;\n"
29 " gl_Position = vec4(gl_Vertex.xy, 0.5, 1.0);\n"
30 " float tw_factor = gl_MultiTexCoord3.y;\n"
31 " texcoord_r = apply_timewarp(gl_MultiTexCoord0.xy, tw_factor);\n"
32 " texcoord_g = apply_timewarp(gl_MultiTexCoord1.xy, tw_factor);\n"
33 " texcoord_b = apply_timewarp(gl_MultiTexCoord2.xy, tw_factor);\n"
34 " vignette = gl_MultiTexCoord3.x;\n"
37 const char fs_source[] =
38 "uniform sampler2D texture;\n"
39 "varying vec2 texcoord_r;\n"
40 "varying vec2 texcoord_g;\n"
41 "varying vec2 texcoord_b;\n"
42 "varying float vignette;\n"
45 " float r = texture2D(texture, texcoord_r).r;\n"
46 " float g = texture2D(texture, texcoord_g).g;\n"
47 " float b = texture2D(texture, texcoord_b).b;\n"
48 " gl_FragColor = vec4(vec3(r, g, b)*vignette, 1.0);\n"
51 void create_distortion_mesh(Msp::GL::Mesh &mesh, ovrHmd hmd, ovrEyeType eye, const ovrFovPort &fov)
53 ovrDistortionMesh ovr_mesh;
54 ovrHmd_CreateDistortionMesh(hmd, eye, fov, ovrDistortionCap_Vignette|ovrDistortionCap_TimeWarp, &ovr_mesh);
56 Msp::GL::MeshBuilder bld(mesh);
57 for(unsigned i=0; i<ovr_mesh.VertexCount; ++i)
59 ovrDistortionVertex &v = ovr_mesh.pVertexData[i];
60 bld.multitexcoord(0, v.TanEyeAnglesR.x, v.TanEyeAnglesR.y);
61 bld.multitexcoord(1, v.TanEyeAnglesG.x, v.TanEyeAnglesG.y);
62 bld.multitexcoord(2, v.TanEyeAnglesB.x, v.TanEyeAnglesB.y);
63 bld.multitexcoord(3, v.VignetteFactor, v.TimeWarpFactor);
64 bld.vertex(v.ScreenPosNDC.x, v.ScreenPosNDC.y);
67 Msp::GL::Batch batch(Msp::GL::TRIANGLES);
68 batch.append(vector<unsigned>(ovr_mesh.pIndexData, ovr_mesh.pIndexData+ovr_mesh.IndexCount));
69 mesh.add_batch(batch);
71 ovrHmd_DestroyDistortionMesh(&ovr_mesh);
79 OculusRiftCombiner::OculusRiftCombiner(OculusRiftDevice &d, GL::View &v):
82 left_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2, GL::TEXCOORD2,3)),
83 right_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2, GL::TEXCOORD2,3)),
84 shprog(vs_source, fs_source)
86 ovrHmd hmd = device.get_private().ovr_hmd;
88 ovrFovPort left_fov = hmd->DefaultEyeFov[ovrEye_Left];
89 ovrFovPort right_fov = hmd->DefaultEyeFov[ovrEye_Right];
90 float vertical = max(max(left_fov.UpTan, left_fov.DownTan), max(right_fov.UpTan, right_fov.DownTan));
91 fov = Geometry::atan<float>(vertical)*2.0f;
93 float inner = max(left_fov.RightTan, right_fov.LeftTan);
94 float outer = max(left_fov.LeftTan, right_fov.RightTan);
95 frustum_skew = (inner-outer)/(inner+outer);
97 left_fov.UpTan = right_fov.UpTan = vertical;
98 left_fov.DownTan = right_fov.DownTan = vertical;
99 left_fov.RightTan = right_fov.LeftTan = inner;
100 left_fov.LeftTan = right_fov.RightTan = outer;
102 create_distortion_mesh(left_mesh, hmd, ovrEye_Left, left_fov);
103 create_distortion_mesh(right_mesh, hmd, ovrEye_Right, right_fov);
105 ovrSizei tex_size = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, left_fov, 1.0);
106 target_width = tex_size.w;
107 target_height = tex_size.h;
108 render_aspect = (inner+outer)/(vertical*2);
110 left_shdata.uniform("texture", 0);
111 right_shdata.uniform("texture", 0);
116 view_rect.Size = tex_size;
117 ovrVector2f uv_scale_offset[2];
118 ovrHmd_GetRenderScaleAndOffset(left_fov, tex_size, view_rect, uv_scale_offset);
119 left_shdata.uniform("uv_scale", uv_scale_offset[0].x, -uv_scale_offset[0].y);
120 left_shdata.uniform("uv_offset", uv_scale_offset[1].x, 1-uv_scale_offset[1].y);
121 ovrHmd_GetRenderScaleAndOffset(right_fov, tex_size, view_rect, uv_scale_offset);
122 right_shdata.uniform("uv_scale", uv_scale_offset[0].x, -uv_scale_offset[0].y);
123 right_shdata.uniform("uv_offset", uv_scale_offset[1].x, 1-uv_scale_offset[1].y);
125 device.configure_window(view.get_window());
128 void OculusRiftCombiner::prepare() const
130 device.begin_frame();
133 void OculusRiftCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const
135 GL::Bind bind_shprog(shprog);
137 ovrHmd hmd = device.get_private().ovr_hmd;
139 if(device.is_timing_active())
141 ovr_WaitTillTime(device.get_timewarp_time());
142 ovrTrackingState state = ovrHmd_GetTrackingState(hmd, device.get_tracking_time());
144 ovrMatrix4f matrices[2];
145 ovrHmd_GetEyeTimewarpMatrices(hmd, ovrEye_Left, state.HeadPose.ThePose, matrices);
146 left_shdata.uniform_matrix4_array("timewarp", 2, &matrices[0].M[0][0]);
148 ovrHmd_GetEyeTimewarpMatrices(hmd, ovrEye_Right, state.HeadPose.ThePose, matrices);
149 right_shdata.uniform_matrix4_array("timewarp", 2, &matrices[0].M[0][0]);
153 GL::Matrix matrices[2];
154 left_shdata.uniform_matrix4_array("timewarp", 2, matrices[0].data());
155 right_shdata.uniform_matrix4_array("timewarp", 2, matrices[0].data());
158 GL::Bind bind_tex(left);
163 right_shdata.apply();
166 view.get_context().swap_buffers();