X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Foculusriftcombiner.cpp;h=0d883fa575f9e7b09450f9f87a1ad4ab4fd668a4;hb=8d1197440f07062a1020d902619ae8e9a494baa0;hp=67b4729638adf27d9fd36fee7aa2614b91a73429;hpb=7576cb135f0b533c302e9c6ea91bd264d7c59954;p=libs%2Fvr.git diff --git a/source/oculusriftcombiner.cpp b/source/oculusriftcombiner.cpp index 67b4729..0d883fa 100644 --- a/source/oculusriftcombiner.cpp +++ b/source/oculusriftcombiner.cpp @@ -2,195 +2,159 @@ #include #include #include "oculusriftcombiner.h" +#include "oculusriftdevice.h" +#include "oculusriftdevice_private.h" using namespace std; + namespace { const char vs_source[] = - "uniform float offset;\n" - "uniform vec2 lens_center;\n" - "uniform vec3 scale;\n" - "varying vec2 texcoord;\n" + "uniform mat4 timewarp[2];\n" + "uniform vec2 uv_scale;\n" + "uniform vec2 uv_offset;\n" + "varying vec2 texcoord_r;\n" + "varying vec2 texcoord_g;\n" + "varying vec2 texcoord_b;\n" + "varying float vignette;\n" + "vec2 apply_timewarp(vec2 coords, float factor)\n" + "{\n" + " vec4 coords4 = vec4(coords, 1.0, 1.0);\n" + " vec4 warped = mix(timewarp[0]*coords4, timewarp[1]*coords4, factor);\n" + " return (warped.xy/warped.z)*uv_scale+uv_offset;\n" + "}\n" "void main()\n" "{\n" - " gl_Position = vec4(gl_Vertex.x*0.5+offset, gl_Vertex.yzw);\n" - " texcoord = (gl_Vertex.xy*0.5+0.5-lens_center)*scale.xy;\n" + " gl_Position = vec4(gl_Vertex.xy, 0.5, 1.0);\n" + " float tw_factor = gl_MultiTexCoord3.y;\n" + " texcoord_r = apply_timewarp(gl_MultiTexCoord0.xy, tw_factor);\n" + " texcoord_g = apply_timewarp(gl_MultiTexCoord1.xy, tw_factor);\n" + " texcoord_b = apply_timewarp(gl_MultiTexCoord2.xy, tw_factor);\n" + " vignette = gl_MultiTexCoord3.x;\n" "}\n"; const char fs_source[] = "uniform sampler2D texture;\n" - "uniform vec4 distortion;\n" - "uniform vec4 chromatic;\n" - "uniform vec2 lens_center;\n" - "uniform vec2 eye_center;\n" - "uniform vec3 scale;\n" - "varying vec2 texcoord;\n" + "varying vec2 texcoord_r;\n" + "varying vec2 texcoord_g;\n" + "varying vec2 texcoord_b;\n" + "varying float vignette;\n" "void main()\n" "{\n" - " float r_sq = dot(texcoord, texcoord);\n" - " float f = dot(distortion, vec4(1.0, r_sq, r_sq*r_sq, r_sq*r_sq*r_sq));\n" - " vec2 dtc = (texcoord*f-eye_center)/(scale.xy*scale.z)+lens_center;\n" - " if(dtc.x<0.0 || dtc.y<0.0 || dtc.x>1.0 || dtc.y>1.0)\n" - " gl_FragColor = vec4(0.0);\n" - " else\n" - " {\n" - " vec2 red_dtc = (texcoord*f*(chromatic[0]+chromatic[1]*r_sq)-eye_center)/(scale.xy*scale.z)+lens_center;\n" - " vec2 blue_dtc = (texcoord*f*(chromatic[2]+chromatic[3]*r_sq)-eye_center)/(scale.xy*scale.z)+lens_center;\n" - " gl_FragColor = vec4(texture2D(texture, red_dtc).r, texture2D(texture, dtc).g, texture2D(texture, blue_dtc).b, 1.0);\n" - " }\n" + " float r = texture2D(texture, texcoord_r).r;\n" + " float g = texture2D(texture, texcoord_g).g;\n" + " float b = texture2D(texture, texcoord_b).b;\n" + " gl_FragColor = vec4(vec3(r, g, b)*vignette, 1.0);\n" "}\n"; -} +void create_distortion_mesh(Msp::GL::Mesh &mesh, ovrHmd hmd, ovrEyeType eye, const ovrFovPort &fov) +{ + ovrDistortionMesh ovr_mesh; + ovrHmd_CreateDistortionMesh(hmd, eye, fov, ovrDistortionCap_Vignette|ovrDistortionCap_TimeWarp, &ovr_mesh); -namespace Msp { -namespace VR { + Msp::GL::MeshBuilder bld(mesh); + for(unsigned i=0; i(ovr_mesh.pIndexData, ovr_mesh.pIndexData+ovr_mesh.IndexCount)); + mesh.add_batch(batch); - left_shdata.uniform("texture", 0); - left_shdata.uniform("offset", -0.5f); - right_shdata.uniform("texture", 0); - right_shdata.uniform("offset", 0.5f); - - chromatic[0] = 1.0f; - chromatic[1] = 0.0f; - chromatic[2] = 1.0f; - chromatic[3] = 0.0f; - - // This will also call update_parameters - set_distortion(1.0f, 0.22f, 0.24f); - - 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(); + ovrHmd_DestroyDistortionMesh(&ovr_mesh); } -void OculusRiftCombiner::set_view_distance(float d) -{ - view_distance = d; - update_parameters(); } -void OculusRiftCombiner::set_lens_separation(float s) -{ - lens_separation = s; - update_parameters(); -} +namespace Msp { +namespace VR { -void OculusRiftCombiner::set_eye_separation(float s) +OculusRiftCombiner::OculusRiftCombiner(const OculusRiftDevice &d): + device(d), + left_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2, GL::TEXCOORD2,3)), + right_mesh((GL::VERTEX2, GL::TEXCOORD2,0, GL::TEXCOORD2,1, GL::TEXCOORD2,2, GL::TEXCOORD2,3)), + shprog(vs_source, fs_source) { - eye_separation = s; - update_parameters(); -} + ovrHmd hmd = device.get_private().ovr_hmd; -void OculusRiftCombiner::set_distortion(float d0, float d1, float d2, float d3) -{ - distortion[0] = d0; - distortion[1] = d1; - distortion[2] = d2; - distortion[3] = d3; + ovrFovPort left_fov = hmd->DefaultEyeFov[ovrEye_Left]; + ovrFovPort right_fov = hmd->DefaultEyeFov[ovrEye_Right]; + float vertical = max(max(left_fov.UpTan, left_fov.DownTan), max(right_fov.UpTan, right_fov.DownTan)); + fov = Geometry::atan(vertical)*2.0f; - update_parameters(); -} + float inner = max(left_fov.RightTan, right_fov.LeftTan); + float outer = max(left_fov.LeftTan, right_fov.RightTan); + frustum_skew = (inner-outer)/(inner+outer); -void OculusRiftCombiner::set_red_aberration(float c0, float c1) -{ - chromatic[0] = c0; - chromatic[1] = c1; + left_fov.UpTan = right_fov.UpTan = vertical; + left_fov.DownTan = right_fov.DownTan = vertical; + left_fov.RightTan = right_fov.LeftTan = inner; + left_fov.LeftTan = right_fov.RightTan = outer; - update_parameters(); -} + create_distortion_mesh(left_mesh, hmd, ovrEye_Left, left_fov); + create_distortion_mesh(right_mesh, hmd, ovrEye_Right, right_fov); -void OculusRiftCombiner::set_blue_aberration(float c2, float c3) -{ - chromatic[2] = c2; - chromatic[3] = c3; + ovrSizei tex_size = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, left_fov, 1.0); + width_factor = tex_size.w*1.0f/hmd->Resolution.w; + height_factor = tex_size.h*1.0f/hmd->Resolution.h; + float aspect = (inner+outer)/(vertical*2); + aspect_factor = aspect*hmd->Resolution.h/hmd->Resolution.w; - update_parameters(); -} + left_shdata.uniform("texture", 0); + right_shdata.uniform("texture", 0); -void OculusRiftCombiner::set_fill_factor(float f) -{ - fill_factor = f; - update_parameters(); + ovrRecti view_rect; + view_rect.Pos.x = 0; + view_rect.Pos.y = 0; + view_rect.Size = tex_size; + ovrVector2f uv_scale_offset[2]; + ovrHmd_GetRenderScaleAndOffset(left_fov, tex_size, view_rect, uv_scale_offset); + left_shdata.uniform("uv_scale", uv_scale_offset[0].x, -uv_scale_offset[0].y); + left_shdata.uniform("uv_offset", uv_scale_offset[1].x, 1-uv_scale_offset[1].y); + ovrHmd_GetRenderScaleAndOffset(right_fov, tex_size, view_rect, uv_scale_offset); + right_shdata.uniform("uv_scale", uv_scale_offset[0].x, -uv_scale_offset[0].y); + right_shdata.uniform("uv_offset", uv_scale_offset[1].x, 1-uv_scale_offset[1].y); } -void OculusRiftCombiner::update_parameters() +void OculusRiftCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const { - left_shdata.uniform4("distortion", distortion); - left_shdata.uniform4("chromatic", chromatic); - right_shdata.uniform4("distortion", distortion); - right_shdata.uniform4("chromatic", chromatic); - - // Set lens center positions, in output texture coordinates - left_shdata.uniform("lens_center", 1.0f-lens_separation, 0.5); - right_shdata.uniform("lens_center", lens_separation, 0.5); - - /* Compute distance between eye and lens centers, in sampling texture - coordinates. */ - float eye_offset = distort((eye_separation-lens_separation)*2); - left_shdata.uniform("eye_center", -eye_offset, 0.0f); - right_shdata.uniform("eye_center", eye_offset, 0.0f); - - /* Determine the necessary scaling factor to avoid quality degradation in - the center of the screen. */ - float horiz_oversize = distort((fill_factor-lens_separation)*2)/((fill_factor-lens_separation)*2)-eye_offset; - float vert_oversize = distort(1.25f*fill_factor)/(1.25f*fill_factor); - oversize = min(horiz_oversize, vert_oversize); - - left_shdata.uniform("scale", 2.0f, 2.5f, oversize); - right_shdata.uniform("scale", 2.0f, 2.5f, oversize); - - fov = Geometry::atan(oversize*0.625f/view_distance/2)*2.0f; - frustum_skew = lens_separation*2-1; -} + GL::Bind bind_shprog(shprog); -float OculusRiftCombiner::distort(float r) const -{ - float r_sq = r*r; - return r*(distortion[0]+(distortion[1]+(distortion[2]+distortion[3]*r_sq)*r_sq)*r_sq); -} + ovrHmd hmd = device.get_private().ovr_hmd; -float OculusRiftCombiner::undistort(float r) const -{ - float x = r; - while(1) + if(device.is_timing_active()) { - float y = distort(x); - if(abs(r-y)<1e-5) - return x; + ovr_WaitTillTime(device.get_timewarp_time()); + ovrTrackingState state = ovrHmd_GetTrackingState(hmd, device.get_tracking_time()); - float x_sq = x*x; - float d = distortion[0]+(3*distortion[1]+(5*distortion[2]+7*distortion[3]*x_sq)*x_sq)*x_sq; - x -= (y-r)/d; - } -} + ovrMatrix4f matrices[2]; + ovrHmd_GetEyeTimewarpMatrices(hmd, ovrEye_Left, state.HeadPose.ThePose, matrices); + left_shdata.uniform_matrix4_array("timewarp", 2, &matrices[0].M[0][0]); -void OculusRiftCombiner::render(const GL::Texture2D &left, const GL::Texture2D &right) const -{ - GL::Bind bind_shprog(shprog); + ovrHmd_GetEyeTimewarpMatrices(hmd, ovrEye_Right, state.HeadPose.ThePose, matrices); + right_shdata.uniform_matrix4_array("timewarp", 2, &matrices[0].M[0][0]); + } + else + { + GL::Matrix matrices[2]; + left_shdata.uniform_matrix4_array("timewarp", 2, matrices[0].data()); + right_shdata.uniform_matrix4_array("timewarp", 2, matrices[0].data()); + } GL::Bind bind_tex(left); left_shdata.apply(); - mesh.draw(); + left_mesh.draw(); right.bind(); right_shdata.apply(); - mesh.draw(); + right_mesh.draw(); } } // namespace VR